diff --git a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp --- a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp @@ -12,6 +12,7 @@ #include "allocator_config.h" #include "chunk.h" #include "combined.h" +#include "mem_map.h" #include #include @@ -495,11 +496,12 @@ // process's signal handlers (GWP-ASan used to do this). TEST(ScudoCombinedDeathTest, SKIP_ON_FUCHSIA(testSEGV)) { const scudo::uptr Size = 4 * scudo::getPageSizeCached(); - scudo::MapPlatformData Data = {}; - void *P = scudo::map(nullptr, Size, "testSEGV", MAP_NOACCESS, &Data); - EXPECT_NE(P, nullptr); + scudo::ReservedMapT ReservedMap; + ReservedMap.reserve(/*Addr=*/0U, Size, "testSEGV"); + void *P = reinterpret_cast(ReservedMap.getMapBase()); + ASSERT_NE(P, nullptr); EXPECT_DEATH(memset(P, 0xaa, Size), ""); - scudo::unmap(P, Size, UNMAP_ALL, &Data); + ReservedMap.unmap(reinterpret_cast(P), Size, UNMAP_ALL); } struct DeathSizeClassConfig { diff --git a/compiler-rt/lib/scudo/standalone/tests/common_test.cpp b/compiler-rt/lib/scudo/standalone/tests/common_test.cpp --- a/compiler-rt/lib/scudo/standalone/tests/common_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/common_test.cpp @@ -10,6 +10,7 @@ #include "tests/scudo_unit_test.h" #include "common.h" +#include "mem_map.h" #include #include @@ -34,39 +35,39 @@ const uptr Size = 1ull << 30; const uptr Threshold = Size >> 3; - MapPlatformData Data = {}; - void *P = map(nullptr, Size, "ResidentMemorySize", 0, &Data); - ASSERT_NE(nullptr, P); + MemMapT MemMap; + ASSERT_TRUE(MemMap.map(/*Addr=*/0U, Size, "ResidentMemorySize")); + void *P = reinterpret_cast(MemMap.getMapBase()); EXPECT_LT(getResidentMemorySize(), OnStart + Threshold); memset(P, 1, Size); EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold); - releasePagesToOS((uptr)P, 0, Size, &Data); + MemMap.releasePagesToOS(reinterpret_cast(P), Size); EXPECT_LT(getResidentMemorySize(), OnStart + Threshold); memset(P, 1, Size); EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold); - unmap(P, Size, 0, &Data); + MemMap.unmap(reinterpret_cast(P), Size); } TEST(ScudoCommonTest, Zeros) { const uptr Size = 1ull << 20; - MapPlatformData Data = {}; - uptr *P = reinterpret_cast(map(nullptr, Size, "Zeros", 0, &Data)); - const ptrdiff_t N = Size / sizeof(*P); - ASSERT_NE(nullptr, P); + MemMapT MemMap; + ASSERT_TRUE(MemMap.map(/*Addr=*/0U, Size, "Zeros")); + uptr *P = reinterpret_cast(MemMap.getMapBase()); + const ptrdiff_t N = Size / sizeof(uptr); EXPECT_EQ(std::count(P, P + N, 0), N); memset(P, 1, Size); EXPECT_EQ(std::count(P, P + N, 0), 0); - releasePagesToOS((uptr)P, 0, Size, &Data); + MemMap.releasePagesToOS(reinterpret_cast(P), Size); EXPECT_EQ(std::count(P, P + N, 0), N); - unmap(P, Size, 0, &Data); + MemMap.unmap(reinterpret_cast(P), Size); } #if 0 diff --git a/compiler-rt/lib/scudo/standalone/tests/map_test.cpp b/compiler-rt/lib/scudo/standalone/tests/map_test.cpp --- a/compiler-rt/lib/scudo/standalone/tests/map_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/map_test.cpp @@ -9,6 +9,7 @@ #include "tests/scudo_unit_test.h" #include "common.h" +#include "mem_map.h" #include #include @@ -22,11 +23,13 @@ TEST(ScudoMapDeathTest, MapNoAccessUnmap) { const scudo::uptr Size = 4 * scudo::getPageSizeCached(); - scudo::MapPlatformData Data = {}; - void *P = scudo::map(nullptr, Size, MappingName, MAP_NOACCESS, &Data); + scudo::ReservedMapT ReservedMap; + ReservedMap.reserve(/*Addr=*/0U, Size, MappingName); + void *P = reinterpret_cast(ReservedMap.getMapBase()); EXPECT_NE(P, nullptr); EXPECT_DEATH(memset(P, 0xaa, Size), ""); - scudo::unmap(P, Size, UNMAP_ALL, &Data); + + ReservedMap.unmap(reinterpret_cast(P), Size, UNMAP_ALL); } TEST(ScudoMapDeathTest, MapUnmap) { @@ -36,10 +39,12 @@ // Repeat few time to avoid missing crash if it's mmaped by unrelated // code. for (int i = 0; i < 10; ++i) { - void *P = scudo::map(nullptr, Size, MappingName, 0, nullptr); + scudo::MemMapT MemMap; + MemMap.map(/*Addr=*/0U, Size, MappingName); + void *P = reinterpret_cast(MemMap.getMapBase()); if (!P) continue; - scudo::unmap(P, Size, 0, nullptr); + MemMap.unmap(reinterpret_cast(P), Size); memset(P, 0xbb, Size); } }, @@ -49,30 +54,39 @@ TEST(ScudoMapDeathTest, MapWithGuardUnmap) { const scudo::uptr PageSize = scudo::getPageSizeCached(); const scudo::uptr Size = 4 * PageSize; - scudo::MapPlatformData Data = {}; - void *P = scudo::map(nullptr, Size + 2 * PageSize, MappingName, MAP_NOACCESS, - &Data); - EXPECT_NE(P, nullptr); - void *Q = - reinterpret_cast(reinterpret_cast(P) + PageSize); - EXPECT_EQ(scudo::map(Q, Size, MappingName, 0, &Data), Q); + scudo::ReservedMapT ReservedMap; + ReservedMap.reserve(/*Addr=*/0U, Size + 2 * PageSize, MappingName); + void *P = reinterpret_cast(ReservedMap.getMapBase()); + ASSERT_NE(P, nullptr); + scudo::MemMapT MemMap = ReservedMap.allocate(reinterpret_cast(P), + Size + 2 * PageSize); + ASSERT_TRUE(MemMap.isAllocated()); + void *Q = reinterpret_cast(MemMap.getMapBase() + PageSize); + ASSERT_TRUE( + MemMap.remap(reinterpret_cast(Q), Size, MappingName)); memset(Q, 0xaa, Size); EXPECT_DEATH(memset(Q, 0xaa, Size + 1), ""); - scudo::unmap(P, Size + 2 * PageSize, UNMAP_ALL, &Data); + MemMap.unmap(MemMap.getMapBase(), MemMap.getMapCapacity(), UNMAP_ALL); } TEST(ScudoMapTest, MapGrowUnmap) { const scudo::uptr PageSize = scudo::getPageSizeCached(); const scudo::uptr Size = 4 * PageSize; - scudo::MapPlatformData Data = {}; - void *P = scudo::map(nullptr, Size, MappingName, MAP_NOACCESS, &Data); - EXPECT_NE(P, nullptr); - void *Q = - reinterpret_cast(reinterpret_cast(P) + PageSize); - EXPECT_EQ(scudo::map(Q, PageSize, MappingName, 0, &Data), Q); + scudo::ReservedMapT ReservedMap; + ReservedMap.reserve(/*Addr=*/0U, Size, MappingName); + void *P = reinterpret_cast(ReservedMap.getMapBase()); + ASSERT_NE(P, nullptr); + + scudo::MemMapT MemMap = + ReservedMap.allocate(reinterpret_cast(P), Size); + ASSERT_TRUE(MemMap.isAllocated()); + void *Q = reinterpret_cast(MemMap.getMapBase() + PageSize); + ASSERT_TRUE( + MemMap.remap(reinterpret_cast(Q), PageSize, MappingName)); memset(Q, 0xaa, PageSize); Q = reinterpret_cast(reinterpret_cast(Q) + PageSize); - EXPECT_EQ(scudo::map(Q, PageSize, MappingName, 0, &Data), Q); + ASSERT_TRUE( + MemMap.remap(reinterpret_cast(Q), PageSize, MappingName)); memset(Q, 0xbb, PageSize); - scudo::unmap(P, Size, UNMAP_ALL, &Data); + MemMap.unmap(MemMap.getMapBase(), MemMap.getMapCapacity(), UNMAP_ALL); } diff --git a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp --- a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "common.h" +#include "mem_map.h" #include "memtag.h" #include "platform.h" #include "tests/scudo_unit_test.h" @@ -45,20 +46,23 @@ GTEST_SKIP() << "Memory tagging is not supported"; BufferSize = getPageSizeCached(); - Buffer = reinterpret_cast( - map(nullptr, BufferSize, "MemtagTest", MAP_MEMTAG, &Data)); + scudo::MemMapT MemMap; + ASSERT_TRUE(MemMap.map(/*Addr=*/0U, BufferSize, "MemtagTest", MAP_MEMTAG)); + Buffer = reinterpret_cast(MemMap.getMapBase()); Addr = reinterpret_cast(Buffer); EXPECT_TRUE(isAligned(Addr, archMemoryTagGranuleSize())); EXPECT_EQ(Addr, untagPointer(Addr)); } void TearDown() override { - if (Buffer) - unmap(Buffer, BufferSize, 0, &Data); + if (Buffer) { + ASSERT_TRUE(MemMap.isAllocated()); + MemMap.unmap(MemMap.getMapBase(), MemMap.getMapCapacity()); + } } uptr BufferSize = 0; - MapPlatformData Data = {}; + scudo::MemMapT MemMap; u8 *Buffer = nullptr; uptr Addr = 0; }; @@ -179,7 +183,7 @@ EXPECT_EQ(LoadPtr, loadTag(LoadPtr)); // Reset tags without using StoreTags. - releasePagesToOS(Addr, 0, BufferSize, &Data); + MemMap.releasePagesToOS(Addr, BufferSize); } }