C memory management stubs.
More...
#include "liboom/oomstub.h"
#include <stdlib.h>
#include <dlfcn.h>
Go to the source code of this file.
C memory management stubs.
This file implements fake malloc, free and realloc functions which can fail and simulate OOM after a countdown. This library is not threadsafe. Inspired from https://github.com/bkthomps/Containers test suite
- Author
- François Cerbelle (Fanfan), franc.nosp@m.ois@.nosp@m.cerbe.nosp@m.lle..nosp@m.net
Definition in file oomstub.c.
◆ RTLD_NEXT
#define RTLD_NEXT ((void *) -1L) |
◆ calloc()
void * calloc |
( |
size_t |
count, |
|
|
size_t |
size |
|
) |
| |
stdc calloc stub function
- Parameters
-
count | How many contiguous records to allocate |
size | Size in bytes of a single record |
- Returns
- Pointer to the allocated heap block
- Return values
-
NULL | The allocation failed |
This function is called instead of the upstream calloc function. If not already done, it creates a backup ot the upstream function pointer. If the countdown equals 1, it simulates OOM and returns NULL. Otherwise, it calls the upstream calloc and returns its return value. Each call to this stub function decrements the countdown until it reaches 0.
Definition at line 113 of file oomstub.c.
◆ malloc()
void * malloc |
( |
size_t |
size | ) |
|
stdc malloc stub function
- Parameters
-
size | Size in bytes to try to allocate |
- Returns
- Pointer to the allocated heap block
- Return values
-
NULL | The allocation failed |
This function is called instead of the upstream malloc function. If not already done, it creates a backup ot the upstream function pointer. If the countdown equals 1, it simulates OOM and returns NULL. Otherwise, it calls the upstream malloc and returns its return value. Each call to this stub function decrements the countdown until it reaches 0.
Definition at line 87 of file oomstub.c.
◆ oomstub_getcountdown()
int oomstub_getcountdown |
( |
| ) |
|
Gets the current malloc countdown before triggering a failure.
- Returns
- The current internal countdown value
- See also
- malloc
-
calloc
-
realloc
Definition at line 72 of file oomstub.c.
◆ oomstub_setcountdown()
void oomstub_setcountdown |
( |
const int |
counter | ) |
|
Sets the malloc countdown before triggering a failure.
- Parameters
-
counter | Value to set in the internal countdown |
- See also
- malloc
-
calloc
-
realloc
Definition at line 62 of file oomstub.c.
◆ realloc()
void * realloc |
( |
void * |
ptr, |
|
|
size_t |
new_size |
|
) |
| |
stdc realloc stub function
- Parameters
-
ptr | Pointer to the dynamic heap block to resize |
new_size | New size in bytes to request |
- Returns
- Pointer to the reallocated heap block
- Return values
-
NULL | The allocation failed (important details in description) |
This function is called instead of the upstream realloc function. If not already done, it creates a backup ot the upstream function pointer. If the countdown equals 1, it simulates OOM and returns NULL. Otherwise, it calls the upstream realloc and returns its return value. Each call to this stub function decrements the countdown until it reaches 0.
- Note
- The failure countdown should not apply when shrinking the size. It is meaningless as it can not fail in real life. It would need bookkeeping to detect arbitrary shrinks. Thus, this stub function also counts shrinking calls in the countdown and apply failures. The tradeof is that the function returns NULL in case of a simulated failure and a pointer in case of success for any case but not for the "free" case. In the "free" case (new_size==0), it returns a not-null value in case of failure and NULL in case of success.
- Warning
- if the compiler optimizes a realloc(ptr,0) with a direct free(ptr) call, the countdown is not decremented. I can not detect this condition and I have no tradeoff/workaround. Diverting free() would decrement the countdown when not expected.
Definition at line 153 of file oomstub.c.