|
infix
A JIT-Powered FFI Library for C
|
Unit test to verify resource cleanup under memory allocation failures. More...
Macros | |
| #define | infix_malloc test_malloc |
| #define | infix_calloc test_calloc |
| #define | infix_free test_free |
| #define | infix_realloc test_realloc |
| #define | DBLTAP_IMPLEMENTATION |
| #define | ALLOCATOR_LOCK() pthread_mutex_lock(&allocator_mutex) |
| #define | ALLOCATOR_UNLOCK() pthread_mutex_unlock(&allocator_mutex) |
| #define | ALLOCATOR_INIT() ((void)0) |
Functions | |
| void | setup_fault_injector (int fail_after_n_allocs) |
| void | reset_fault_injector () |
| void * | test_malloc (size_t size) |
| void * | test_calloc (size_t num, size_t size) |
| void | test_free (void *ptr) |
| void * | test_realloc (void *ptr, size_t new_size) |
| void | fault_injection_handler (void) |
| subtest ("Leak test for infix_forward_create failures") | |
| subtest ("Leak test for infix_reverse_create_callback failures") | |
| subtest ("Leak test for infix_type_from_signature failures") | |
Variables | |
| static int | allocation_countdown = -1 |
| static int | allocation_counter = 0 |
| static bool | fault_triggered = false |
| static pthread_mutex_t | allocator_mutex = PTHREAD_MUTEX_INITIALIZER |
| TEST | |
Unit test to verify resource cleanup under memory allocation failures.
This test uses a fault injection technique to test the library's resilience to memory allocation failures. It overrides the standard malloc and calloc functions with custom versions (test_malloc, test_calloc) that can be configured to fail after a specific number of successful allocations.
The test strategy is as follows:
N = 0 to a maximum limit.N, it configures the custom allocator to fail on the N-th allocation.infix API function (e.g., infix_forward_create).INFIX_ERROR_ALLOCATION_FAILED status.The most important part of this test is not the assertion itself, but running it under a memory analysis tool like Valgrind or AddressSanitizer (ASan). A "pass" for this test is a clean report from the memory tool, which proves that even when an allocation fails midway through an operation, the infix library correctly cleans up all memory it had allocated up to that point, preventing memory leaks in error paths.
| ALLOCATOR_INIT | ( | ) | ((void)0) |
| #define ALLOCATOR_LOCK | ( | ) | pthread_mutex_lock(&allocator_mutex) |
| #define ALLOCATOR_UNLOCK | ( | ) | pthread_mutex_unlock(&allocator_mutex) |
| #define DBLTAP_IMPLEMENTATION |
| #define infix_calloc test_calloc |
| #define infix_free test_free |
| #define infix_malloc test_malloc |
| #define infix_realloc test_realloc |
| void fault_injection_handler | ( | void | ) |
| void reset_fault_injector | ( | ) |
| void setup_fault_injector | ( | int | fail_after_n_allocs | ) |
| subtest | ( | "Leak test for infix_forward_create failures" | ) |
| subtest | ( | "Leak test for infix_reverse_create_callback failures" | ) |
| subtest | ( | "Leak test for infix_type_from_signature failures" | ) |
| void * test_calloc | ( | size_t | num, |
| size_t | size | ||
| ) |
| void test_free | ( | void * | ptr | ) |
| void * test_malloc | ( | size_t | size | ) |
| void * test_realloc | ( | void * | ptr, |
| size_t | new_size | ||
| ) |
|
static |
|
static |
|
static |
|
static |