Skip to content

Commit c745ece

Browse files
authored
[compiler-rt][rtsan] page regions api interception update. (llvm#123601)
madvise/mprotect/msync/mincore calls with care for signature difference for the latter.
1 parent 6b1db79 commit c745ece

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,35 @@ 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, posix_madvise, void *addr, size_t length, int flag) {
817+
__rtsan_notify_intercepted_call("posix_madvise");
818+
return REAL(posix_madvise)(addr, length, flag);
819+
}
820+
821+
INTERCEPTOR(int, mprotect, void *addr, size_t length, int prot) {
822+
__rtsan_notify_intercepted_call("mprotect");
823+
return REAL(mprotect)(addr, length, prot);
824+
}
825+
826+
INTERCEPTOR(int, msync, void *addr, size_t length, int flag) {
827+
__rtsan_notify_intercepted_call("msync");
828+
return REAL(msync)(addr, length, flag);
829+
}
830+
831+
#if SANITIZER_APPLE
832+
INTERCEPTOR(int, mincore, const void *addr, size_t length, char *vec) {
833+
#else
834+
INTERCEPTOR(int, mincore, void *addr, size_t length, unsigned char *vec) {
835+
#endif
836+
__rtsan_notify_intercepted_call("mincore");
837+
return REAL(mincore)(addr, length, vec);
838+
}
839+
811840
INTERCEPTOR(int, shm_open, const char *name, int oflag, mode_t mode) {
812841
__rtsan_notify_intercepted_call("shm_open");
813842
return REAL(shm_open)(name, oflag, mode);
@@ -1187,6 +1216,11 @@ void __rtsan::InitializeInterceptors() {
11871216
INTERCEPT_FUNCTION(mmap);
11881217
RTSAN_MAYBE_INTERCEPT_MMAP64;
11891218
INTERCEPT_FUNCTION(munmap);
1219+
INTERCEPT_FUNCTION(madvise);
1220+
INTERCEPT_FUNCTION(posix_madvise);
1221+
INTERCEPT_FUNCTION(mprotect);
1222+
INTERCEPT_FUNCTION(msync);
1223+
INTERCEPT_FUNCTION(mincore);
11901224
INTERCEPT_FUNCTION(shm_open);
11911225
INTERCEPT_FUNCTION(shm_unlink);
11921226
RTSAN_MAYBE_INTERCEPT_MEMALIGN;

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

+69
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,75 @@ 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, PosixMadviseDiesWhenRealtime) {
248+
auto Func = [this]() { posix_madvise(GetAddr(), GetSize(), MADV_NORMAL); };
249+
ExpectRealtimeDeath(Func, "posix_madvise");
250+
ExpectNonRealtimeSurvival(Func);
251+
}
252+
253+
TEST_F(RtsanOpenedMmapTest, MprotectDiesWhenRealtime) {
254+
auto Func = [this]() { mprotect(GetAddr(), GetSize(), PROT_READ); };
255+
ExpectRealtimeDeath(Func, "mprotect");
256+
ExpectNonRealtimeSurvival(Func);
257+
}
258+
259+
TEST_F(RtsanOpenedMmapTest, MsyncDiesWhenRealtime) {
260+
auto Func = [this]() { msync(GetAddr(), GetSize(), MS_INVALIDATE); };
261+
ExpectRealtimeDeath(Func, "msync");
262+
ExpectNonRealtimeSurvival(Func);
263+
}
264+
265+
TEST_F(RtsanOpenedMmapTest, MincoreDiesWhenRealtime) {
266+
#if SANITIZER_APPLE
267+
std::vector<char> vec(GetSize() / 1024);
268+
#else
269+
std::vector<unsigned char> vec(GetSize() / 1024);
270+
#endif
271+
auto Func = [this, &vec]() { mincore(GetAddr(), GetSize(), vec.data()); };
272+
ExpectRealtimeDeath(Func, "mincore");
273+
ExpectNonRealtimeSurvival(Func);
274+
}
275+
207276
TEST(TestRtsanInterceptors, ShmOpenDiesWhenRealtime) {
208277
auto Func = []() { shm_open("/rtsan_test_shm", O_CREAT | O_RDWR, 0); };
209278
ExpectRealtimeDeath(Func, "shm_open");

0 commit comments

Comments
 (0)