Skip to content
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
7 changes: 6 additions & 1 deletion compiler-rt/lib/scudo/standalone/mem_map_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ void MemMapLinux::setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags) {
void MemMapLinux::releaseAndZeroPagesToOSImpl(uptr From, uptr Size) {
void *Addr = reinterpret_cast<void *>(From);

while (madvise(Addr, Size, MADV_DONTNEED) == -1 && errno == EAGAIN) {
int rc;
while ((rc = madvise(Addr, Size, MADV_DONTNEED)) == -1 && errno == EAGAIN) {
}
if (rc == -1) {
// If we can't madvies the memory, then we still need to zero it.
memset(Addr, 0, Size);
Comment on lines +128 to +130
Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking if we want to check something like "errno == EPERM". It seems not making any difference to the problem but maybe it helps with some other cases. I guess not...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, when I checked this the errno was EINVAL, which is not what I was expecting. It seems safer to just memset when it fails for whatever reason.

}
}

Expand Down
46 changes: 46 additions & 0 deletions compiler-rt/lib/scudo/standalone/tests/map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include <string.h>
#include <unistd.h>

#if SCUDO_LINUX
#include <sys/mman.h>
#endif

static const char *MappingName = "scudo:test";

TEST(ScudoMapTest, PageSize) {
Expand Down Expand Up @@ -89,3 +93,45 @@ TEST(ScudoMapTest, MapGrowUnmap) {
memset(reinterpret_cast<void *>(Q), 0xbb, PageSize);
MemMap.unmap();
}

// Verify that zeroing works properly.
TEST(ScudoMapTest, Zeroing) {
scudo::ReservedMemoryT ReservedMemory;
const scudo::uptr PageSize = scudo::getPageSizeCached();
const scudo::uptr Size = 3 * PageSize;
ReservedMemory.create(/*Addr=*/0U, Size, MappingName);
ASSERT_TRUE(ReservedMemory.isCreated());

scudo::MemMapT MemMap = ReservedMemory.dispatch(ReservedMemory.getBase(),
ReservedMemory.getCapacity());
EXPECT_TRUE(
MemMap.remap(MemMap.getBase(), MemMap.getCapacity(), MappingName));
unsigned char *Data = reinterpret_cast<unsigned char *>(MemMap.getBase());
memset(Data, 1U, MemMap.getCapacity());
// Spot check some values.
EXPECT_EQ(1U, Data[0]);
EXPECT_EQ(1U, Data[PageSize]);
EXPECT_EQ(1U, Data[PageSize * 2]);
MemMap.releaseAndZeroPagesToOS(MemMap.getBase(), MemMap.getCapacity());
EXPECT_EQ(0U, Data[0]);
EXPECT_EQ(0U, Data[PageSize]);
EXPECT_EQ(0U, Data[PageSize * 2]);

#if SCUDO_LINUX
// Now verify that if madvise fails, the data is still zeroed.
memset(Data, 1U, MemMap.getCapacity());
EXPECT_NE(-1, mlock(Data, MemMap.getCapacity()));

EXPECT_EQ(1U, Data[0]);
EXPECT_EQ(1U, Data[PageSize]);
EXPECT_EQ(1U, Data[PageSize * 2]);
MemMap.releaseAndZeroPagesToOS(MemMap.getBase(), MemMap.getCapacity());
EXPECT_EQ(0U, Data[0]);
EXPECT_EQ(0U, Data[PageSize]);
EXPECT_EQ(0U, Data[PageSize * 2]);

EXPECT_NE(-1, munlock(Data, MemMap.getCapacity()));
#endif

MemMap.unmap();
}
Loading