xref: /netbsd-src/external/bsd/jemalloc/dist/test/integration/overflow.c (revision 7bdf38e5b7a28439665f2fdeff81e36913eef7dd)
1a0698ed9Schristos #include "test/jemalloc_test.h"
2a0698ed9Schristos 
3*7bdf38e5Schristos /*
4*7bdf38e5Schristos  * GCC "-Walloc-size-larger-than" warning detects when one of the memory
5*7bdf38e5Schristos  * allocation functions is called with a size larger than the maximum size that
6*7bdf38e5Schristos  * they support. Here we want to explicitly test that the allocation functions
7*7bdf38e5Schristos  * do indeed fail properly when this is the case, which triggers the warning.
8*7bdf38e5Schristos  * Therefore we disable the warning for these tests.
9*7bdf38e5Schristos  */
10*7bdf38e5Schristos JEMALLOC_DIAGNOSTIC_PUSH
11*7bdf38e5Schristos JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
12*7bdf38e5Schristos 
13a0698ed9Schristos TEST_BEGIN(test_overflow) {
14a0698ed9Schristos 	unsigned nlextents;
15a0698ed9Schristos 	size_t mib[4];
16a0698ed9Schristos 	size_t sz, miblen, max_size_class;
17a0698ed9Schristos 	void *p;
18a0698ed9Schristos 
19a0698ed9Schristos 	sz = sizeof(unsigned);
20*7bdf38e5Schristos 	expect_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
21a0698ed9Schristos 	    0), 0, "Unexpected mallctl() error");
22a0698ed9Schristos 
23a0698ed9Schristos 	miblen = sizeof(mib) / sizeof(size_t);
24*7bdf38e5Schristos 	expect_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
25a0698ed9Schristos 	    "Unexpected mallctlnametomib() error");
26a0698ed9Schristos 	mib[2] = nlextents - 1;
27a0698ed9Schristos 
28a0698ed9Schristos 	sz = sizeof(size_t);
29*7bdf38e5Schristos 	expect_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
30a0698ed9Schristos 	    NULL, 0), 0, "Unexpected mallctlbymib() error");
31a0698ed9Schristos 
32*7bdf38e5Schristos 	expect_ptr_null(malloc(max_size_class + 1),
33a0698ed9Schristos 	    "Expected OOM due to over-sized allocation request");
34*7bdf38e5Schristos 	expect_ptr_null(malloc(SIZE_T_MAX),
35a0698ed9Schristos 	    "Expected OOM due to over-sized allocation request");
36a0698ed9Schristos 
37*7bdf38e5Schristos 	expect_ptr_null(calloc(1, max_size_class + 1),
38a0698ed9Schristos 	    "Expected OOM due to over-sized allocation request");
39*7bdf38e5Schristos 	expect_ptr_null(calloc(1, SIZE_T_MAX),
40a0698ed9Schristos 	    "Expected OOM due to over-sized allocation request");
41a0698ed9Schristos 
42a0698ed9Schristos 	p = malloc(1);
43*7bdf38e5Schristos 	expect_ptr_not_null(p, "Unexpected malloc() OOM");
44*7bdf38e5Schristos 	expect_ptr_null(realloc(p, max_size_class + 1),
45a0698ed9Schristos 	    "Expected OOM due to over-sized allocation request");
46*7bdf38e5Schristos 	expect_ptr_null(realloc(p, SIZE_T_MAX),
47a0698ed9Schristos 	    "Expected OOM due to over-sized allocation request");
48a0698ed9Schristos 	free(p);
49a0698ed9Schristos }
50a0698ed9Schristos TEST_END
51a0698ed9Schristos 
52*7bdf38e5Schristos /* Re-enable the "-Walloc-size-larger-than=" warning */
53*7bdf38e5Schristos JEMALLOC_DIAGNOSTIC_POP
54*7bdf38e5Schristos 
55a0698ed9Schristos int
56a0698ed9Schristos main(void) {
57a0698ed9Schristos 	return test(
58a0698ed9Schristos 	    test_overflow);
59a0698ed9Schristos }
60