Skip to content

Commit 856c382

Browse files
committed
[compiler-rt][rtsan] page regions api interception update.
madvise/mprotect/msync/mincore calls with care for signature difference for the latter.
1 parent 5139c90 commit 856c382

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,30 @@ INTERCEPTOR(int, munmap, void *addr, size_t length) {
808808
return REAL(munmap)(addr, length);
809809
}
810810

811+
INTERCEPTOR(int, madvise, void *addr, size_t length, int flag) {
812+
__rtsan_notify_intercepted_call("madvise");
813+
return REAL(madvise)(addr, length, flag);
814+
}
815+
816+
INTERCEPTOR(int, mprotect, void *addr, size_t length, int prot) {
817+
__rtsan_notify_intercepted_call("mprotect");
818+
return REAL(mprotect)(addr, length, prot);
819+
}
820+
821+
INTERCEPTOR(int, msync, void *addr, size_t length, int flag) {
822+
__rtsan_notify_intercepted_call("msync");
823+
return REAL(msync)(addr, length, flag);
824+
}
825+
826+
#if SANITIZER_APPLE
827+
INTERCEPTOR(int, mincore, const void *addr, size_t length, char *vec) {
828+
#else
829+
INTERCEPTOR(int, mincore, void *addr, size_t length, unsigned char *vec) {
830+
#endif
831+
__rtsan_notify_intercepted_call("mincore");
832+
return REAL(mincore)(addr, length, vec);
833+
}
834+
811835
INTERCEPTOR(int, shm_open, const char *name, int oflag, mode_t mode) {
812836
__rtsan_notify_intercepted_call("shm_open");
813837
return REAL(shm_open)(name, oflag, mode);
@@ -1148,6 +1172,10 @@ void __rtsan::InitializeInterceptors() {
11481172
INTERCEPT_FUNCTION(mmap);
11491173
RTSAN_MAYBE_INTERCEPT_MMAP64;
11501174
INTERCEPT_FUNCTION(munmap);
1175+
INTERCEPT_FUNCTION(madvise);
1176+
INTERCEPT_FUNCTION(mprotect);
1177+
INTERCEPT_FUNCTION(msync);
1178+
INTERCEPT_FUNCTION(mincore);
11511179
INTERCEPT_FUNCTION(shm_open);
11521180
INTERCEPT_FUNCTION(shm_unlink);
11531181
RTSAN_MAYBE_INTERCEPT_MEMALIGN;

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,69 @@ TEST(TestRtsanInterceptors, MunmapDiesWhenRealtime) {
204204
ExpectNonRealtimeSurvival(Func);
205205
}
206206

207+
class RtsanOpenedMmapTest : public RtsanFileTest {
208+
protected:
209+
void SetUp() override {
210+
RtsanFileTest::SetUp();
211+
file = fopen(GetTemporaryFilePath(), "w+");
212+
ASSERT_THAT(file, Ne(nullptr));
213+
fd = fileno(file);
214+
ASSERT_THAT(fd, Ne(-1));
215+
int ret = ftruncate(GetOpenFd(), size);
216+
ASSERT_THAT(ret, Ne(-1));
217+
addr =
218+
mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, GetOpenFd(), 0);
219+
ASSERT_THAT(addr, Ne(MAP_FAILED));
220+
ASSERT_THAT(addr, Ne(nullptr));
221+
}
222+
223+
void TearDown() override {
224+
if (addr != nullptr && addr != MAP_FAILED)
225+
munmap(addr, size);
226+
RtsanFileTest::TearDown();
227+
}
228+
229+
void *GetAddr() { return addr; }
230+
static constexpr size_t GetSize() { return size; }
231+
232+
int GetOpenFd() { return fd; }
233+
234+
private:
235+
void *addr = nullptr;
236+
static constexpr size_t size = 4096;
237+
FILE *file = nullptr;
238+
int fd = -1;
239+
};
240+
241+
TEST_F(RtsanOpenedMmapTest, MadviseDiesWhenRealtime) {
242+
auto Func = [this]() { madvise(GetAddr(), GetSize(), MADV_NORMAL); };
243+
ExpectRealtimeDeath(Func, "madvise");
244+
ExpectNonRealtimeSurvival(Func);
245+
}
246+
247+
TEST_F(RtsanOpenedMmapTest, MprotectDiesWhenRealtime) {
248+
auto Func = [this]() { mprotect(GetAddr(), GetSize(), PROT_READ); };
249+
ExpectRealtimeDeath(Func, "mprotect");
250+
ExpectNonRealtimeSurvival(Func);
251+
}
252+
253+
TEST_F(RtsanOpenedMmapTest, MsyncDiesWhenRealtime) {
254+
auto Func = [this]() { msync(GetAddr(), GetSize(), MS_INVALIDATE); };
255+
ExpectRealtimeDeath(Func, "msync");
256+
ExpectNonRealtimeSurvival(Func);
257+
}
258+
259+
TEST_F(RtsanOpenedMmapTest, MincoreDiesWhenRealtime) {
260+
#if SANITIZER_APPLE
261+
char vec[GetSize() / 1024];
262+
#else
263+
unsigned char vec[GetSize() / 1024];
264+
#endif
265+
auto Func = [this, &vec]() { mincore(GetAddr(), GetSize(), vec); };
266+
ExpectRealtimeDeath(Func, "mincore");
267+
ExpectNonRealtimeSurvival(Func);
268+
}
269+
207270
TEST(TestRtsanInterceptors, ShmOpenDiesWhenRealtime) {
208271
auto Func = []() { shm_open("/rtsan_test_shm", O_CREAT | O_RDWR, 0); };
209272
ExpectRealtimeDeath(Func, "shm_open");

0 commit comments

Comments
 (0)