san_bump.c 3.6 KB

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