1*1208bc7eSAndroid Build Coastguard Worker #include "test/jemalloc_test.h"
2*1208bc7eSAndroid Build Coastguard Worker
3*1208bc7eSAndroid Build Coastguard Worker static const bool config_stats =
4*1208bc7eSAndroid Build Coastguard Worker #ifdef JEMALLOC_STATS
5*1208bc7eSAndroid Build Coastguard Worker true
6*1208bc7eSAndroid Build Coastguard Worker #else
7*1208bc7eSAndroid Build Coastguard Worker false
8*1208bc7eSAndroid Build Coastguard Worker #endif
9*1208bc7eSAndroid Build Coastguard Worker ;
10*1208bc7eSAndroid Build Coastguard Worker
11*1208bc7eSAndroid Build Coastguard Worker static void *
thd_start(void * arg)12*1208bc7eSAndroid Build Coastguard Worker thd_start(void *arg) {
13*1208bc7eSAndroid Build Coastguard Worker int err;
14*1208bc7eSAndroid Build Coastguard Worker void *p;
15*1208bc7eSAndroid Build Coastguard Worker uint64_t a0, a1, d0, d1;
16*1208bc7eSAndroid Build Coastguard Worker uint64_t *ap0, *ap1, *dp0, *dp1;
17*1208bc7eSAndroid Build Coastguard Worker size_t sz, usize;
18*1208bc7eSAndroid Build Coastguard Worker
19*1208bc7eSAndroid Build Coastguard Worker sz = sizeof(a0);
20*1208bc7eSAndroid Build Coastguard Worker if ((err = mallctl("thread.allocated", (void *)&a0, &sz, NULL, 0))) {
21*1208bc7eSAndroid Build Coastguard Worker if (err == ENOENT) {
22*1208bc7eSAndroid Build Coastguard Worker goto label_ENOENT;
23*1208bc7eSAndroid Build Coastguard Worker }
24*1208bc7eSAndroid Build Coastguard Worker test_fail("%s(): Error in mallctl(): %s", __func__,
25*1208bc7eSAndroid Build Coastguard Worker strerror(err));
26*1208bc7eSAndroid Build Coastguard Worker }
27*1208bc7eSAndroid Build Coastguard Worker sz = sizeof(ap0);
28*1208bc7eSAndroid Build Coastguard Worker if ((err = mallctl("thread.allocatedp", (void *)&ap0, &sz, NULL, 0))) {
29*1208bc7eSAndroid Build Coastguard Worker if (err == ENOENT) {
30*1208bc7eSAndroid Build Coastguard Worker goto label_ENOENT;
31*1208bc7eSAndroid Build Coastguard Worker }
32*1208bc7eSAndroid Build Coastguard Worker test_fail("%s(): Error in mallctl(): %s", __func__,
33*1208bc7eSAndroid Build Coastguard Worker strerror(err));
34*1208bc7eSAndroid Build Coastguard Worker }
35*1208bc7eSAndroid Build Coastguard Worker assert_u64_eq(*ap0, a0,
36*1208bc7eSAndroid Build Coastguard Worker "\"thread.allocatedp\" should provide a pointer to internal "
37*1208bc7eSAndroid Build Coastguard Worker "storage");
38*1208bc7eSAndroid Build Coastguard Worker
39*1208bc7eSAndroid Build Coastguard Worker sz = sizeof(d0);
40*1208bc7eSAndroid Build Coastguard Worker if ((err = mallctl("thread.deallocated", (void *)&d0, &sz, NULL, 0))) {
41*1208bc7eSAndroid Build Coastguard Worker if (err == ENOENT) {
42*1208bc7eSAndroid Build Coastguard Worker goto label_ENOENT;
43*1208bc7eSAndroid Build Coastguard Worker }
44*1208bc7eSAndroid Build Coastguard Worker test_fail("%s(): Error in mallctl(): %s", __func__,
45*1208bc7eSAndroid Build Coastguard Worker strerror(err));
46*1208bc7eSAndroid Build Coastguard Worker }
47*1208bc7eSAndroid Build Coastguard Worker sz = sizeof(dp0);
48*1208bc7eSAndroid Build Coastguard Worker if ((err = mallctl("thread.deallocatedp", (void *)&dp0, &sz, NULL,
49*1208bc7eSAndroid Build Coastguard Worker 0))) {
50*1208bc7eSAndroid Build Coastguard Worker if (err == ENOENT) {
51*1208bc7eSAndroid Build Coastguard Worker goto label_ENOENT;
52*1208bc7eSAndroid Build Coastguard Worker }
53*1208bc7eSAndroid Build Coastguard Worker test_fail("%s(): Error in mallctl(): %s", __func__,
54*1208bc7eSAndroid Build Coastguard Worker strerror(err));
55*1208bc7eSAndroid Build Coastguard Worker }
56*1208bc7eSAndroid Build Coastguard Worker assert_u64_eq(*dp0, d0,
57*1208bc7eSAndroid Build Coastguard Worker "\"thread.deallocatedp\" should provide a pointer to internal "
58*1208bc7eSAndroid Build Coastguard Worker "storage");
59*1208bc7eSAndroid Build Coastguard Worker
60*1208bc7eSAndroid Build Coastguard Worker p = malloc(1);
61*1208bc7eSAndroid Build Coastguard Worker assert_ptr_not_null(p, "Unexpected malloc() error");
62*1208bc7eSAndroid Build Coastguard Worker
63*1208bc7eSAndroid Build Coastguard Worker sz = sizeof(a1);
64*1208bc7eSAndroid Build Coastguard Worker mallctl("thread.allocated", (void *)&a1, &sz, NULL, 0);
65*1208bc7eSAndroid Build Coastguard Worker sz = sizeof(ap1);
66*1208bc7eSAndroid Build Coastguard Worker mallctl("thread.allocatedp", (void *)&ap1, &sz, NULL, 0);
67*1208bc7eSAndroid Build Coastguard Worker assert_u64_eq(*ap1, a1,
68*1208bc7eSAndroid Build Coastguard Worker "Dereferenced \"thread.allocatedp\" value should equal "
69*1208bc7eSAndroid Build Coastguard Worker "\"thread.allocated\" value");
70*1208bc7eSAndroid Build Coastguard Worker assert_ptr_eq(ap0, ap1,
71*1208bc7eSAndroid Build Coastguard Worker "Pointer returned by \"thread.allocatedp\" should not change");
72*1208bc7eSAndroid Build Coastguard Worker
73*1208bc7eSAndroid Build Coastguard Worker usize = malloc_usable_size(p);
74*1208bc7eSAndroid Build Coastguard Worker assert_u64_le(a0 + usize, a1,
75*1208bc7eSAndroid Build Coastguard Worker "Allocated memory counter should increase by at least the amount "
76*1208bc7eSAndroid Build Coastguard Worker "explicitly allocated");
77*1208bc7eSAndroid Build Coastguard Worker
78*1208bc7eSAndroid Build Coastguard Worker free(p);
79*1208bc7eSAndroid Build Coastguard Worker
80*1208bc7eSAndroid Build Coastguard Worker sz = sizeof(d1);
81*1208bc7eSAndroid Build Coastguard Worker mallctl("thread.deallocated", (void *)&d1, &sz, NULL, 0);
82*1208bc7eSAndroid Build Coastguard Worker sz = sizeof(dp1);
83*1208bc7eSAndroid Build Coastguard Worker mallctl("thread.deallocatedp", (void *)&dp1, &sz, NULL, 0);
84*1208bc7eSAndroid Build Coastguard Worker assert_u64_eq(*dp1, d1,
85*1208bc7eSAndroid Build Coastguard Worker "Dereferenced \"thread.deallocatedp\" value should equal "
86*1208bc7eSAndroid Build Coastguard Worker "\"thread.deallocated\" value");
87*1208bc7eSAndroid Build Coastguard Worker assert_ptr_eq(dp0, dp1,
88*1208bc7eSAndroid Build Coastguard Worker "Pointer returned by \"thread.deallocatedp\" should not change");
89*1208bc7eSAndroid Build Coastguard Worker
90*1208bc7eSAndroid Build Coastguard Worker assert_u64_le(d0 + usize, d1,
91*1208bc7eSAndroid Build Coastguard Worker "Deallocated memory counter should increase by at least the amount "
92*1208bc7eSAndroid Build Coastguard Worker "explicitly deallocated");
93*1208bc7eSAndroid Build Coastguard Worker
94*1208bc7eSAndroid Build Coastguard Worker return NULL;
95*1208bc7eSAndroid Build Coastguard Worker label_ENOENT:
96*1208bc7eSAndroid Build Coastguard Worker assert_false(config_stats,
97*1208bc7eSAndroid Build Coastguard Worker "ENOENT should only be returned if stats are disabled");
98*1208bc7eSAndroid Build Coastguard Worker test_skip("\"thread.allocated\" mallctl not available");
99*1208bc7eSAndroid Build Coastguard Worker return NULL;
100*1208bc7eSAndroid Build Coastguard Worker }
101*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_main_thread)102*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_main_thread) {
103*1208bc7eSAndroid Build Coastguard Worker thd_start(NULL);
104*1208bc7eSAndroid Build Coastguard Worker }
105*1208bc7eSAndroid Build Coastguard Worker TEST_END
106*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_subthread)107*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_subthread) {
108*1208bc7eSAndroid Build Coastguard Worker thd_t thd;
109*1208bc7eSAndroid Build Coastguard Worker
110*1208bc7eSAndroid Build Coastguard Worker thd_create(&thd, thd_start, NULL);
111*1208bc7eSAndroid Build Coastguard Worker thd_join(thd, NULL);
112*1208bc7eSAndroid Build Coastguard Worker }
113*1208bc7eSAndroid Build Coastguard Worker TEST_END
114*1208bc7eSAndroid Build Coastguard Worker
115*1208bc7eSAndroid Build Coastguard Worker int
main(void)116*1208bc7eSAndroid Build Coastguard Worker main(void) {
117*1208bc7eSAndroid Build Coastguard Worker /* Run tests multiple times to check for bad interactions. */
118*1208bc7eSAndroid Build Coastguard Worker return test(
119*1208bc7eSAndroid Build Coastguard Worker test_main_thread,
120*1208bc7eSAndroid Build Coastguard Worker test_subthread,
121*1208bc7eSAndroid Build Coastguard Worker test_main_thread,
122*1208bc7eSAndroid Build Coastguard Worker test_subthread,
123*1208bc7eSAndroid Build Coastguard Worker test_main_thread);
124*1208bc7eSAndroid Build Coastguard Worker }
125