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