Skip to content

[compiler-rt][rtsan] Fix madvise/posix_madvise for macOs. #124020

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
Jan 22, 2025

Conversation

devnexen
Copy link
Member

only bsd and linux intercept these syscalls.
Fix #123601

@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

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

Author: David CARLIER (devnexen)

Changes

only bsd and linux intercept these syscalls.
Fix #123601


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

2 Files Affected:

  • (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+9-2)
  • (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (+3-1)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 71938d3edba38d..a9812f90dec079 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -808,6 +808,7 @@ INTERCEPTOR(int, munmap, void *addr, size_t length) {
   return REAL(munmap)(addr, length);
 }
 
+#if !SANITIZER_APPLE
 INTERCEPTOR(int, madvise, void *addr, size_t length, int flag) {
   __rtsan_notify_intercepted_call("madvise");
   return REAL(madvise)(addr, length, flag);
@@ -817,6 +818,12 @@ INTERCEPTOR(int, posix_madvise, void *addr, size_t length, int flag) {
   __rtsan_notify_intercepted_call("posix_madvise");
   return REAL(posix_madvise)(addr, length, flag);
 }
+#define RTSAN_MAYBE_INTERCEPT_MADVISE INTERCEPT_FUNCTION(madvise)
+#define RTSAN_MAYBE_INTERCEPT_POSIX_MADVISE INTERCEPT_FUNCTION(posix_madvise)
+#else
+#define RTSAN_MAYBE_INTERCEPT_MADVISE
+#define RTSAN_MAYBE_INTERCEPT_POSIX_MADVISE
+#endif
 
 INTERCEPTOR(int, mprotect, void *addr, size_t length, int prot) {
   __rtsan_notify_intercepted_call("mprotect");
@@ -1216,8 +1223,8 @@ void __rtsan::InitializeInterceptors() {
   INTERCEPT_FUNCTION(mmap);
   RTSAN_MAYBE_INTERCEPT_MMAP64;
   INTERCEPT_FUNCTION(munmap);
-  INTERCEPT_FUNCTION(madvise);
-  INTERCEPT_FUNCTION(posix_madvise);
+  RTSAN_MAYBE_INTERCEPT_MADVISE;
+  RTSAN_MAYBE_INTERCEPT_POSIX_MADVISE;
   INTERCEPT_FUNCTION(mprotect);
   INTERCEPT_FUNCTION(msync);
   INTERCEPT_FUNCTION(mincore);
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 0a59ae0ea92548..b6243e82473bbb 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -238,6 +238,7 @@ class RtsanOpenedMmapTest : public RtsanFileTest {
   int fd = -1;
 };
 
+#if !SANITIZER_APPLE
 TEST_F(RtsanOpenedMmapTest, MadviseDiesWhenRealtime) {
   auto Func = [this]() { madvise(GetAddr(), GetSize(), MADV_NORMAL); };
   ExpectRealtimeDeath(Func, "madvise");
@@ -245,10 +246,11 @@ TEST_F(RtsanOpenedMmapTest, MadviseDiesWhenRealtime) {
 }
 
 TEST_F(RtsanOpenedMmapTest, PosixMadviseDiesWhenRealtime) {
-  auto Func = [this]() { posix_madvise(GetAddr(), GetSize(), MADV_NORMAL); };
+  auto Func = [this]() { posix_madvise(GetAddr(), GetSize(), POSIX_MADV_NORMAL); };
   ExpectRealtimeDeath(Func, "posix_madvise");
   ExpectNonRealtimeSurvival(Func);
 }
+#endif
 
 TEST_F(RtsanOpenedMmapTest, MprotectDiesWhenRealtime) {
   auto Func = [this]() { mprotect(GetAddr(), GetSize(), PROT_READ); };

@devnexen devnexen requested a review from cjappl January 22, 2025 22:21
Copy link

github-actions bot commented Jan 22, 2025

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

only bsd and linux intercept these syscalls.
Fix llvm#123601
@devnexen devnexen merged commit 630177c into llvm:main Jan 22, 2025
5 of 6 checks passed
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.

3 participants