| 1 |
#include <sqlite3.h> |
| 2 |
|
| 3 |
#include "fault.h" |
| 4 |
#include "heap.h" |
| 5 |
|
| 6 |
/* This structure is used to encapsulate the global state variables used by |
| 7 |
* malloc() fault simulation. */ |
| 8 |
struct mem_fault |
| 9 |
{ |
| 10 |
struct test_fault fault; /* Fault trigger */ |
| 11 |
sqlite3_mem_methods m; /* Actual malloc implementation */ |
| 12 |
}; |
| 13 |
|
| 14 |
/* We need to use a global variable here because after a sqlite3_mem_methods |
| 15 |
* instance has been installed using sqlite3_config(), and after |
| 16 |
* sqlite3_initialize() has been called, there's no way to retrieve it back with |
| 17 |
* sqlite3_config(). */ |
| 18 |
static struct mem_fault memFault; |
| 19 |
|
| 20 |
/* A version of sqlite3_mem_methods.xMalloc() that includes fault simulation |
| 21 |
* logic.*/ |
| 22 |
static void *mem_fault_malloc(int n) |
| 23 |
{ |
| 24 |
void *p = NULL; |
| 25 |
|
| 26 |
if (!test_fault_tick(&memFault.fault)) { |
| 27 |
p = memFault.m.xMalloc(n); |
| 28 |
} |
| 29 |
|
| 30 |
return p; |
| 31 |
} |
| 32 |
|
| 33 |
/* A version of sqlite3_mem_methods.xRealloc() that includes fault simulation |
| 34 |
* logic. */ |
| 35 |
static void *mem_fault_realloc(void *old, int n) |
| 36 |
{ |
| 37 |
void *p = NULL; |
| 38 |
|
| 39 |
if (!test_fault_tick(&memFault.fault)) { |
| 40 |
p = memFault.m.xRealloc(old, n); |
| 41 |
} |
| 42 |
|
| 43 |
return p; |
| 44 |
} |
| 45 |
|
| 46 |
/* The following method calls are passed directly through to the underlying |
| 47 |
* malloc system: |
| 48 |
* |
| 49 |
* xFree |
| 50 |
* xSize |
| 51 |
* xRoundup |
| 52 |
* xInit |
| 53 |
* xShutdown |
| 54 |
*/ |
| 55 |
static void mem_fault_free(void *p) |
| 56 |
{ |
| 57 |
memFault.m.xFree(p); |
| 58 |
} |
| 59 |
|
| 60 |
static int mem_fault_size(void *p) |
| 61 |
{ |
| 62 |
return memFault.m.xSize(p); |
| 63 |
} |
| 64 |
|
| 65 |
static int mem_fault_roundup(int n) |
| 66 |
{ |
| 67 |
return memFault.m.xRoundup(n); |
| 68 |
} |
| 69 |
|
| 70 |
static int mem_fault_init(void *p) |
| 71 |
{ |
| 72 |
(void)p; |
| 73 |
return memFault.m.xInit(memFault.m.pAppData); |
| 74 |
} |
| 75 |
|
| 76 |
static void mem_fault_shutdown(void *p) |
| 77 |
{ |
| 78 |
(void)p; |
| 79 |
memFault.m.xShutdown(memFault.m.pAppData); |
| 80 |
} |
| 81 |
|
| 82 |
/* Wrap the given SQLite memory management instance with the faulty memory |
| 83 |
* management interface. By default no faults will be triggered. */ |
| 84 |
static void mem_wrap(sqlite3_mem_methods *m, sqlite3_mem_methods *wrap) |
| 85 |
{ |
| 86 |
test_fault_init(&memFault.fault); |
| 87 |
memFault.m = *m; |
| 88 |
|
| 89 |
wrap->xMalloc = mem_fault_malloc; |
| 90 |
wrap->xFree = mem_fault_free; |
| 91 |
wrap->xRealloc = mem_fault_realloc; |
| 92 |
wrap->xSize = mem_fault_size; |
| 93 |
wrap->xRoundup = mem_fault_roundup; |
| 94 |
wrap->xInit = mem_fault_init; |
| 95 |
wrap->xShutdown = mem_fault_shutdown; |
| 96 |
|
| 97 |
wrap->pAppData = &memFault; |
| 98 |
} |
| 99 |
|
| 100 |
/* Unwrap the given faulty memory management instance returning the original |
| 101 |
* one. */ |
| 102 |
static void mem_unwrap(sqlite3_mem_methods *wrap, sqlite3_mem_methods *m) |
| 103 |
{ |
| 104 |
(void)wrap; |
| 105 |
|
| 106 |
*m = memFault.m; |
| 107 |
} |
| 108 |
|
| 109 |
/* Get the current number of outstanding malloc()'s without a matching free() |
| 110 |
* and the total number of used memory. */ |
| 111 |
static void mem_stats(int *malloc_count, int *memory_used) |
| 112 |
{ |
| 113 |
int rc; |
| 114 |
int watermark; |
| 115 |
|
| 116 |
rc = sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, malloc_count, |
| 117 |
&watermark, 1); |
| 118 |
if (rc != SQLITE_OK) { |
| 119 |
munit_errorf("can't get malloc count: %s", sqlite3_errstr(rc)); |
| 120 |
} |
| 121 |
|
| 122 |
rc = sqlite3_status(SQLITE_STATUS_MEMORY_USED, memory_used, &watermark, |
| 123 |
1); |
| 124 |
if (rc != SQLITE_OK) { |
| 125 |
munit_errorf("can't get memory: %s\n:", sqlite3_errstr(rc)); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* Ensure we're starting from a clean memory state with no allocations and |
| 130 |
* optionally inject malloc failures. */ |
| 131 |
void test_heap_setup(const MunitParameter params[], void *user_data) |
| 132 |
{ |
| 133 |
int malloc_count; |
| 134 |
int memory_used; |
| 135 |
const char *fault_delay; |
| 136 |
const char *fault_repeat; |
| 137 |
sqlite3_mem_methods mem; |
| 138 |
sqlite3_mem_methods mem_fault; |
| 139 |
int rc; |
| 140 |
|
| 141 |
(void)params; |
| 142 |
(void)user_data; |
| 143 |
|
| 144 |
/* Install the faulty malloc implementation */ |
| 145 |
rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem); |
| 146 |
if (rc != SQLITE_OK) { |
| 147 |
munit_errorf("can't get default mem: %s", sqlite3_errstr(rc)); |
| 148 |
} |
| 149 |
|
| 150 |
mem_wrap(&mem, &mem_fault); |
| 151 |
|
| 152 |
rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &mem_fault); |
| 153 |
if (rc != SQLITE_OK) { |
| 154 |
munit_errorf("can't set faulty mem: %s", sqlite3_errstr(rc)); |
| 155 |
} |
| 156 |
|
| 157 |
/* Check that memory is clean. */ |
| 158 |
mem_stats(&malloc_count, &memory_used); |
| 159 |
if (malloc_count > 0 || memory_used > 0) { |
| 160 |
munit_errorf( |
| 161 |
"setup memory:\n bytes: %11d\n allocations: %5d\n", |
| 162 |
malloc_count, memory_used); |
| 163 |
} |
| 164 |
|
| 165 |
/* Optionally inject memory allocation failures. */ |
| 166 |
fault_delay = munit_parameters_get(params, "mem-fault-delay"); |
| 167 |
fault_repeat = munit_parameters_get(params, "mem-fault-repeat"); |
| 168 |
|
| 169 |
munit_assert((fault_delay != NULL && fault_repeat != NULL) || |
| 170 |
(fault_delay == NULL && fault_repeat == NULL)); |
| 171 |
|
| 172 |
if (fault_delay != NULL) { |
| 173 |
test_heap_fault_config(atoi(fault_delay), atoi(fault_repeat)); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/* Ensure we're starting leaving a clean memory behind. */ |
| 178 |
void test_heap_tear_down(void *data) |
| 179 |
{ |
| 180 |
sqlite3_mem_methods mem; |
| 181 |
sqlite3_mem_methods mem_fault; |
| 182 |
int rc; |
| 183 |
|
| 184 |
(void)data; |
| 185 |
|
| 186 |
int malloc_count; |
| 187 |
int memory_used; |
| 188 |
|
| 189 |
mem_stats(&malloc_count, &memory_used); |
| 190 |
if (malloc_count > 0 || memory_used > 0) { |
| 191 |
munit_errorf( |
| 192 |
"teardown memory:\n bytes: %11d\n allocations: %5d\n", |
| 193 |
memory_used, malloc_count); |
| 194 |
} |
| 195 |
|
| 196 |
/* Restore default memory management. */ |
| 197 |
rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem_fault); |
| 198 |
if (rc != SQLITE_OK) { |
| 199 |
munit_errorf("can't get faulty mem: %s", sqlite3_errstr(rc)); |
| 200 |
} |
| 201 |
|
| 202 |
mem_unwrap(&mem_fault, &mem); |
| 203 |
|
| 204 |
rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &mem); |
| 205 |
if (rc != SQLITE_OK) { |
| 206 |
munit_errorf("can't reset default mem: %s", sqlite3_errstr(rc)); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void test_heap_fault_config(int delay, int repeat) |
| 211 |
{ |
| 212 |
test_fault_config(&memFault.fault, delay, repeat); |
| 213 |
} |
| 214 |
|
| 215 |
void test_heap_fault_enable() |
| 216 |
{ |
| 217 |
test_fault_enable(&memFault.fault); |
| 218 |
} |