Skip to content

[scudo][NFC] Add a default unmap() to unmap all pages #102234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions compiler-rt/lib/scudo/standalone/combined.h
Original file line number Diff line number Diff line change
Expand Up @@ -1706,14 +1706,12 @@ class Allocator {
return;
// N.B. because RawStackDepotMap is part of RawRingBufferMap, the order
// is very important.
RB->RawStackDepotMap.unmap(RB->RawStackDepotMap.getBase(),
RB->RawStackDepotMap.getCapacity());
RB->RawStackDepotMap.unmap();
// Note that the `RB->RawRingBufferMap` is stored on the pages managed by
// itself. Take over the ownership before calling unmap() so that any
// operation along with unmap() won't touch inaccessible pages.
MemMapT RawRingBufferMap = RB->RawRingBufferMap;
RawRingBufferMap.unmap(RawRingBufferMap.getBase(),
RawRingBufferMap.getCapacity());
RawRingBufferMap.unmap();
atomic_store(&RingBufferAddress, 0, memory_order_release);
}

Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/scudo/standalone/mem_map_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ template <class Derived> class MemMapBase {
DCHECK((Addr == getBase()) || (Addr + Size == getBase() + getCapacity()));
invokeImpl(&Derived::unmapImpl, Addr, Size);
}
// A default implementation to unmap all pages.
void unmap() { unmap(getBase(), getCapacity()); }

// This is used to remap a mapped range (either from map() or dispatched from
// ReservedMemory). For example, we have reserved several pages and then we
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/scudo/standalone/primary64.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ template <typename Config> class SizeClassAllocator64 {
ScopedLock ML(Region->MMLock);
MemMapT MemMap = Region->MemMapInfo.MemMap;
if (MemMap.isAllocated())
MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
MemMap.unmap();
}
*Region = {};
}
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/scudo/standalone/release.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class BufferPool {
DCHECK_EQ((Mask & (static_cast<uptr>(1) << Buf.BufferIndex)), 0U);
Mask |= static_cast<uptr>(1) << Buf.BufferIndex;
} else {
Buf.MemMap.unmap(Buf.MemMap.getBase(), Buf.MemMap.getCapacity());
Buf.MemMap.unmap();
}
}

Expand Down
4 changes: 1 addition & 3 deletions compiler-rt/lib/scudo/standalone/secondary.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ template <typename Config> static Header *getHeader(const void *Ptr) {

} // namespace LargeBlock

static inline void unmap(MemMapT &MemMap) {
MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
}
static inline void unmap(MemMapT &MemMap) { MemMap.unmap(); }

namespace {

Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/scudo/standalone/tests/common_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST(ScudoCommonTest, SKIP_ON_FUCHSIA(ResidentMemorySize)) {
memset(P, 1, Size);
EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold);

MemMap.unmap(MemMap.getBase(), Size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume that the usage of Size here was incorrect and this was supposed to be a clean-up, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, the Size is always equal to getCapacity() here.

MemMap.unmap();
}

TEST(ScudoCommonTest, Zeros) {
Expand All @@ -69,7 +69,7 @@ TEST(ScudoCommonTest, Zeros) {
MemMap.releasePagesToOS(MemMap.getBase(), Size);
EXPECT_EQ(std::count(P, P + N, 0), N);

MemMap.unmap(MemMap.getBase(), Size);
MemMap.unmap();
}

} // namespace scudo
6 changes: 3 additions & 3 deletions compiler-rt/lib/scudo/standalone/tests/map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST(ScudoMapDeathTest, MapUnmap) {
scudo::uptr P = MemMap.getBase();
if (P == 0U)
continue;
MemMap.unmap(MemMap.getBase(), Size);
MemMap.unmap();
memset(reinterpret_cast<void *>(P), 0xbb, Size);
}
},
Expand All @@ -68,7 +68,7 @@ TEST(ScudoMapDeathTest, MapWithGuardUnmap) {
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());
MemMap.unmap();
}

TEST(ScudoMapTest, MapGrowUnmap) {
Expand All @@ -87,5 +87,5 @@ TEST(ScudoMapTest, MapGrowUnmap) {
Q += PageSize;
ASSERT_TRUE(MemMap.remap(Q, PageSize, MappingName));
memset(reinterpret_cast<void *>(Q), 0xbb, PageSize);
MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
MemMap.unmap();
}
2 changes: 1 addition & 1 deletion compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MemtagTest : public Test {
void TearDown() override {
if (Buffer) {
ASSERT_TRUE(MemMap.isAllocated());
MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
MemMap.unmap();
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/scudo/standalone/tests/strings_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ TEST(ScudoStringsTest, CapacityIncreaseFails) {
scudo::MemMapT MemMap;
if (MemMap.map(/*Addr=*/0U, scudo::getPageSizeCached(), "scudo:test",
MAP_ALLOWNOMEM)) {
MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
MemMap.unmap();
setrlimit(RLIMIT_AS, &Limit);
TEST_SKIP("Limiting address space does not prevent mmap.");
}
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/scudo/standalone/tests/vector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ TEST(ScudoVectorTest, ReallocateFails) {
scudo::MemMapT MemMap;
if (MemMap.map(/*Addr=*/0U, scudo::getPageSizeCached(), "scudo:test",
MAP_ALLOWNOMEM)) {
MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
MemMap.unmap();
setrlimit(RLIMIT_AS, &Limit);
TEST_SKIP("Limiting address space does not prevent mmap.");
}
Expand Down
3 changes: 1 addition & 2 deletions compiler-rt/lib/scudo/standalone/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ template <typename T, size_t StaticNumEntries> class VectorNoCtor {
}
void destroy() {
if (Data != &LocalData[0])
ExternalBuffer.unmap(ExternalBuffer.getBase(),
ExternalBuffer.getCapacity());
ExternalBuffer.unmap();
}

private:
Expand Down
Loading