1 /*
2 * testOOM.c: Test out-of-memory handling
3 *
4 * See Copyright for the status of this software.
5 *
6 * Copyright 2003 Red Hat, Inc.
7 * Written by: hp@redhat.com
8 */
9
10 #include "testOOMlib.h"
11
12 #include <stdlib.h>
13 #include <string.h>
14
15 #define _TEST_INT_MAX 2147483647
16 #ifndef TRUE
17 #define TRUE (1)
18 #endif
19 #ifndef FALSE
20 #define FALSE (0)
21 #endif
22 #ifndef NULL
23 #define NULL ((void*)0)
24 #endif
25
26 #include <libxml/xmlmemory.h>
27
28 static int fail_alloc_counter = _TEST_INT_MAX;
29 static int n_failures_per_failure = 1;
30 static int n_failures_this_failure = 0;
31 static int n_blocks_outstanding = 0;
32
33 /**
34 * set_fail_alloc_counter:
35 * @until_next_fail: number of successful allocs before one fails
36 *
37 * Sets the number of allocations until we simulate a failed
38 * allocation. If set to 0, the next allocation to run
39 * fails; if set to 1, one succeeds then the next fails; etc.
40 * Set to _TEST_INT_MAX to not fail anything.
41 */
42 static void
set_fail_alloc_counter(int until_next_fail)43 set_fail_alloc_counter (int until_next_fail)
44 {
45 fail_alloc_counter = until_next_fail;
46 }
47
48 /**
49 * get_fail_alloc_counter:
50 *
51 * Returns the number of successful allocs until we'll simulate
52 * a failed alloc.
53 */
54 static int
get_fail_alloc_counter(void)55 get_fail_alloc_counter (void)
56 {
57 return fail_alloc_counter;
58 }
59
60 /**
61 * set_fail_alloc_failures:
62 * @failures_per_failure: number to fail
63 *
64 * Sets how many mallocs to fail when the fail alloc counter reaches
65 * 0.
66 *
67 */
68 static void
set_fail_alloc_failures(int failures_per_failure)69 set_fail_alloc_failures (int failures_per_failure)
70 {
71 n_failures_per_failure = failures_per_failure;
72 }
73
74 /**
75 * decrement_fail_alloc_counter:
76 *
77 * Called when about to alloc some memory; if
78 * it returns #TRUE, then the allocation should
79 * fail. If it returns #FALSE, then the allocation
80 * should not fail.
81 *
82 * returns #TRUE if this alloc should fail
83 */
84 static int
decrement_fail_alloc_counter(void)85 decrement_fail_alloc_counter (void)
86 {
87 if (fail_alloc_counter <= 0)
88 {
89 n_failures_this_failure += 1;
90 if (n_failures_this_failure >= n_failures_per_failure)
91 {
92 fail_alloc_counter = _TEST_INT_MAX;
93
94 n_failures_this_failure = 0;
95 }
96
97 return TRUE;
98 }
99 else
100 {
101 fail_alloc_counter -= 1;
102 return FALSE;
103 }
104 }
105
106 /**
107 * test_get_malloc_blocks_outstanding:
108 *
109 * Get the number of outstanding malloc()'d blocks.
110 *
111 * Returns number of blocks
112 */
113 int
test_get_malloc_blocks_outstanding(void)114 test_get_malloc_blocks_outstanding (void)
115 {
116 return n_blocks_outstanding;
117 }
118
119 void*
test_malloc(size_t bytes)120 test_malloc (size_t bytes)
121 {
122 if (decrement_fail_alloc_counter ())
123 {
124 /* FAIL the malloc */
125 return NULL;
126 }
127
128 if (bytes == 0) /* some system mallocs handle this, some don't */
129 return NULL;
130 else
131 {
132 void *mem;
133 mem = xmlMemMalloc (bytes);
134
135 if (mem)
136 n_blocks_outstanding += 1;
137
138 return mem;
139 }
140 }
141
142 void*
test_realloc(void * memory,size_t bytes)143 test_realloc (void *memory,
144 size_t bytes)
145 {
146 if (decrement_fail_alloc_counter ())
147 {
148 /* FAIL */
149 return NULL;
150 }
151
152 if (bytes == 0) /* guarantee this is safe */
153 {
154 test_free (memory);
155 return NULL;
156 }
157 else
158 {
159 void *mem;
160 mem = xmlMemRealloc (memory, bytes);
161
162 if (memory == NULL && mem != NULL)
163 n_blocks_outstanding += 1;
164
165 return mem;
166 }
167 }
168
169 void
test_free(void * memory)170 test_free (void *memory)
171 {
172 if (memory) /* we guarantee it's safe to free (NULL) */
173 {
174 n_blocks_outstanding -= 1;
175
176 xmlMemFree (memory);
177 }
178 }
179
180 char*
test_strdup(const char * str)181 test_strdup (const char *str)
182 {
183 int len;
184 char *copy;
185
186 if (str == NULL)
187 return NULL;
188
189 len = strlen (str);
190
191 copy = test_malloc (len + 1);
192 if (copy == NULL)
193 return NULL;
194
195 memcpy (copy, str, len + 1);
196
197 return copy;
198 }
199
200 static int
run_failing_each_malloc(int n_mallocs,TestMemoryFunction func,void * data)201 run_failing_each_malloc (int n_mallocs,
202 TestMemoryFunction func,
203 void *data)
204 {
205 n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
206
207 while (n_mallocs >= 0)
208 {
209 set_fail_alloc_counter (n_mallocs);
210
211 if (!(* func) (data))
212 return FALSE;
213
214 n_mallocs -= 1;
215 }
216
217 set_fail_alloc_counter (_TEST_INT_MAX);
218
219 return TRUE;
220 }
221
222 /**
223 * test_oom_handling:
224 * @func: function to call
225 * @data: data to pass to function
226 *
227 * Tests how well the given function responds to out-of-memory
228 * situations. Calls the function repeatedly, failing a different
229 * call to malloc() each time. If the function ever returns #FALSE,
230 * the test fails. The function should return #TRUE whenever something
231 * valid (such as returning an error, or succeeding) occurs, and #FALSE
232 * if it gets confused in some way.
233 *
234 * Returns #TRUE if the function never returns FALSE
235 */
236 int
test_oom_handling(TestMemoryFunction func,void * data)237 test_oom_handling (TestMemoryFunction func,
238 void *data)
239 {
240 int approx_mallocs;
241
242 /* Run once to see about how many mallocs are involved */
243
244 set_fail_alloc_counter (_TEST_INT_MAX);
245
246 if (!(* func) (data))
247 return FALSE;
248
249 approx_mallocs = _TEST_INT_MAX - get_fail_alloc_counter ();
250
251 set_fail_alloc_failures (1);
252 if (!run_failing_each_malloc (approx_mallocs, func, data))
253 return FALSE;
254
255 set_fail_alloc_failures (2);
256 if (!run_failing_each_malloc (approx_mallocs, func, data))
257 return FALSE;
258
259 set_fail_alloc_failures (3);
260 if (!run_failing_each_malloc (approx_mallocs, func, data))
261 return FALSE;
262
263 set_fail_alloc_counter (_TEST_INT_MAX);
264
265 return TRUE;
266 }
267