liboom 1.0.1
Debugging helper library with Memory leak detection
|
Library safely (rlimited) consumming RAM to trigger OOMs (only on POSIX systems) More...
#include "liboom/oomfill.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/resource.h>
#include <string.h>
#include <alloca.h>
#include <unistd.h>
Go to the source code of this file.
Macros | |
#define | RAMBLOCKS_MAX 1000 |
Maximum number of fragmented blocks to allocate. More... | |
Functions | |
size_t | oomfill_config (const size_t hardlimit) |
Sets the oomfill helpers hard rlimit and enables the oomfill helper features. More... | |
bool | oomfill_enabled () |
Variables | |
size_t(* | oomfill_fill )(const size_t minHeap, const size_t minStack) =oomfill_fill_preinit |
Starts an almost OOM single and simple test. More... | |
void(* | oomfill_free )() =oomfill_free_preinit |
Ends a single simple OOM test. More... | |
size_t(* | oomfill_enable )(const size_t softlimit) =oomfill_enable_preinit |
Starts a new oomfill environment or reconfigure the soft limit. More... | |
size_t(* | oomfill_disable )() =oomfill_disable_preinit |
Stops the current oomfill. More... | |
Library safely (rlimited) consumming RAM to trigger OOMs (only on POSIX systems)
Definition in file oomfill.c.
#define RAMBLOCKS_MAX 1000 |
size_t oomfill_config | ( | const size_t | hardlimit | ) |
Sets the oomfill helpers hard rlimit and enables the oomfill helper features.
This function needs to be invoqued BEFORE any other helper from the framework. It will configure an hard RAM limit for the current process and each of his children. Then, it will enable the other oomfill helper functions.
It refuses to set a limit over the actually installed physical RAM to limit pushing RAM pages to the swap. If the requested value is zero, it will default to the actually installed physical RAM.
If anything goes wrong or did not behaves as expected, it abort the process to avoid RAM bombing and swapping.
Despite it was designed to be invoked only once, from the main parent process, it can be invoked several times as long as the hardlimit parameter is always less than the previous call, otherwise it will fail and abort the process.
[in] | hardlimit | the size in bytes to limit the process to. |
size_t(* oomfill_disable) () | ( | ) | =oomfill_disable_preinit |
Stops the current oomfill.
This function can be invoked any time after the oomfill_config initialized the environment. If invocated before, it will abort the current process to help detecting mistakes in the test code.
This function disables the fill/free oomfill functions and free the allocated RAM before reverting the soft limit to the hard limit value.
It is designed to be invoked t the end of a test case.
Despite it could be invoked twice or without a prior call to oomfill_enable, it is illegal and not allowed to help detecting mistakes in the test code.
size_t(* oomfill_enable) (const size_t softlimit) | ( | const size_t | softlimit | ) | =oomfill_enable_preinit |
Starts a new oomfill environment or reconfigure the soft limit.
This function can only be invoked after at least a first call to oomfill_config to initialize the oomfill helpers environment. If invocated before, it will abort the current process to help detecting mistakes in the test code.
It will (soft)limit the current process and his children to the provided value in bytes. Then, it will enable the fill/free oomfill helper functions which are disabled otherwise.
It is designed to be invoked at the begining of a test case to apply for all tests in this testcase.
In case of any unexpected behavior, it should abort the current process.
[in] | softlimit | Soft limit to set in bytes. It has to be less than the hardlimit otherwise it will fail and abort. If set to 0, it will apply the same value as the hardlimit. |
size_t(* oomfill_fill) (const size_t minHeap, const size_t minStack) | ( | const size_t | minHeap, |
const size_t | minStack | ||
) | =oomfill_fill_preinit |
Starts an almost OOM single and simple test.
This function is only enabled after a call to oomfill_enable and will have no effect otherwise. Its goal is to completely fill the RAM until the very last bytes, to keep only between minHeap and maxHeap bytes available in the heap and minStack bytes in the stack.
If oomfill_enable was not invoked beforehand, this function will simply return without any RAM consumption.
It should be called immediately before the test and should be reverted with oomfill_free immediately after. It can create so much pressure on the available memory that a simple printf could fail.
It is designed to fail if invoked twice as this is very probably a mistake in the test code. Should you need to change the RAM filling values, first call oomfill_free and reapply oomfill_fill. This ensure a really wanted behavior and not a mistake in your test code.
[in] | minHeap | |
[in] | minStack |
void(* oomfill_free) () | ( | ) | =oomfill_free_preinit |
Ends a single simple OOM test.
If this function is invoked between an oomfill_enable and an oomfill_disable invocations, it deallocates the RAM allocated by the oomfill_fill function. It should be called immediately after the single and simple test because the RAM pressure can even make a printf to fail.
If called without a prior oomfill_enable invocation, this function simply returns without any action.
This function is designed to fail and abort the process if invocated whereas there is no current RAM allocated, when called twice, for example. This helps to avoid mistakes in the test scenario.