blob: 30ebe37a4504fa9a25442d5dfa76923bb1198177 [file] [log] [blame]
Jason Evans6b694c42014-01-07 16:47:56 -08001#include "test/jemalloc_test.h"
2
3#ifdef JEMALLOC_FILL
4const char *malloc_conf =
5 "abort:false,junk:false,zero:true,redzone:false,quarantine:0";
6#endif
7
8static void
9test_zero(size_t sz_min, size_t sz_max)
10{
Jason Evans96aa67a2016-04-04 19:55:19 -040011 uint8_t *s;
Jason Evans6b694c42014-01-07 16:47:56 -080012 size_t sz_prev, sz, i;
Jason Evans96aa67a2016-04-04 19:55:19 -040013#define MAGIC ((uint8_t)0x61)
Jason Evans6b694c42014-01-07 16:47:56 -080014
15 sz_prev = 0;
Jason Evans96aa67a2016-04-04 19:55:19 -040016 s = (uint8_t *)mallocx(sz_min, 0);
Jason Evans6b694c42014-01-07 16:47:56 -080017 assert_ptr_not_null((void *)s, "Unexpected mallocx() failure");
18
19 for (sz = sallocx(s, 0); sz <= sz_max;
20 sz_prev = sz, sz = sallocx(s, 0)) {
21 if (sz_prev > 0) {
Jason Evans96aa67a2016-04-04 19:55:19 -040022 assert_u_eq(s[0], MAGIC,
Jason Evans6b694c42014-01-07 16:47:56 -080023 "Previously allocated byte %zu/%zu is corrupted",
Jason Evansab8c79f2014-03-30 11:21:09 -070024 ZU(0), sz_prev);
Jason Evans96aa67a2016-04-04 19:55:19 -040025 assert_u_eq(s[sz_prev-1], MAGIC,
Jason Evans6b694c42014-01-07 16:47:56 -080026 "Previously allocated byte %zu/%zu is corrupted",
27 sz_prev-1, sz_prev);
28 }
29
30 for (i = sz_prev; i < sz; i++) {
Jason Evans96aa67a2016-04-04 19:55:19 -040031 assert_u_eq(s[i], 0x0,
Jason Evans6b694c42014-01-07 16:47:56 -080032 "Newly allocated byte %zu/%zu isn't zero-filled",
33 i, sz);
Jason Evans96aa67a2016-04-04 19:55:19 -040034 s[i] = MAGIC;
Jason Evans6b694c42014-01-07 16:47:56 -080035 }
36
37 if (xallocx(s, sz+1, 0, 0) == sz) {
Jason Evans96aa67a2016-04-04 19:55:19 -040038 s = (uint8_t *)rallocx(s, sz+1, 0);
Jason Evans6b694c42014-01-07 16:47:56 -080039 assert_ptr_not_null((void *)s,
40 "Unexpected rallocx() failure");
41 }
42 }
43
44 dallocx(s, 0);
Jason Evans96aa67a2016-04-04 19:55:19 -040045#undef MAGIC
Jason Evans6b694c42014-01-07 16:47:56 -080046}
47
48TEST_BEGIN(test_zero_small)
49{
50
51 test_skip_if(!config_fill);
52 test_zero(1, SMALL_MAXCLASS-1);
53}
54TEST_END
55
56TEST_BEGIN(test_zero_large)
57{
58
59 test_skip_if(!config_fill);
Jason Evans676df882015-09-11 20:50:20 -070060 test_zero(SMALL_MAXCLASS+1, large_maxclass);
Jason Evans6b694c42014-01-07 16:47:56 -080061}
62TEST_END
63
64TEST_BEGIN(test_zero_huge)
65{
66
67 test_skip_if(!config_fill);
Jason Evans676df882015-09-11 20:50:20 -070068 test_zero(large_maxclass+1, chunksize*2);
Jason Evans6b694c42014-01-07 16:47:56 -080069}
70TEST_END
71
72int
73main(void)
74{
75
76 return (test(
77 test_zero_small,
78 test_zero_large,
79 test_zero_huge));
80}