What is xmalloc?
And when should I use it?

What is it? What does it do?

xmalloc is a non-standard function that wraps malloc and terminates the program if malloc fails to the allocate the requested memory.

I was running some of the examples in bork’s linux tracing post, and noticed that when running the uprobe example:

sudo ./bin/uprobe 'r:bash:readline +0($retval):string'

it returned an error:

ERROR: missing symbol "malloc" in /usr/bin/bash

which was odd but perhaps that version of bash used by bork was indeed using malloc and current versions of bash used a different flavor of it, like calloc, realloc, etc…

So I queried the binary for the string malloc and grepped the output:

strings /bin/bash | grep malloc

And got:

xmalloc
malloc_trace_at_exit
xmalloc

Down the rabbithole

Which led me to this article on the xmalloc convention.

In addition there’s a quite interesting discussion on the usage of xmalloc vs malloc in SO

When to use it?

The gist of it is that if you’re writing library code, you shouldn’t use xmalloc since applications depending on that library might lose work or information since they cannot gracefully shutdown.

Though there are cases where it’s not necessary (and “pollutes the codebase”) to check for the return code of malloc for example writing a user-end application, and considering the kernel’s memory overcommitment behavior being as it, xmalloc could be an alright choice.

TIL!


Last modified on 2022-06-20

You can make sure that the author wrote this post by copy-pasting this signature into this Keybase page.