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