xref: /freebsd-src/contrib/libcbor/test/test_allocator.c (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
1*5d3e7166SEd Maste #include "test_allocator.h"
2*5d3e7166SEd Maste 
3*5d3e7166SEd Maste #ifdef HAS_EXECINFO
4*5d3e7166SEd Maste #include <execinfo.h>
5*5d3e7166SEd Maste #endif
6*5d3e7166SEd Maste 
7*5d3e7166SEd Maste // How many alloc calls we expect
8*5d3e7166SEd Maste int alloc_calls_expected;
9*5d3e7166SEd Maste // How many alloc calls we got
10*5d3e7166SEd Maste int alloc_calls;
11*5d3e7166SEd Maste // Array of booleans indicating whether to return a block or fail with NULL
12*5d3e7166SEd Maste call_expectation *expectations;
13*5d3e7166SEd Maste 
14*5d3e7166SEd Maste void set_mock_malloc(int calls, ...) {
15*5d3e7166SEd Maste   va_list args;
16*5d3e7166SEd Maste   va_start(args, calls);
17*5d3e7166SEd Maste   alloc_calls_expected = calls;
18*5d3e7166SEd Maste   alloc_calls = 0;
19*5d3e7166SEd Maste   expectations = calloc(calls, sizeof(expectations));
20*5d3e7166SEd Maste   for (int i = 0; i < calls; i++) {
21*5d3e7166SEd Maste     // Promotable types, baby
22*5d3e7166SEd Maste     expectations[i] = va_arg(args, call_expectation);
23*5d3e7166SEd Maste   }
24*5d3e7166SEd Maste   va_end(args);
25*5d3e7166SEd Maste }
26*5d3e7166SEd Maste 
27*5d3e7166SEd Maste void finalize_mock_malloc(void) {
28*5d3e7166SEd Maste   assert_int_equal(alloc_calls, alloc_calls_expected);
29*5d3e7166SEd Maste   free(expectations);
30*5d3e7166SEd Maste }
31*5d3e7166SEd Maste 
32*5d3e7166SEd Maste void print_backtrace() {
33*5d3e7166SEd Maste #if HAS_EXECINFO
34*5d3e7166SEd Maste   void *buffer[128];
35*5d3e7166SEd Maste   int frames = backtrace(buffer, 128);
36*5d3e7166SEd Maste   char **symbols = backtrace_symbols(buffer, frames);
37*5d3e7166SEd Maste   // Skip this function and the caller
38*5d3e7166SEd Maste   for (int i = 2; i < frames; ++i) {
39*5d3e7166SEd Maste     printf("%s\n", symbols[i]);
40*5d3e7166SEd Maste   }
41*5d3e7166SEd Maste   free(symbols);
42*5d3e7166SEd Maste #endif
43*5d3e7166SEd Maste }
44*5d3e7166SEd Maste 
45*5d3e7166SEd Maste void *instrumented_malloc(size_t size) {
46*5d3e7166SEd Maste   if (alloc_calls >= alloc_calls_expected) {
47*5d3e7166SEd Maste     goto error;
48*5d3e7166SEd Maste   }
49*5d3e7166SEd Maste 
50*5d3e7166SEd Maste   if (expectations[alloc_calls] == MALLOC) {
51*5d3e7166SEd Maste     alloc_calls++;
52*5d3e7166SEd Maste     return malloc(size);
53*5d3e7166SEd Maste   } else if (expectations[alloc_calls] == MALLOC_FAIL) {
54*5d3e7166SEd Maste     alloc_calls++;
55*5d3e7166SEd Maste     return NULL;
56*5d3e7166SEd Maste   }
57*5d3e7166SEd Maste 
58*5d3e7166SEd Maste error:
59*5d3e7166SEd Maste   print_error(
60*5d3e7166SEd Maste       "Unexpected call to malloc(%zu) at position %d of %d; expected %d\n",
61*5d3e7166SEd Maste       size, alloc_calls, alloc_calls_expected,
62*5d3e7166SEd Maste       alloc_calls < alloc_calls_expected ? expectations[alloc_calls] : -1);
63*5d3e7166SEd Maste   print_backtrace();
64*5d3e7166SEd Maste   fail();
65*5d3e7166SEd Maste   return NULL;
66*5d3e7166SEd Maste }
67*5d3e7166SEd Maste 
68*5d3e7166SEd Maste void *instrumented_realloc(void *ptr, size_t size) {
69*5d3e7166SEd Maste   if (alloc_calls >= alloc_calls_expected) {
70*5d3e7166SEd Maste     goto error;
71*5d3e7166SEd Maste   }
72*5d3e7166SEd Maste 
73*5d3e7166SEd Maste   if (expectations[alloc_calls] == REALLOC) {
74*5d3e7166SEd Maste     alloc_calls++;
75*5d3e7166SEd Maste     return realloc(ptr, size);
76*5d3e7166SEd Maste   } else if (expectations[alloc_calls] == REALLOC_FAIL) {
77*5d3e7166SEd Maste     alloc_calls++;
78*5d3e7166SEd Maste     return NULL;
79*5d3e7166SEd Maste   }
80*5d3e7166SEd Maste 
81*5d3e7166SEd Maste error:
82*5d3e7166SEd Maste   print_error(
83*5d3e7166SEd Maste       "Unexpected call to realloc(%zu) at position %d of %d; expected %d\n",
84*5d3e7166SEd Maste       size, alloc_calls, alloc_calls_expected,
85*5d3e7166SEd Maste       alloc_calls < alloc_calls_expected ? expectations[alloc_calls] : -1);
86*5d3e7166SEd Maste   print_backtrace();
87*5d3e7166SEd Maste   fail();
88*5d3e7166SEd Maste   return NULL;
89*5d3e7166SEd Maste }
90