Skip to content

[rtsan] Added mmap and shm interceptors #114862

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
Nov 5, 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
37 changes: 37 additions & 0 deletions compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,38 @@ INTERCEPTOR(void *, pvalloc, size_t size) {
}
#endif

INTERCEPTOR(void *, mmap, void *addr, size_t length, int prot, int flags,
int fd, off_t offset) {
__rtsan_notify_intercepted_call("mmap");
return REAL(mmap)(addr, length, prot, flags, fd, offset);
}

#if SANITIZER_INTERCEPT_MMAP64
INTERCEPTOR(void *, mmap64, void *addr, size_t length, int prot, int flags,
int fd, off64_t offset) {
__rtsan_notify_intercepted_call("mmap64");
return REAL(mmap64)(addr, length, prot, flags, fd, offset);
}
#define RTSAN_MAYBE_INTERCEPT_MMAP64 INTERCEPT_FUNCTION(mmap64)
#else
#define RTSAN_MAYBE_INTERCEPT_MMAP64
#endif // SANITIZER_INTERCEPT_MMAP64

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

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);
}

INTERCEPTOR(int, shm_unlink, const char *name) {
__rtsan_notify_intercepted_call("shm_unlink");
return REAL(shm_unlink)(name);
}

// Sockets
INTERCEPTOR(int, socket, int domain, int type, int protocol) {
__rtsan_notify_intercepted_call("socket");
Expand Down Expand Up @@ -558,6 +590,11 @@ void __rtsan::InitializeInterceptors() {
INTERCEPT_FUNCTION(valloc);
RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
INTERCEPT_FUNCTION(posix_memalign);
INTERCEPT_FUNCTION(mmap);
RTSAN_MAYBE_INTERCEPT_MMAP64;
INTERCEPT_FUNCTION(munmap);
INTERCEPT_FUNCTION(shm_open);
INTERCEPT_FUNCTION(shm_unlink);
#if SANITIZER_INTERCEPT_MEMALIGN
INTERCEPT_FUNCTION(memalign);
#endif
Expand Down
34 changes: 34 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 @@ -36,6 +36,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/uio.h>

Expand All @@ -47,6 +48,7 @@ const char *const kOpenAtFunctionName = "openat64";
const char *const kOpenFunctionName = "open64";
const char *const kPreadFunctionName = "pread64";
const char *const kPwriteFunctionName = "pwrite64";
const char *const kMmapFunctionName = "mmap64";
#else
const char *const kCreatFunctionName = "creat";
const char *const kFcntlFunctionName = "fcntl";
Expand All @@ -55,6 +57,7 @@ const char *const kOpenAtFunctionName = "openat";
const char *const kOpenFunctionName = "open";
const char *const kPreadFunctionName = "pread";
const char *const kPwriteFunctionName = "pwrite";
const char *const kMmapFunctionName = "mmap";
#endif

using namespace testing;
Expand Down Expand Up @@ -179,6 +182,37 @@ TEST(TestRtsanInterceptors, PvallocDiesWhenRealtime) {
}
#endif

TEST(TestRtsanInterceptors, MmapDiesWhenRealtime) {
auto Func = []() {
void *_ = mmap(nullptr, 8, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
};
ExpectRealtimeDeath(Func, kMmapFunctionName);
ExpectNonRealtimeSurvival(Func);
}

TEST(TestRtsanInterceptors, MunmapDiesWhenRealtime) {
void *ptr = mmap(nullptr, 8, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
EXPECT_NE(ptr, nullptr);
auto Func = [ptr]() { munmap(ptr, 8); };
printf("Right before death munmap\n");
ExpectRealtimeDeath(Func, "munmap");
ExpectNonRealtimeSurvival(Func);
}

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

TEST(TestRtsanInterceptors, ShmUnlinkDiesWhenRealtime) {
auto Func = []() { shm_unlink("/rtsan_test_shm"); };
ExpectRealtimeDeath(Func, "shm_unlink");
ExpectNonRealtimeSurvival(Func);
}

/*
Sleeping
*/
Expand Down
Loading