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