Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Assertions.h>
28#include <AK/Types.h>
29#include <fcntl.h>
30#include <stdio.h>
31#include <sys/mman.h>
32#include <unistd.h>
33
34#define EXPECT_OK(syscall, address, size) \
35 do { \
36 rc = syscall(fd, (void*)(address), (size_t)(size)); \
37 if (rc < 0) { \
38 fprintf(stderr, "Expected success: " #syscall "(%p, %zu), got rc=%d, errno=%d\n", (void*)(address), (size_t)(size), rc, errno); \
39 } \
40 } while(0)
41
42#define EXPECT_EFAULT(syscall, address, size) \
43 do { \
44 rc = syscall(fd, (void*)(address), (size_t)(size)); \
45 if (rc >= 0 || errno != EFAULT) { \
46 fprintf(stderr, "Expected EFAULT: " #syscall "(%p, %zu), got rc=%d, errno=%d\n", (void*)(address), (size_t)(size), rc, errno); \
47 } \
48 } while(0)
49
50#define EXPECT_EFAULT_NO_FD(syscall, address, size) \
51 do { \
52 rc = syscall((address), (size_t)(size)); \
53 if (rc >= 0 || errno != EFAULT) { \
54 fprintf(stderr, "Expected EFAULT: " #syscall "(%p, %zu), got rc=%d, errno=%d\n", (void*)(address), (size_t)(size), rc, errno); \
55 } \
56 } while(0)
57
58
59int main(int, char**)
60{
61 int fd = open("/dev/zero", O_RDONLY);
62 int rc;
63
64 // Test a one-page mapping (4KB)
65 u8* one_page = (u8*)mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
66 ASSERT(one_page);
67 EXPECT_OK(read, one_page, 4096);
68 EXPECT_EFAULT(read, one_page, 4097);
69 EXPECT_EFAULT(read, one_page - 1, 4096);
70
71 // Test a two-page mapping (8KB)
72 u8* two_page = (u8*)mmap(nullptr, 8192, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
73 ASSERT(two_page);
74
75 EXPECT_OK(read, two_page, 4096);
76 EXPECT_OK(read, two_page + 4096, 4096);
77 EXPECT_OK(read, two_page, 8192);
78 EXPECT_OK(read, two_page + 4095, 4097);
79 EXPECT_OK(read, two_page + 1, 8191);
80 EXPECT_EFAULT(read, two_page, 8193);
81 EXPECT_EFAULT(read, two_page - 1, 1);
82
83 // Check validation of pages between the first and last address.
84 ptrdiff_t distance = two_page - one_page;
85 EXPECT_EFAULT(read, one_page, (u32)distance + 1024);
86
87 // Test every kernel page just because.
88 for (u64 kernel_address = 0xc0000000; kernel_address <= 0xffffffff; kernel_address += PAGE_SIZE) {
89 EXPECT_EFAULT(read, (void*)kernel_address, 1);
90 }
91
92 char buffer[4096];
93 EXPECT_EFAULT_NO_FD(dbgputstr, buffer, 0xffffff00);
94
95 // Test the page just below where the kernel VM begins.
96 u8* jerk_page = (u8*)mmap((void*)(0xc0000000 - PAGE_SIZE), PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, 0, 0);
97 ASSERT(jerk_page == (void*)(0xc0000000 - PAGE_SIZE));
98
99 EXPECT_OK(read, jerk_page, 4096);
100 EXPECT_EFAULT(read, jerk_page, 4097);
101
102 // Test something that would wrap around the 2^32 mark.
103 EXPECT_EFAULT(read, jerk_page, 0x50000000);
104
105 return 0;
106}