File: heap.c

package info (click to toggle)
dqlite 1.18.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,372 kB
  • sloc: ansic: 57,583; makefile: 336; sh: 243
file content (218 lines) | stat: -rw-r--r-- 5,326 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <sqlite3.h>

#include "fault.h"
#include "heap.h"

/* This structure is used to encapsulate the global state variables used by
 * malloc() fault simulation. */
struct mem_fault
{
	struct test_fault fault; /* Fault trigger */
	sqlite3_mem_methods m;   /* Actual malloc implementation */
};

/* We need to use a global variable here because after a sqlite3_mem_methods
 * instance has been installed using sqlite3_config(), and after
 * sqlite3_initialize() has been called, there's no way to retrieve it back with
 * sqlite3_config(). */
static struct mem_fault memFault;

/* A version of sqlite3_mem_methods.xMalloc() that includes fault simulation
 * logic.*/
static void *mem_fault_malloc(int n)
{
	void *p = NULL;

	if (!test_fault_tick(&memFault.fault)) {
		p = memFault.m.xMalloc(n);
	}

	return p;
}

/* A version of sqlite3_mem_methods.xRealloc() that includes fault simulation
 * logic. */
static void *mem_fault_realloc(void *old, int n)
{
	void *p = NULL;

	if (!test_fault_tick(&memFault.fault)) {
		p = memFault.m.xRealloc(old, n);
	}

	return p;
}

/* The following method calls are passed directly through to the underlying
 * malloc system:
 *
 *     xFree
 *     xSize
 *     xRoundup
 *     xInit
 *     xShutdown
 */
static void mem_fault_free(void *p)
{
	memFault.m.xFree(p);
}

static int mem_fault_size(void *p)
{
	return memFault.m.xSize(p);
}

static int mem_fault_roundup(int n)
{
	return memFault.m.xRoundup(n);
}

static int mem_fault_init(void *p)
{
	(void)p;
	return memFault.m.xInit(memFault.m.pAppData);
}

static void mem_fault_shutdown(void *p)
{
	(void)p;
	memFault.m.xShutdown(memFault.m.pAppData);
}

/* Wrap the given SQLite memory management instance with the faulty memory
 * management interface. By default no faults will be triggered. */
static void mem_wrap(sqlite3_mem_methods *m, sqlite3_mem_methods *wrap)
{
	test_fault_init(&memFault.fault);
	memFault.m = *m;

	wrap->xMalloc = mem_fault_malloc;
	wrap->xFree = mem_fault_free;
	wrap->xRealloc = mem_fault_realloc;
	wrap->xSize = mem_fault_size;
	wrap->xRoundup = mem_fault_roundup;
	wrap->xInit = mem_fault_init;
	wrap->xShutdown = mem_fault_shutdown;

	wrap->pAppData = &memFault;
}

/* Unwrap the given faulty memory management instance returning the original
 * one. */
static void mem_unwrap(sqlite3_mem_methods *wrap, sqlite3_mem_methods *m)
{
	(void)wrap;

	*m = memFault.m;
}

/* Get the current number of outstanding malloc()'s without a matching free()
 * and the total number of used memory. */
static void mem_stats(int *malloc_count, int *memory_used)
{
	int rc;
	int watermark;

	rc = sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, malloc_count,
			    &watermark, 1);
	if (rc != SQLITE_OK) {
		munit_errorf("can't get malloc count: %s", sqlite3_errstr(rc));
	}

	rc = sqlite3_status(SQLITE_STATUS_MEMORY_USED, memory_used, &watermark,
			    1);
	if (rc != SQLITE_OK) {
		munit_errorf("can't get memory: %s\n:", sqlite3_errstr(rc));
	}
}

/* Ensure we're starting from a clean memory state with no allocations and
 * optionally inject malloc failures. */
void test_heap_setup(const MunitParameter params[], void *user_data)
{
	int malloc_count;
	int memory_used;
	const char *fault_delay;
	const char *fault_repeat;
	sqlite3_mem_methods mem;
	sqlite3_mem_methods mem_fault;
	int rc;

	(void)params;
	(void)user_data;

	/* Install the faulty malloc implementation */
	rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
	if (rc != SQLITE_OK) {
		munit_errorf("can't get default mem: %s", sqlite3_errstr(rc));
	}

	mem_wrap(&mem, &mem_fault);

	rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &mem_fault);
	if (rc != SQLITE_OK) {
		munit_errorf("can't set faulty mem: %s", sqlite3_errstr(rc));
	}

	/* Check that memory is clean. */
	mem_stats(&malloc_count, &memory_used);
	if (malloc_count > 0 || memory_used > 0) {
		munit_errorf(
		    "setup memory:\n    bytes: %11d\n    allocations: %5d\n",
		    malloc_count, memory_used);
	}

	/* Optionally inject memory allocation failures. */
	fault_delay = munit_parameters_get(params, "mem-fault-delay");
	fault_repeat = munit_parameters_get(params, "mem-fault-repeat");

	munit_assert((fault_delay != NULL && fault_repeat != NULL) ||
		     (fault_delay == NULL && fault_repeat == NULL));

	if (fault_delay != NULL) {
		test_heap_fault_config(atoi(fault_delay), atoi(fault_repeat));
	}
}

/* Ensure we're starting leaving a clean memory behind. */
void test_heap_tear_down(void *data)
{
	sqlite3_mem_methods mem;
	sqlite3_mem_methods mem_fault;
	int rc;

	(void)data;

	int malloc_count;
	int memory_used;

	mem_stats(&malloc_count, &memory_used);
	if (malloc_count > 0 || memory_used > 0) {
		/* munit_errorf(
		    "teardown memory:\n    bytes: %11d\n    allocations: %5d\n",
		    memory_used, malloc_count); */
	}

	/* Restore default memory management. */
	rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem_fault);
	if (rc != SQLITE_OK) {
		munit_errorf("can't get faulty mem: %s", sqlite3_errstr(rc));
	}

	mem_unwrap(&mem_fault, &mem);

	rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
	if (rc != SQLITE_OK) {
		munit_errorf("can't reset default mem: %s", sqlite3_errstr(rc));
	}
}

void test_heap_fault_config(int delay, int repeat)
{
	test_fault_config(&memFault.fault, delay, repeat);
}

void test_heap_fault_enable()
{
	test_fault_enable(&memFault.fault);
}