Skip to content

[compiler-rt][rtsan] NFC: Introduce __rtsan_expect_not_realtime helper #106314

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
Aug 28, 2024

Conversation

cjappl
Copy link
Contributor

@cjappl cjappl commented Aug 28, 2024

We are extracting this function into the C API so we can eventually install it when a user marks a function [[clang::blocking]].

@llvmbot
Copy link
Member

llvmbot commented Aug 28, 2024

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

Author: Chris Apple (cjappl)

Changes

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

3 Files Affected:

  • (modified) compiler-rt/lib/rtsan/rtsan.cpp (+7)
  • (modified) compiler-rt/lib/rtsan/rtsan.h (+3)
  • (modified) compiler-rt/lib/rtsan/rtsan_interceptors.cpp (+52-59)
diff --git a/compiler-rt/lib/rtsan/rtsan.cpp b/compiler-rt/lib/rtsan/rtsan.cpp
index 237475eedf5005..8a7ff03c611c65 100644
--- a/compiler-rt/lib/rtsan/rtsan.cpp
+++ b/compiler-rt/lib/rtsan/rtsan.cpp
@@ -66,4 +66,11 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_on() {
   __rtsan::GetContextForThisThread().BypassPop();
 }
 
+SANITIZER_INTERFACE_ATTRIBUTE void
+__rtsan_expect_not_realtime(const char *intercepted_function_name) {
+  __rtsan_ensure_initialized();
+  __rtsan::GetContextForThisThread().ExpectNotRealtime(
+      intercepted_function_name);
+}
+
 } // extern "C"
diff --git a/compiler-rt/lib/rtsan/rtsan.h b/compiler-rt/lib/rtsan/rtsan.h
index 094c989895d22b..3d665c98aed184 100644
--- a/compiler-rt/lib/rtsan/rtsan.h
+++ b/compiler-rt/lib/rtsan/rtsan.h
@@ -44,4 +44,7 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_off();
 // The counterpart to `__rtsan_off`.
 SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_on();
 
+SANITIZER_INTERFACE_ATTRIBUTE void
+__rtsan_expect_not_realtime(const char *intercepted_function_name);
+
 } // extern "C"
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
index 1b1655f94eaa5a..409e27c3ad3234 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors.cpp
@@ -57,19 +57,12 @@ struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
 };
 } // namespace
 
-void ExpectNotRealtime(const char *intercepted_function_name) {
-  __rtsan_ensure_initialized();
-
-  __rtsan::GetContextForThisThread().ExpectNotRealtime(
-      intercepted_function_name);
-}
-
 // Filesystem
 
 INTERCEPTOR(int, open, const char *path, int oflag, ...) {
   // TODO Establish whether we should intercept here if the flag contains
   // O_NONBLOCK
-  ExpectNotRealtime("open");
+  __rtsan_expect_not_realtime("open");
 
   va_list args;
   va_start(args, oflag);
@@ -83,7 +76,7 @@ INTERCEPTOR(int, open, const char *path, int oflag, ...) {
 INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) {
   // TODO Establish whether we should intercept here if the flag contains
   // O_NONBLOCK
-  ExpectNotRealtime("openat");
+  __rtsan_expect_not_realtime("openat");
 
   va_list args;
   va_start(args, oflag);
@@ -97,13 +90,13 @@ INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) {
 INTERCEPTOR(int, creat, const char *path, mode_t mode) {
   // TODO Establish whether we should intercept here if the flag contains
   // O_NONBLOCK
-  ExpectNotRealtime("creat");
+  __rtsan_expect_not_realtime("creat");
   const int result = REAL(creat)(path, mode);
   return result;
 }
 
 INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {
-  ExpectNotRealtime("fcntl");
+  __rtsan_expect_not_realtime("fcntl");
 
   va_list args;
   va_start(args, cmd);
@@ -124,71 +117,71 @@ INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {
 }
 
 INTERCEPTOR(int, close, int filedes) {
-  ExpectNotRealtime("close");
+  __rtsan_expect_not_realtime("close");
   return REAL(close)(filedes);
 }
 
 INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
-  ExpectNotRealtime("fopen");
+  __rtsan_expect_not_realtime("fopen");
   return REAL(fopen)(path, mode);
 }
 
 INTERCEPTOR(size_t, fread, void *ptr, size_t size, size_t nitems,
             FILE *stream) {
-  ExpectNotRealtime("fread");
+  __rtsan_expect_not_realtime("fread");
   return REAL(fread)(ptr, size, nitems, stream);
 }
 
 INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems,
             FILE *stream) {
-  ExpectNotRealtime("fwrite");
+  __rtsan_expect_not_realtime("fwrite");
   return REAL(fwrite)(ptr, size, nitems, stream);
 }
 
 INTERCEPTOR(int, fclose, FILE *stream) {
-  ExpectNotRealtime("fclose");
+  __rtsan_expect_not_realtime("fclose");
   return REAL(fclose)(stream);
 }
 
 INTERCEPTOR(int, fputs, const char *s, FILE *stream) {
-  ExpectNotRealtime("fputs");
+  __rtsan_expect_not_realtime("fputs");
   return REAL(fputs)(s, stream);
 }
 
 // Streams
 INTERCEPTOR(int, puts, const char *s) {
-  ExpectNotRealtime("puts");
+  __rtsan_expect_not_realtime("puts");
   return REAL(puts)(s);
 }
 
 INTERCEPTOR(ssize_t, read, int fd, void *buf, size_t count) {
-  ExpectNotRealtime("read");
+  __rtsan_expect_not_realtime("read");
   return REAL(read)(fd, buf, count);
 }
 
 INTERCEPTOR(ssize_t, write, int fd, const void *buf, size_t count) {
-  ExpectNotRealtime("write");
+  __rtsan_expect_not_realtime("write");
   return REAL(write)(fd, buf, count);
 }
 
 INTERCEPTOR(ssize_t, pread, int fd, void *buf, size_t count, off_t offset) {
-  ExpectNotRealtime("pread");
+  __rtsan_expect_not_realtime("pread");
   return REAL(pread)(fd, buf, count, offset);
 }
 
 INTERCEPTOR(ssize_t, readv, int fd, const struct iovec *iov, int iovcnt) {
-  ExpectNotRealtime("readv");
+  __rtsan_expect_not_realtime("readv");
   return REAL(readv)(fd, iov, iovcnt);
 }
 
 INTERCEPTOR(ssize_t, pwrite, int fd, const void *buf, size_t count,
             off_t offset) {
-  ExpectNotRealtime("pwrite");
+  __rtsan_expect_not_realtime("pwrite");
   return REAL(pwrite)(fd, buf, count, offset);
 }
 
 INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {
-  ExpectNotRealtime("writev");
+  __rtsan_expect_not_realtime("writev");
   return REAL(writev)(fd, iov, iovcnt);
 }
 
@@ -198,95 +191,95 @@ INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {
 // OSSpinLockLock is deprecated, but still in use in libc++
 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
 INTERCEPTOR(void, OSSpinLockLock, volatile OSSpinLock *lock) {
-  ExpectNotRealtime("OSSpinLockLock");
+  __rtsan_expect_not_realtime("OSSpinLockLock");
   return REAL(OSSpinLockLock)(lock);
 }
 #pragma clang diagnostic pop
 
 INTERCEPTOR(void, os_unfair_lock_lock, os_unfair_lock_t lock) {
-  ExpectNotRealtime("os_unfair_lock_lock");
+  __rtsan_expect_not_realtime("os_unfair_lock_lock");
   return REAL(os_unfair_lock_lock)(lock);
 }
 #elif SANITIZER_LINUX
 INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *spinlock) {
-  ExpectNotRealtime("pthread_spin_lock");
+  __rtsan_expect_not_realtime("pthread_spin_lock");
   return REAL(pthread_spin_lock)(spinlock);
 }
 #endif
 
 INTERCEPTOR(int, pthread_create, pthread_t *thread, const pthread_attr_t *attr,
             void *(*start_routine)(void *), void *arg) {
-  ExpectNotRealtime("pthread_create");
+  __rtsan_expect_not_realtime("pthread_create");
   return REAL(pthread_create)(thread, attr, start_routine, arg);
 }
 
 INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *mutex) {
-  ExpectNotRealtime("pthread_mutex_lock");
+  __rtsan_expect_not_realtime("pthread_mutex_lock");
   return REAL(pthread_mutex_lock)(mutex);
 }
 
 INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *mutex) {
-  ExpectNotRealtime("pthread_mutex_unlock");
+  __rtsan_expect_not_realtime("pthread_mutex_unlock");
   return REAL(pthread_mutex_unlock)(mutex);
 }
 
 INTERCEPTOR(int, pthread_join, pthread_t thread, void **value_ptr) {
-  ExpectNotRealtime("pthread_join");
+  __rtsan_expect_not_realtime("pthread_join");
   return REAL(pthread_join)(thread, value_ptr);
 }
 
 INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) {
-  ExpectNotRealtime("pthread_cond_signal");
+  __rtsan_expect_not_realtime("pthread_cond_signal");
   return REAL(pthread_cond_signal)(cond);
 }
 
 INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *cond) {
-  ExpectNotRealtime("pthread_cond_broadcast");
+  __rtsan_expect_not_realtime("pthread_cond_broadcast");
   return REAL(pthread_cond_broadcast)(cond);
 }
 
 INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *cond,
             pthread_mutex_t *mutex) {
-  ExpectNotRealtime("pthread_cond_wait");
+  __rtsan_expect_not_realtime("pthread_cond_wait");
   return REAL(pthread_cond_wait)(cond, mutex);
 }
 
 INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *cond,
             pthread_mutex_t *mutex, const timespec *ts) {
-  ExpectNotRealtime("pthread_cond_timedwait");
+  __rtsan_expect_not_realtime("pthread_cond_timedwait");
   return REAL(pthread_cond_timedwait)(cond, mutex, ts);
 }
 
 INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) {
-  ExpectNotRealtime("pthread_rwlock_rdlock");
+  __rtsan_expect_not_realtime("pthread_rwlock_rdlock");
   return REAL(pthread_rwlock_rdlock)(lock);
 }
 
 INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *lock) {
-  ExpectNotRealtime("pthread_rwlock_unlock");
+  __rtsan_expect_not_realtime("pthread_rwlock_unlock");
   return REAL(pthread_rwlock_unlock)(lock);
 }
 
 INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *lock) {
-  ExpectNotRealtime("pthread_rwlock_wrlock");
+  __rtsan_expect_not_realtime("pthread_rwlock_wrlock");
   return REAL(pthread_rwlock_wrlock)(lock);
 }
 
 // Sleeping
 
 INTERCEPTOR(unsigned int, sleep, unsigned int s) {
-  ExpectNotRealtime("sleep");
+  __rtsan_expect_not_realtime("sleep");
   return REAL(sleep)(s);
 }
 
 INTERCEPTOR(int, usleep, useconds_t u) {
-  ExpectNotRealtime("usleep");
+  __rtsan_expect_not_realtime("usleep");
   return REAL(usleep)(u);
 }
 
 INTERCEPTOR(int, nanosleep, const struct timespec *rqtp,
             struct timespec *rmtp) {
-  ExpectNotRealtime("nanosleep");
+  __rtsan_expect_not_realtime("nanosleep");
   return REAL(nanosleep)(rqtp, rmtp);
 }
 
@@ -296,7 +289,7 @@ INTERCEPTOR(void *, calloc, SIZE_T num, SIZE_T size) {
   if (DlsymAlloc::Use())
     return DlsymAlloc::Callocate(num, size);
 
-  ExpectNotRealtime("calloc");
+  __rtsan_expect_not_realtime("calloc");
   return REAL(calloc)(num, size);
 }
 
@@ -305,7 +298,7 @@ INTERCEPTOR(void, free, void *ptr) {
     return DlsymAlloc::Free(ptr);
 
   if (ptr != NULL) {
-    ExpectNotRealtime("free");
+    __rtsan_expect_not_realtime("free");
   }
   return REAL(free)(ptr);
 }
@@ -314,7 +307,7 @@ INTERCEPTOR(void *, malloc, SIZE_T size) {
   if (DlsymAlloc::Use())
     return DlsymAlloc::Allocate(size);
 
-  ExpectNotRealtime("malloc");
+  __rtsan_expect_not_realtime("malloc");
   return REAL(malloc)(size);
 }
 
@@ -322,23 +315,23 @@ INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
   if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
     return DlsymAlloc::Realloc(ptr, size);
 
-  ExpectNotRealtime("realloc");
+  __rtsan_expect_not_realtime("realloc");
   return REAL(realloc)(ptr, size);
 }
 
 INTERCEPTOR(void *, reallocf, void *ptr, SIZE_T size) {
-  ExpectNotRealtime("reallocf");
+  __rtsan_expect_not_realtime("reallocf");
   return REAL(reallocf)(ptr, size);
 }
 
 INTERCEPTOR(void *, valloc, SIZE_T size) {
-  ExpectNotRealtime("valloc");
+  __rtsan_expect_not_realtime("valloc");
   return REAL(valloc)(size);
 }
 
 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
-  ExpectNotRealtime("aligned_alloc");
+  __rtsan_expect_not_realtime("aligned_alloc");
   return REAL(aligned_alloc)(alignment, size);
 }
 #define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
@@ -347,65 +340,65 @@ INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
 #endif
 
 INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
-  ExpectNotRealtime("posix_memalign");
+  __rtsan_expect_not_realtime("posix_memalign");
   return REAL(posix_memalign)(memptr, alignment, size);
 }
 
 #if SANITIZER_INTERCEPT_MEMALIGN
 INTERCEPTOR(void *, memalign, size_t alignment, size_t size) {
-  ExpectNotRealtime("memalign");
+  __rtsan_expect_not_realtime("memalign");
   return REAL(memalign)(alignment, size);
 }
 #endif
 
 #if SANITIZER_INTERCEPT_PVALLOC
 INTERCEPTOR(void *, pvalloc, size_t size) {
-  ExpectNotRealtime("pvalloc");
+  __rtsan_expect_not_realtime("pvalloc");
   return REAL(pvalloc)(size);
 }
 #endif
 
 // Sockets
 INTERCEPTOR(int, socket, int domain, int type, int protocol) {
-  ExpectNotRealtime("socket");
+  __rtsan_expect_not_realtime("socket");
   return REAL(socket)(domain, type, protocol);
 }
 
 INTERCEPTOR(ssize_t, send, int sockfd, const void *buf, size_t len, int flags) {
-  ExpectNotRealtime("send");
+  __rtsan_expect_not_realtime("send");
   return REAL(send)(sockfd, buf, len, flags);
 }
 
 INTERCEPTOR(ssize_t, sendmsg, int socket, const struct msghdr *message,
             int flags) {
-  ExpectNotRealtime("sendmsg");
+  __rtsan_expect_not_realtime("sendmsg");
   return REAL(sendmsg)(socket, message, flags);
 }
 
 INTERCEPTOR(ssize_t, sendto, int socket, const void *buffer, size_t length,
             int flags, const struct sockaddr *dest_addr, socklen_t dest_len) {
-  ExpectNotRealtime("sendto");
+  __rtsan_expect_not_realtime("sendto");
   return REAL(sendto)(socket, buffer, length, flags, dest_addr, dest_len);
 }
 
 INTERCEPTOR(ssize_t, recv, int socket, void *buffer, size_t length, int flags) {
-  ExpectNotRealtime("recv");
+  __rtsan_expect_not_realtime("recv");
   return REAL(recv)(socket, buffer, length, flags);
 }
 
 INTERCEPTOR(ssize_t, recvfrom, int socket, void *buffer, size_t length,
             int flags, struct sockaddr *address, socklen_t *address_len) {
-  ExpectNotRealtime("recvfrom");
+  __rtsan_expect_not_realtime("recvfrom");
   return REAL(recvfrom)(socket, buffer, length, flags, address, address_len);
 }
 
 INTERCEPTOR(ssize_t, recvmsg, int socket, struct msghdr *message, int flags) {
-  ExpectNotRealtime("recvmsg");
+  __rtsan_expect_not_realtime("recvmsg");
   return REAL(recvmsg)(socket, message, flags);
 }
 
 INTERCEPTOR(int, shutdown, int socket, int how) {
-  ExpectNotRealtime("shutdown");
+  __rtsan_expect_not_realtime("shutdown");
   return REAL(shutdown)(socket, how);
 }
 

@cjappl
Copy link
Contributor Author

cjappl commented Aug 28, 2024

Failure is in unrelated code:

�_bk;t=1724809278452�/var/lib/buildkite-agent/builds/linux-56-59b8f5d88-c4bpt-1/llvm-project/github-pull-requests/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp:249: Failure

�_bk;t=1724809278452�Value of: I1 != SimplifiedValuesVector[5].end()

�_bk;t=1724809278452�  Actual: false

�_bk;t=1724809278452�Expected: true


@cjappl
Copy link
Contributor Author

cjappl commented Aug 28, 2024

CC @davidtrevelyan

@cjappl cjappl merged commit fee4836 into llvm:main Aug 28, 2024
8 of 10 checks passed
@cjappl cjappl deleted the expect_not_realtime branch August 28, 2024 14:10
@cjappl cjappl requested review from MaskRay and vitalybuka August 28, 2024 14:13
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Sep 30, 2024
…7d312fac8

Local branch amd-gfx 0447d31 Merged main:0e8208eca1c316b7302de7803ab0d85a1dd77076 into amd-gfx:4ce515f18a36
Remote branch main fee4836 [compiler-rt][rtsan] NFC: Introduce __rtsan_expect_not_realtime helper (llvm#106314)
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.

2 participants