Home | History | Annotate | Line # | Download | only in unit
      1 #include "test/jemalloc_test.h"
      2 #include "test/arena_util.h"
      3 
      4 #include "jemalloc/internal/arena_structs.h"
      5 #include "jemalloc/internal/san_bump.h"
      6 
      7 TEST_BEGIN(test_san_bump_alloc) {
      8 	test_skip_if(!maps_coalesce || !opt_retain);
      9 
     10 	tsdn_t *tsdn = tsdn_fetch();
     11 
     12 	san_bump_alloc_t sba;
     13 	san_bump_alloc_init(&sba);
     14 
     15 	unsigned arena_ind = do_arena_create(0, 0);
     16 	assert_u_ne(arena_ind, UINT_MAX, "Failed to create an arena");
     17 
     18 	arena_t *arena = arena_get(tsdn, arena_ind, false);
     19 	pac_t *pac = &arena->pa_shard.pac;
     20 
     21 	size_t alloc_size = PAGE * 16;
     22 	size_t alloc_n = alloc_size / sizeof(unsigned);
     23 	edata_t* edata = san_bump_alloc(tsdn, &sba, pac, pac_ehooks_get(pac),
     24 	    alloc_size, /* zero */ false);
     25 
     26 	expect_ptr_not_null(edata, "Failed to allocate edata");
     27 	expect_u_eq(edata_arena_ind_get(edata), arena_ind,
     28 	    "Edata was assigned an incorrect arena id");
     29 	expect_zu_eq(edata_size_get(edata), alloc_size,
     30 	    "Allocated edata of incorrect size");
     31 	expect_false(edata_slab_get(edata),
     32 	    "Bump allocator incorrectly assigned 'slab' to true");
     33 	expect_true(edata_committed_get(edata), "Edata is not committed");
     34 
     35 	void *ptr = edata_addr_get(edata);
     36 	expect_ptr_not_null(ptr, "Edata was assigned an invalid address");
     37 	/* Test that memory is allocated; no guard pages are misplaced */
     38 	for (unsigned i = 0; i < alloc_n; ++i) {
     39 		((unsigned *)ptr)[i] = 1;
     40 	}
     41 
     42 	size_t alloc_size2 = PAGE * 28;
     43 	size_t alloc_n2 = alloc_size / sizeof(unsigned);
     44 	edata_t *edata2 = san_bump_alloc(tsdn, &sba, pac, pac_ehooks_get(pac),
     45 	    alloc_size2, /* zero */ true);
     46 
     47 	expect_ptr_not_null(edata2, "Failed to allocate edata");
     48 	expect_u_eq(edata_arena_ind_get(edata2), arena_ind,
     49 	    "Edata was assigned an incorrect arena id");
     50 	expect_zu_eq(edata_size_get(edata2), alloc_size2,
     51 	    "Allocated edata of incorrect size");
     52 	expect_false(edata_slab_get(edata2),
     53 	    "Bump allocator incorrectly assigned 'slab' to true");
     54 	expect_true(edata_committed_get(edata2), "Edata is not committed");
     55 
     56 	void *ptr2 = edata_addr_get(edata2);
     57 	expect_ptr_not_null(ptr, "Edata was assigned an invalid address");
     58 
     59 	uintptr_t ptrdiff = ptr2 > ptr ? (uintptr_t)ptr2 - (uintptr_t)ptr
     60 	    : (uintptr_t)ptr - (uintptr_t)ptr2;
     61 	size_t between_allocs = (size_t)ptrdiff - alloc_size;
     62 
     63 	expect_zu_ge(between_allocs, PAGE,
     64 	    "Guard page between allocs is missing");
     65 
     66 	for (unsigned i = 0; i < alloc_n2; ++i) {
     67 		expect_u_eq(((unsigned *)ptr2)[i], 0, "Memory is not zeroed");
     68 	}
     69 }
     70 TEST_END
     71 
     72 TEST_BEGIN(test_large_alloc_size) {
     73 	test_skip_if(!maps_coalesce || !opt_retain);
     74 
     75 	tsdn_t *tsdn = tsdn_fetch();
     76 
     77 	san_bump_alloc_t sba;
     78 	san_bump_alloc_init(&sba);
     79 
     80 	unsigned arena_ind = do_arena_create(0, 0);
     81 	assert_u_ne(arena_ind, UINT_MAX, "Failed to create an arena");
     82 
     83 	arena_t *arena = arena_get(tsdn, arena_ind, false);
     84 	pac_t *pac = &arena->pa_shard.pac;
     85 
     86 	size_t alloc_size = SBA_RETAINED_ALLOC_SIZE * 2;
     87 	edata_t* edata = san_bump_alloc(tsdn, &sba, pac, pac_ehooks_get(pac),
     88 	    alloc_size, /* zero */ false);
     89 	expect_u_eq(edata_arena_ind_get(edata), arena_ind,
     90 	    "Edata was assigned an incorrect arena id");
     91 	expect_zu_eq(edata_size_get(edata), alloc_size,
     92 	    "Allocated edata of incorrect size");
     93 	expect_false(edata_slab_get(edata),
     94 	    "Bump allocator incorrectly assigned 'slab' to true");
     95 	expect_true(edata_committed_get(edata), "Edata is not committed");
     96 
     97 	void *ptr = edata_addr_get(edata);
     98 	expect_ptr_not_null(ptr, "Edata was assigned an invalid address");
     99 	/* Test that memory is allocated; no guard pages are misplaced */
    100 	for (unsigned i = 0; i < alloc_size / PAGE; ++i) {
    101 		*((char *)ptr + PAGE * i) = 1;
    102 	}
    103 }
    104 TEST_END
    105 
    106 int
    107 main(void) {
    108 	return test(
    109 	    test_san_bump_alloc,
    110 	    test_large_alloc_size);
    111 }
    112