Commit 0b70a9a6 authored by Rich Prohaska's avatar Rich Prohaska Committed by Yoni Fogel

#3346 #3351 fix the loader malloc error injector refs[t:3346] refs[t:3351]

git-svn-id: file:///svn/toku/tokudb@29297 c7de825b-a66e-492c-adef-691d508d4ae1
parent 7e5eb439
......@@ -102,16 +102,10 @@ static void reset_my_malloc_counts(void) {
__attribute__((__unused__))
static void *my_malloc(size_t n) {
void *caller = __builtin_return_address(0);
if (!((void*)toku_malloc <= caller && caller <= (void*)toku_free))
goto skip;
(void) toku_sync_fetch_and_increment_int32(&my_malloc_count); // my_malloc_count++;
if (n >= my_big_malloc_limit) {
(void) toku_sync_fetch_and_increment_int32(&my_big_malloc_count); // my_big_malloc_count++;
if (do_malloc_errors) {
caller = __builtin_return_address(1);
if ((void*)toku_xmalloc <= caller && caller <= (void*)toku_set_func_malloc)
goto skip;
if (event_add_and_fetch()== event_count_trigger) {
event_hit();
errno = ENOMEM;
......@@ -119,7 +113,6 @@ static void *my_malloc(size_t n) {
}
}
}
skip:
return malloc(n);
}
......@@ -127,16 +120,10 @@ static int do_realloc_errors = 0;
__attribute__((__unused__))
static void *my_realloc(void *p, size_t n) {
void *caller = __builtin_return_address(0);
if (!((void*)toku_realloc <= caller && caller <= (void*)toku_free))
goto skip;
(void) toku_sync_increment_and_fetch_int32(&my_realloc_count); // my_realloc_count++;
if (n >= my_big_malloc_limit) {
(void) toku_sync_increment_and_fetch_int32(&my_big_realloc_count); // my_big_realloc_count++;
if (do_realloc_errors) {
caller = __builtin_return_address(1);
if ((void*)toku_xrealloc <= caller && caller <= (void*)toku_set_func_malloc)
goto skip;
if (event_add_and_fetch() == event_count_trigger) {
event_hit();
errno = ENOMEM;
......@@ -144,7 +131,6 @@ static void *my_realloc(void *p, size_t n) {
}
}
}
skip:
return realloc(p, n);
}
......
......@@ -176,16 +176,10 @@ static void reset_my_malloc_counts(void) {
size_t min_malloc_error_size = 0;
static void *my_malloc(size_t n) {
void *caller = __builtin_return_address(0);
if (!((void*)toku_malloc <= caller && caller <= (void*)toku_free))
goto skip;
my_malloc_count++;
if (n >= min_malloc_error_size) {
my_big_malloc_count++;
if (my_malloc_event) {
caller = __builtin_return_address(1);
if ((void*)toku_xmalloc <= caller && caller <= (void*)toku_set_func_malloc)
goto skip;
event_count++;
if (event_count == event_count_trigger) {
event_hit();
......@@ -195,23 +189,16 @@ static void *my_malloc(size_t n) {
}
}
}
skip:
return malloc(n);
}
static int do_realloc_errors = 1;
static void *my_realloc(void *p, size_t n) {
void *caller = __builtin_return_address(0);
if (!((void*)toku_realloc <= caller && caller <= (void*)toku_free))
goto skip;
my_realloc_count++;
if (n >= min_malloc_error_size) {
my_big_realloc_count++;
if (do_realloc_errors) {
caller = __builtin_return_address(1);
if ((void*)toku_xrealloc <= caller && caller <= (void*)toku_set_func_malloc)
goto skip;
event_count++;
if (event_count == event_count_trigger) {
event_hit();
......@@ -221,7 +208,6 @@ static void *my_realloc(void *p, size_t n) {
}
}
}
skip:
return realloc(p, n);
}
......@@ -380,8 +366,8 @@ static void test (const char *directory, BOOL is_error) {
assert(r==0);
}
toku_set_func_malloc(my_malloc);
toku_set_func_realloc(my_realloc);
toku_set_func_malloc_only(my_malloc);
toku_set_func_realloc_only(my_realloc);
brtloader_set_os_fwrite(bad_fwrite);
toku_set_func_write(bad_write);
toku_set_func_pwrite(bad_pwrite);
......
......@@ -124,8 +124,8 @@ static void write_dbfile (char *template, int n, char *output_name, BOOL expect_
int fd = open(output_name, O_RDWR | O_CREAT | O_BINARY, S_IRWXU|S_IRWXG|S_IRWXO);
assert(fd>=0);
toku_set_func_malloc(my_malloc);
toku_set_func_realloc(my_realloc);
toku_set_func_malloc_only(my_malloc);
toku_set_func_realloc_only(my_realloc);
brtloader_set_os_fwrite(bad_fwrite);
toku_set_func_write(bad_write);
toku_set_func_pwrite(bad_pwrite);
......@@ -136,8 +136,8 @@ static void write_dbfile (char *template, int n, char *output_name, BOOL expect_
// if (!(expect_error ? r != 0 : r == 0)) printf("WARNING%%d expect_error=%d r=%d\n", __LINE__, expect_error, r);
assert(expect_error ? r != 0 : r == 0);
toku_set_func_malloc(NULL);
toku_set_func_realloc(NULL);
toku_set_func_malloc_only(NULL);
toku_set_func_realloc_only(NULL);
brtloader_set_os_fwrite(NULL);
toku_set_func_write(NULL);
toku_set_func_pwrite(NULL);
......
......@@ -6246,17 +6246,20 @@ db_env_set_func_loader_fwrite (size_t (*fwrite_fun)(const void*,size_t,size_t,FI
int
db_env_set_func_malloc (void *(*f)(size_t)) {
return toku_set_func_malloc(f);
toku_set_func_malloc(f);
return 0;
}
int
db_env_set_func_realloc (void *(*f)(void*, size_t)) {
return toku_set_func_realloc(f);
toku_set_func_realloc(f);
return 0;
}
int
db_env_set_func_free (void (*f)(void*)) {
return toku_set_func_free(f);
toku_set_func_free(f);
return 0;
}
......
......@@ -95,8 +95,20 @@ extern int toku_realloc_counter;
extern int toku_calloc_counter;
extern int toku_free_counter;
typedef void *(*malloc_fun_t)(size_t);
typedef void (*free_fun_t)(void*);
typedef void *(*realloc_fun_t)(void*,size_t);
void toku_set_func_malloc(malloc_fun_t f);
void toku_set_func_xmalloc_only(malloc_fun_t f);
void toku_set_func_malloc_only(malloc_fun_t f);
void toku_set_func_realloc(realloc_fun_t f);
void toku_set_func_xrealloc_only(realloc_fun_t f);
void toku_set_func_realloc_only(realloc_fun_t f);
void toku_set_func_free(free_fun_t f);
#if defined(__cplusplus) || defined(__cilkplusplus)
};
}
#endif
#endif
......@@ -191,9 +191,6 @@ void toku_get_fsync_sched(uint64_t *fsync_count, uint64_t *fsync_time);
// set a new fsync function (for debugging)
int toku_set_func_fsync (int (*fsync_function)(int));
int toku_set_func_malloc (void *(*)(size_t));
int toku_set_func_realloc (void *(*)(void*,size_t));
int toku_set_func_free (void (*)(void*));
int toku_set_func_pwrite (ssize_t (*)(int, const void *, size_t, toku_off_t));
int toku_set_func_full_pwrite (ssize_t (*)(int, const void *, size_t, toku_off_t));
int toku_set_func_write (ssize_t (*)(int, const void *, size_t));
......@@ -208,7 +205,7 @@ int toku_portability_init (void);
int toku_portability_destroy (void);
#if defined(__cplusplus) || defined(__cilkplusplus)
};
}
#endif
#endif
......@@ -9,60 +9,45 @@
int toku_memory_check=0;
typedef void *(*malloc_fun_t)(size_t);
typedef void (*free_fun_t)(void*);
typedef void *(*realloc_fun_t)(void*,size_t);
static malloc_fun_t t_malloc = 0;
static malloc_fun_t t_xmalloc = 0;
static free_fun_t t_free = 0;
static realloc_fun_t t_realloc = 0;
static realloc_fun_t t_xrealloc = 0;
void *toku_malloc(size_t size) {
void *p;
if (t_malloc)
p = t_malloc(size);
else
p = os_malloc(size);
void *p = t_malloc ? t_malloc(size) : os_malloc(size);
return p;
}
void *
toku_calloc(size_t nmemb, size_t size)
{
toku_calloc(size_t nmemb, size_t size) {
size_t newsize = nmemb * size;
void *vp = toku_malloc(newsize);
if (vp) memset(vp, 0, newsize);
return vp;
void *p = toku_malloc(newsize);
if (p) memset(p, 0, newsize);
return p;
}
void *
toku_realloc(void *p, size_t size)
{
void *q;
if (t_realloc)
q = t_realloc(p, size);
else
q = os_realloc(p, size);
toku_realloc(void *p, size_t size) {
void *q = t_realloc ? t_realloc(p, size) : os_realloc(p, size);
return q;
}
void *
toku_memdup (const void *v, size_t len)
{
void *r=toku_malloc(len);
if (r) memcpy(r,v,len);
return r;
toku_memdup(const void *v, size_t len) {
void *p = toku_malloc(len);
if (p) memcpy(p, v,len);
return p;
}
char *
toku_strdup (const char *s)
{
toku_strdup(const char *s) {
return toku_memdup(s, strlen(s)+1);
}
void
toku_free(void *p)
{
toku_free(void *p) {
if (t_free)
t_free(p);
else
......@@ -70,22 +55,20 @@ toku_free(void *p)
}
void
toku_free_n(void* p, size_t size __attribute__((unused)))
{
toku_free_n(void* p, size_t size __attribute__((unused))) {
toku_free(p);
}
void *
toku_xmalloc(size_t size) {
void *r = toku_malloc(size);
if (r == 0) // avoid function call in common case
resource_assert(r);
return r;
void *p = t_xmalloc ? t_xmalloc(size) : os_malloc(size);
if (p == NULL) // avoid function call in common case
resource_assert(p);
return p;
}
void *
toku_xcalloc(size_t nmemb, size_t size)
{
toku_xcalloc(size_t nmemb, size_t size) {
size_t newsize = nmemb * size;
void *vp = toku_xmalloc(newsize);
if (vp) memset(vp, 0, newsize);
......@@ -93,42 +76,60 @@ toku_xcalloc(size_t nmemb, size_t size)
}
void *
toku_xrealloc(void *v, size_t size)
{
void *r = toku_realloc(v, size);
if (r == 0) // avoid function call in common case
resource_assert(r);
return r;
toku_xrealloc(void *v, size_t size) {
void *p = t_xrealloc ? t_xrealloc(v, size) : os_realloc(v, size);
if (p == 0) // avoid function call in common case
resource_assert(p);
return p;
}
void *
toku_xmemdup (const void *v, size_t len)
{
void *r=toku_xmalloc(len);
memcpy(r,v,len);
return r;
toku_xmemdup (const void *v, size_t len) {
void *p = toku_xmalloc(len);
memcpy(p, v, len);
return p;
}
char *
toku_xstrdup (const char *s)
{
toku_xstrdup (const char *s) {
return toku_xmemdup(s, strlen(s)+1);
}
int
void
toku_set_func_malloc(malloc_fun_t f) {
t_malloc = f;
return 0;
t_xmalloc = f;
}
int
void
toku_set_func_xmalloc_only(malloc_fun_t f) {
t_xmalloc = f;
}
void
toku_set_func_malloc_only(malloc_fun_t f) {
t_malloc = f;
}
void
toku_set_func_realloc(realloc_fun_t f) {
t_realloc = f;
return 0;
t_xrealloc = f;
}
void
toku_set_func_xrealloc_only(realloc_fun_t f) {
t_xrealloc = f;
}
void
toku_set_func_realloc_only(realloc_fun_t f) {
t_realloc = f;
}
int
void
toku_set_func_free(free_fun_t f) {
t_free = f;
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment