2015-06-24 09:47:07 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2018-05-03 17:40:53 +08:00
|
|
|
/*
|
|
|
|
* GCC "-Walloc-size-larger-than" warning detects when one of the memory
|
|
|
|
* allocation functions is called with a size larger than the maximum size that
|
|
|
|
* they support. Here we want to explicitly test that the allocation functions
|
|
|
|
* do indeed fail properly when this is the case, which triggers the warning.
|
|
|
|
* Therefore we disable the warning for these tests.
|
|
|
|
*/
|
|
|
|
JEMALLOC_DIAGNOSTIC_PUSH
|
|
|
|
JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_overflow) {
|
2016-06-01 05:50:21 +08:00
|
|
|
unsigned nlextents;
|
2015-06-24 09:47:07 +08:00
|
|
|
size_t mib[4];
|
|
|
|
size_t sz, miblen, max_size_class;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
sz = sizeof(unsigned);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
|
2016-10-28 12:31:25 +08:00
|
|
|
0), 0, "Unexpected mallctl() error");
|
2015-06-24 09:47:07 +08:00
|
|
|
|
|
|
|
miblen = sizeof(mib) / sizeof(size_t);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
|
2015-06-24 09:47:07 +08:00
|
|
|
"Unexpected mallctlnametomib() error");
|
2016-06-01 05:50:21 +08:00
|
|
|
mib[2] = nlextents - 1;
|
2015-06-24 09:47:07 +08:00
|
|
|
|
|
|
|
sz = sizeof(size_t);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
|
2016-10-28 12:31:25 +08:00
|
|
|
NULL, 0), 0, "Unexpected mallctlbymib() error");
|
2015-06-24 09:47:07 +08:00
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(malloc(max_size_class + 1),
|
2015-06-24 09:47:07 +08:00
|
|
|
"Expected OOM due to over-sized allocation request");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(malloc(SIZE_T_MAX),
|
2015-06-24 09:47:07 +08:00
|
|
|
"Expected OOM due to over-sized allocation request");
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(calloc(1, max_size_class + 1),
|
2015-06-24 09:47:07 +08:00
|
|
|
"Expected OOM due to over-sized allocation request");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(calloc(1, SIZE_T_MAX),
|
2015-06-24 09:47:07 +08:00
|
|
|
"Expected OOM due to over-sized allocation request");
|
|
|
|
|
|
|
|
p = malloc(1);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(p, "Unexpected malloc() OOM");
|
|
|
|
expect_ptr_null(realloc(p, max_size_class + 1),
|
2015-06-24 09:47:07 +08:00
|
|
|
"Expected OOM due to over-sized allocation request");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(realloc(p, SIZE_T_MAX),
|
2015-06-24 09:47:07 +08:00
|
|
|
"Expected OOM due to over-sized allocation request");
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2018-05-03 17:40:53 +08:00
|
|
|
/* Re-enable the "-Walloc-size-larger-than=" warning */
|
|
|
|
JEMALLOC_DIAGNOSTIC_POP
|
|
|
|
|
2015-06-24 09:47:07 +08:00
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
|
|
|
test_overflow);
|
2015-06-24 09:47:07 +08:00
|
|
|
}
|