24

My C application uses 3rd libraries, which do their own memory management. In order to be robust, my application has code to deal with failures of library functions due to lack of free memory.

I would like to test this code, and for this, I need to simulate failures due to lack of memory.

What tool/s are recommended for this? My environment is Linux/gcc.

1

11 Answers 11

24

You can use ulimit to limit the amount of resources a user can use, including memory. So you create a test user, limit their memory use to something just enough to launch your program, and watch it die :)

Example:

ulimit -m 64

Sets a memory limit of 64kb.

0
13

Create your own malloc wrapper which will randomly return null instead of a valid pointer. Well, or which fails consistently if you want to unit test.

1
  • And also calloc, realloc, strdup, and any other memory allocation functions the code uses. And you would want a way to turn this feature off when building for a release. Jun 10, 2015 at 19:52
9

On operating systems that overcommit memory (for example, Linux or Windows), it is simply not possible to handle out-of-memory errors. malloc may return a valid pointer and later, when you try to dereference it, your operating system may determine that you are out of memory and kill the process.

http://www.reddit.com/comments/60vys/how_not_to_write_a_shared_library/ is a good write-up on this.

3
  • This behaviour has caused my project serious pain recently. man malloc and look under the BUGS heading. echo /proc/sys/vm/overcommit_memory If it is 2 then overcommit is off.
    – mat_geek
    Sep 30, 2008 at 1:26
  • This answer is just wrong. Overcommit is easily disabled, and other factors like ulimit or exhaustion of the entire virtual address space can also lead to allocation failures. Feb 6, 2012 at 0:03
  • 2
    If you disable overcommit then you are no longer on a system that overcommits memory; my comment was about systems that DO overcommit memory. In many cases, you, the programmer, don't have the ability to disable overcommit. Your link to the oom_pardon discussion was very amusing. :) Feb 6, 2012 at 14:17
6

You can write your own mock library with the same interface as your 3rd party library instead of it. You can also use LD_PRELOAD to override selected functions of the 3rd party library.

3

I can give a Linux (maybe POSIX) specific version: __malloc_hook, __realloc_hook, __free_hook. These are declared in malloc.h.

EDIT: A little elaboration: these are function pointers (see malloc.h and their man-page for the exact declaration), but beware: these are not exactly standards, just GNU extensions. So if portability is an issue, don't use this.

A little less platform-dependent solution might be that you declare a malloc macro. If you're testing, this calls a hook and the real malloc.

memhook.h:

#define malloc(s)    (my_malloc(s))

memhook.c:

#include "memhook.h"
#undef malloc
#include <stdlib.h>

etc.

You can use this to detect leaks, randomly fail the allocation, etc.

1
  • 2
    A malloc macro would not help simulate failures inside 3rd party libraries - they are already compiled with calls to the original malloc. LD_PRELOAD would help, though.
    – Omer Zak
    Sep 20, 2008 at 19:28
2

Have a look at the way sqlite3 does this. They perform extensive unit testing, including out of memory testing.

You may also want to look at their page on malloc, particularly Section 4.0.

1

In addition, you should use Valgrind to test it all and get real useful reports about memory behavior of your program

1
  • 1
    The subtool is called massif. massif is a heap profiler. It measures how much heap memory your program uses. It will generate a log and a postscript (.ps) graph.
    – mat_geek
    Sep 30, 2008 at 1:22
1

You can setup a define in the header file to return NULL whenever malloc is used:

Usually malloc will be protected the following way:

if ((int *x = malloc(sizeof(int))) == NULL)
{
   return NULL;
}

So you use a define to force a NULL return; pseudocode example:

# define malloc(X) NULL

And check if you get a segfault

0

You want the ulimit command in bash. Try

help ulimit
at a bash shell prompt.

1
  • I am not the fastest gun in the west. Or even the esat, it would appear.
    – jfm3
    Sep 20, 2008 at 19:03
0

(As a complement to some of the previous answers)

Checkout "Electric Fence" for an example of a malloc-intercepting library that you can use with your executable (using the LD_PRELOAD trick, for instance).

Once you've intercepted malloc, you can use whatever you want to trigger failures. A randomly triggered failure would be a good stress test for the various parts of the system. You could also modify the failure probability based on the amount of memory requested.

Yours is an interesting idea, by the way, clearly something I'd like to do on some of my code...

0

You might want to look at some of the recovery oriented computing sites, such as the Berkeley/Stanford ROC group. I've heard some of these folks talk before, and they use code to randomly inject errors in the C runtime. There's a link to their FIT tool at the bottom of their page.

1
  • It seems that the tool name is FIG, not FIT. Oct 24, 2008 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.