Skip to content

Commit

Permalink
[Sanitizers] intercept clock_getcpuclockid on FreeBSD, and pthread_ge…
Browse files Browse the repository at this point in the history
…tcpuclockid.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D108884
  • Loading branch information
devnexen committed Sep 2, 2021
1 parent 8749a55 commit 6f9a96e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
16 changes: 14 additions & 2 deletions compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 0 additions & 20 deletions compiler-rt/test/sanitizer_common/TestCases/Linux/getcpuclockid.c
Original file line number Diff line number Diff line change
@@ -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;
}
36 changes: 36 additions & 0 deletions compiler-rt/test/sanitizer_common/TestCases/Posix/getcpuclockid.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 6f9a96e

Please sign in to comment.