Realloc: Make behavior of realloc(ptr, 0) configurable.
This commit is contained in:
committed by
David Goldblatt
parent
ee961c2310
commit
9cfa805947
@@ -428,15 +428,21 @@ TEST_BEGIN(test_hooks_realloc_as_malloc_or_free) {
|
||||
free(ptr);
|
||||
|
||||
/* realloc(ptr, 0) as free */
|
||||
ptr = malloc(1);
|
||||
reset();
|
||||
realloc(ptr, 0);
|
||||
assert_d_eq(call_count, 1, "Hook not called");
|
||||
assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
|
||||
assert_d_eq(arg_type, (int)hook_dalloc_realloc, "Wrong hook type");
|
||||
assert_ptr_eq(ptr, arg_address, "Wrong pointer freed");
|
||||
assert_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
|
||||
assert_u64_eq((uintptr_t)0, arg_args_raw[1], "Wrong raw arg");
|
||||
if (opt_zero_realloc_action == zero_realloc_action_free) {
|
||||
ptr = malloc(1);
|
||||
reset();
|
||||
realloc(ptr, 0);
|
||||
assert_d_eq(call_count, 1, "Hook not called");
|
||||
assert_ptr_eq(arg_extra, (void *)123, "Wrong extra");
|
||||
assert_d_eq(arg_type, (int)hook_dalloc_realloc,
|
||||
"Wrong hook type");
|
||||
assert_ptr_eq(ptr, arg_address,
|
||||
"Wrong pointer freed");
|
||||
assert_u64_eq((uintptr_t)ptr, arg_args_raw[0],
|
||||
"Wrong raw arg");
|
||||
assert_u64_eq((uintptr_t)0, arg_args_raw[1],
|
||||
"Wrong raw arg");
|
||||
}
|
||||
|
||||
/* realloc(NULL, 0) as malloc(0) */
|
||||
reset();
|
||||
|
@@ -880,6 +880,16 @@ TEST_BEGIN(test_hooks_exhaustion) {
|
||||
}
|
||||
TEST_END
|
||||
|
||||
TEST_BEGIN(test_zero_realloc) {
|
||||
const char *val;
|
||||
size_t sz = sizeof(val);
|
||||
int err = mallctl("opt.zero_realloc", &val, &sz, NULL, 0);
|
||||
assert_d_eq(err, 0, "Unexpected mallctl result");
|
||||
assert_str_eq(val, "strict",
|
||||
"Unexpected default zero_realloc_beahvior");
|
||||
}
|
||||
TEST_END
|
||||
|
||||
int
|
||||
main(void) {
|
||||
return test(
|
||||
@@ -911,5 +921,6 @@ main(void) {
|
||||
test_prof_active,
|
||||
test_stats_arenas,
|
||||
test_hooks,
|
||||
test_hooks_exhaustion);
|
||||
test_hooks_exhaustion,
|
||||
test_zero_realloc);
|
||||
}
|
||||
|
26
test/unit/zero_realloc_abort.c
Normal file
26
test/unit/zero_realloc_abort.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
static bool abort_called = false;
|
||||
|
||||
void set_abort_called() {
|
||||
abort_called = true;
|
||||
};
|
||||
|
||||
TEST_BEGIN(test_realloc_abort) {
|
||||
abort_called = false;
|
||||
safety_check_set_abort(&set_abort_called);
|
||||
void *ptr = mallocx(42, 0);
|
||||
assert_ptr_not_null(ptr, "Unexpected mallocx error");
|
||||
ptr = realloc(ptr, 0);
|
||||
assert_true(abort_called, "Realloc with zero size didn't abort");
|
||||
}
|
||||
TEST_END
|
||||
|
||||
int
|
||||
main(void) {
|
||||
return test(
|
||||
test_realloc_abort);
|
||||
}
|
||||
|
3
test/unit/zero_realloc_abort.sh
Normal file
3
test/unit/zero_realloc_abort.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
export MALLOC_CONF="zero_realloc:abort"
|
33
test/unit/zero_realloc_free.c
Normal file
33
test/unit/zero_realloc_free.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
static uint64_t
|
||||
deallocated() {
|
||||
if (!config_stats) {
|
||||
return 0;
|
||||
}
|
||||
uint64_t deallocated;
|
||||
size_t sz = sizeof(deallocated);
|
||||
assert_d_eq(mallctl("thread.deallocated", (void *)&deallocated, &sz,
|
||||
NULL, 0), 0, "Unexpected mallctl failure");
|
||||
return deallocated;
|
||||
}
|
||||
|
||||
TEST_BEGIN(test_realloc_free) {
|
||||
void *ptr = mallocx(42, 0);
|
||||
assert_ptr_not_null(ptr, "Unexpected mallocx error");
|
||||
uint64_t deallocated_before = deallocated();
|
||||
ptr = realloc(ptr, 0);
|
||||
uint64_t deallocated_after = deallocated();
|
||||
assert_ptr_null(ptr, "Realloc didn't free");
|
||||
if (config_stats) {
|
||||
assert_u64_gt(deallocated_after, deallocated_before,
|
||||
"Realloc didn't free");
|
||||
}
|
||||
}
|
||||
TEST_END
|
||||
|
||||
int
|
||||
main(void) {
|
||||
return test(
|
||||
test_realloc_free);
|
||||
}
|
3
test/unit/zero_realloc_free.sh
Normal file
3
test/unit/zero_realloc_free.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
export MALLOC_CONF="zero_realloc:free"
|
48
test/unit/zero_realloc_strict.c
Normal file
48
test/unit/zero_realloc_strict.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
static uint64_t
|
||||
allocated() {
|
||||
if (!config_stats) {
|
||||
return 0;
|
||||
}
|
||||
uint64_t allocated;
|
||||
size_t sz = sizeof(allocated);
|
||||
assert_d_eq(mallctl("thread.allocated", (void *)&allocated, &sz, NULL,
|
||||
0), 0, "Unexpected mallctl failure");
|
||||
return allocated;
|
||||
}
|
||||
|
||||
static uint64_t
|
||||
deallocated() {
|
||||
if (!config_stats) {
|
||||
return 0;
|
||||
}
|
||||
uint64_t deallocated;
|
||||
size_t sz = sizeof(deallocated);
|
||||
assert_d_eq(mallctl("thread.deallocated", (void *)&deallocated, &sz,
|
||||
NULL, 0), 0, "Unexpected mallctl failure");
|
||||
return deallocated;
|
||||
}
|
||||
|
||||
TEST_BEGIN(test_realloc_strict) {
|
||||
void *ptr = mallocx(1, 0);
|
||||
assert_ptr_not_null(ptr, "Unexpected mallocx error");
|
||||
uint64_t allocated_before = allocated();
|
||||
uint64_t deallocated_before = deallocated();
|
||||
ptr = realloc(ptr, 0);
|
||||
uint64_t allocated_after = allocated();
|
||||
uint64_t deallocated_after = deallocated();
|
||||
if (config_stats) {
|
||||
assert_u64_lt(allocated_before, allocated_after,
|
||||
"Unexpected stats change");
|
||||
assert_u64_lt(deallocated_before, deallocated_after,
|
||||
"Unexpected stats change");
|
||||
}
|
||||
dallocx(ptr, 0);
|
||||
}
|
||||
TEST_END
|
||||
int
|
||||
main(void) {
|
||||
return test(
|
||||
test_realloc_strict);
|
||||
}
|
3
test/unit/zero_realloc_strict.sh
Normal file
3
test/unit/zero_realloc_strict.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
export MALLOC_CONF="zero_realloc:strict"
|
Reference in New Issue
Block a user