[compiler-rt] 828c94c - [MSAN] Add interceptor for pthread_getaffinity_np.

Kevin Athey via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 7 16:27:50 PDT 2022


Author: Kevin Athey
Date: 2022-06-07T16:27:44-07:00
New Revision: 828c94c0f6b3d75a7e54592ac4cbcf2f455e4b77

URL: https://github.com/llvm/llvm-project/commit/828c94c0f6b3d75a7e54592ac4cbcf2f455e4b77
DIFF: https://github.com/llvm/llvm-project/commit/828c94c0f6b3d75a7e54592ac4cbcf2f455e4b77.diff

LOG: [MSAN] Add interceptor for pthread_getaffinity_np.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D127185

Added: 
    compiler-rt/test/msan/Linux/pthread_getaffinity_np.cpp
    compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 8a4594fb7dd41..4ab47e8056aa9 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -4963,6 +4963,27 @@ INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
 #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
 #endif
 
+#if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
+INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
+            void *cpuset) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
+                           cpuset);
+  // FIXME: under ASan the call below may write to freed memory and corrupt
+  // its metadata. See
+  // https://github.com/google/sanitizers/issues/321.
+  int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
+  if (!res && cpusetsize && cpuset)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
+  return res;
+}
+
+#define INIT_PTHREAD_GETAFFINITY_NP \
+  COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
+#else
+#define INIT_PTHREAD_GETAFFINITY_NP
+#endif
+
 #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
 INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
 #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
@@ -10535,6 +10556,7 @@ static void InitializeCommonInterceptors() {
   INIT_PTHREAD_ATTR_GET_SCHED;
   INIT_PTHREAD_ATTR_GETINHERITSCHED;
   INIT_PTHREAD_ATTR_GETAFFINITY_NP;
+  INIT_PTHREAD_GETAFFINITY_NP;
   INIT_PTHREAD_MUTEXATTR_GETPSHARED;
   INIT_PTHREAD_MUTEXATTR_GETTYPE;
   INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index 12ced9505eb08..dc099d6cd2571 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -348,6 +348,7 @@
 #define SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED \
   (SI_FREEBSD || SI_NETBSD || SI_MAC || SI_LINUX_NOT_ANDROID || SI_SOLARIS)
 #define SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP SI_GLIBC
+#define SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP SI_LINUX
 #define SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED SI_POSIX
 #define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED \
   (SI_POSIX && !SI_NETBSD)

diff  --git a/compiler-rt/test/msan/Linux/pthread_getaffinity_np.cpp b/compiler-rt/test/msan/Linux/pthread_getaffinity_np.cpp
new file mode 100644
index 0000000000000..41adb7de5f91e
--- /dev/null
+++ b/compiler-rt/test/msan/Linux/pthread_getaffinity_np.cpp
@@ -0,0 +1,15 @@
+// RUN: %clangxx_msan -O0 %s -o %t && %run %t
+
+#include <assert.h>
+#include <pthread.h>
+
+#include <sanitizer/msan_interface.h>
+
+int main() {
+  cpu_set_t set_x;
+  int res = pthread_getaffinity_np(pthread_self(), sizeof(set_x), &set_x);
+  assert(res == 0);
+  __msan_check_mem_is_initialized(&set_x, sizeof(set_x));
+
+  return 0;
+}

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp
new file mode 100644
index 0000000000000..591602aeaec65
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp
@@ -0,0 +1,16 @@
+// RUN: %clangxx -O0 %s -o %t && %run %t
+
+#include <assert.h>
+#include <pthread.h>
+#include <sys/sysinfo.h>
+
+#include <sanitizer/msan_interface.h>
+
+int main() {
+  cpu_set_t set_x;
+  int res = pthread_getaffinity_np(pthread_self(), sizeof(set_x), &set_x);
+  assert(res == 0);
+  assert(CPU_COUNT_S(sizeof(set_x), &set_x) == get_nprocs());
+
+  return 0;
+}


        


More information about the llvm-commits mailing list