Skip to content

[compiler-rt][rtsan] page regions api interception update. #123601

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 2 commits into from
Jan 22, 2025
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
34 changes: 34 additions & 0 deletions compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,35 @@ INTERCEPTOR(int, munmap, void *addr, size_t length) {
return REAL(munmap)(addr, length);
}

INTERCEPTOR(int, madvise, void *addr, size_t length, int flag) {
__rtsan_notify_intercepted_call("madvise");
return REAL(madvise)(addr, length, flag);
}

INTERCEPTOR(int, posix_madvise, void *addr, size_t length, int flag) {
__rtsan_notify_intercepted_call("posix_madvise");
return REAL(posix_madvise)(addr, length, flag);
}

INTERCEPTOR(int, mprotect, void *addr, size_t length, int prot) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could consider posix_madvise as well while you're in the neighborhood.

__rtsan_notify_intercepted_call("mprotect");
return REAL(mprotect)(addr, length, prot);
}

INTERCEPTOR(int, msync, void *addr, size_t length, int flag) {
__rtsan_notify_intercepted_call("msync");
return REAL(msync)(addr, length, flag);
}

#if SANITIZER_APPLE
INTERCEPTOR(int, mincore, const void *addr, size_t length, char *vec) {
#else
INTERCEPTOR(int, mincore, void *addr, size_t length, unsigned char *vec) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this may be in other BSD flavors as well, if you want to perhaps add a SANITIZER_INTERCEPT macro in the big header

     The mincore() function first appeared in 4.4BSD.

Copy link
Contributor

Choose a reason for hiding this comment

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

And it shows up on gentoo linux as well when I just looked

#endif
__rtsan_notify_intercepted_call("mincore");
return REAL(mincore)(addr, length, vec);
}

INTERCEPTOR(int, shm_open, const char *name, int oflag, mode_t mode) {
__rtsan_notify_intercepted_call("shm_open");
return REAL(shm_open)(name, oflag, mode);
Expand Down Expand Up @@ -1148,6 +1177,11 @@ void __rtsan::InitializeInterceptors() {
INTERCEPT_FUNCTION(mmap);
RTSAN_MAYBE_INTERCEPT_MMAP64;
INTERCEPT_FUNCTION(munmap);
INTERCEPT_FUNCTION(madvise);
INTERCEPT_FUNCTION(posix_madvise);
INTERCEPT_FUNCTION(mprotect);
INTERCEPT_FUNCTION(msync);
INTERCEPT_FUNCTION(mincore);
INTERCEPT_FUNCTION(shm_open);
INTERCEPT_FUNCTION(shm_unlink);
RTSAN_MAYBE_INTERCEPT_MEMALIGN;
Expand Down
69 changes: 69 additions & 0 deletions compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,75 @@ TEST(TestRtsanInterceptors, MunmapDiesWhenRealtime) {
ExpectNonRealtimeSurvival(Func);
}

class RtsanOpenedMmapTest : public RtsanFileTest {
protected:
void SetUp() override {
RtsanFileTest::SetUp();
file = fopen(GetTemporaryFilePath(), "w+");
ASSERT_THAT(file, Ne(nullptr));
fd = fileno(file);
ASSERT_THAT(fd, Ne(-1));
int ret = ftruncate(GetOpenFd(), size);
ASSERT_THAT(ret, Ne(-1));
addr =
mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, GetOpenFd(), 0);
ASSERT_THAT(addr, Ne(MAP_FAILED));
ASSERT_THAT(addr, Ne(nullptr));
}

void TearDown() override {
if (addr != nullptr && addr != MAP_FAILED)
munmap(addr, size);
RtsanFileTest::TearDown();
}

void *GetAddr() { return addr; }
static constexpr size_t GetSize() { return size; }

int GetOpenFd() { return fd; }

private:
void *addr = nullptr;
static constexpr size_t size = 4096;
FILE *file = nullptr;
int fd = -1;
};

TEST_F(RtsanOpenedMmapTest, MadviseDiesWhenRealtime) {
auto Func = [this]() { madvise(GetAddr(), GetSize(), MADV_NORMAL); };
ExpectRealtimeDeath(Func, "madvise");
ExpectNonRealtimeSurvival(Func);
}

TEST_F(RtsanOpenedMmapTest, PosixMadviseDiesWhenRealtime) {
auto Func = [this]() { posix_madvise(GetAddr(), GetSize(), MADV_NORMAL); };
ExpectRealtimeDeath(Func, "posix_madvise");
ExpectNonRealtimeSurvival(Func);
}

TEST_F(RtsanOpenedMmapTest, MprotectDiesWhenRealtime) {
auto Func = [this]() { mprotect(GetAddr(), GetSize(), PROT_READ); };
ExpectRealtimeDeath(Func, "mprotect");
ExpectNonRealtimeSurvival(Func);
}

TEST_F(RtsanOpenedMmapTest, MsyncDiesWhenRealtime) {
auto Func = [this]() { msync(GetAddr(), GetSize(), MS_INVALIDATE); };
ExpectRealtimeDeath(Func, "msync");
ExpectNonRealtimeSurvival(Func);
}

TEST_F(RtsanOpenedMmapTest, MincoreDiesWhenRealtime) {
#if SANITIZER_APPLE
std::vector<char> vec(GetSize() / 1024);
#else
std::vector<unsigned char> vec(GetSize() / 1024);
#endif
auto Func = [this, &vec]() { mincore(GetAddr(), GetSize(), vec.data()); };
ExpectRealtimeDeath(Func, "mincore");
ExpectNonRealtimeSurvival(Func);
}

TEST(TestRtsanInterceptors, ShmOpenDiesWhenRealtime) {
auto Func = []() { shm_open("/rtsan_test_shm", O_CREAT | O_RDWR, 0); };
ExpectRealtimeDeath(Func, "shm_open");
Expand Down
Loading