infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
811_fault_injection.c File Reference

An advanced stress test that uses fault injection to find memory leaks in the library's error-handling code paths, adapted for the new arena model. More...

#include "common/double_tap.h"
#include <infix/infix.h>
#include <stddef.h>
#include <pthread.h>
Include dependency graph for 811_fault_injection.c:

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)
 Configures the allocator to fail after a specific number of successful allocations.
 
void reset_fault_injector ()
 Resets the allocator to its default (non-failing) behavior.
 
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
 

Detailed Description

An advanced stress test that uses fault injection to find memory leaks in the library's error-handling code paths, adapted for the new arena model.

Copyright (c) 2025 Sanko Robinson

This source code is dual-licensed under the Artistic License 2.0 or the MIT License. You may choose to use this code under the terms of either license.

SPDX-License-Identifier: (Artistic-2.0 OR MIT)

The documentation blocks within this file are licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).

SPDX-License-Identifier: CC-BY-4.0

This test replaces the standard malloc/calloc/free/realloc functions with a custom, thread-safe allocator that can be programmed to fail after a specific number of successful allocations.

By repeatedly attempting to create complex FFI objects and forcing a heap allocation failure at every possible point (e.g., arena creation, handle creation, executable memory allocation), this test rigorously exercises all error-handling and cleanup code in the library. It is designed to be run under Valgrind's memcheck tool.

The test is considered successful if two conditions are met:

  1. The test program itself passes, confirming that the library correctly propagates INFIX_ERROR_ALLOCATION_FAILED status codes up the call stack.
  2. Valgrind reports ZERO memory leaks, proving that all internal cleanup paths correctly free any partially allocated resources.

This test targets the two most allocation-heavy high-level operations:

  • infix_reverse_create_callback
  • infix_type_from_signature

Macro Definition Documentation

◆ ALLOCATOR_INIT

ALLOCATOR_INIT ( )    ((void)0)

◆ ALLOCATOR_LOCK

#define ALLOCATOR_LOCK ( )    pthread_mutex_lock(&allocator_mutex)

◆ ALLOCATOR_UNLOCK

#define ALLOCATOR_UNLOCK ( )    pthread_mutex_unlock(&allocator_mutex)

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

◆ infix_calloc

#define infix_calloc   test_calloc

◆ infix_free

#define infix_free   test_free

◆ infix_malloc

#define infix_malloc   test_malloc

◆ infix_realloc

#define infix_realloc   test_realloc

Function Documentation

◆ fault_injection_handler()

void fault_injection_handler ( void  )

◆ reset_fault_injector()

void reset_fault_injector ( )

Resets the allocator to its default (non-failing) behavior.

◆ setup_fault_injector()

void setup_fault_injector ( int  fail_after_n_allocs)

Configures the allocator to fail after a specific number of successful allocations.

◆ subtest() [1/3]

subtest ( "Leak test for infix_forward_create failures"  )

◆ subtest() [2/3]

subtest ( "Leak test for infix_reverse_create_callback failures"  )

◆ subtest() [3/3]

subtest ( "Leak test for infix_type_from_signature failures"  )

◆ test_calloc()

void * test_calloc ( size_t  num,
size_t  size 
)

◆ test_free()

void test_free ( void *  ptr)

◆ test_malloc()

void * test_malloc ( size_t  size)

◆ test_realloc()

void * test_realloc ( void *  ptr,
size_t  new_size 
)

Variable Documentation

◆ allocation_countdown

int allocation_countdown = -1
static

◆ allocation_counter

int allocation_counter = 0
static

◆ allocator_mutex

pthread_mutex_t allocator_mutex = PTHREAD_MUTEX_INITIALIZER
static

◆ fault_triggered

bool fault_triggered = false
static

◆ TEST

TEST
Initial value:
{
plan(3)
#define plan(count)
Definition double_tap.h:132