#include "../core/allocator.h" #include "../core/page_allocator.h" #include "./allocator_utils.h" #include "greatest.h" #include TEST larger_than_page_size(void) { SandAllocator *a = sand_get_page_allocator(); long page_size = sysconf(_SC_PAGESIZE); if (page_size < 0) { FAILm("Failed to get page size!?"); } size_t size = page_size + 128; unsigned char *ptr = sand_allocate(a, size); for (size_t i = 0; i < size; ++i) { // This will crash under ASAN if invalid memory is returned. ptr[i] = 0xFE; } sand_deallocate(a, ptr, size); PASS(); } TEST five_page_allocation(void) { SandAllocator *a = sand_get_page_allocator(); long page_size = sysconf(_SC_PAGESIZE); if (page_size < 0) { FAILm("Failed to get page size!?"); } size_t size = 5 * page_size; unsigned char *ptr = sand_allocate(a, size); ASSERT_NEQm("Allocating 5 pqgs should not fail", ptr, NULL); for (size_t i = 0; i < size; ++i) { // This will crash under ASAN if invalid memory is returned. ptr[i] = 0xFE; } sand_deallocate(a, ptr, size); PASS(); } SUITE(page_allocator) { #define RUN_WITH_PAGE_ALLOC(test) \ do { \ SandAllocator *a = sand_get_page_allocator(); \ RUN_TEST1(test, a); \ } while (0); RUN_ALLOCATOR_TESTS(RUN_WITH_PAGE_ALLOC); #undef X RUN_TEST(larger_than_page_size); RUN_TEST(five_page_allocation); }