Skip to content

[scudo] Make release to OS test more specific. #147852

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
70 changes: 43 additions & 27 deletions compiler-rt/lib/scudo/standalone/tests/common_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,60 @@

#include "common.h"
#include "mem_map.h"

#include <errno.h>
#include <string.h>
#include <sys/mman.h>

#include <algorithm>
#include <fstream>
#include <vector>

namespace scudo {

static uptr getResidentMemorySize() {
if (!SCUDO_LINUX)
UNREACHABLE("Not implemented!");
uptr Size;
uptr Resident;
std::ifstream IFS("/proc/self/statm");
IFS >> Size;
IFS >> Resident;
return Resident * getPageSizeCached();
static void getResidentPages(void *BaseAddress, size_t TotalPages,
size_t *ResidentPages) {
std::vector<unsigned char> Pages(TotalPages, 0);
ASSERT_EQ(
0, mincore(BaseAddress, TotalPages * getPageSizeCached(), Pages.data()))
<< strerror(errno);
*ResidentPages = 0;
for (unsigned char Value : Pages) {
if (Value & 1) {
++*ResidentPages;
}
}
}

// Fuchsia needs getResidentMemorySize implementation.
// Fuchsia needs getResidentPages implementation.
TEST(ScudoCommonTest, SKIP_ON_FUCHSIA(ResidentMemorySize)) {
uptr OnStart = getResidentMemorySize();
EXPECT_GT(OnStart, 0UL);

const uptr Size = 1ull << 30;
const uptr Threshold = Size >> 3;
// Make sure to have the size of the map on a page boundary.
const uptr PageSize = getPageSizeCached();
const size_t NumPages = 1000;
const uptr SizeBytes = NumPages * PageSize;

MemMapT MemMap;
ASSERT_TRUE(MemMap.map(/*Addr=*/0U, Size, "ResidentMemorySize"));
ASSERT_TRUE(MemMap.map(/*Addr=*/0U, SizeBytes, "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);

MemMap.releasePagesToOS(MemMap.getBase(), Size);
EXPECT_LT(getResidentMemorySize(), OnStart + Threshold);

memset(P, 1, Size);
EXPECT_GT(getResidentMemorySize(), OnStart + Size - Threshold);
void *P = reinterpret_cast<void *>(MemMap.getBase());
size_t ResidentPages;
getResidentPages(P, NumPages, &ResidentPages);
EXPECT_EQ(0U, ResidentPages);

// Make the entire map resident.
memset(P, 1, SizeBytes);
getResidentPages(P, NumPages, &ResidentPages);
EXPECT_EQ(NumPages, ResidentPages);

// Should release the memory to the kernel immediately.
MemMap.releasePagesToOS(MemMap.getBase(), SizeBytes);
getResidentPages(P, NumPages, &ResidentPages);
EXPECT_EQ(0U, ResidentPages);

// Make the entire map resident again.
memset(P, 1, SizeBytes);
getResidentPages(P, NumPages, &ResidentPages);
EXPECT_EQ(NumPages, ResidentPages);

MemMap.unmap();
}
Expand Down
Loading