1 1.1 christos #include "test/jemalloc_test.h" 2 1.1 christos 3 1.1 christos static void * 4 1.1 christos tcache_stack_alloc_fail(tsdn_t *tsdn, size_t size, size_t alignment) { 5 1.1 christos return NULL; 6 1.1 christos } 7 1.1 christos 8 1.1 christos TEST_BEGIN(test_tcache_data_init_oom) { 9 1.1 christos bool orig_opt_abort = opt_abort; 10 1.1 christos void *(*orig_tcache_stack_alloc)(tsdn_t *, size_t, size_t) = 11 1.1 christos tcache_stack_alloc; 12 1.1 christos 13 1.1 christos opt_abort = false; 14 1.1 christos tcache_stack_alloc = tcache_stack_alloc_fail; 15 1.1 christos 16 1.1 christos /* 17 1.1 christos * Trigger init through tcache_enabled_set by enabling and 18 1.1 christos * disabling the tcache. 19 1.1 christos */ 20 1.1 christos bool e0, e1; 21 1.1 christos size_t bool_sz = sizeof(bool); 22 1.1 christos 23 1.1 christos /* Disable the tcache. */ 24 1.1 christos e1 = false; 25 1.1 christos expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &bool_sz, 26 1.1 christos (void *)&e1, bool_sz), 0, "Unexpected mallctl failure"); 27 1.1 christos 28 1.1 christos /* Try to enable the tcache. Initialization should fail. */ 29 1.1 christos e1 = true; 30 1.1 christos expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &bool_sz, 31 1.1 christos (void *)&e1, bool_sz), 0, "Unexpected mallctl failure"); 32 1.1 christos 33 1.1 christos /* The tcache should be disabled. */ 34 1.1 christos tsd_t *tsd = tsd_fetch(); 35 1.1 christos expect_false(tsd_tcache_enabled_get(tsd), 36 1.1 christos "tcache should be disabled after init failure"); 37 1.1 christos 38 1.1 christos /* Allocations should go to the arena. */ 39 1.1 christos void *p = malloc(64); 40 1.1 christos expect_ptr_not_null(p, "malloc should succeed without tcache"); 41 1.1 christos free(p); 42 1.1 christos 43 1.1 christos /* Restore the original values */ 44 1.1 christos tcache_stack_alloc = orig_tcache_stack_alloc; 45 1.1 christos opt_abort = orig_opt_abort; 46 1.1 christos 47 1.1 christos /* 48 1.1 christos * Try to enable the tcache again. This time initialization 49 1.1 christos * should succeed. 50 1.1 christos */ 51 1.1 christos e1 = true; 52 1.1 christos expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &bool_sz, 53 1.1 christos (void *)&e1, bool_sz), 0, "Unexpected mallctl failure"); 54 1.1 christos } 55 1.1 christos TEST_END 56 1.1 christos 57 1.1 christos TEST_BEGIN(test_tcache_reinit_oom) { 58 1.1 christos bool orig_opt_abort = opt_abort; 59 1.1 christos void *(*orig_tcache_stack_alloc)(tsdn_t *, size_t, size_t) = 60 1.1 christos tcache_stack_alloc; 61 1.1 christos 62 1.1 christos /* Read current tcache max. */ 63 1.1 christos size_t old_tcache_max, sz; 64 1.1 christos sz = sizeof(old_tcache_max); 65 1.1 christos expect_d_eq(mallctl("thread.tcache.max", (void *)&old_tcache_max, &sz, 66 1.1 christos NULL, 0), 0, "Unexpected mallctl failure"); 67 1.1 christos 68 1.1 christos opt_abort = false; 69 1.1 christos tcache_stack_alloc = tcache_stack_alloc_fail; 70 1.1 christos 71 1.1 christos /* 72 1.1 christos * Setting thread.tcache.max causes a reinitialization. With 73 1.1 christos * the thread_stack_alloc override reinitialization should 74 1.1 christos * fail and disable tcache. 75 1.1 christos */ 76 1.1 christos size_t new_tcache_max = 1024; 77 1.1 christos new_tcache_max = sz_s2u(new_tcache_max); 78 1.1 christos expect_d_eq(mallctl("thread.tcache.max", NULL, NULL, 79 1.1 christos (void *)&new_tcache_max, sizeof(new_tcache_max)), 0, 80 1.1 christos "Unexpected mallctl failure"); 81 1.1 christos 82 1.1 christos /* Check that the tcache was disabled. */ 83 1.1 christos tsd_t *tsd = tsd_fetch(); 84 1.1 christos expect_false(tsd_tcache_enabled_get(tsd), 85 1.1 christos "tcache should be disabled after reinit failure"); 86 1.1 christos 87 1.1 christos /* Allocations should go to the arena. */ 88 1.1 christos void *p = malloc(64); 89 1.1 christos expect_ptr_not_null(p, "malloc should succeed without tcache"); 90 1.1 christos free(p); 91 1.1 christos 92 1.1 christos /* Restore the original values */ 93 1.1 christos tcache_stack_alloc = orig_tcache_stack_alloc; 94 1.1 christos opt_abort = orig_opt_abort; 95 1.1 christos 96 1.1 christos /* 97 1.1 christos * Try to enable the tcache again. This time initialization 98 1.1 christos * should succeed. 99 1.1 christos */ 100 1.1 christos bool e0, e1; 101 1.1 christos size_t bool_sz = sizeof(bool); 102 1.1 christos e1 = true; 103 1.1 christos expect_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &bool_sz, 104 1.1 christos (void *)&e1, bool_sz), 0, "Unexpected mallctl failure"); 105 1.1 christos 106 1.1 christos /* Restore the original tcache max. */ 107 1.1 christos expect_d_eq(mallctl("thread.tcache.max", NULL, NULL, 108 1.1 christos (void *)&old_tcache_max, sizeof(old_tcache_max)), 0, 109 1.1 christos "Unexpected mallctl failure"); 110 1.1 christos } 111 1.1 christos TEST_END 112 1.1 christos 113 1.1 christos int 114 1.1 christos main(void) { 115 1.1 christos return test(test_tcache_data_init_oom, test_tcache_reinit_oom); 116 1.1 christos } 117