1 #include "test/jemalloc_test.h"
2 
3 static bool did_prof_dump_open;
4 
5 static int
prof_dump_open_intercept(bool propagate_err,const char * filename)6 prof_dump_open_intercept(bool propagate_err, const char *filename) {
7 	int fd;
8 
9 	did_prof_dump_open = true;
10 
11 	fd = open("/dev/null", O_WRONLY);
12 	assert_d_ne(fd, -1, "Unexpected open() failure");
13 
14 	return fd;
15 }
16 
TEST_BEGIN(test_gdump)17 TEST_BEGIN(test_gdump) {
18 	bool active, gdump, gdump_old;
19 	void *p, *q, *r, *s;
20 	size_t sz;
21 
22 	test_skip_if(!config_prof);
23 
24 	active = true;
25 	assert_d_eq(mallctl("prof.active", NULL, NULL, (void *)&active,
26 	    sizeof(active)), 0,
27 	    "Unexpected mallctl failure while activating profiling");
28 
29 	prof_dump_open = prof_dump_open_intercept;
30 
31 	did_prof_dump_open = false;
32 	p = mallocx((1U << LG_LARGE_MINCLASS), 0);
33 	assert_ptr_not_null(p, "Unexpected mallocx() failure");
34 	assert_true(did_prof_dump_open, "Expected a profile dump");
35 
36 	did_prof_dump_open = false;
37 	q = mallocx((1U << LG_LARGE_MINCLASS), 0);
38 	assert_ptr_not_null(q, "Unexpected mallocx() failure");
39 	assert_true(did_prof_dump_open, "Expected a profile dump");
40 
41 	gdump = false;
42 	sz = sizeof(gdump_old);
43 	assert_d_eq(mallctl("prof.gdump", (void *)&gdump_old, &sz,
44 	    (void *)&gdump, sizeof(gdump)), 0,
45 	    "Unexpected mallctl failure while disabling prof.gdump");
46 	assert(gdump_old);
47 	did_prof_dump_open = false;
48 	r = mallocx((1U << LG_LARGE_MINCLASS), 0);
49 	assert_ptr_not_null(q, "Unexpected mallocx() failure");
50 	assert_false(did_prof_dump_open, "Unexpected profile dump");
51 
52 	gdump = true;
53 	sz = sizeof(gdump_old);
54 	assert_d_eq(mallctl("prof.gdump", (void *)&gdump_old, &sz,
55 	    (void *)&gdump, sizeof(gdump)), 0,
56 	    "Unexpected mallctl failure while enabling prof.gdump");
57 	assert(!gdump_old);
58 	did_prof_dump_open = false;
59 	s = mallocx((1U << LG_LARGE_MINCLASS), 0);
60 	assert_ptr_not_null(q, "Unexpected mallocx() failure");
61 	assert_true(did_prof_dump_open, "Expected a profile dump");
62 
63 	dallocx(p, 0);
64 	dallocx(q, 0);
65 	dallocx(r, 0);
66 	dallocx(s, 0);
67 }
68 TEST_END
69 
70 int
main(void)71 main(void) {
72 	return test_no_reentrancy(
73 	    test_gdump);
74 }
75