[compiler-rt] 42170b3 - [Sanitizers] Implement getcpuclockid interceptor

Gui Andrade via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 15:12:27 PDT 2020


Author: Gui Andrade
Date: 2020-07-13T22:12:20Z
New Revision: 42170b3b4e1f7d30b377a3da07c354feae9b852e

URL: https://github.com/llvm/llvm-project/commit/42170b3b4e1f7d30b377a3da07c354feae9b852e
DIFF: https://github.com/llvm/llvm-project/commit/42170b3b4e1f7d30b377a3da07c354feae9b852e.diff

LOG: [Sanitizers] Implement getcpuclockid interceptor

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

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

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 ea9c71ba8803..4b02ad2670fe 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -2199,6 +2199,23 @@ INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
 #define INIT_CLOCK_GETTIME
 #endif
 
+#if SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID
+INTERCEPTOR(int, clock_getcpuclockid, pid_t pid, __sanitizer_clockid_t *clockid) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, clock_getcpuclockid, pid, clockid);
+  int res = REAL(clock_getcpuclockid)(pid, clockid);
+  if (!res && clockid) {
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
+  }
+  return res;
+}
+
+#define INIT_CLOCK_GETCPUCLOCKID                  \
+  COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid);
+#else
+#define INIT_CLOCK_GETCPUCLOCKID
+#endif
+
 #if SANITIZER_INTERCEPT_GETITIMER
 INTERCEPTOR(int, getitimer, int which, void *curr_value) {
   void *ctx;
@@ -9914,6 +9931,7 @@ static void InitializeCommonInterceptors() {
   INIT_FGETGRENT_R;
   INIT_SETPWENT;
   INIT_CLOCK_GETTIME;
+  INIT_CLOCK_GETCPUCLOCKID;
   INIT_GETITIMER;
   INIT_TIME;
   INIT_GLOB;

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index 2d48e9d0ae1a..e28bb937ae83 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -240,6 +240,7 @@
   (SI_MAC || SI_LINUX_NOT_ANDROID || SI_SOLARIS)
 #define SANITIZER_INTERCEPT_CLOCK_GETTIME \
   (SI_FREEBSD || SI_NETBSD || SI_OPENBSD || SI_LINUX || SI_SOLARIS)
+#define SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID SI_LINUX
 #define SANITIZER_INTERCEPT_GETITIMER SI_POSIX
 #define SANITIZER_INTERCEPT_TIME SI_POSIX
 #define SANITIZER_INTERCEPT_GLOB SI_LINUX_NOT_ANDROID || SI_SOLARIS

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c
new file mode 100644
index 000000000000..6999a80b638e
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c
@@ -0,0 +1,20 @@
+// 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;
+}


        


More information about the llvm-commits mailing list