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

Conversation

devnexen
Copy link
Member

madvise/mprotect/msync/mincore calls with care for signature difference for the latter.

@llvmbot
Copy link
Member

llvmbot commented Jan 20, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: David CARLIER (devnexen)

Changes

madvise/mprotect/msync/mincore calls with care for signature difference for the latter.


Full diff: https://github.com/llvm/llvm-project/pull/123601.diff

2 Files Affected:

  • (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+28)
  • (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (+78-25)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 34c2d4cb37fd0c..e69c75216daa84 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -808,6 +808,30 @@ 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, mprotect, void *addr, size_t length, int prot) {
+  __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) {
+#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);
@@ -1148,6 +1172,10 @@ void __rtsan::InitializeInterceptors() {
   INTERCEPT_FUNCTION(mmap);
   RTSAN_MAYBE_INTERCEPT_MMAP64;
   INTERCEPT_FUNCTION(munmap);
+  INTERCEPT_FUNCTION(madvise);
+  INTERCEPT_FUNCTION(mprotect);
+  INTERCEPT_FUNCTION(msync);
+  INTERCEPT_FUNCTION(mincore);
   INTERCEPT_FUNCTION(shm_open);
   INTERCEPT_FUNCTION(shm_unlink);
   RTSAN_MAYBE_INTERCEPT_MEMALIGN;
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index c858a5a771fe45..633465cdb7ec7a 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -204,6 +204,84 @@ TEST(TestRtsanInterceptors, MunmapDiesWhenRealtime) {
   ExpectNonRealtimeSurvival(Func);
 }
 
+class RtsanOpenedFileTest : 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));
+  }
+
+  void TearDown() override {
+    if (file != nullptr)
+      fclose(file);
+    RtsanFileTest::TearDown();
+  }
+
+  FILE *GetOpenFile() { return file; }
+
+  int GetOpenFd() { return fd; }
+
+private:
+  FILE *file = nullptr;
+  int fd = -1;
+};
+
+class RtsanOpenedMmapTest : public RtsanOpenedFileTest {
+protected:
+  void SetUp() override {
+    RtsanOpenedFileTest::SetUp();
+    addr =
+        mmap(nullptr, size, PROT_READ, MAP_PRIVATE | MAP_ANON, 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);
+    RtsanOpenedFileTest::TearDown();
+  }
+
+  void *GetAddr() { return addr; }
+  static constexpr size_t GetSize() { return size; }
+
+private:
+  void *addr = nullptr;
+  static constexpr size_t size = 4096;
+};
+
+TEST_F(RtsanOpenedMmapTest, MadviseDiesWhenRealtime) {
+  auto Func = [this]() { madvise(GetAddr(), GetSize(), MADV_NORMAL); };
+  ExpectRealtimeDeath(Func, "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
+  char vec[GetSize() / 1024];
+#else
+  unsigned char vec[GetSize() / 1024];
+#endif
+  auto Func = [this, &vec]() { mincore(GetAddr(), GetSize(), vec); };
+  ExpectRealtimeDeath(Func, "mincore");
+  ExpectNonRealtimeSurvival(Func);
+}
+
 TEST(TestRtsanInterceptors, ShmOpenDiesWhenRealtime) {
   auto Func = []() { shm_open("/rtsan_test_shm", O_CREAT | O_RDWR, 0); };
   ExpectRealtimeDeath(Func, "shm_open");
@@ -453,31 +531,6 @@ TEST_F(RtsanFileTest, SetbufferDieWhenRealtime) {
 }
 #endif
 
-class RtsanOpenedFileTest : 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));
-  }
-
-  void TearDown() override {
-    if (file != nullptr)
-      fclose(file);
-    RtsanFileTest::TearDown();
-  }
-
-  FILE *GetOpenFile() { return file; }
-
-  int GetOpenFd() { return fd; }
-
-private:
-  FILE *file = nullptr;
-  int fd = -1;
-};
-
 #if SANITIZER_INTERCEPT_FSEEK
 TEST_F(RtsanOpenedFileTest, FgetposDieWhenRealtime) {
   auto Func = [this]() {

Copy link

github-actions bot commented Jan 20, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

#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

return REAL(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.


TEST_F(RtsanOpenedMmapTest, MincoreDiesWhenRealtime) {
#if SANITIZER_APPLE
char vec[GetSize() / 1024];
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a VLR, is this OK to do? should we do something like a std::vector instead and pass in the .data?

madvise/mprotect/msync/mincore calls with care for signature difference
for the latter.
@devnexen devnexen merged commit c745ece into llvm:main Jan 22, 2025
7 checks passed
@benlangmuir
Copy link
Collaborator

One of the new tests is failing at least on Darwin: https://green.lab.llvm.org/job/llvm.org/job/clang-stage1-cmake-RA-expensive/3192/testReport/

Looks like memadvise vs. posix_madvise string match is the issue?

Death test: RealtimeInvoke(std::forward<Function>(Func))
    Result: died but not with expected error.
  Expected: contains regular expression ".*==ERROR: RealtimeSanitizer: unsafe-library-call.*Intercepted call to real-time unsafe function `madvise` in real-time context!"
Actual msg:
[  DEATH   ] ==73227==ERROR: RealtimeSanitizer: unsafe-library-call
[  DEATH   ] Intercepted call to real-time unsafe function `posix_madvise` in real-time context!

@devnexen
Copy link
Member Author

hmm let me try in my mac..

@devnexen
Copy link
Member Author

I can reproduce, looking at sanitizer, seems only bsd and linux intercept those. Fix coming up very soon !

devnexen added a commit to devnexen/llvm-project that referenced this pull request Jan 22, 2025
only bsd and linux intercept these syscalls.
Fix llvm#123601
devnexen added a commit that referenced this pull request Jan 22, 2025
only bsd and linux intercept these syscalls.
Fix #123601
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants