xref: /haiku/src/tests/system/kernel/mmap_cut_tests.cpp (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /*
2  * Copyright 2023, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <stdio.h>
7 #include <string.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <OS.h>
11 
12 
13 int
14 main()
15 {
16 	int fd = open("/boot/system/lib/libroot.so", O_CLOEXEC | O_RDONLY);
17 	if (fd < 0)
18 		return -1;
19 
20 	// should fail (negative offset)
21 	void* ptr0 = mmap(NULL, B_PAGE_SIZE, PROT_READ, MAP_PRIVATE, fd, -4096);
22 	if (ptr0 != NULL) {
23 		printf("map-negative-offset unexpectedly succeeded!\n");
24 		return -1;
25 	}
26 
27 	uint8* ptr1 = (uint8*)mmap(NULL, 16 * B_PAGE_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
28 	uint8 chunk[128];
29 	memcpy(chunk, &ptr1[3 * B_PAGE_SIZE], sizeof(chunk));
30 
31 	// now cut the area
32 	uint8* ptr2 = (uint8*)mmap(&ptr1[B_PAGE_SIZE], B_PAGE_SIZE,
33 		PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
34 
35 	// validate that the area after the cut still has the expected data
36 	int status = memcmp(&ptr1[3 * B_PAGE_SIZE], chunk, sizeof(chunk));
37 	if (status != 0) {
38 		printf("map-cut-compare test failed!\n");
39 		return status;
40 	}
41 
42 	return 0;
43 }
44