2020-08-04 09:23:36 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
#include "jemalloc/internal/safety_check.h"
|
|
|
|
|
|
|
|
bool fake_abort_called;
|
|
|
|
void fake_abort(const char *message) {
|
|
|
|
(void)message;
|
|
|
|
fake_abort_called = true;
|
|
|
|
}
|
|
|
|
|
2020-10-15 07:45:19 +08:00
|
|
|
#define SMALL_SIZE1 SC_SMALL_MAXCLASS
|
|
|
|
#define SMALL_SIZE2 (SC_SMALL_MAXCLASS / 2)
|
2020-08-04 09:23:36 +08:00
|
|
|
|
2020-10-15 07:45:19 +08:00
|
|
|
#define LARGE_SIZE1 SC_LARGE_MINCLASS
|
|
|
|
#define LARGE_SIZE2 (LARGE_SIZE1 * 2)
|
|
|
|
|
2023-05-09 03:37:18 +08:00
|
|
|
static void *
|
2020-10-15 07:45:19 +08:00
|
|
|
test_invalid_size_pre(size_t sz) {
|
2020-08-04 09:23:36 +08:00
|
|
|
safety_check_set_abort(&fake_abort);
|
|
|
|
|
|
|
|
fake_abort_called = false;
|
2020-10-15 07:45:19 +08:00
|
|
|
void *ptr = malloc(sz);
|
2020-08-04 09:23:36 +08:00
|
|
|
assert_ptr_not_null(ptr, "Unexpected failure");
|
|
|
|
|
2020-10-15 07:45:19 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2023-05-09 03:37:18 +08:00
|
|
|
static void
|
2020-10-15 07:45:19 +08:00
|
|
|
test_invalid_size_post(void) {
|
|
|
|
expect_true(fake_abort_called, "Safety check didn't fire");
|
2020-08-04 09:23:36 +08:00
|
|
|
safety_check_set_abort(NULL);
|
|
|
|
}
|
2020-10-15 07:45:19 +08:00
|
|
|
|
|
|
|
TEST_BEGIN(test_invalid_size_sdallocx) {
|
|
|
|
test_skip_if(!config_opt_size_checks);
|
|
|
|
|
|
|
|
void *ptr = test_invalid_size_pre(SMALL_SIZE1);
|
|
|
|
sdallocx(ptr, SMALL_SIZE2, 0);
|
|
|
|
test_invalid_size_post();
|
|
|
|
|
|
|
|
ptr = test_invalid_size_pre(LARGE_SIZE1);
|
|
|
|
sdallocx(ptr, LARGE_SIZE2, 0);
|
|
|
|
test_invalid_size_post();
|
|
|
|
}
|
2020-08-04 09:23:36 +08:00
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_BEGIN(test_invalid_size_sdallocx_nonzero_flag) {
|
|
|
|
test_skip_if(!config_opt_size_checks);
|
|
|
|
|
2020-10-15 07:45:19 +08:00
|
|
|
void *ptr = test_invalid_size_pre(SMALL_SIZE1);
|
|
|
|
sdallocx(ptr, SMALL_SIZE2, MALLOCX_TCACHE_NONE);
|
|
|
|
test_invalid_size_post();
|
2020-08-04 09:23:36 +08:00
|
|
|
|
2020-10-15 07:45:19 +08:00
|
|
|
ptr = test_invalid_size_pre(LARGE_SIZE1);
|
|
|
|
sdallocx(ptr, LARGE_SIZE2, MALLOCX_TCACHE_NONE);
|
|
|
|
test_invalid_size_post();
|
2020-08-04 09:23:36 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_BEGIN(test_invalid_size_sdallocx_noflags) {
|
|
|
|
test_skip_if(!config_opt_size_checks);
|
|
|
|
|
2020-10-15 07:45:19 +08:00
|
|
|
void *ptr = test_invalid_size_pre(SMALL_SIZE1);
|
|
|
|
je_sdallocx_noflags(ptr, SMALL_SIZE2);
|
|
|
|
test_invalid_size_post();
|
2020-08-04 09:23:36 +08:00
|
|
|
|
2020-10-15 07:45:19 +08:00
|
|
|
ptr = test_invalid_size_pre(LARGE_SIZE1);
|
|
|
|
je_sdallocx_noflags(ptr, LARGE_SIZE2);
|
|
|
|
test_invalid_size_post();
|
2020-08-04 09:23:36 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void) {
|
|
|
|
return test(
|
|
|
|
test_invalid_size_sdallocx,
|
|
|
|
test_invalid_size_sdallocx_nonzero_flag,
|
|
|
|
test_invalid_size_sdallocx_noflags);
|
|
|
|
}
|