1 #include "test/jemalloc_test.h"
2
3 #ifdef JEMALLOC_FILL
4 const char *malloc_conf = "junk:false";
5 #endif
6
7 /*
8 * Use a separate arena for xallocx() extension/contraction tests so that
9 * internal allocation e.g. by heap profiling can't interpose allocations where
10 * xallocx() would ordinarily be able to extend.
11 */
12 static unsigned
arena_ind(void)13 arena_ind(void)
14 {
15 static unsigned ind = 0;
16
17 if (ind == 0) {
18 size_t sz = sizeof(ind);
19 assert_d_eq(mallctl("arenas.extend", (void *)&ind, &sz, NULL,
20 0), 0, "Unexpected mallctl failure creating arena");
21 }
22
23 return (ind);
24 }
25
TEST_BEGIN(test_same_size)26 TEST_BEGIN(test_same_size)
27 {
28 void *p;
29 size_t sz, tsz;
30
31 p = mallocx(42, 0);
32 assert_ptr_not_null(p, "Unexpected mallocx() error");
33 sz = sallocx(p, 0);
34
35 tsz = xallocx(p, sz, 0, 0);
36 assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
37
38 dallocx(p, 0);
39 }
40 TEST_END
41
TEST_BEGIN(test_extra_no_move)42 TEST_BEGIN(test_extra_no_move)
43 {
44 void *p;
45 size_t sz, tsz;
46
47 p = mallocx(42, 0);
48 assert_ptr_not_null(p, "Unexpected mallocx() error");
49 sz = sallocx(p, 0);
50
51 tsz = xallocx(p, sz, sz-42, 0);
52 assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
53
54 dallocx(p, 0);
55 }
56 TEST_END
57
TEST_BEGIN(test_no_move_fail)58 TEST_BEGIN(test_no_move_fail)
59 {
60 void *p;
61 size_t sz, tsz;
62
63 p = mallocx(42, 0);
64 assert_ptr_not_null(p, "Unexpected mallocx() error");
65 sz = sallocx(p, 0);
66
67 tsz = xallocx(p, sz + 5, 0, 0);
68 assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
69
70 dallocx(p, 0);
71 }
72 TEST_END
73
74 static unsigned
get_nsizes_impl(const char * cmd)75 get_nsizes_impl(const char *cmd)
76 {
77 unsigned ret;
78 size_t z;
79
80 z = sizeof(unsigned);
81 assert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
82 "Unexpected mallctl(\"%s\", ...) failure", cmd);
83
84 return (ret);
85 }
86
87 static unsigned
get_nsmall(void)88 get_nsmall(void)
89 {
90
91 return (get_nsizes_impl("arenas.nbins"));
92 }
93
94 static unsigned
get_nlarge(void)95 get_nlarge(void)
96 {
97
98 return (get_nsizes_impl("arenas.nlruns"));
99 }
100
101 static unsigned
get_nhuge(void)102 get_nhuge(void)
103 {
104
105 return (get_nsizes_impl("arenas.nhchunks"));
106 }
107
108 static size_t
get_size_impl(const char * cmd,size_t ind)109 get_size_impl(const char *cmd, size_t ind)
110 {
111 size_t ret;
112 size_t z;
113 size_t mib[4];
114 size_t miblen = 4;
115
116 z = sizeof(size_t);
117 assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
118 0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
119 mib[2] = ind;
120 z = sizeof(size_t);
121 assert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
122 0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
123
124 return (ret);
125 }
126
127 static size_t
get_small_size(size_t ind)128 get_small_size(size_t ind)
129 {
130
131 return (get_size_impl("arenas.bin.0.size", ind));
132 }
133
134 static size_t
get_large_size(size_t ind)135 get_large_size(size_t ind)
136 {
137
138 return (get_size_impl("arenas.lrun.0.size", ind));
139 }
140
141 static size_t
get_huge_size(size_t ind)142 get_huge_size(size_t ind)
143 {
144
145 return (get_size_impl("arenas.hchunk.0.size", ind));
146 }
147
TEST_BEGIN(test_size)148 TEST_BEGIN(test_size)
149 {
150 size_t small0, hugemax;
151 void *p;
152
153 /* Get size classes. */
154 small0 = get_small_size(0);
155 hugemax = get_huge_size(get_nhuge()-1);
156
157 p = mallocx(small0, 0);
158 assert_ptr_not_null(p, "Unexpected mallocx() error");
159
160 /* Test smallest supported size. */
161 assert_zu_eq(xallocx(p, 1, 0, 0), small0,
162 "Unexpected xallocx() behavior");
163
164 /* Test largest supported size. */
165 assert_zu_le(xallocx(p, hugemax, 0, 0), hugemax,
166 "Unexpected xallocx() behavior");
167
168 /* Test size overflow. */
169 assert_zu_le(xallocx(p, hugemax+1, 0, 0), hugemax,
170 "Unexpected xallocx() behavior");
171 assert_zu_le(xallocx(p, SIZE_T_MAX, 0, 0), hugemax,
172 "Unexpected xallocx() behavior");
173
174 dallocx(p, 0);
175 }
176 TEST_END
177
TEST_BEGIN(test_size_extra_overflow)178 TEST_BEGIN(test_size_extra_overflow)
179 {
180 size_t small0, hugemax;
181 void *p;
182
183 /* Get size classes. */
184 small0 = get_small_size(0);
185 hugemax = get_huge_size(get_nhuge()-1);
186
187 p = mallocx(small0, 0);
188 assert_ptr_not_null(p, "Unexpected mallocx() error");
189
190 /* Test overflows that can be resolved by clamping extra. */
191 assert_zu_le(xallocx(p, hugemax-1, 2, 0), hugemax,
192 "Unexpected xallocx() behavior");
193 assert_zu_le(xallocx(p, hugemax, 1, 0), hugemax,
194 "Unexpected xallocx() behavior");
195
196 /* Test overflow such that hugemax-size underflows. */
197 assert_zu_le(xallocx(p, hugemax+1, 2, 0), hugemax,
198 "Unexpected xallocx() behavior");
199 assert_zu_le(xallocx(p, hugemax+2, 3, 0), hugemax,
200 "Unexpected xallocx() behavior");
201 assert_zu_le(xallocx(p, SIZE_T_MAX-2, 2, 0), hugemax,
202 "Unexpected xallocx() behavior");
203 assert_zu_le(xallocx(p, SIZE_T_MAX-1, 1, 0), hugemax,
204 "Unexpected xallocx() behavior");
205
206 dallocx(p, 0);
207 }
208 TEST_END
209
TEST_BEGIN(test_extra_small)210 TEST_BEGIN(test_extra_small)
211 {
212 size_t small0, small1, hugemax;
213 void *p;
214
215 /* Get size classes. */
216 small0 = get_small_size(0);
217 small1 = get_small_size(1);
218 hugemax = get_huge_size(get_nhuge()-1);
219
220 p = mallocx(small0, 0);
221 assert_ptr_not_null(p, "Unexpected mallocx() error");
222
223 assert_zu_eq(xallocx(p, small1, 0, 0), small0,
224 "Unexpected xallocx() behavior");
225
226 assert_zu_eq(xallocx(p, small1, 0, 0), small0,
227 "Unexpected xallocx() behavior");
228
229 assert_zu_eq(xallocx(p, small0, small1 - small0, 0), small0,
230 "Unexpected xallocx() behavior");
231
232 /* Test size+extra overflow. */
233 assert_zu_eq(xallocx(p, small0, hugemax - small0 + 1, 0), small0,
234 "Unexpected xallocx() behavior");
235 assert_zu_eq(xallocx(p, small0, SIZE_T_MAX - small0, 0), small0,
236 "Unexpected xallocx() behavior");
237
238 dallocx(p, 0);
239 }
240 TEST_END
241
TEST_BEGIN(test_extra_large)242 TEST_BEGIN(test_extra_large)
243 {
244 int flags = MALLOCX_ARENA(arena_ind());
245 size_t smallmax, large0, large1, large2, huge0, hugemax;
246 void *p;
247
248 /* Get size classes. */
249 smallmax = get_small_size(get_nsmall()-1);
250 large0 = get_large_size(0);
251 large1 = get_large_size(1);
252 large2 = get_large_size(2);
253 huge0 = get_huge_size(0);
254 hugemax = get_huge_size(get_nhuge()-1);
255
256 p = mallocx(large2, flags);
257 assert_ptr_not_null(p, "Unexpected mallocx() error");
258
259 assert_zu_eq(xallocx(p, large2, 0, flags), large2,
260 "Unexpected xallocx() behavior");
261 /* Test size decrease with zero extra. */
262 assert_zu_eq(xallocx(p, large0, 0, flags), large0,
263 "Unexpected xallocx() behavior");
264 assert_zu_eq(xallocx(p, smallmax, 0, flags), large0,
265 "Unexpected xallocx() behavior");
266
267 assert_zu_eq(xallocx(p, large2, 0, flags), large2,
268 "Unexpected xallocx() behavior");
269 /* Test size decrease with non-zero extra. */
270 assert_zu_eq(xallocx(p, large0, large2 - large0, flags), large2,
271 "Unexpected xallocx() behavior");
272 assert_zu_eq(xallocx(p, large1, large2 - large1, flags), large2,
273 "Unexpected xallocx() behavior");
274 assert_zu_eq(xallocx(p, large0, large1 - large0, flags), large1,
275 "Unexpected xallocx() behavior");
276 assert_zu_eq(xallocx(p, smallmax, large0 - smallmax, flags), large0,
277 "Unexpected xallocx() behavior");
278
279 assert_zu_eq(xallocx(p, large0, 0, flags), large0,
280 "Unexpected xallocx() behavior");
281 /* Test size increase with zero extra. */
282 assert_zu_eq(xallocx(p, large2, 0, flags), large2,
283 "Unexpected xallocx() behavior");
284 assert_zu_eq(xallocx(p, huge0, 0, flags), large2,
285 "Unexpected xallocx() behavior");
286
287 assert_zu_eq(xallocx(p, large0, 0, flags), large0,
288 "Unexpected xallocx() behavior");
289 /* Test size increase with non-zero extra. */
290 assert_zu_lt(xallocx(p, large0, huge0 - large0, flags), huge0,
291 "Unexpected xallocx() behavior");
292
293 assert_zu_eq(xallocx(p, large0, 0, flags), large0,
294 "Unexpected xallocx() behavior");
295 /* Test size increase with non-zero extra. */
296 assert_zu_eq(xallocx(p, large0, large2 - large0, flags), large2,
297 "Unexpected xallocx() behavior");
298
299 assert_zu_eq(xallocx(p, large2, 0, flags), large2,
300 "Unexpected xallocx() behavior");
301 /* Test size+extra overflow. */
302 assert_zu_lt(xallocx(p, large2, hugemax - large2 + 1, flags), huge0,
303 "Unexpected xallocx() behavior");
304
305 dallocx(p, flags);
306 }
307 TEST_END
308
TEST_BEGIN(test_extra_huge)309 TEST_BEGIN(test_extra_huge)
310 {
311 int flags = MALLOCX_ARENA(arena_ind());
312 size_t largemax, huge1, huge2, huge3, hugemax;
313 void *p;
314
315 /* Get size classes. */
316 largemax = get_large_size(get_nlarge()-1);
317 huge1 = get_huge_size(1);
318 huge2 = get_huge_size(2);
319 huge3 = get_huge_size(3);
320 hugemax = get_huge_size(get_nhuge()-1);
321
322 p = mallocx(huge3, flags);
323 assert_ptr_not_null(p, "Unexpected mallocx() error");
324
325 assert_zu_eq(xallocx(p, huge3, 0, flags), huge3,
326 "Unexpected xallocx() behavior");
327 /* Test size decrease with zero extra. */
328 assert_zu_ge(xallocx(p, huge1, 0, flags), huge1,
329 "Unexpected xallocx() behavior");
330 assert_zu_ge(xallocx(p, largemax, 0, flags), huge1,
331 "Unexpected xallocx() behavior");
332
333 assert_zu_eq(xallocx(p, huge3, 0, flags), huge3,
334 "Unexpected xallocx() behavior");
335 /* Test size decrease with non-zero extra. */
336 assert_zu_eq(xallocx(p, huge1, huge3 - huge1, flags), huge3,
337 "Unexpected xallocx() behavior");
338 assert_zu_eq(xallocx(p, huge2, huge3 - huge2, flags), huge3,
339 "Unexpected xallocx() behavior");
340 assert_zu_eq(xallocx(p, huge1, huge2 - huge1, flags), huge2,
341 "Unexpected xallocx() behavior");
342 assert_zu_ge(xallocx(p, largemax, huge1 - largemax, flags), huge1,
343 "Unexpected xallocx() behavior");
344
345 assert_zu_ge(xallocx(p, huge1, 0, flags), huge1,
346 "Unexpected xallocx() behavior");
347 /* Test size increase with zero extra. */
348 assert_zu_le(xallocx(p, huge3, 0, flags), huge3,
349 "Unexpected xallocx() behavior");
350 assert_zu_le(xallocx(p, hugemax+1, 0, flags), huge3,
351 "Unexpected xallocx() behavior");
352
353 assert_zu_ge(xallocx(p, huge1, 0, flags), huge1,
354 "Unexpected xallocx() behavior");
355 /* Test size increase with non-zero extra. */
356 assert_zu_le(xallocx(p, huge1, SIZE_T_MAX - huge1, flags), hugemax,
357 "Unexpected xallocx() behavior");
358
359 assert_zu_ge(xallocx(p, huge1, 0, flags), huge1,
360 "Unexpected xallocx() behavior");
361 /* Test size increase with non-zero extra. */
362 assert_zu_le(xallocx(p, huge1, huge3 - huge1, flags), huge3,
363 "Unexpected xallocx() behavior");
364
365 assert_zu_eq(xallocx(p, huge3, 0, flags), huge3,
366 "Unexpected xallocx() behavior");
367 /* Test size+extra overflow. */
368 assert_zu_le(xallocx(p, huge3, hugemax - huge3 + 1, flags), hugemax,
369 "Unexpected xallocx() behavior");
370
371 dallocx(p, flags);
372 }
373 TEST_END
374
375 static void
print_filled_extents(const void * p,uint8_t c,size_t len)376 print_filled_extents(const void *p, uint8_t c, size_t len)
377 {
378 const uint8_t *pc = (const uint8_t *)p;
379 size_t i, range0;
380 uint8_t c0;
381
382 malloc_printf(" p=%p, c=%#x, len=%zu:", p, c, len);
383 range0 = 0;
384 c0 = pc[0];
385 for (i = 0; i < len; i++) {
386 if (pc[i] != c0) {
387 malloc_printf(" %#x[%zu..%zu)", c0, range0, i);
388 range0 = i;
389 c0 = pc[i];
390 }
391 }
392 malloc_printf(" %#x[%zu..%zu)\n", c0, range0, i);
393 }
394
395 static bool
validate_fill(const void * p,uint8_t c,size_t offset,size_t len)396 validate_fill(const void *p, uint8_t c, size_t offset, size_t len)
397 {
398 const uint8_t *pc = (const uint8_t *)p;
399 bool err;
400 size_t i;
401
402 for (i = offset, err = false; i < offset+len; i++) {
403 if (pc[i] != c)
404 err = true;
405 }
406
407 if (err)
408 print_filled_extents(p, c, offset + len);
409
410 return (err);
411 }
412
413 static void
test_zero(size_t szmin,size_t szmax)414 test_zero(size_t szmin, size_t szmax)
415 {
416 int flags = MALLOCX_ARENA(arena_ind()) | MALLOCX_ZERO;
417 size_t sz, nsz;
418 void *p;
419 #define FILL_BYTE 0x7aU
420
421 sz = szmax;
422 p = mallocx(sz, flags);
423 assert_ptr_not_null(p, "Unexpected mallocx() error");
424 assert_false(validate_fill(p, 0x00, 0, sz), "Memory not filled: sz=%zu",
425 sz);
426
427 /*
428 * Fill with non-zero so that non-debug builds are more likely to detect
429 * errors.
430 */
431 memset(p, FILL_BYTE, sz);
432 assert_false(validate_fill(p, FILL_BYTE, 0, sz),
433 "Memory not filled: sz=%zu", sz);
434
435 /* Shrink in place so that we can expect growing in place to succeed. */
436 sz = szmin;
437 assert_zu_eq(xallocx(p, sz, 0, flags), sz,
438 "Unexpected xallocx() error");
439 assert_false(validate_fill(p, FILL_BYTE, 0, sz),
440 "Memory not filled: sz=%zu", sz);
441
442 for (sz = szmin; sz < szmax; sz = nsz) {
443 nsz = nallocx(sz+1, flags);
444 assert_zu_eq(xallocx(p, sz+1, 0, flags), nsz,
445 "Unexpected xallocx() failure");
446 assert_false(validate_fill(p, FILL_BYTE, 0, sz),
447 "Memory not filled: sz=%zu", sz);
448 assert_false(validate_fill(p, 0x00, sz, nsz-sz),
449 "Memory not filled: sz=%zu, nsz-sz=%zu", sz, nsz-sz);
450 memset((void *)((uintptr_t)p + sz), FILL_BYTE, nsz-sz);
451 assert_false(validate_fill(p, FILL_BYTE, 0, nsz),
452 "Memory not filled: nsz=%zu", nsz);
453 }
454
455 dallocx(p, flags);
456 }
457
TEST_BEGIN(test_zero_large)458 TEST_BEGIN(test_zero_large)
459 {
460 size_t large0, largemax;
461
462 /* Get size classes. */
463 large0 = get_large_size(0);
464 largemax = get_large_size(get_nlarge()-1);
465
466 test_zero(large0, largemax);
467 }
468 TEST_END
469
TEST_BEGIN(test_zero_huge)470 TEST_BEGIN(test_zero_huge)
471 {
472 size_t huge0, huge1;
473
474 /* Get size classes. */
475 huge0 = get_huge_size(0);
476 huge1 = get_huge_size(1);
477
478 test_zero(huge1, huge0 * 2);
479 }
480 TEST_END
481
482 int
main(void)483 main(void)
484 {
485
486 return (test(
487 test_same_size,
488 test_extra_no_move,
489 test_no_move_fail,
490 test_size,
491 test_size_extra_overflow,
492 test_extra_small,
493 test_extra_large,
494 test_extra_huge,
495 test_zero_large,
496 test_zero_huge));
497 }
498