[compiler-rt] 7557710 - Reland D146570 "[scudo] Switch to use MemMap in tests"
Chia-hung Duan via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 6 20:06:41 PDT 2023
Author: Chia-hung Duan
Date: 2023-04-07T03:05:56Z
New Revision: 755771045fc92da9b202118299884a25cd0942fc
URL: https://github.com/llvm/llvm-project/commit/755771045fc92da9b202118299884a25cd0942fc
DIFF: https://github.com/llvm/llvm-project/commit/755771045fc92da9b202118299884a25cd0942fc.diff
LOG: Reland D146570 "[scudo] Switch to use MemMap in tests"
This reverts commit 89cc5304969ded574550e0ad113f59f3d4f50303 and fix the
variable shadowing bug.
Differential Revision: https://reviews.llvm.org/D147672
Added:
Modified:
compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
compiler-rt/lib/scudo/standalone/tests/common_test.cpp
compiler-rt/lib/scudo/standalone/tests/map_test.cpp
compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
index 550fdd3dd7606..7bf580e37706a 100644
--- 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 <condition_variable>
#include <memory>
@@ -495,11 +496,12 @@ SCUDO_TYPED_TEST(ScudoCombinedTest, ThreadedCombined) {
// 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::ReservedMemoryT ReservedMemory;
+ ASSERT_TRUE(ReservedMemory.create(/*Addr=*/0U, Size, "testSEGV"));
+ void *P = reinterpret_cast<void *>(ReservedMemory.getBase());
+ ASSERT_NE(P, nullptr);
EXPECT_DEATH(memset(P, 0xaa, Size), "");
- scudo::unmap(P, Size, UNMAP_ALL, &Data);
+ ReservedMemory.release();
}
struct DeathSizeClassConfig {
diff --git a/compiler-rt/lib/scudo/standalone/tests/common_test.cpp b/compiler-rt/lib/scudo/standalone/tests/common_test.cpp
index a9794f03fd7a5..b1e55e80d09bf 100644
--- 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 <algorithm>
#include <fstream>
@@ -34,39 +35,41 @@ TEST(ScudoCommonTest, SKIP_ON_FUCHSIA(ResidentMemorySize)) {
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"));
+ ASSERT_NE(MemMap.getBase(), 0U);
+ void *P = reinterpret_cast<void *>(MemMap.getBase());
EXPECT_LT(getResidentMemorySize(), OnStart + Threshold);
memset(P, 1, Size);
EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold);
- releasePagesToOS((uptr)P, 0, Size, &Data);
+ MemMap.releasePagesToOS(MemMap.getBase(), Size);
EXPECT_LT(getResidentMemorySize(), OnStart + Threshold);
memset(P, 1, Size);
EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold);
- unmap(P, Size, 0, &Data);
+ MemMap.unmap(MemMap.getBase(), Size);
}
TEST(ScudoCommonTest, Zeros) {
const uptr Size = 1ull << 20;
- MapPlatformData Data = {};
- uptr *P = reinterpret_cast<uptr *>(map(nullptr, Size, "Zeros", 0, &Data));
- const ptr
diff _t N = Size / sizeof(*P);
- ASSERT_NE(nullptr, P);
+ MemMapT MemMap;
+ ASSERT_TRUE(MemMap.map(/*Addr=*/0U, Size, "Zeros"));
+ ASSERT_NE(MemMap.getBase(), 0U);
+ uptr *P = reinterpret_cast<uptr *>(MemMap.getBase());
+ const ptr
diff _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(MemMap.getBase(), Size);
EXPECT_EQ(std::count(P, P + N, 0), N);
- unmap(P, Size, 0, &Data);
+ MemMap.unmap(MemMap.getBase(), 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
index ff05258db58d1..06a56f848030e 100644
--- 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 <string.h>
#include <unistd.h>
@@ -22,11 +23,15 @@ TEST(ScudoMapTest, PageSize) {
TEST(ScudoMapDeathTest, MapNoAccessUnmap) {
const scudo::uptr Size = 4 * scudo::getPageSizeCached();
- scudo::MapPlatformData Data = {};
- void *P = scudo::map(nullptr, Size, MappingName, MAP_NOACCESS, &Data);
- EXPECT_NE(P, nullptr);
- EXPECT_DEATH(memset(P, 0xaa, Size), "");
- scudo::unmap(P, Size, UNMAP_ALL, &Data);
+ scudo::ReservedMemoryT ReservedMemory;
+
+ ASSERT_TRUE(ReservedMemory.create(/*Addr=*/0U, Size, MappingName));
+ EXPECT_NE(ReservedMemory.getBase(), 0U);
+ EXPECT_DEATH(
+ memset(reinterpret_cast<void *>(ReservedMemory.getBase()), 0xaa, Size),
+ "");
+
+ ReservedMemory.release();
}
TEST(ScudoMapDeathTest, MapUnmap) {
@@ -36,11 +41,13 @@ TEST(ScudoMapDeathTest, MapUnmap) {
// 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);
- if (!P)
+ scudo::MemMapT MemMap;
+ MemMap.map(/*Addr=*/0U, Size, MappingName);
+ scudo::uptr P = MemMap.getBase();
+ if (P == 0U)
continue;
- scudo::unmap(P, Size, 0, nullptr);
- memset(P, 0xbb, Size);
+ MemMap.unmap(MemMap.getBase(), Size);
+ memset(reinterpret_cast<void *>(P), 0xbb, Size);
}
},
"");
@@ -49,30 +56,36 @@ TEST(ScudoMapDeathTest, MapUnmap) {
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<void *>(reinterpret_cast<scudo::uptr>(P) + PageSize);
- EXPECT_EQ(scudo::map(Q, Size, MappingName, 0, &Data), Q);
- memset(Q, 0xaa, Size);
- EXPECT_DEATH(memset(Q, 0xaa, Size + 1), "");
- scudo::unmap(P, Size + 2 * PageSize, UNMAP_ALL, &Data);
+ scudo::ReservedMemoryT ReservedMemory;
+ ASSERT_TRUE(
+ ReservedMemory.create(/*Addr=*/0U, Size + 2 * PageSize, MappingName));
+ ASSERT_NE(ReservedMemory.getBase(), 0U);
+
+ scudo::MemMapT MemMap =
+ ReservedMemory.dispatch(ReservedMemory.getBase(), Size + 2 * PageSize);
+ ASSERT_TRUE(MemMap.isAllocated());
+ scudo::uptr Q = MemMap.getBase() + PageSize;
+ ASSERT_TRUE(MemMap.remap(Q, Size, MappingName));
+ memset(reinterpret_cast<void *>(Q), 0xaa, Size);
+ EXPECT_DEATH(memset(reinterpret_cast<void *>(Q), 0xaa, Size + 1), "");
+ MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
}
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<void *>(reinterpret_cast<scudo::uptr>(P) + PageSize);
- EXPECT_EQ(scudo::map(Q, PageSize, MappingName, 0, &Data), Q);
- memset(Q, 0xaa, PageSize);
- Q = reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(Q) + PageSize);
- EXPECT_EQ(scudo::map(Q, PageSize, MappingName, 0, &Data), Q);
- memset(Q, 0xbb, PageSize);
- scudo::unmap(P, Size, UNMAP_ALL, &Data);
+ scudo::ReservedMemoryT ReservedMemory;
+ ReservedMemory.create(/*Addr=*/0U, Size, MappingName);
+ ASSERT_TRUE(ReservedMemory.isCreated());
+
+ scudo::MemMapT MemMap =
+ ReservedMemory.dispatch(ReservedMemory.getBase(), Size);
+ ASSERT_TRUE(MemMap.isAllocated());
+ scudo::uptr Q = MemMap.getBase() + PageSize;
+ ASSERT_TRUE(MemMap.remap(Q, PageSize, MappingName));
+ memset(reinterpret_cast<void *>(Q), 0xaa, PageSize);
+ Q += PageSize;
+ ASSERT_TRUE(MemMap.remap(Q, PageSize, MappingName));
+ memset(reinterpret_cast<void *>(Q), 0xbb, PageSize);
+ MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
}
diff --git a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
index 8a40eda3a571a..d4c39aabe91f7 100644
--- 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,24 @@ class MemtagTest : public Test {
GTEST_SKIP() << "Memory tagging is not supported";
BufferSize = getPageSizeCached();
- Buffer = reinterpret_cast<u8 *>(
- map(nullptr, BufferSize, "MemtagTest", MAP_MEMTAG, &Data));
- Addr = reinterpret_cast<uptr>(Buffer);
+ ASSERT_FALSE(MemMap.isAllocated());
+ ASSERT_TRUE(MemMap.map(/*Addr=*/0U, BufferSize, "MemtagTest", MAP_MEMTAG));
+ ASSERT_NE(MemMap.getBase(), 0U);
+ Addr = MemMap.getBase();
+ Buffer = reinterpret_cast<u8 *>(Addr);
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.getBase(), MemMap.getCapacity());
+ }
}
uptr BufferSize = 0;
- MapPlatformData Data = {};
+ scudo::MemMapT MemMap = {};
u8 *Buffer = nullptr;
uptr Addr = 0;
};
@@ -179,7 +184,7 @@ TEST_F(MemtagTest, StoreTags) {
EXPECT_EQ(LoadPtr, loadTag(LoadPtr));
// Reset tags without using StoreTags.
- releasePagesToOS(Addr, 0, BufferSize, &Data);
+ MemMap.releasePagesToOS(Addr, BufferSize);
}
}
More information about the llvm-commits
mailing list