1 //===-- map_test.cpp --------------------------------------------*- C++ -*-===//
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 "tests/scudo_unit_test.h"
10
11 #include "common.h"
12
13 #include <string.h>
14 #include <unistd.h>
15
16 static const char *MappingName = "scudo:test";
17
TEST(ScudoMapTest,PageSize)18 TEST(ScudoMapTest, PageSize) {
19 EXPECT_EQ(scudo::getPageSizeCached(),
20 static_cast<scudo::uptr>(getpagesize()));
21 }
22
TEST(ScudoMapTest,MapNoAccessUnmap)23 TEST(ScudoMapTest, MapNoAccessUnmap) {
24 const scudo::uptr Size = 4 * scudo::getPageSizeCached();
25 scudo::MapPlatformData Data = {};
26 void *P = scudo::map(nullptr, Size, MappingName, MAP_NOACCESS, &Data);
27 EXPECT_NE(P, nullptr);
28 EXPECT_DEATH(memset(P, 0xaa, Size), "");
29 scudo::unmap(P, Size, UNMAP_ALL, &Data);
30 }
31
TEST(ScudoMapTest,MapUnmap)32 TEST(ScudoMapTest, MapUnmap) {
33 const scudo::uptr Size = 4 * scudo::getPageSizeCached();
34 void *P = scudo::map(nullptr, Size, MappingName, 0, nullptr);
35 EXPECT_NE(P, nullptr);
36 memset(P, 0xaa, Size);
37 scudo::unmap(P, Size, 0, nullptr);
38 EXPECT_DEATH(memset(P, 0xbb, Size), "");
39 }
40
TEST(ScudoMapTest,MapWithGuardUnmap)41 TEST(ScudoMapTest, MapWithGuardUnmap) {
42 const scudo::uptr PageSize = scudo::getPageSizeCached();
43 const scudo::uptr Size = 4 * PageSize;
44 scudo::MapPlatformData Data = {};
45 void *P = scudo::map(nullptr, Size + 2 * PageSize, MappingName, MAP_NOACCESS,
46 &Data);
47 EXPECT_NE(P, nullptr);
48 void *Q =
49 reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(P) + PageSize);
50 EXPECT_EQ(scudo::map(Q, Size, MappingName, 0, &Data), Q);
51 memset(Q, 0xaa, Size);
52 EXPECT_DEATH(memset(Q, 0xaa, Size + 1), "");
53 scudo::unmap(P, Size + 2 * PageSize, UNMAP_ALL, &Data);
54 }
55
TEST(ScudoMapTest,MapGrowUnmap)56 TEST(ScudoMapTest, MapGrowUnmap) {
57 const scudo::uptr PageSize = scudo::getPageSizeCached();
58 const scudo::uptr Size = 4 * PageSize;
59 scudo::MapPlatformData Data = {};
60 void *P = scudo::map(nullptr, Size, MappingName, MAP_NOACCESS, &Data);
61 EXPECT_NE(P, nullptr);
62 void *Q =
63 reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(P) + PageSize);
64 EXPECT_EQ(scudo::map(Q, PageSize, MappingName, 0, &Data), Q);
65 memset(Q, 0xaa, PageSize);
66 Q = reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(Q) + PageSize);
67 EXPECT_EQ(scudo::map(Q, PageSize, MappingName, 0, &Data), Q);
68 memset(Q, 0xbb, PageSize);
69 scudo::unmap(P, Size, UNMAP_ALL, &Data);
70 }
71