1 #include "test/jemalloc_test.h" 2 3 #include "test/extent_hooks.h" 4 5 static bool 6 check_background_thread_enabled(void) { 7 bool enabled; 8 size_t sz = sizeof(bool); 9 int ret = mallctl("background_thread", (void *)&enabled, &sz, NULL,0); 10 if (ret == ENOENT) { 11 return false; 12 } 13 assert_d_eq(ret, 0, "Unexpected mallctl error"); 14 return enabled; 15 } 16 17 static void 18 test_extent_body(unsigned arena_ind) { 19 void *p; 20 size_t large0, large1, large2, sz; 21 size_t purge_mib[3]; 22 size_t purge_miblen; 23 int flags; 24 bool xallocx_success_a, xallocx_success_b, xallocx_success_c; 25 26 flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE; 27 28 /* Get large size classes. */ 29 sz = sizeof(size_t); 30 assert_d_eq(mallctl("arenas.lextent.0.size", (void *)&large0, &sz, NULL, 31 0), 0, "Unexpected arenas.lextent.0.size failure"); 32 assert_d_eq(mallctl("arenas.lextent.1.size", (void *)&large1, &sz, NULL, 33 0), 0, "Unexpected arenas.lextent.1.size failure"); 34 assert_d_eq(mallctl("arenas.lextent.2.size", (void *)&large2, &sz, NULL, 35 0), 0, "Unexpected arenas.lextent.2.size failure"); 36 37 /* Test dalloc/decommit/purge cascade. */ 38 purge_miblen = sizeof(purge_mib)/sizeof(size_t); 39 assert_d_eq(mallctlnametomib("arena.0.purge", purge_mib, &purge_miblen), 40 0, "Unexpected mallctlnametomib() failure"); 41 purge_mib[1] = (size_t)arena_ind; 42 called_alloc = false; 43 try_alloc = true; 44 try_dalloc = false; 45 try_decommit = false; 46 p = mallocx(large0 * 2, flags); 47 assert_ptr_not_null(p, "Unexpected mallocx() error"); 48 assert_true(called_alloc, "Expected alloc call"); 49 called_dalloc = false; 50 called_decommit = false; 51 did_purge_lazy = false; 52 did_purge_forced = false; 53 called_split = false; 54 xallocx_success_a = (xallocx(p, large0, 0, flags) == large0); 55 assert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0), 56 0, "Unexpected arena.%u.purge error", arena_ind); 57 if (xallocx_success_a) { 58 assert_true(called_dalloc, "Expected dalloc call"); 59 assert_true(called_decommit, "Expected decommit call"); 60 assert_true(did_purge_lazy || did_purge_forced, 61 "Expected purge"); 62 } 63 assert_true(called_split, "Expected split call"); 64 dallocx(p, flags); 65 try_dalloc = true; 66 67 /* Test decommit/commit and observe split/merge. */ 68 try_dalloc = false; 69 try_decommit = true; 70 p = mallocx(large0 * 2, flags); 71 assert_ptr_not_null(p, "Unexpected mallocx() error"); 72 did_decommit = false; 73 did_commit = false; 74 called_split = false; 75 did_split = false; 76 did_merge = false; 77 xallocx_success_b = (xallocx(p, large0, 0, flags) == large0); 78 assert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0), 79 0, "Unexpected arena.%u.purge error", arena_ind); 80 if (xallocx_success_b) { 81 assert_true(did_split, "Expected split"); 82 } 83 xallocx_success_c = (xallocx(p, large0 * 2, 0, flags) == large0 * 2); 84 if (did_split) { 85 assert_b_eq(did_decommit, did_commit, 86 "Expected decommit/commit match"); 87 } 88 if (xallocx_success_b && xallocx_success_c) { 89 assert_true(did_merge, "Expected merge"); 90 } 91 dallocx(p, flags); 92 try_dalloc = true; 93 try_decommit = false; 94 95 /* Make sure non-large allocation succeeds. */ 96 p = mallocx(42, flags); 97 assert_ptr_not_null(p, "Unexpected mallocx() error"); 98 dallocx(p, flags); 99 } 100 101 static void 102 test_manual_hook_auto_arena(void) { 103 unsigned narenas; 104 size_t old_size, new_size, sz; 105 size_t hooks_mib[3]; 106 size_t hooks_miblen; 107 extent_hooks_t *new_hooks, *old_hooks; 108 109 extent_hooks_prep(); 110 111 sz = sizeof(unsigned); 112 /* Get number of auto arenas. */ 113 assert_d_eq(mallctl("opt.narenas", (void *)&narenas, &sz, NULL, 0), 114 0, "Unexpected mallctl() failure"); 115 if (narenas == 1) { 116 return; 117 } 118 119 /* Install custom extent hooks on arena 1 (might not be initialized). */ 120 hooks_miblen = sizeof(hooks_mib)/sizeof(size_t); 121 assert_d_eq(mallctlnametomib("arena.0.extent_hooks", hooks_mib, 122 &hooks_miblen), 0, "Unexpected mallctlnametomib() failure"); 123 hooks_mib[1] = 1; 124 old_size = sizeof(extent_hooks_t *); 125 new_hooks = &hooks; 126 new_size = sizeof(extent_hooks_t *); 127 assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks, 128 &old_size, (void *)&new_hooks, new_size), 0, 129 "Unexpected extent_hooks error"); 130 static bool auto_arena_created = false; 131 if (old_hooks != &hooks) { 132 assert_b_eq(auto_arena_created, false, 133 "Expected auto arena 1 created only once."); 134 auto_arena_created = true; 135 } 136 } 137 138 static void 139 test_manual_hook_body(void) { 140 unsigned arena_ind; 141 size_t old_size, new_size, sz; 142 size_t hooks_mib[3]; 143 size_t hooks_miblen; 144 extent_hooks_t *new_hooks, *old_hooks; 145 146 extent_hooks_prep(); 147 148 sz = sizeof(unsigned); 149 assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0), 150 0, "Unexpected mallctl() failure"); 151 152 /* Install custom extent hooks. */ 153 hooks_miblen = sizeof(hooks_mib)/sizeof(size_t); 154 assert_d_eq(mallctlnametomib("arena.0.extent_hooks", hooks_mib, 155 &hooks_miblen), 0, "Unexpected mallctlnametomib() failure"); 156 hooks_mib[1] = (size_t)arena_ind; 157 old_size = sizeof(extent_hooks_t *); 158 new_hooks = &hooks; 159 new_size = sizeof(extent_hooks_t *); 160 assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks, 161 &old_size, (void *)&new_hooks, new_size), 0, 162 "Unexpected extent_hooks error"); 163 assert_ptr_ne(old_hooks->alloc, extent_alloc_hook, 164 "Unexpected extent_hooks error"); 165 assert_ptr_ne(old_hooks->dalloc, extent_dalloc_hook, 166 "Unexpected extent_hooks error"); 167 assert_ptr_ne(old_hooks->commit, extent_commit_hook, 168 "Unexpected extent_hooks error"); 169 assert_ptr_ne(old_hooks->decommit, extent_decommit_hook, 170 "Unexpected extent_hooks error"); 171 assert_ptr_ne(old_hooks->purge_lazy, extent_purge_lazy_hook, 172 "Unexpected extent_hooks error"); 173 assert_ptr_ne(old_hooks->purge_forced, extent_purge_forced_hook, 174 "Unexpected extent_hooks error"); 175 assert_ptr_ne(old_hooks->split, extent_split_hook, 176 "Unexpected extent_hooks error"); 177 assert_ptr_ne(old_hooks->merge, extent_merge_hook, 178 "Unexpected extent_hooks error"); 179 180 if (!check_background_thread_enabled()) { 181 test_extent_body(arena_ind); 182 } 183 184 /* Restore extent hooks. */ 185 assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, NULL, NULL, 186 (void *)&old_hooks, new_size), 0, "Unexpected extent_hooks error"); 187 assert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks, 188 &old_size, NULL, 0), 0, "Unexpected extent_hooks error"); 189 assert_ptr_eq(old_hooks, default_hooks, "Unexpected extent_hooks error"); 190 assert_ptr_eq(old_hooks->alloc, default_hooks->alloc, 191 "Unexpected extent_hooks error"); 192 assert_ptr_eq(old_hooks->dalloc, default_hooks->dalloc, 193 "Unexpected extent_hooks error"); 194 assert_ptr_eq(old_hooks->commit, default_hooks->commit, 195 "Unexpected extent_hooks error"); 196 assert_ptr_eq(old_hooks->decommit, default_hooks->decommit, 197 "Unexpected extent_hooks error"); 198 assert_ptr_eq(old_hooks->purge_lazy, default_hooks->purge_lazy, 199 "Unexpected extent_hooks error"); 200 assert_ptr_eq(old_hooks->purge_forced, default_hooks->purge_forced, 201 "Unexpected extent_hooks error"); 202 assert_ptr_eq(old_hooks->split, default_hooks->split, 203 "Unexpected extent_hooks error"); 204 assert_ptr_eq(old_hooks->merge, default_hooks->merge, 205 "Unexpected extent_hooks error"); 206 } 207 208 TEST_BEGIN(test_extent_manual_hook) { 209 test_manual_hook_auto_arena(); 210 test_manual_hook_body(); 211 212 /* Test failure paths. */ 213 try_split = false; 214 test_manual_hook_body(); 215 try_merge = false; 216 test_manual_hook_body(); 217 try_purge_lazy = false; 218 try_purge_forced = false; 219 test_manual_hook_body(); 220 221 try_split = try_merge = try_purge_lazy = try_purge_forced = true; 222 } 223 TEST_END 224 225 TEST_BEGIN(test_extent_auto_hook) { 226 unsigned arena_ind; 227 size_t new_size, sz; 228 extent_hooks_t *new_hooks; 229 230 extent_hooks_prep(); 231 232 sz = sizeof(unsigned); 233 new_hooks = &hooks; 234 new_size = sizeof(extent_hooks_t *); 235 assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, 236 (void *)&new_hooks, new_size), 0, "Unexpected mallctl() failure"); 237 238 test_skip_if(check_background_thread_enabled()); 239 test_extent_body(arena_ind); 240 } 241 TEST_END 242 243 int 244 main(void) { 245 return test( 246 test_extent_manual_hook, 247 test_extent_auto_hook); 248 } 249