1//===-- Unittests for mprotect --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/errno/libc_errno.h"
10#include "src/sys/mman/mmap.h"
11#include "src/sys/mman/mprotect.h"
12#include "src/sys/mman/munmap.h"
13#include "test/UnitTest/ErrnoSetterMatcher.h"
14#include "test/UnitTest/Test.h"
15
16#include <signal.h>
17#include <sys/mman.h>
18
19using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
20using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
21
22TEST(LlvmLibcMProtectTest, NoError) {
23 size_t alloc_size = 128;
24 LIBC_NAMESPACE::libc_errno = 0;
25 void *addr = LIBC_NAMESPACE::mmap(addr: nullptr, size: alloc_size, PROT_READ,
26 MAP_ANONYMOUS | MAP_PRIVATE, fd: -1, offset: 0);
27 ASSERT_ERRNO_SUCCESS();
28 EXPECT_NE(addr, MAP_FAILED);
29
30 int *array = reinterpret_cast<int *>(addr);
31 // Reading from the memory should not crash the test.
32 // Since we used the MAP_ANONYMOUS flag, the contents of the newly
33 // allocated memory should be initialized to zero.
34 EXPECT_EQ(array[0], 0);
35
36 // By setting the memory protection to read and write, we should be able to
37 // modify that memory.
38 EXPECT_THAT(
39 LIBC_NAMESPACE::mprotect(addr, alloc_size, PROT_READ | PROT_WRITE),
40 Succeeds());
41 array[0] = 1;
42 EXPECT_EQ(array[0], 1);
43
44 EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, alloc_size), Succeeds());
45}
46
47// This test is disabled currently due to flakeyness. It will be re-enabled once
48// it is less flakey.
49/*
50TEST(LlvmLibcMProtectTest, Error_InvalidWrite) {
51 // attempting to write to a read-only protected part of memory should cause a
52 // segfault.
53 EXPECT_DEATH(
54 [] {
55 size_t alloc_size = 128;
56 void *addr =
57 LIBC_NAMESPACE::mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE,
58 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
59 LIBC_NAMESPACE::mprotect(addr, alloc_size, PROT_READ);
60
61 (reinterpret_cast<char *>(addr))[0] = 'A';
62 },
63 WITH_SIGNAL(SIGSEGV));
64 // Reading from a write only segment may succeed on some platforms, so there's
65 // no test to check that.
66}
67*/
68

source code of libc/test/src/sys/mman/linux/mprotect_test.cpp