Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / scudo / standalone / tests / map_test.cpp
blob06a56f848030e6e3f53bb6c912779d06a56680cf
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 //===----------------------------------------------------------------------===//
9 #include "tests/scudo_unit_test.h"
11 #include "common.h"
12 #include "mem_map.h"
14 #include <string.h>
15 #include <unistd.h>
17 static const char *MappingName = "scudo:test";
19 TEST(ScudoMapTest, PageSize) {
20 EXPECT_EQ(scudo::getPageSizeCached(),
21 static_cast<scudo::uptr>(sysconf(_SC_PAGESIZE)));
24 TEST(ScudoMapDeathTest, MapNoAccessUnmap) {
25 const scudo::uptr Size = 4 * scudo::getPageSizeCached();
26 scudo::ReservedMemoryT ReservedMemory;
28 ASSERT_TRUE(ReservedMemory.create(/*Addr=*/0U, Size, MappingName));
29 EXPECT_NE(ReservedMemory.getBase(), 0U);
30 EXPECT_DEATH(
31 memset(reinterpret_cast<void *>(ReservedMemory.getBase()), 0xaa, Size),
32 "");
34 ReservedMemory.release();
37 TEST(ScudoMapDeathTest, MapUnmap) {
38 const scudo::uptr Size = 4 * scudo::getPageSizeCached();
39 EXPECT_DEATH(
41 // Repeat few time to avoid missing crash if it's mmaped by unrelated
42 // code.
43 for (int i = 0; i < 10; ++i) {
44 scudo::MemMapT MemMap;
45 MemMap.map(/*Addr=*/0U, Size, MappingName);
46 scudo::uptr P = MemMap.getBase();
47 if (P == 0U)
48 continue;
49 MemMap.unmap(MemMap.getBase(), Size);
50 memset(reinterpret_cast<void *>(P), 0xbb, Size);
53 "");
56 TEST(ScudoMapDeathTest, MapWithGuardUnmap) {
57 const scudo::uptr PageSize = scudo::getPageSizeCached();
58 const scudo::uptr Size = 4 * PageSize;
59 scudo::ReservedMemoryT ReservedMemory;
60 ASSERT_TRUE(
61 ReservedMemory.create(/*Addr=*/0U, Size + 2 * PageSize, MappingName));
62 ASSERT_NE(ReservedMemory.getBase(), 0U);
64 scudo::MemMapT MemMap =
65 ReservedMemory.dispatch(ReservedMemory.getBase(), Size + 2 * PageSize);
66 ASSERT_TRUE(MemMap.isAllocated());
67 scudo::uptr Q = MemMap.getBase() + PageSize;
68 ASSERT_TRUE(MemMap.remap(Q, Size, MappingName));
69 memset(reinterpret_cast<void *>(Q), 0xaa, Size);
70 EXPECT_DEATH(memset(reinterpret_cast<void *>(Q), 0xaa, Size + 1), "");
71 MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
74 TEST(ScudoMapTest, MapGrowUnmap) {
75 const scudo::uptr PageSize = scudo::getPageSizeCached();
76 const scudo::uptr Size = 4 * PageSize;
77 scudo::ReservedMemoryT ReservedMemory;
78 ReservedMemory.create(/*Addr=*/0U, Size, MappingName);
79 ASSERT_TRUE(ReservedMemory.isCreated());
81 scudo::MemMapT MemMap =
82 ReservedMemory.dispatch(ReservedMemory.getBase(), Size);
83 ASSERT_TRUE(MemMap.isAllocated());
84 scudo::uptr Q = MemMap.getBase() + PageSize;
85 ASSERT_TRUE(MemMap.remap(Q, PageSize, MappingName));
86 memset(reinterpret_cast<void *>(Q), 0xaa, PageSize);
87 Q += PageSize;
88 ASSERT_TRUE(MemMap.remap(Q, PageSize, MappingName));
89 memset(reinterpret_cast<void *>(Q), 0xbb, PageSize);
90 MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());