1 #include "test/jemalloc_test.h"
2 
3 static unsigned
get_nsizes_impl(const char * cmd)4 get_nsizes_impl(const char *cmd) {
5 	unsigned ret;
6 	size_t z;
7 
8 	z = sizeof(unsigned);
9 	assert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
10 	    "Unexpected mallctl(\"%s\", ...) failure", cmd);
11 
12 	return ret;
13 }
14 
15 static unsigned
get_nlarge(void)16 get_nlarge(void) {
17 	return get_nsizes_impl("arenas.nlextents");
18 }
19 
20 static size_t
get_size_impl(const char * cmd,size_t ind)21 get_size_impl(const char *cmd, size_t ind) {
22 	size_t ret;
23 	size_t z;
24 	size_t mib[4];
25 	size_t miblen = 4;
26 
27 	z = sizeof(size_t);
28 	assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
29 	    0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
30 	mib[2] = ind;
31 	z = sizeof(size_t);
32 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
33 	    0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
34 
35 	return ret;
36 }
37 
38 static size_t
get_large_size(size_t ind)39 get_large_size(size_t ind) {
40 	return get_size_impl("arenas.lextent.0.size", ind);
41 }
42 
TEST_BEGIN(test_grow_and_shrink)43 TEST_BEGIN(test_grow_and_shrink) {
44 	void *p, *q;
45 	size_t tsz;
46 #define NCYCLES 3
47 	unsigned i, j;
48 #define NSZS 1024
49 	size_t szs[NSZS];
50 #define MAXSZ ZU(12 * 1024 * 1024)
51 
52 	p = mallocx(1, 0);
53 	assert_ptr_not_null(p, "Unexpected mallocx() error");
54 	szs[0] = sallocx(p, 0);
55 
56 	for (i = 0; i < NCYCLES; i++) {
57 		for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
58 			q = rallocx(p, szs[j-1]+1, 0);
59 			assert_ptr_not_null(q,
60 			    "Unexpected rallocx() error for size=%zu-->%zu",
61 			    szs[j-1], szs[j-1]+1);
62 			szs[j] = sallocx(q, 0);
63 			assert_zu_ne(szs[j], szs[j-1]+1,
64 			    "Expected size to be at least: %zu", szs[j-1]+1);
65 			p = q;
66 		}
67 
68 		for (j--; j > 0; j--) {
69 			q = rallocx(p, szs[j-1], 0);
70 			assert_ptr_not_null(q,
71 			    "Unexpected rallocx() error for size=%zu-->%zu",
72 			    szs[j], szs[j-1]);
73 			tsz = sallocx(q, 0);
74 			assert_zu_eq(tsz, szs[j-1],
75 			    "Expected size=%zu, got size=%zu", szs[j-1], tsz);
76 			p = q;
77 		}
78 	}
79 
80 	dallocx(p, 0);
81 #undef MAXSZ
82 #undef NSZS
83 #undef NCYCLES
84 }
85 TEST_END
86 
87 static bool
validate_fill(const void * p,uint8_t c,size_t offset,size_t len)88 validate_fill(const void *p, uint8_t c, size_t offset, size_t len) {
89 	bool ret = false;
90 	const uint8_t *buf = (const uint8_t *)p;
91 	size_t i;
92 
93 	for (i = 0; i < len; i++) {
94 		uint8_t b = buf[offset+i];
95 		if (b != c) {
96 			test_fail("Allocation at %p (len=%zu) contains %#x "
97 			    "rather than %#x at offset %zu", p, len, b, c,
98 			    offset+i);
99 			ret = true;
100 		}
101 	}
102 
103 	return ret;
104 }
105 
TEST_BEGIN(test_zero)106 TEST_BEGIN(test_zero) {
107 	void *p, *q;
108 	size_t psz, qsz, i, j;
109 	size_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};
110 #define FILL_BYTE 0xaaU
111 #define RANGE 2048
112 
113 	for (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {
114 		size_t start_size = start_sizes[i];
115 		p = mallocx(start_size, MALLOCX_ZERO);
116 		assert_ptr_not_null(p, "Unexpected mallocx() error");
117 		psz = sallocx(p, 0);
118 
119 		assert_false(validate_fill(p, 0, 0, psz),
120 		    "Expected zeroed memory");
121 		memset(p, FILL_BYTE, psz);
122 		assert_false(validate_fill(p, FILL_BYTE, 0, psz),
123 		    "Expected filled memory");
124 
125 		for (j = 1; j < RANGE; j++) {
126 			q = rallocx(p, start_size+j, MALLOCX_ZERO);
127 			assert_ptr_not_null(q, "Unexpected rallocx() error");
128 			qsz = sallocx(q, 0);
129 			if (q != p || qsz != psz) {
130 				assert_false(validate_fill(q, FILL_BYTE, 0,
131 				    psz), "Expected filled memory");
132 				assert_false(validate_fill(q, 0, psz, qsz-psz),
133 				    "Expected zeroed memory");
134 			}
135 			if (psz != qsz) {
136 				memset((void *)((uintptr_t)q+psz), FILL_BYTE,
137 				    qsz-psz);
138 				psz = qsz;
139 			}
140 			p = q;
141 		}
142 		assert_false(validate_fill(p, FILL_BYTE, 0, psz),
143 		    "Expected filled memory");
144 		dallocx(p, 0);
145 	}
146 #undef FILL_BYTE
147 }
148 TEST_END
149 
TEST_BEGIN(test_align)150 TEST_BEGIN(test_align) {
151 	void *p, *q;
152 	size_t align;
153 #define MAX_ALIGN (ZU(1) << 25)
154 
155 	align = ZU(1);
156 	p = mallocx(1, MALLOCX_ALIGN(align));
157 	assert_ptr_not_null(p, "Unexpected mallocx() error");
158 
159 	for (align <<= 1; align <= MAX_ALIGN; align <<= 1) {
160 		q = rallocx(p, 1, MALLOCX_ALIGN(align));
161 		assert_ptr_not_null(q,
162 		    "Unexpected rallocx() error for align=%zu", align);
163 		assert_ptr_null(
164 		    (void *)((uintptr_t)q & (align-1)),
165 		    "%p inadequately aligned for align=%zu",
166 		    q, align);
167 		p = q;
168 	}
169 	dallocx(p, 0);
170 #undef MAX_ALIGN
171 }
172 TEST_END
173 
TEST_BEGIN(test_lg_align_and_zero)174 TEST_BEGIN(test_lg_align_and_zero) {
175 	void *p, *q;
176 	unsigned lg_align;
177 	size_t sz;
178 #define MAX_LG_ALIGN 25
179 #define MAX_VALIDATE (ZU(1) << 22)
180 
181 	lg_align = 0;
182 	p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
183 	assert_ptr_not_null(p, "Unexpected mallocx() error");
184 
185 	for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {
186 		q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
187 		assert_ptr_not_null(q,
188 		    "Unexpected rallocx() error for lg_align=%u", lg_align);
189 		assert_ptr_null(
190 		    (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),
191 		    "%p inadequately aligned for lg_align=%u", q, lg_align);
192 		sz = sallocx(q, 0);
193 		if ((sz << 1) <= MAX_VALIDATE) {
194 			assert_false(validate_fill(q, 0, 0, sz),
195 			    "Expected zeroed memory");
196 		} else {
197 			assert_false(validate_fill(q, 0, 0, MAX_VALIDATE),
198 			    "Expected zeroed memory");
199 			assert_false(validate_fill(
200 			    (void *)((uintptr_t)q+sz-MAX_VALIDATE),
201 			    0, 0, MAX_VALIDATE), "Expected zeroed memory");
202 		}
203 		p = q;
204 	}
205 	dallocx(p, 0);
206 #undef MAX_VALIDATE
207 #undef MAX_LG_ALIGN
208 }
209 TEST_END
210 
211 /*
212  * GCC "-Walloc-size-larger-than" warning detects when one of the memory
213  * allocation functions is called with a size larger than the maximum size that
214  * they support. Here we want to explicitly test that the allocation functions
215  * do indeed fail properly when this is the case, which triggers the warning.
216  * Therefore we disable the warning for these tests.
217  */
218 JEMALLOC_DIAGNOSTIC_PUSH
219 JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
220 
TEST_BEGIN(test_overflow)221 TEST_BEGIN(test_overflow) {
222 	size_t largemax;
223 	void *p;
224 
225 	largemax = get_large_size(get_nlarge()-1);
226 
227 	p = mallocx(1, 0);
228 	assert_ptr_not_null(p, "Unexpected mallocx() failure");
229 
230 	assert_ptr_null(rallocx(p, largemax+1, 0),
231 	    "Expected OOM for rallocx(p, size=%#zx, 0)", largemax+1);
232 
233 	assert_ptr_null(rallocx(p, ZU(PTRDIFF_MAX)+1, 0),
234 	    "Expected OOM for rallocx(p, size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
235 
236 	assert_ptr_null(rallocx(p, SIZE_T_MAX, 0),
237 	    "Expected OOM for rallocx(p, size=%#zx, 0)", SIZE_T_MAX);
238 
239 	assert_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),
240 	    "Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))",
241 	    ZU(PTRDIFF_MAX)+1);
242 
243 	dallocx(p, 0);
244 }
245 TEST_END
246 
247 /* Re-enable the "-Walloc-size-larger-than=" warning */
248 JEMALLOC_DIAGNOSTIC_POP
249 
250 int
main(void)251 main(void) {
252 	return test(
253 	    test_grow_and_shrink,
254 	    test_zero,
255 	    test_align,
256 	    test_lg_align_and_zero,
257 	    test_overflow);
258 }
259