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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
* testOOM.c: Test out-of-memory handling
*
* See Copyright for the status of this software.
*
* Copyright 2003 Red Hat, Inc.
* Written by: hp@redhat.com
*/
#include "testOOMlib.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <string.h>
#define _TEST_INT_MAX 2147483647
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
#include <libxml/xmlmemory.h>
static int fail_alloc_counter = _TEST_INT_MAX;
static int n_failures_per_failure = 1;
static int n_failures_this_failure = 0;
static int n_blocks_outstanding = 0;
/**
* set_fail_alloc_counter:
* @until_next_fail: number of successful allocs before one fails
*
* Sets the number of allocations until we simulate a failed
* allocation. If set to 0, the next allocation to run
* fails; if set to 1, one succeeds then the next fails; etc.
* Set to _TEST_INT_MAX to not fail anything.
*/
static void
set_fail_alloc_counter (int until_next_fail)
{
fail_alloc_counter = until_next_fail;
}
/**
* get_fail_alloc_counter:
*
* Returns the number of successful allocs until we'll simulate
* a failed alloc.
*/
static int
get_fail_alloc_counter (void)
{
return fail_alloc_counter;
}
/**
* set_fail_alloc_failures:
* @failures_per_failure: number to fail
*
* Sets how many mallocs to fail when the fail alloc counter reaches
* 0.
*
*/
static void
set_fail_alloc_failures (int failures_per_failure)
{
n_failures_per_failure = failures_per_failure;
}
/**
* decrement_fail_alloc_counter:
*
* Called when about to alloc some memory; if
* it returns #TRUE, then the allocation should
* fail. If it returns #FALSE, then the allocation
* should not fail.
*
* returns #TRUE if this alloc should fail
*/
static int
decrement_fail_alloc_counter (void)
{
if (fail_alloc_counter <= 0)
{
n_failures_this_failure += 1;
if (n_failures_this_failure >= n_failures_per_failure)
{
fail_alloc_counter = _TEST_INT_MAX;
n_failures_this_failure = 0;
}
return TRUE;
}
else
{
fail_alloc_counter -= 1;
return FALSE;
}
}
/**
* test_get_malloc_blocks_outstanding:
*
* Get the number of outstanding malloc()'d blocks.
*
* Returns number of blocks
*/
int
test_get_malloc_blocks_outstanding (void)
{
return n_blocks_outstanding;
}
void*
test_malloc (size_t bytes)
{
if (decrement_fail_alloc_counter ())
{
/* FAIL the malloc */
return NULL;
}
if (bytes == 0) /* some system mallocs handle this, some don't */
return NULL;
else
{
void *mem;
mem = xmlMemMalloc (bytes);
if (mem)
n_blocks_outstanding += 1;
return mem;
}
}
void*
test_realloc (void *memory,
size_t bytes)
{
if (decrement_fail_alloc_counter ())
{
/* FAIL */
return NULL;
}
if (bytes == 0) /* guarantee this is safe */
{
test_free (memory);
return NULL;
}
else
{
void *mem;
mem = xmlMemRealloc (memory, bytes);
if (memory == NULL && mem != NULL)
n_blocks_outstanding += 1;
return mem;
}
}
void
test_free (void *memory)
{
if (memory) /* we guarantee it's safe to free (NULL) */
{
n_blocks_outstanding -= 1;
xmlMemFree (memory);
}
}
char*
test_strdup (const char *str)
{
int len;
char *copy;
if (str == NULL)
return NULL;
len = strlen (str);
copy = test_malloc (len + 1);
if (copy == NULL)
return NULL;
memcpy (copy, str, len + 1);
return copy;
}
static int
run_failing_each_malloc (int n_mallocs,
TestMemoryFunction func,
void *data)
{
n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
while (n_mallocs >= 0)
{
set_fail_alloc_counter (n_mallocs);
if (!(* func) (data))
return FALSE;
n_mallocs -= 1;
}
set_fail_alloc_counter (_TEST_INT_MAX);
return TRUE;
}
/**
* test_oom_handling:
* @func: function to call
* @data: data to pass to function
*
* Tests how well the given function responds to out-of-memory
* situations. Calls the function repeatedly, failing a different
* call to malloc() each time. If the function ever returns #FALSE,
* the test fails. The function should return #TRUE whenever something
* valid (such as returning an error, or succeeding) occurs, and #FALSE
* if it gets confused in some way.
*
* Returns #TRUE if the function never returns FALSE
*/
int
test_oom_handling (TestMemoryFunction func,
void *data)
{
int approx_mallocs;
/* Run once to see about how many mallocs are involved */
set_fail_alloc_counter (_TEST_INT_MAX);
if (!(* func) (data))
return FALSE;
approx_mallocs = _TEST_INT_MAX - get_fail_alloc_counter ();
set_fail_alloc_failures (1);
if (!run_failing_each_malloc (approx_mallocs, func, data))
return FALSE;
set_fail_alloc_failures (2);
if (!run_failing_each_malloc (approx_mallocs, func, data))
return FALSE;
set_fail_alloc_failures (3);
if (!run_failing_each_malloc (approx_mallocs, func, data))
return FALSE;
set_fail_alloc_counter (_TEST_INT_MAX);
return TRUE;
}
|