Blame memkind-1.10.0/jemalloc/test/integration/overflow.c

Packit 345191
#include "test/jemalloc_test.h"
Packit 345191
Packit 345191
/*
Packit 345191
 * GCC "-Walloc-size-larger-than" warning detects when one of the memory
Packit 345191
 * allocation functions is called with a size larger than the maximum size that
Packit 345191
 * they support. Here we want to explicitly test that the allocation functions
Packit 345191
 * do indeed fail properly when this is the case, which triggers the warning.
Packit 345191
 * Therefore we disable the warning for these tests.
Packit 345191
 */
Packit 345191
JEMALLOC_DIAGNOSTIC_PUSH
Packit 345191
JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
Packit 345191
Packit 345191
TEST_BEGIN(test_overflow) {
Packit 345191
	unsigned nlextents;
Packit 345191
	size_t mib[4];
Packit 345191
	size_t sz, miblen, max_size_class;
Packit 345191
	void *p;
Packit 345191
Packit 345191
	sz = sizeof(unsigned);
Packit 345191
	assert_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
Packit 345191
	    0), 0, "Unexpected mallctl() error");
Packit 345191
Packit 345191
	miblen = sizeof(mib) / sizeof(size_t);
Packit 345191
	assert_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
Packit 345191
	    "Unexpected mallctlnametomib() error");
Packit 345191
	mib[2] = nlextents - 1;
Packit 345191
Packit 345191
	sz = sizeof(size_t);
Packit 345191
	assert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
Packit 345191
	    NULL, 0), 0, "Unexpected mallctlbymib() error");
Packit 345191
Packit 345191
	assert_ptr_null(malloc(max_size_class + 1),
Packit 345191
	    "Expected OOM due to over-sized allocation request");
Packit 345191
	assert_ptr_null(malloc(SIZE_T_MAX),
Packit 345191
	    "Expected OOM due to over-sized allocation request");
Packit 345191
Packit 345191
	assert_ptr_null(calloc(1, max_size_class + 1),
Packit 345191
	    "Expected OOM due to over-sized allocation request");
Packit 345191
	assert_ptr_null(calloc(1, SIZE_T_MAX),
Packit 345191
	    "Expected OOM due to over-sized allocation request");
Packit 345191
Packit 345191
	p = malloc(1);
Packit 345191
	assert_ptr_not_null(p, "Unexpected malloc() OOM");
Packit 345191
	assert_ptr_null(realloc(p, max_size_class + 1),
Packit 345191
	    "Expected OOM due to over-sized allocation request");
Packit 345191
	assert_ptr_null(realloc(p, SIZE_T_MAX),
Packit 345191
	    "Expected OOM due to over-sized allocation request");
Packit 345191
	free(p);
Packit 345191
}
Packit 345191
TEST_END
Packit 345191
Packit 345191
/* Re-enable the "-Walloc-size-larger-than=" warning */
Packit 345191
JEMALLOC_DIAGNOSTIC_POP
Packit 345191
Packit 345191
int
Packit 345191
main(void) {
Packit 345191
	return test(
Packit 345191
	    test_overflow);
Packit 345191
}