[Sanitizers] intercept clock_getcpuclockid on FreeBSD, and pthread_getcpuclockid.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D108884
This commit is contained in:
David Carlier 2021-09-02 22:34:23 +01:00
parent 8749a556da
commit 6f9a96e9cd
4 changed files with 51 additions and 23 deletions

View File

@ -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

View File

@ -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)

View File

@ -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;
}

View File

@ -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;
}