/*
* On systems which can't merge extents, tests that call this function generate
* a lot of dirty memory very quickly. Purging between cycles mitigates
* potential OOM on e.g. 32-bit Windows.
*/
static void
purge(void) {
expect_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
"Unexpected mallctl error");
}
/*
* 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
TEST_BEGIN(test_overflow) {
size_t largemax;
largemax = get_large_size(get_nlarge()-1);
expect_ptr_null(smallocx(largemax+1, 0).ptr,
"Expected OOM for smallocx(size=%#zx, 0)", largemax+1);
expect_ptr_null(smallocx(ZU(PTRDIFF_MAX)+1, 0).ptr,
"Expected OOM for smallocx(size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
expect_ptr_null(smallocx(SIZE_T_MAX, 0).ptr,
"Expected OOM for smallocx(size=%#zx, 0)", SIZE_T_MAX);
expect_ptr_null(smallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)).ptr,
"Expected OOM for smallocx(size=1, MALLOCX_ALIGN(%#zx))",
ZU(PTRDIFF_MAX)+1);
}
TEST_END
/*
* It should be impossible to allocate three objects that each consume
* nearly half the virtual address space.
*/
largemax = get_large_size(get_nlarge()-1);
oom = false;
for (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {
ptrs[i] = smallocx(largemax, 0).ptr;
if (ptrs[i] == NULL) {
oom = true;
}
}
expect_true(oom,
"Expected OOM during series of calls to smallocx(size=%zu, 0)",
largemax);
for (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {
if (ptrs[i] != NULL) {
dallocx(ptrs[i], 0);
}
}
purge();
#if LG_SIZEOF_PTR == 3
expect_ptr_null(smallocx(0x8000000000000000ULL,
MALLOCX_ALIGN(0x8000000000000000ULL)).ptr,
"Expected OOM for smallocx()");
expect_ptr_null(smallocx(0x8000000000000000ULL,
MALLOCX_ALIGN(0x80000000)).ptr,
"Expected OOM for smallocx()");
#else
expect_ptr_null(smallocx(0x80000000UL, MALLOCX_ALIGN(0x80000000UL)).ptr,
"Expected OOM for smallocx()");
#endif
}
TEST_END
/* Re-enable the "-Walloc-size-larger-than=" warning */
JEMALLOC_DIAGNOSTIC_POP