1 #include "test/jemalloc_test.h"
2 
3 #include "jemalloc/internal/hook.h"
4 #include "jemalloc/internal/util.h"
5 
TEST_BEGIN(test_mallctl_errors)6 TEST_BEGIN(test_mallctl_errors) {
7 	uint64_t epoch;
8 	size_t sz;
9 
10 	assert_d_eq(mallctl("no_such_name", NULL, NULL, NULL, 0), ENOENT,
11 	    "mallctl() should return ENOENT for non-existent names");
12 
13 	assert_d_eq(mallctl("version", NULL, NULL, "0.0.0", strlen("0.0.0")),
14 	    EPERM, "mallctl() should return EPERM on attempt to write "
15 	    "read-only value");
16 
17 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch,
18 	    sizeof(epoch)-1), EINVAL,
19 	    "mallctl() should return EINVAL for input size mismatch");
20 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch,
21 	    sizeof(epoch)+1), EINVAL,
22 	    "mallctl() should return EINVAL for input size mismatch");
23 
24 	sz = sizeof(epoch)-1;
25 	assert_d_eq(mallctl("epoch", (void *)&epoch, &sz, NULL, 0), EINVAL,
26 	    "mallctl() should return EINVAL for output size mismatch");
27 	sz = sizeof(epoch)+1;
28 	assert_d_eq(mallctl("epoch", (void *)&epoch, &sz, NULL, 0), EINVAL,
29 	    "mallctl() should return EINVAL for output size mismatch");
30 }
31 TEST_END
32 
TEST_BEGIN(test_mallctlnametomib_errors)33 TEST_BEGIN(test_mallctlnametomib_errors) {
34 	size_t mib[1];
35 	size_t miblen;
36 
37 	miblen = sizeof(mib)/sizeof(size_t);
38 	assert_d_eq(mallctlnametomib("no_such_name", mib, &miblen), ENOENT,
39 	    "mallctlnametomib() should return ENOENT for non-existent names");
40 }
41 TEST_END
42 
TEST_BEGIN(test_mallctlbymib_errors)43 TEST_BEGIN(test_mallctlbymib_errors) {
44 	uint64_t epoch;
45 	size_t sz;
46 	size_t mib[1];
47 	size_t miblen;
48 
49 	miblen = sizeof(mib)/sizeof(size_t);
50 	assert_d_eq(mallctlnametomib("version", mib, &miblen), 0,
51 	    "Unexpected mallctlnametomib() failure");
52 
53 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, "0.0.0",
54 	    strlen("0.0.0")), EPERM, "mallctl() should return EPERM on "
55 	    "attempt to write read-only value");
56 
57 	miblen = sizeof(mib)/sizeof(size_t);
58 	assert_d_eq(mallctlnametomib("epoch", mib, &miblen), 0,
59 	    "Unexpected mallctlnametomib() failure");
60 
61 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&epoch,
62 	    sizeof(epoch)-1), EINVAL,
63 	    "mallctlbymib() should return EINVAL for input size mismatch");
64 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&epoch,
65 	    sizeof(epoch)+1), EINVAL,
66 	    "mallctlbymib() should return EINVAL for input size mismatch");
67 
68 	sz = sizeof(epoch)-1;
69 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&epoch, &sz, NULL, 0),
70 	    EINVAL,
71 	    "mallctlbymib() should return EINVAL for output size mismatch");
72 	sz = sizeof(epoch)+1;
73 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&epoch, &sz, NULL, 0),
74 	    EINVAL,
75 	    "mallctlbymib() should return EINVAL for output size mismatch");
76 }
77 TEST_END
78 
TEST_BEGIN(test_mallctl_read_write)79 TEST_BEGIN(test_mallctl_read_write) {
80 	uint64_t old_epoch, new_epoch;
81 	size_t sz = sizeof(old_epoch);
82 
83 	/* Blind. */
84 	assert_d_eq(mallctl("epoch", NULL, NULL, NULL, 0), 0,
85 	    "Unexpected mallctl() failure");
86 	assert_zu_eq(sz, sizeof(old_epoch), "Unexpected output size");
87 
88 	/* Read. */
89 	assert_d_eq(mallctl("epoch", (void *)&old_epoch, &sz, NULL, 0), 0,
90 	    "Unexpected mallctl() failure");
91 	assert_zu_eq(sz, sizeof(old_epoch), "Unexpected output size");
92 
93 	/* Write. */
94 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&new_epoch,
95 	    sizeof(new_epoch)), 0, "Unexpected mallctl() failure");
96 	assert_zu_eq(sz, sizeof(old_epoch), "Unexpected output size");
97 
98 	/* Read+write. */
99 	assert_d_eq(mallctl("epoch", (void *)&old_epoch, &sz,
100 	    (void *)&new_epoch, sizeof(new_epoch)), 0,
101 	    "Unexpected mallctl() failure");
102 	assert_zu_eq(sz, sizeof(old_epoch), "Unexpected output size");
103 }
104 TEST_END
105 
TEST_BEGIN(test_mallctlnametomib_short_mib)106 TEST_BEGIN(test_mallctlnametomib_short_mib) {
107 	size_t mib[4];
108 	size_t miblen;
109 
110 	miblen = 3;
111 	mib[3] = 42;
112 	assert_d_eq(mallctlnametomib("arenas.bin.0.nregs", mib, &miblen), 0,
113 	    "Unexpected mallctlnametomib() failure");
114 	assert_zu_eq(miblen, 3, "Unexpected mib output length");
115 	assert_zu_eq(mib[3], 42,
116 	    "mallctlnametomib() wrote past the end of the input mib");
117 }
118 TEST_END
119 
TEST_BEGIN(test_mallctl_config)120 TEST_BEGIN(test_mallctl_config) {
121 #define TEST_MALLCTL_CONFIG(config, t) do {				\
122 	t oldval;							\
123 	size_t sz = sizeof(oldval);					\
124 	assert_d_eq(mallctl("config."#config, (void *)&oldval, &sz,	\
125 	    NULL, 0), 0, "Unexpected mallctl() failure");		\
126 	assert_b_eq(oldval, config_##config, "Incorrect config value");	\
127 	assert_zu_eq(sz, sizeof(oldval), "Unexpected output size");	\
128 } while (0)
129 
130 	TEST_MALLCTL_CONFIG(cache_oblivious, bool);
131 	TEST_MALLCTL_CONFIG(debug, bool);
132 	TEST_MALLCTL_CONFIG(fill, bool);
133 	TEST_MALLCTL_CONFIG(lazy_lock, bool);
134 	TEST_MALLCTL_CONFIG(malloc_conf, const char *);
135 	TEST_MALLCTL_CONFIG(prof, bool);
136 	TEST_MALLCTL_CONFIG(prof_libgcc, bool);
137 	TEST_MALLCTL_CONFIG(prof_libunwind, bool);
138 	TEST_MALLCTL_CONFIG(stats, bool);
139 	TEST_MALLCTL_CONFIG(utrace, bool);
140 	TEST_MALLCTL_CONFIG(xmalloc, bool);
141 
142 #undef TEST_MALLCTL_CONFIG
143 }
144 TEST_END
145 
TEST_BEGIN(test_mallctl_opt)146 TEST_BEGIN(test_mallctl_opt) {
147 	bool config_always = true;
148 
149 #define TEST_MALLCTL_OPT(t, opt, config) do {				\
150 	t oldval;							\
151 	size_t sz = sizeof(oldval);					\
152 	int expected = config_##config ? 0 : ENOENT;			\
153 	int result = mallctl("opt."#opt, (void *)&oldval, &sz, NULL,	\
154 	    0);								\
155 	assert_d_eq(result, expected,					\
156 	    "Unexpected mallctl() result for opt."#opt);		\
157 	assert_zu_eq(sz, sizeof(oldval), "Unexpected output size");	\
158 } while (0)
159 
160 	TEST_MALLCTL_OPT(bool, abort, always);
161 	TEST_MALLCTL_OPT(bool, abort_conf, always);
162 	TEST_MALLCTL_OPT(bool, confirm_conf, always);
163 	TEST_MALLCTL_OPT(const char *, metadata_thp, always);
164 	TEST_MALLCTL_OPT(bool, retain, always);
165 	TEST_MALLCTL_OPT(const char *, dss, always);
166 	TEST_MALLCTL_OPT(unsigned, narenas, always);
167 	TEST_MALLCTL_OPT(const char *, percpu_arena, always);
168 	TEST_MALLCTL_OPT(size_t, oversize_threshold, always);
169 	TEST_MALLCTL_OPT(bool, background_thread, always);
170 	TEST_MALLCTL_OPT(ssize_t, dirty_decay_ms, always);
171 	TEST_MALLCTL_OPT(ssize_t, muzzy_decay_ms, always);
172 	TEST_MALLCTL_OPT(bool, stats_print, always);
173 	TEST_MALLCTL_OPT(const char *, junk, fill);
174 	TEST_MALLCTL_OPT(bool, zero, fill);
175 	TEST_MALLCTL_OPT(bool, utrace, utrace);
176 	TEST_MALLCTL_OPT(bool, xmalloc, xmalloc);
177 	TEST_MALLCTL_OPT(bool, tcache, always);
178 	TEST_MALLCTL_OPT(size_t, lg_extent_max_active_fit, always);
179 	TEST_MALLCTL_OPT(size_t, lg_tcache_max, always);
180 	TEST_MALLCTL_OPT(const char *, thp, always);
181 	TEST_MALLCTL_OPT(bool, prof, prof);
182 	TEST_MALLCTL_OPT(const char *, prof_prefix, prof);
183 	TEST_MALLCTL_OPT(bool, prof_active, prof);
184 	TEST_MALLCTL_OPT(ssize_t, lg_prof_sample, prof);
185 	TEST_MALLCTL_OPT(bool, prof_accum, prof);
186 	TEST_MALLCTL_OPT(ssize_t, lg_prof_interval, prof);
187 	TEST_MALLCTL_OPT(bool, prof_gdump, prof);
188 	TEST_MALLCTL_OPT(bool, prof_final, prof);
189 	TEST_MALLCTL_OPT(bool, prof_leak, prof);
190 
191 #undef TEST_MALLCTL_OPT
192 }
193 TEST_END
194 
TEST_BEGIN(test_manpage_example)195 TEST_BEGIN(test_manpage_example) {
196 	unsigned nbins, i;
197 	size_t mib[4];
198 	size_t len, miblen;
199 
200 	len = sizeof(nbins);
201 	assert_d_eq(mallctl("arenas.nbins", (void *)&nbins, &len, NULL, 0), 0,
202 	    "Unexpected mallctl() failure");
203 
204 	miblen = 4;
205 	assert_d_eq(mallctlnametomib("arenas.bin.0.size", mib, &miblen), 0,
206 	    "Unexpected mallctlnametomib() failure");
207 	for (i = 0; i < nbins; i++) {
208 		size_t bin_size;
209 
210 		mib[2] = i;
211 		len = sizeof(bin_size);
212 		assert_d_eq(mallctlbymib(mib, miblen, (void *)&bin_size, &len,
213 		    NULL, 0), 0, "Unexpected mallctlbymib() failure");
214 		/* Do something with bin_size... */
215 	}
216 }
217 TEST_END
218 
TEST_BEGIN(test_tcache_none)219 TEST_BEGIN(test_tcache_none) {
220 	test_skip_if(!opt_tcache);
221 
222 	/* Allocate p and q. */
223 	void *p0 = mallocx(42, 0);
224 	assert_ptr_not_null(p0, "Unexpected mallocx() failure");
225 	void *q = mallocx(42, 0);
226 	assert_ptr_not_null(q, "Unexpected mallocx() failure");
227 
228 	/* Deallocate p and q, but bypass the tcache for q. */
229 	dallocx(p0, 0);
230 	dallocx(q, MALLOCX_TCACHE_NONE);
231 
232 	/* Make sure that tcache-based allocation returns p, not q. */
233 	void *p1 = mallocx(42, 0);
234 	assert_ptr_not_null(p1, "Unexpected mallocx() failure");
235 	assert_ptr_eq(p0, p1, "Expected tcache to allocate cached region");
236 
237 	/* Clean up. */
238 	dallocx(p1, MALLOCX_TCACHE_NONE);
239 }
240 TEST_END
241 
TEST_BEGIN(test_tcache)242 TEST_BEGIN(test_tcache) {
243 #define NTCACHES	10
244 	unsigned tis[NTCACHES];
245 	void *ps[NTCACHES];
246 	void *qs[NTCACHES];
247 	unsigned i;
248 	size_t sz, psz, qsz;
249 
250 	psz = 42;
251 	qsz = nallocx(psz, 0) + 1;
252 
253 	/* Create tcaches. */
254 	for (i = 0; i < NTCACHES; i++) {
255 		sz = sizeof(unsigned);
256 		assert_d_eq(mallctl("tcache.create", (void *)&tis[i], &sz, NULL,
257 		    0), 0, "Unexpected mallctl() failure, i=%u", i);
258 	}
259 
260 	/* Exercise tcache ID recycling. */
261 	for (i = 0; i < NTCACHES; i++) {
262 		assert_d_eq(mallctl("tcache.destroy", NULL, NULL,
263 		    (void *)&tis[i], sizeof(unsigned)), 0,
264 		    "Unexpected mallctl() failure, i=%u", i);
265 	}
266 	for (i = 0; i < NTCACHES; i++) {
267 		sz = sizeof(unsigned);
268 		assert_d_eq(mallctl("tcache.create", (void *)&tis[i], &sz, NULL,
269 		    0), 0, "Unexpected mallctl() failure, i=%u", i);
270 	}
271 
272 	/* Flush empty tcaches. */
273 	for (i = 0; i < NTCACHES; i++) {
274 		assert_d_eq(mallctl("tcache.flush", NULL, NULL, (void *)&tis[i],
275 		    sizeof(unsigned)), 0, "Unexpected mallctl() failure, i=%u",
276 		    i);
277 	}
278 
279 	/* Cache some allocations. */
280 	for (i = 0; i < NTCACHES; i++) {
281 		ps[i] = mallocx(psz, MALLOCX_TCACHE(tis[i]));
282 		assert_ptr_not_null(ps[i], "Unexpected mallocx() failure, i=%u",
283 		    i);
284 		dallocx(ps[i], MALLOCX_TCACHE(tis[i]));
285 
286 		qs[i] = mallocx(qsz, MALLOCX_TCACHE(tis[i]));
287 		assert_ptr_not_null(qs[i], "Unexpected mallocx() failure, i=%u",
288 		    i);
289 		dallocx(qs[i], MALLOCX_TCACHE(tis[i]));
290 	}
291 
292 	/* Verify that tcaches allocate cached regions. */
293 	for (i = 0; i < NTCACHES; i++) {
294 		void *p0 = ps[i];
295 		ps[i] = mallocx(psz, MALLOCX_TCACHE(tis[i]));
296 		assert_ptr_not_null(ps[i], "Unexpected mallocx() failure, i=%u",
297 		    i);
298 		assert_ptr_eq(ps[i], p0,
299 		    "Expected mallocx() to allocate cached region, i=%u", i);
300 	}
301 
302 	/* Verify that reallocation uses cached regions. */
303 	for (i = 0; i < NTCACHES; i++) {
304 		void *q0 = qs[i];
305 		qs[i] = rallocx(ps[i], qsz, MALLOCX_TCACHE(tis[i]));
306 		assert_ptr_not_null(qs[i], "Unexpected rallocx() failure, i=%u",
307 		    i);
308 		assert_ptr_eq(qs[i], q0,
309 		    "Expected rallocx() to allocate cached region, i=%u", i);
310 		/* Avoid undefined behavior in case of test failure. */
311 		if (qs[i] == NULL) {
312 			qs[i] = ps[i];
313 		}
314 	}
315 	for (i = 0; i < NTCACHES; i++) {
316 		dallocx(qs[i], MALLOCX_TCACHE(tis[i]));
317 	}
318 
319 	/* Flush some non-empty tcaches. */
320 	for (i = 0; i < NTCACHES/2; i++) {
321 		assert_d_eq(mallctl("tcache.flush", NULL, NULL, (void *)&tis[i],
322 		    sizeof(unsigned)), 0, "Unexpected mallctl() failure, i=%u",
323 		    i);
324 	}
325 
326 	/* Destroy tcaches. */
327 	for (i = 0; i < NTCACHES; i++) {
328 		assert_d_eq(mallctl("tcache.destroy", NULL, NULL,
329 		    (void *)&tis[i], sizeof(unsigned)), 0,
330 		    "Unexpected mallctl() failure, i=%u", i);
331 	}
332 }
333 TEST_END
334 
TEST_BEGIN(test_thread_arena)335 TEST_BEGIN(test_thread_arena) {
336 	unsigned old_arena_ind, new_arena_ind, narenas;
337 
338 	const char *opa;
339 	size_t sz = sizeof(opa);
340 	assert_d_eq(mallctl("opt.percpu_arena", (void *)&opa, &sz, NULL, 0), 0,
341 	    "Unexpected mallctl() failure");
342 
343 	sz = sizeof(unsigned);
344 	assert_d_eq(mallctl("arenas.narenas", (void *)&narenas, &sz, NULL, 0),
345 	    0, "Unexpected mallctl() failure");
346 	if (opt_oversize_threshold != 0) {
347 		narenas--;
348 	}
349 	assert_u_eq(narenas, opt_narenas, "Number of arenas incorrect");
350 
351 	if (strcmp(opa, "disabled") == 0) {
352 		new_arena_ind = narenas - 1;
353 		assert_d_eq(mallctl("thread.arena", (void *)&old_arena_ind, &sz,
354 		    (void *)&new_arena_ind, sizeof(unsigned)), 0,
355 		    "Unexpected mallctl() failure");
356 		new_arena_ind = 0;
357 		assert_d_eq(mallctl("thread.arena", (void *)&old_arena_ind, &sz,
358 		    (void *)&new_arena_ind, sizeof(unsigned)), 0,
359 		    "Unexpected mallctl() failure");
360 	} else {
361 		assert_d_eq(mallctl("thread.arena", (void *)&old_arena_ind, &sz,
362 		    NULL, 0), 0, "Unexpected mallctl() failure");
363 		new_arena_ind = percpu_arena_ind_limit(opt_percpu_arena) - 1;
364 		if (old_arena_ind != new_arena_ind) {
365 			assert_d_eq(mallctl("thread.arena",
366 			    (void *)&old_arena_ind, &sz, (void *)&new_arena_ind,
367 			    sizeof(unsigned)), EPERM, "thread.arena ctl "
368 			    "should not be allowed with percpu arena");
369 		}
370 	}
371 }
372 TEST_END
373 
TEST_BEGIN(test_arena_i_initialized)374 TEST_BEGIN(test_arena_i_initialized) {
375 	unsigned narenas, i;
376 	size_t sz;
377 	size_t mib[3];
378 	size_t miblen = sizeof(mib) / sizeof(size_t);
379 	bool initialized;
380 
381 	sz = sizeof(narenas);
382 	assert_d_eq(mallctl("arenas.narenas", (void *)&narenas, &sz, NULL, 0),
383 	    0, "Unexpected mallctl() failure");
384 
385 	assert_d_eq(mallctlnametomib("arena.0.initialized", mib, &miblen), 0,
386 	    "Unexpected mallctlnametomib() failure");
387 	for (i = 0; i < narenas; i++) {
388 		mib[1] = i;
389 		sz = sizeof(initialized);
390 		assert_d_eq(mallctlbymib(mib, miblen, &initialized, &sz, NULL,
391 		    0), 0, "Unexpected mallctl() failure");
392 	}
393 
394 	mib[1] = MALLCTL_ARENAS_ALL;
395 	sz = sizeof(initialized);
396 	assert_d_eq(mallctlbymib(mib, miblen, &initialized, &sz, NULL, 0), 0,
397 	    "Unexpected mallctl() failure");
398 	assert_true(initialized,
399 	    "Merged arena statistics should always be initialized");
400 
401 	/* Equivalent to the above but using mallctl() directly. */
402 	sz = sizeof(initialized);
403 	assert_d_eq(mallctl(
404 	    "arena." STRINGIFY(MALLCTL_ARENAS_ALL) ".initialized",
405 	    (void *)&initialized, &sz, NULL, 0), 0,
406 	    "Unexpected mallctl() failure");
407 	assert_true(initialized,
408 	    "Merged arena statistics should always be initialized");
409 }
410 TEST_END
411 
TEST_BEGIN(test_arena_i_dirty_decay_ms)412 TEST_BEGIN(test_arena_i_dirty_decay_ms) {
413 	ssize_t dirty_decay_ms, orig_dirty_decay_ms, prev_dirty_decay_ms;
414 	size_t sz = sizeof(ssize_t);
415 
416 	assert_d_eq(mallctl("arena.0.dirty_decay_ms",
417 	    (void *)&orig_dirty_decay_ms, &sz, NULL, 0), 0,
418 	    "Unexpected mallctl() failure");
419 
420 	dirty_decay_ms = -2;
421 	assert_d_eq(mallctl("arena.0.dirty_decay_ms", NULL, NULL,
422 	    (void *)&dirty_decay_ms, sizeof(ssize_t)), EFAULT,
423 	    "Unexpected mallctl() success");
424 
425 	dirty_decay_ms = 0x7fffffff;
426 	assert_d_eq(mallctl("arena.0.dirty_decay_ms", NULL, NULL,
427 	    (void *)&dirty_decay_ms, sizeof(ssize_t)), 0,
428 	    "Unexpected mallctl() failure");
429 
430 	for (prev_dirty_decay_ms = dirty_decay_ms, dirty_decay_ms = -1;
431 	    dirty_decay_ms < 20; prev_dirty_decay_ms = dirty_decay_ms,
432 	    dirty_decay_ms++) {
433 		ssize_t old_dirty_decay_ms;
434 
435 		assert_d_eq(mallctl("arena.0.dirty_decay_ms",
436 		    (void *)&old_dirty_decay_ms, &sz, (void *)&dirty_decay_ms,
437 		    sizeof(ssize_t)), 0, "Unexpected mallctl() failure");
438 		assert_zd_eq(old_dirty_decay_ms, prev_dirty_decay_ms,
439 		    "Unexpected old arena.0.dirty_decay_ms");
440 	}
441 }
442 TEST_END
443 
TEST_BEGIN(test_arena_i_muzzy_decay_ms)444 TEST_BEGIN(test_arena_i_muzzy_decay_ms) {
445 	ssize_t muzzy_decay_ms, orig_muzzy_decay_ms, prev_muzzy_decay_ms;
446 	size_t sz = sizeof(ssize_t);
447 
448 	assert_d_eq(mallctl("arena.0.muzzy_decay_ms",
449 	    (void *)&orig_muzzy_decay_ms, &sz, NULL, 0), 0,
450 	    "Unexpected mallctl() failure");
451 
452 	muzzy_decay_ms = -2;
453 	assert_d_eq(mallctl("arena.0.muzzy_decay_ms", NULL, NULL,
454 	    (void *)&muzzy_decay_ms, sizeof(ssize_t)), EFAULT,
455 	    "Unexpected mallctl() success");
456 
457 	muzzy_decay_ms = 0x7fffffff;
458 	assert_d_eq(mallctl("arena.0.muzzy_decay_ms", NULL, NULL,
459 	    (void *)&muzzy_decay_ms, sizeof(ssize_t)), 0,
460 	    "Unexpected mallctl() failure");
461 
462 	for (prev_muzzy_decay_ms = muzzy_decay_ms, muzzy_decay_ms = -1;
463 	    muzzy_decay_ms < 20; prev_muzzy_decay_ms = muzzy_decay_ms,
464 	    muzzy_decay_ms++) {
465 		ssize_t old_muzzy_decay_ms;
466 
467 		assert_d_eq(mallctl("arena.0.muzzy_decay_ms",
468 		    (void *)&old_muzzy_decay_ms, &sz, (void *)&muzzy_decay_ms,
469 		    sizeof(ssize_t)), 0, "Unexpected mallctl() failure");
470 		assert_zd_eq(old_muzzy_decay_ms, prev_muzzy_decay_ms,
471 		    "Unexpected old arena.0.muzzy_decay_ms");
472 	}
473 }
474 TEST_END
475 
TEST_BEGIN(test_arena_i_purge)476 TEST_BEGIN(test_arena_i_purge) {
477 	unsigned narenas;
478 	size_t sz = sizeof(unsigned);
479 	size_t mib[3];
480 	size_t miblen = 3;
481 
482 	assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
483 	    "Unexpected mallctl() failure");
484 
485 	assert_d_eq(mallctl("arenas.narenas", (void *)&narenas, &sz, NULL, 0),
486 	    0, "Unexpected mallctl() failure");
487 	assert_d_eq(mallctlnametomib("arena.0.purge", mib, &miblen), 0,
488 	    "Unexpected mallctlnametomib() failure");
489 	mib[1] = narenas;
490 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
491 	    "Unexpected mallctlbymib() failure");
492 
493 	mib[1] = MALLCTL_ARENAS_ALL;
494 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
495 	    "Unexpected mallctlbymib() failure");
496 }
497 TEST_END
498 
TEST_BEGIN(test_arena_i_decay)499 TEST_BEGIN(test_arena_i_decay) {
500 	unsigned narenas;
501 	size_t sz = sizeof(unsigned);
502 	size_t mib[3];
503 	size_t miblen = 3;
504 
505 	assert_d_eq(mallctl("arena.0.decay", NULL, NULL, NULL, 0), 0,
506 	    "Unexpected mallctl() failure");
507 
508 	assert_d_eq(mallctl("arenas.narenas", (void *)&narenas, &sz, NULL, 0),
509 	    0, "Unexpected mallctl() failure");
510 	assert_d_eq(mallctlnametomib("arena.0.decay", mib, &miblen), 0,
511 	    "Unexpected mallctlnametomib() failure");
512 	mib[1] = narenas;
513 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
514 	    "Unexpected mallctlbymib() failure");
515 
516 	mib[1] = MALLCTL_ARENAS_ALL;
517 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
518 	    "Unexpected mallctlbymib() failure");
519 }
520 TEST_END
521 
TEST_BEGIN(test_arena_i_dss)522 TEST_BEGIN(test_arena_i_dss) {
523 	const char *dss_prec_old, *dss_prec_new;
524 	size_t sz = sizeof(dss_prec_old);
525 	size_t mib[3];
526 	size_t miblen;
527 
528 	miblen = sizeof(mib)/sizeof(size_t);
529 	assert_d_eq(mallctlnametomib("arena.0.dss", mib, &miblen), 0,
530 	    "Unexpected mallctlnametomib() error");
531 
532 	dss_prec_new = "disabled";
533 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz,
534 	    (void *)&dss_prec_new, sizeof(dss_prec_new)), 0,
535 	    "Unexpected mallctl() failure");
536 	assert_str_ne(dss_prec_old, "primary",
537 	    "Unexpected default for dss precedence");
538 
539 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_new, &sz,
540 	    (void *)&dss_prec_old, sizeof(dss_prec_old)), 0,
541 	    "Unexpected mallctl() failure");
542 
543 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz, NULL,
544 	    0), 0, "Unexpected mallctl() failure");
545 	assert_str_ne(dss_prec_old, "primary",
546 	    "Unexpected value for dss precedence");
547 
548 	mib[1] = narenas_total_get();
549 	dss_prec_new = "disabled";
550 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz,
551 	    (void *)&dss_prec_new, sizeof(dss_prec_new)), 0,
552 	    "Unexpected mallctl() failure");
553 	assert_str_ne(dss_prec_old, "primary",
554 	    "Unexpected default for dss precedence");
555 
556 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_new, &sz,
557 	    (void *)&dss_prec_old, sizeof(dss_prec_new)), 0,
558 	    "Unexpected mallctl() failure");
559 
560 	assert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz, NULL,
561 	    0), 0, "Unexpected mallctl() failure");
562 	assert_str_ne(dss_prec_old, "primary",
563 	    "Unexpected value for dss precedence");
564 }
565 TEST_END
566 
TEST_BEGIN(test_arena_i_retain_grow_limit)567 TEST_BEGIN(test_arena_i_retain_grow_limit) {
568 	size_t old_limit, new_limit, default_limit;
569 	size_t mib[3];
570 	size_t miblen;
571 
572 	bool retain_enabled;
573 	size_t sz = sizeof(retain_enabled);
574 	assert_d_eq(mallctl("opt.retain", &retain_enabled, &sz, NULL, 0),
575 	    0, "Unexpected mallctl() failure");
576 	test_skip_if(!retain_enabled);
577 
578 	sz = sizeof(default_limit);
579 	miblen = sizeof(mib)/sizeof(size_t);
580 	assert_d_eq(mallctlnametomib("arena.0.retain_grow_limit", mib, &miblen),
581 	    0, "Unexpected mallctlnametomib() error");
582 
583 	assert_d_eq(mallctlbymib(mib, miblen, &default_limit, &sz, NULL, 0), 0,
584 	    "Unexpected mallctl() failure");
585 	assert_zu_eq(default_limit, SC_LARGE_MAXCLASS,
586 	    "Unexpected default for retain_grow_limit");
587 
588 	new_limit = PAGE - 1;
589 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &new_limit,
590 	    sizeof(new_limit)), EFAULT, "Unexpected mallctl() success");
591 
592 	new_limit = PAGE + 1;
593 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &new_limit,
594 	    sizeof(new_limit)), 0, "Unexpected mallctl() failure");
595 	assert_d_eq(mallctlbymib(mib, miblen, &old_limit, &sz, NULL, 0), 0,
596 	    "Unexpected mallctl() failure");
597 	assert_zu_eq(old_limit, PAGE,
598 	    "Unexpected value for retain_grow_limit");
599 
600 	/* Expect grow less than psize class 10. */
601 	new_limit = sz_pind2sz(10) - 1;
602 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &new_limit,
603 	    sizeof(new_limit)), 0, "Unexpected mallctl() failure");
604 	assert_d_eq(mallctlbymib(mib, miblen, &old_limit, &sz, NULL, 0), 0,
605 	    "Unexpected mallctl() failure");
606 	assert_zu_eq(old_limit, sz_pind2sz(9),
607 	    "Unexpected value for retain_grow_limit");
608 
609 	/* Restore to default. */
610 	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &default_limit,
611 	    sizeof(default_limit)), 0, "Unexpected mallctl() failure");
612 }
613 TEST_END
614 
TEST_BEGIN(test_arenas_dirty_decay_ms)615 TEST_BEGIN(test_arenas_dirty_decay_ms) {
616 	ssize_t dirty_decay_ms, orig_dirty_decay_ms, prev_dirty_decay_ms;
617 	size_t sz = sizeof(ssize_t);
618 
619 	assert_d_eq(mallctl("arenas.dirty_decay_ms",
620 	    (void *)&orig_dirty_decay_ms, &sz, NULL, 0), 0,
621 	    "Unexpected mallctl() failure");
622 
623 	dirty_decay_ms = -2;
624 	assert_d_eq(mallctl("arenas.dirty_decay_ms", NULL, NULL,
625 	    (void *)&dirty_decay_ms, sizeof(ssize_t)), EFAULT,
626 	    "Unexpected mallctl() success");
627 
628 	dirty_decay_ms = 0x7fffffff;
629 	assert_d_eq(mallctl("arenas.dirty_decay_ms", NULL, NULL,
630 	    (void *)&dirty_decay_ms, sizeof(ssize_t)), 0,
631 	    "Expected mallctl() failure");
632 
633 	for (prev_dirty_decay_ms = dirty_decay_ms, dirty_decay_ms = -1;
634 	    dirty_decay_ms < 20; prev_dirty_decay_ms = dirty_decay_ms,
635 	    dirty_decay_ms++) {
636 		ssize_t old_dirty_decay_ms;
637 
638 		assert_d_eq(mallctl("arenas.dirty_decay_ms",
639 		    (void *)&old_dirty_decay_ms, &sz, (void *)&dirty_decay_ms,
640 		    sizeof(ssize_t)), 0, "Unexpected mallctl() failure");
641 		assert_zd_eq(old_dirty_decay_ms, prev_dirty_decay_ms,
642 		    "Unexpected old arenas.dirty_decay_ms");
643 	}
644 }
645 TEST_END
646 
TEST_BEGIN(test_arenas_muzzy_decay_ms)647 TEST_BEGIN(test_arenas_muzzy_decay_ms) {
648 	ssize_t muzzy_decay_ms, orig_muzzy_decay_ms, prev_muzzy_decay_ms;
649 	size_t sz = sizeof(ssize_t);
650 
651 	assert_d_eq(mallctl("arenas.muzzy_decay_ms",
652 	    (void *)&orig_muzzy_decay_ms, &sz, NULL, 0), 0,
653 	    "Unexpected mallctl() failure");
654 
655 	muzzy_decay_ms = -2;
656 	assert_d_eq(mallctl("arenas.muzzy_decay_ms", NULL, NULL,
657 	    (void *)&muzzy_decay_ms, sizeof(ssize_t)), EFAULT,
658 	    "Unexpected mallctl() success");
659 
660 	muzzy_decay_ms = 0x7fffffff;
661 	assert_d_eq(mallctl("arenas.muzzy_decay_ms", NULL, NULL,
662 	    (void *)&muzzy_decay_ms, sizeof(ssize_t)), 0,
663 	    "Expected mallctl() failure");
664 
665 	for (prev_muzzy_decay_ms = muzzy_decay_ms, muzzy_decay_ms = -1;
666 	    muzzy_decay_ms < 20; prev_muzzy_decay_ms = muzzy_decay_ms,
667 	    muzzy_decay_ms++) {
668 		ssize_t old_muzzy_decay_ms;
669 
670 		assert_d_eq(mallctl("arenas.muzzy_decay_ms",
671 		    (void *)&old_muzzy_decay_ms, &sz, (void *)&muzzy_decay_ms,
672 		    sizeof(ssize_t)), 0, "Unexpected mallctl() failure");
673 		assert_zd_eq(old_muzzy_decay_ms, prev_muzzy_decay_ms,
674 		    "Unexpected old arenas.muzzy_decay_ms");
675 	}
676 }
677 TEST_END
678 
TEST_BEGIN(test_arenas_constants)679 TEST_BEGIN(test_arenas_constants) {
680 #define TEST_ARENAS_CONSTANT(t, name, expected) do {			\
681 	t name;								\
682 	size_t sz = sizeof(t);						\
683 	assert_d_eq(mallctl("arenas."#name, (void *)&name, &sz, NULL,	\
684 	    0), 0, "Unexpected mallctl() failure");			\
685 	assert_zu_eq(name, expected, "Incorrect "#name" size");		\
686 } while (0)
687 
688 	TEST_ARENAS_CONSTANT(size_t, quantum, QUANTUM);
689 	TEST_ARENAS_CONSTANT(size_t, page, PAGE);
690 	TEST_ARENAS_CONSTANT(unsigned, nbins, SC_NBINS);
691 	TEST_ARENAS_CONSTANT(unsigned, nlextents, SC_NSIZES - SC_NBINS);
692 
693 #undef TEST_ARENAS_CONSTANT
694 }
695 TEST_END
696 
TEST_BEGIN(test_arenas_bin_constants)697 TEST_BEGIN(test_arenas_bin_constants) {
698 #define TEST_ARENAS_BIN_CONSTANT(t, name, expected) do {		\
699 	t name;								\
700 	size_t sz = sizeof(t);						\
701 	assert_d_eq(mallctl("arenas.bin.0."#name, (void *)&name, &sz,	\
702 	    NULL, 0), 0, "Unexpected mallctl() failure");		\
703 	assert_zu_eq(name, expected, "Incorrect "#name" size");		\
704 } while (0)
705 
706 	TEST_ARENAS_BIN_CONSTANT(size_t, size, bin_infos[0].reg_size);
707 	TEST_ARENAS_BIN_CONSTANT(uint32_t, nregs, bin_infos[0].nregs);
708 	TEST_ARENAS_BIN_CONSTANT(size_t, slab_size,
709 	    bin_infos[0].slab_size);
710 	TEST_ARENAS_BIN_CONSTANT(uint32_t, nshards, bin_infos[0].n_shards);
711 
712 #undef TEST_ARENAS_BIN_CONSTANT
713 }
714 TEST_END
715 
TEST_BEGIN(test_arenas_lextent_constants)716 TEST_BEGIN(test_arenas_lextent_constants) {
717 #define TEST_ARENAS_LEXTENT_CONSTANT(t, name, expected) do {		\
718 	t name;								\
719 	size_t sz = sizeof(t);						\
720 	assert_d_eq(mallctl("arenas.lextent.0."#name, (void *)&name,	\
721 	    &sz, NULL, 0), 0, "Unexpected mallctl() failure");		\
722 	assert_zu_eq(name, expected, "Incorrect "#name" size");		\
723 } while (0)
724 
725 	TEST_ARENAS_LEXTENT_CONSTANT(size_t, size,
726 	    SC_LARGE_MINCLASS);
727 
728 #undef TEST_ARENAS_LEXTENT_CONSTANT
729 }
730 TEST_END
731 
TEST_BEGIN(test_arenas_create)732 TEST_BEGIN(test_arenas_create) {
733 	unsigned narenas_before, arena, narenas_after;
734 	size_t sz = sizeof(unsigned);
735 
736 	assert_d_eq(mallctl("arenas.narenas", (void *)&narenas_before, &sz,
737 	    NULL, 0), 0, "Unexpected mallctl() failure");
738 	assert_d_eq(mallctl("arenas.create", (void *)&arena, &sz, NULL, 0), 0,
739 	    "Unexpected mallctl() failure");
740 	assert_d_eq(mallctl("arenas.narenas", (void *)&narenas_after, &sz, NULL,
741 	    0), 0, "Unexpected mallctl() failure");
742 
743 	assert_u_eq(narenas_before+1, narenas_after,
744 	    "Unexpected number of arenas before versus after extension");
745 	assert_u_eq(arena, narenas_after-1, "Unexpected arena index");
746 }
747 TEST_END
748 
TEST_BEGIN(test_arenas_lookup)749 TEST_BEGIN(test_arenas_lookup) {
750 	unsigned arena, arena1;
751 	void *ptr;
752 	size_t sz = sizeof(unsigned);
753 
754 	assert_d_eq(mallctl("arenas.create", (void *)&arena, &sz, NULL, 0), 0,
755 	    "Unexpected mallctl() failure");
756 	ptr = mallocx(42, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE);
757 	assert_ptr_not_null(ptr, "Unexpected mallocx() failure");
758 	assert_d_eq(mallctl("arenas.lookup", &arena1, &sz, &ptr, sizeof(ptr)),
759 	    0, "Unexpected mallctl() failure");
760 	assert_u_eq(arena, arena1, "Unexpected arena index");
761 	dallocx(ptr, 0);
762 }
763 TEST_END
764 
TEST_BEGIN(test_stats_arenas)765 TEST_BEGIN(test_stats_arenas) {
766 #define TEST_STATS_ARENAS(t, name) do {					\
767 	t name;								\
768 	size_t sz = sizeof(t);						\
769 	assert_d_eq(mallctl("stats.arenas.0."#name, (void *)&name, &sz,	\
770 	    NULL, 0), 0, "Unexpected mallctl() failure");		\
771 } while (0)
772 
773 	TEST_STATS_ARENAS(unsigned, nthreads);
774 	TEST_STATS_ARENAS(const char *, dss);
775 	TEST_STATS_ARENAS(ssize_t, dirty_decay_ms);
776 	TEST_STATS_ARENAS(ssize_t, muzzy_decay_ms);
777 	TEST_STATS_ARENAS(size_t, pactive);
778 	TEST_STATS_ARENAS(size_t, pdirty);
779 
780 #undef TEST_STATS_ARENAS
781 }
782 TEST_END
783 
784 static void
alloc_hook(void * extra,UNUSED hook_alloc_t type,UNUSED void * result,UNUSED uintptr_t result_raw,UNUSED uintptr_t args_raw[3])785 alloc_hook(void *extra, UNUSED hook_alloc_t type, UNUSED void *result,
786     UNUSED uintptr_t result_raw, UNUSED uintptr_t args_raw[3]) {
787 	*(bool *)extra = true;
788 }
789 
790 static void
dalloc_hook(void * extra,UNUSED hook_dalloc_t type,UNUSED void * address,UNUSED uintptr_t args_raw[3])791 dalloc_hook(void *extra, UNUSED hook_dalloc_t type,
792     UNUSED void *address, UNUSED uintptr_t args_raw[3]) {
793 	*(bool *)extra = true;
794 }
795 
TEST_BEGIN(test_hooks)796 TEST_BEGIN(test_hooks) {
797 	bool hook_called = false;
798 	hooks_t hooks = {&alloc_hook, &dalloc_hook, NULL, &hook_called};
799 	void *handle = NULL;
800 	size_t sz = sizeof(handle);
801 	int err = mallctl("experimental.hooks.install", &handle, &sz, &hooks,
802 	    sizeof(hooks));
803 	assert_d_eq(err, 0, "Hook installation failed");
804 	assert_ptr_ne(handle, NULL, "Hook installation gave null handle");
805 	void *ptr = mallocx(1, 0);
806 	assert_true(hook_called, "Alloc hook not called");
807 	hook_called = false;
808 	free(ptr);
809 	assert_true(hook_called, "Free hook not called");
810 
811 	err = mallctl("experimental.hooks.remove", NULL, NULL, &handle,
812 	    sizeof(handle));
813 	assert_d_eq(err, 0, "Hook removal failed");
814 	hook_called = false;
815 	ptr = mallocx(1, 0);
816 	free(ptr);
817 	assert_false(hook_called, "Hook called after removal");
818 }
819 TEST_END
820 
TEST_BEGIN(test_hooks_exhaustion)821 TEST_BEGIN(test_hooks_exhaustion) {
822 	bool hook_called = false;
823 	hooks_t hooks = {&alloc_hook, &dalloc_hook, NULL, &hook_called};
824 
825 	void *handle;
826 	void *handles[HOOK_MAX];
827 	size_t sz = sizeof(handle);
828 	int err;
829 	for (int i = 0; i < HOOK_MAX; i++) {
830 		handle = NULL;
831 		err = mallctl("experimental.hooks.install", &handle, &sz,
832 		    &hooks, sizeof(hooks));
833 		assert_d_eq(err, 0, "Error installation hooks");
834 		assert_ptr_ne(handle, NULL, "Got NULL handle");
835 		handles[i] = handle;
836 	}
837 	err = mallctl("experimental.hooks.install", &handle, &sz, &hooks,
838 	    sizeof(hooks));
839 	assert_d_eq(err, EAGAIN, "Should have failed hook installation");
840 	for (int i = 0; i < HOOK_MAX; i++) {
841 		err = mallctl("experimental.hooks.remove", NULL, NULL,
842 		    &handles[i], sizeof(handles[i]));
843 		assert_d_eq(err, 0, "Hook removal failed");
844 	}
845 	/* Insertion failed, but then we removed some; it should work now. */
846 	handle = NULL;
847 	err = mallctl("experimental.hooks.install", &handle, &sz, &hooks,
848 	    sizeof(hooks));
849 	assert_d_eq(err, 0, "Hook insertion failed");
850 	assert_ptr_ne(handle, NULL, "Got NULL handle");
851 	err = mallctl("experimental.hooks.remove", NULL, NULL, &handle,
852 	    sizeof(handle));
853 	assert_d_eq(err, 0, "Hook removal failed");
854 }
855 TEST_END
856 
857 int
main(void)858 main(void) {
859 	return test(
860 	    test_mallctl_errors,
861 	    test_mallctlnametomib_errors,
862 	    test_mallctlbymib_errors,
863 	    test_mallctl_read_write,
864 	    test_mallctlnametomib_short_mib,
865 	    test_mallctl_config,
866 	    test_mallctl_opt,
867 	    test_manpage_example,
868 	    test_tcache_none,
869 	    test_tcache,
870 	    test_thread_arena,
871 	    test_arena_i_initialized,
872 	    test_arena_i_dirty_decay_ms,
873 	    test_arena_i_muzzy_decay_ms,
874 	    test_arena_i_purge,
875 	    test_arena_i_decay,
876 	    test_arena_i_dss,
877 	    test_arena_i_retain_grow_limit,
878 	    test_arenas_dirty_decay_ms,
879 	    test_arenas_muzzy_decay_ms,
880 	    test_arenas_constants,
881 	    test_arenas_bin_constants,
882 	    test_arenas_lextent_constants,
883 	    test_arenas_create,
884 	    test_arenas_lookup,
885 	    test_stats_arenas,
886 	    test_hooks,
887 	    test_hooks_exhaustion);
888 }
889