summaryrefslogtreecommitdiff
path: root/src/unit/test_page_allocator.c
blob: ea90355d151fefbf929c5836efae93395056b51c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "../core/allocator.h"
#include "../core/page_allocator.h"
#include "./allocator_utils.h"

#include "greatest.h"
#include <unistd.h>

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);
}