[compiler-rt] 6f9a96e - [Sanitizers] intercept clock_getcpuclockid on FreeBSD, and pthread_getcpuclockid.

David Carlier via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 2 14:35:55 PDT 2021


Author: David Carlier
Date: 2021-09-02T22:35:04+01:00
New Revision: 6f9a96e9cda32b7bf0084e115cc9d70a9554cbd4

URL: https://github.com/llvm/llvm-project/commit/6f9a96e9cda32b7bf0084e115cc9d70a9554cbd4
DIFF: https://github.com/llvm/llvm-project/commit/6f9a96e9cda32b7bf0084e115cc9d70a9554cbd4.diff

LOG: [Sanitizers] intercept clock_getcpuclockid on FreeBSD, and pthread_getcpuclockid.

Reviewed By: vitalybuka

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

Added: 
    compiler-rt/test/sanitizer_common/TestCases/Posix/getcpuclockid.c

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
    compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 5ab3186e159f1..5e731ac399102 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -2229,8 +2229,20 @@ INTERCEPTOR(int, clock_getcpuclockid, pid_t pid,
   return res;
 }
 
-#define INIT_CLOCK_GETCPUCLOCKID                  \
-  COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid);
+INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
+            __sanitizer_clockid_t *clockid) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, pthread_getcpuclockid, thread, clockid);
+  int res = REAL(pthread_getcpuclockid)(thread, clockid);
+  if (!res && clockid) {
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
+  }
+  return res;
+}
+
+#define INIT_CLOCK_GETCPUCLOCKID                   \
+  COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid);  \
+  COMMON_INTERCEPT_FUNCTION(pthread_getcpuclockid);
 #else
 #define INIT_CLOCK_GETCPUCLOCKID
 #endif

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index a65a39afe9f8b..bdb07ff132730 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -229,7 +229,7 @@
   (SI_MAC || SI_LINUX_NOT_ANDROID || SI_SOLARIS)
 #define SANITIZER_INTERCEPT_CLOCK_GETTIME \
   (SI_FREEBSD || SI_NETBSD || SI_LINUX || SI_SOLARIS)
-#define SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID SI_LINUX
+#define SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID (SI_LINUX || SI_FREEBSD)
 #define SANITIZER_INTERCEPT_GETITIMER SI_POSIX
 #define SANITIZER_INTERCEPT_TIME SI_POSIX
 #define SANITIZER_INTERCEPT_GLOB (SI_GLIBC || SI_SOLARIS)

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c
index 6999a80b638e5..e69de29bb2d1d 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c
@@ -1,20 +0,0 @@
-// RUN: %clang %s -Wl,-as-needed -o %t && %run %t
-#include <time.h>
-#include <unistd.h>
-#include <assert.h>
-
-long cpu_ns() {
-  clockid_t clk;
-  struct timespec ts;
-  int res = clock_getcpuclockid(getpid(), &clk);
-  assert(!res);
-  res = clock_gettime(clk, &ts);
-  assert(!res);
-  return ts.tv_nsec;
-}
-
-int main() {
-  long cpuns = cpu_ns();
-  asm volatile ("" :: "r"(cpuns));
-  return 0;
-}

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/getcpuclockid.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/getcpuclockid.c
new file mode 100644
index 0000000000000..87ce3f5cddf38
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/getcpuclockid.c
@@ -0,0 +1,36 @@
+// RUN: %clang -pthread %s -Wl,-as-needed -o %t && %run %t
+//
+// UNSUPPORTED: darwin, netbsd, solaris
+
+#include <time.h>
+#include <unistd.h>
+#include <assert.h>
+#include <pthread.h>
+
+long cpu_ns() {
+  clockid_t clk;
+  struct timespec ts;
+  int res = clock_getcpuclockid(getpid(), &clk);
+  assert(!res);
+  res = clock_gettime(clk, &ts);
+  assert(!res);
+  return ts.tv_nsec;
+}
+
+long th_cpu_ns() {
+  clockid_t clk;
+  struct timespec ts;
+  int res = pthread_getcpuclockid(pthread_self(), &clk);
+  assert(!res);
+  res = clock_gettime(clk, &ts);
+  assert(!res);
+  return ts.tv_nsec;
+}
+
+int main() {
+  long cpuns = cpu_ns();
+  asm volatile ("" :: "r"(cpuns));
+  cpuns = th_cpu_ns();
+  asm volatile ("" :: "r"(cpuns));
+  return 0;
+}


        


More information about the llvm-commits mailing list