1 //===-- map_test.cpp --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "tests/scudo_unit_test.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);
31 memset(reinterpret_cast<void *>(ReservedMemory
.getBase()), 0xaa, Size
),
34 ReservedMemory
.release();
37 TEST(ScudoMapDeathTest
, MapUnmap
) {
38 const scudo::uptr Size
= 4 * scudo::getPageSizeCached();
41 // Repeat few time to avoid missing crash if it's mmaped by unrelated
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();
49 MemMap
.unmap(MemMap
.getBase(), Size
);
50 memset(reinterpret_cast<void *>(P
), 0xbb, Size
);
56 TEST(ScudoMapDeathTest
, MapWithGuardUnmap
) {
57 const scudo::uptr PageSize
= scudo::getPageSizeCached();
58 const scudo::uptr Size
= 4 * PageSize
;
59 scudo::ReservedMemoryT ReservedMemory
;
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
);
88 ASSERT_TRUE(MemMap
.remap(Q
, PageSize
, MappingName
));
89 memset(reinterpret_cast<void *>(Q
), 0xbb, PageSize
);
90 MemMap
.unmap(MemMap
.getBase(), MemMap
.getCapacity());