blob: 66ad8660a46be173b8b33883677b7a4bf6ae2986 [file] [log] [blame]
Jason Evansd82a5e62013-12-12 22:35:52 -08001#include "test/jemalloc_test.h"
2
Christopher Ferrise4294032016-03-02 14:33:02 -08003static unsigned
4get_nsizes_impl(const char *cmd)
5{
6 unsigned ret;
7 size_t z;
8
9 z = sizeof(unsigned);
10 assert_d_eq(mallctl(cmd, &ret, &z, NULL, 0), 0,
11 "Unexpected mallctl(\"%s\", ...) failure", cmd);
12
13 return (ret);
14}
15
16static unsigned
17get_nhuge(void)
18{
19
20 return (get_nsizes_impl("arenas.nhchunks"));
21}
22
23static size_t
24get_size_impl(const char *cmd, size_t ind)
25{
26 size_t ret;
27 size_t z;
28 size_t mib[4];
29 size_t miblen = 4;
30
31 z = sizeof(size_t);
32 assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
33 0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
34 mib[2] = ind;
35 z = sizeof(size_t);
36 assert_d_eq(mallctlbymib(mib, miblen, &ret, &z, NULL, 0),
37 0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
38
39 return (ret);
40}
41
42static size_t
43get_huge_size(size_t ind)
44{
45
46 return (get_size_impl("arenas.hchunk.0.size", ind));
47}
48
Jason Evansd82a5e62013-12-12 22:35:52 -080049TEST_BEGIN(test_grow_and_shrink)
50{
51 void *p, *q;
52 size_t tsz;
53#define NCYCLES 3
54 unsigned i, j;
55#define NSZS 2500
56 size_t szs[NSZS];
57#define MAXSZ ZU(12 * 1024 * 1024)
58
59 p = mallocx(1, 0);
60 assert_ptr_not_null(p, "Unexpected mallocx() error");
61 szs[0] = sallocx(p, 0);
62
63 for (i = 0; i < NCYCLES; i++) {
64 for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
65 q = rallocx(p, szs[j-1]+1, 0);
66 assert_ptr_not_null(q,
67 "Unexpected rallocx() error for size=%zu-->%zu",
68 szs[j-1], szs[j-1]+1);
69 szs[j] = sallocx(q, 0);
70 assert_zu_ne(szs[j], szs[j-1]+1,
Jason Evans560a4e12015-09-11 16:18:53 -070071 "Expected size to be at least: %zu", szs[j-1]+1);
Jason Evansd82a5e62013-12-12 22:35:52 -080072 p = q;
73 }
74
75 for (j--; j > 0; j--) {
76 q = rallocx(p, szs[j-1], 0);
77 assert_ptr_not_null(q,
78 "Unexpected rallocx() error for size=%zu-->%zu",
79 szs[j], szs[j-1]);
80 tsz = sallocx(q, 0);
81 assert_zu_eq(tsz, szs[j-1],
82 "Expected size=%zu, got size=%zu", szs[j-1], tsz);
83 p = q;
84 }
85 }
86
87 dallocx(p, 0);
Jason Evans5a658b92013-12-15 15:54:18 -080088#undef MAXSZ
89#undef NSZS
90#undef NCYCLES
91}
92TEST_END
93
94static bool
95validate_fill(const void *p, uint8_t c, size_t offset, size_t len)
96{
97 bool ret = false;
98 const uint8_t *buf = (const uint8_t *)p;
99 size_t i;
100
101 for (i = 0; i < len; i++) {
102 uint8_t b = buf[offset+i];
103 if (b != c) {
Jason Evans40cbd302015-07-24 18:18:03 -0700104 test_fail("Allocation at %p (len=%zu) contains %#x "
105 "rather than %#x at offset %zu", p, len, b, c,
106 offset+i);
Jason Evans5a658b92013-12-15 15:54:18 -0800107 ret = true;
108 }
109 }
110
111 return (ret);
112}
113
114TEST_BEGIN(test_zero)
115{
116 void *p, *q;
117 size_t psz, qsz, i, j;
118 size_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};
119#define FILL_BYTE 0xaaU
120#define RANGE 2048
121
122 for (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {
123 size_t start_size = start_sizes[i];
124 p = mallocx(start_size, MALLOCX_ZERO);
125 assert_ptr_not_null(p, "Unexpected mallocx() error");
126 psz = sallocx(p, 0);
127
128 assert_false(validate_fill(p, 0, 0, psz),
129 "Expected zeroed memory");
130 memset(p, FILL_BYTE, psz);
131 assert_false(validate_fill(p, FILL_BYTE, 0, psz),
132 "Expected filled memory");
133
134 for (j = 1; j < RANGE; j++) {
135 q = rallocx(p, start_size+j, MALLOCX_ZERO);
136 assert_ptr_not_null(q, "Unexpected rallocx() error");
137 qsz = sallocx(q, 0);
138 if (q != p || qsz != psz) {
139 assert_false(validate_fill(q, FILL_BYTE, 0,
140 psz), "Expected filled memory");
141 assert_false(validate_fill(q, 0, psz, qsz-psz),
142 "Expected zeroed memory");
143 }
144 if (psz != qsz) {
Mike Hommeyb54aef12014-05-28 14:17:01 +0900145 memset((void *)((uintptr_t)q+psz), FILL_BYTE,
Mike Hommey3a730df2014-05-21 18:13:21 +0900146 qsz-psz);
Jason Evans5a658b92013-12-15 15:54:18 -0800147 psz = qsz;
148 }
149 p = q;
150 }
151 assert_false(validate_fill(p, FILL_BYTE, 0, psz),
152 "Expected filled memory");
153 dallocx(p, 0);
154 }
155#undef FILL_BYTE
156}
157TEST_END
158
159TEST_BEGIN(test_align)
160{
161 void *p, *q;
162 size_t align;
Jason Evansada84472014-03-30 11:22:23 -0700163#define MAX_ALIGN (ZU(1) << 25)
Jason Evans5a658b92013-12-15 15:54:18 -0800164
165 align = ZU(1);
166 p = mallocx(1, MALLOCX_ALIGN(align));
167 assert_ptr_not_null(p, "Unexpected mallocx() error");
168
169 for (align <<= 1; align <= MAX_ALIGN; align <<= 1) {
170 q = rallocx(p, 1, MALLOCX_ALIGN(align));
171 assert_ptr_not_null(q,
172 "Unexpected rallocx() error for align=%zu", align);
173 assert_ptr_null(
174 (void *)((uintptr_t)q & (align-1)),
175 "%p inadequately aligned for align=%zu",
176 q, align);
177 p = q;
178 }
179 dallocx(p, 0);
180#undef MAX_ALIGN
181}
182TEST_END
183
Jason Evanse935c072013-12-16 13:37:21 -0800184TEST_BEGIN(test_lg_align_and_zero)
Jason Evans5a658b92013-12-15 15:54:18 -0800185{
186 void *p, *q;
Christopher Ferrise4294032016-03-02 14:33:02 -0800187 unsigned lg_align;
188 size_t sz;
Jason Evansada84472014-03-30 11:22:23 -0700189#define MAX_LG_ALIGN 25
Jason Evanse935c072013-12-16 13:37:21 -0800190#define MAX_VALIDATE (ZU(1) << 22)
Jason Evans5a658b92013-12-15 15:54:18 -0800191
Christopher Ferrise4294032016-03-02 14:33:02 -0800192 lg_align = 0;
Jason Evanse935c072013-12-16 13:37:21 -0800193 p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
Jason Evans5a658b92013-12-15 15:54:18 -0800194 assert_ptr_not_null(p, "Unexpected mallocx() error");
195
196 for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {
Jason Evanse935c072013-12-16 13:37:21 -0800197 q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
Jason Evans5a658b92013-12-15 15:54:18 -0800198 assert_ptr_not_null(q,
Christopher Ferrise4294032016-03-02 14:33:02 -0800199 "Unexpected rallocx() error for lg_align=%u", lg_align);
Jason Evans5a658b92013-12-15 15:54:18 -0800200 assert_ptr_null(
201 (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),
Christopher Ferrise4294032016-03-02 14:33:02 -0800202 "%p inadequately aligned for lg_align=%u", q, lg_align);
Jason Evanse935c072013-12-16 13:37:21 -0800203 sz = sallocx(q, 0);
204 if ((sz << 1) <= MAX_VALIDATE) {
205 assert_false(validate_fill(q, 0, 0, sz),
206 "Expected zeroed memory");
207 } else {
208 assert_false(validate_fill(q, 0, 0, MAX_VALIDATE),
209 "Expected zeroed memory");
Mike Hommey3a730df2014-05-21 18:13:21 +0900210 assert_false(validate_fill(
Mike Hommeyb54aef12014-05-28 14:17:01 +0900211 (void *)((uintptr_t)q+sz-MAX_VALIDATE),
Mike Hommey3a730df2014-05-21 18:13:21 +0900212 0, 0, MAX_VALIDATE), "Expected zeroed memory");
Jason Evanse935c072013-12-16 13:37:21 -0800213 }
Jason Evans5a658b92013-12-15 15:54:18 -0800214 p = q;
215 }
216 dallocx(p, 0);
Jason Evanse935c072013-12-16 13:37:21 -0800217#undef MAX_VALIDATE
Jason Evans5a658b92013-12-15 15:54:18 -0800218#undef MAX_LG_ALIGN
Jason Evansd82a5e62013-12-12 22:35:52 -0800219}
220TEST_END
221
Christopher Ferrise4294032016-03-02 14:33:02 -0800222TEST_BEGIN(test_overflow)
223{
224 size_t hugemax;
225 void *p;
226
227 hugemax = get_huge_size(get_nhuge()-1);
228
229 p = mallocx(1, 0);
230 assert_ptr_not_null(p, "Unexpected mallocx() failure");
231
232 assert_ptr_null(rallocx(p, hugemax+1, 0),
233 "Expected OOM for rallocx(p, size=%#zx, 0)", hugemax+1);
234
235 assert_ptr_null(rallocx(p, ZU(PTRDIFF_MAX)+1, 0),
236 "Expected OOM for rallocx(p, size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
237
238 assert_ptr_null(rallocx(p, SIZE_T_MAX, 0),
239 "Expected OOM for rallocx(p, size=%#zx, 0)", SIZE_T_MAX);
240
241 assert_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),
242 "Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))",
243 ZU(PTRDIFF_MAX)+1);
244
245 dallocx(p, 0);
246}
247TEST_END
248
Jason Evansd82a5e62013-12-12 22:35:52 -0800249int
250main(void)
251{
252
253 return (test(
Jason Evans5a658b92013-12-15 15:54:18 -0800254 test_grow_and_shrink,
255 test_zero,
256 test_align,
Christopher Ferrise4294032016-03-02 14:33:02 -0800257 test_lg_align_and_zero,
258 test_overflow));
Jason Evansd82a5e62013-12-12 22:35:52 -0800259}