Skip to content

Commit 6f9a96e

Browse files
committed
[Sanitizers] intercept clock_getcpuclockid on FreeBSD, and pthread_getcpuclockid.
Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D108884
1 parent 8749a55 commit 6f9a96e

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

+14-2
Original file line numberDiff line numberDiff line change
@@ -2229,8 +2229,20 @@ INTERCEPTOR(int, clock_getcpuclockid, pid_t pid,
22292229
return res;
22302230
}
22312231

2232-
#define INIT_CLOCK_GETCPUCLOCKID \
2233-
COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid);
2232+
INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
2233+
__sanitizer_clockid_t *clockid) {
2234+
void *ctx;
2235+
COMMON_INTERCEPTOR_ENTER(ctx, pthread_getcpuclockid, thread, clockid);
2236+
int res = REAL(pthread_getcpuclockid)(thread, clockid);
2237+
if (!res && clockid) {
2238+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
2239+
}
2240+
return res;
2241+
}
2242+
2243+
#define INIT_CLOCK_GETCPUCLOCKID \
2244+
COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid); \
2245+
COMMON_INTERCEPT_FUNCTION(pthread_getcpuclockid);
22342246
#else
22352247
#define INIT_CLOCK_GETCPUCLOCKID
22362248
#endif

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229
(SI_MAC || SI_LINUX_NOT_ANDROID || SI_SOLARIS)
230230
#define SANITIZER_INTERCEPT_CLOCK_GETTIME \
231231
(SI_FREEBSD || SI_NETBSD || SI_LINUX || SI_SOLARIS)
232-
#define SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID SI_LINUX
232+
#define SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID (SI_LINUX || SI_FREEBSD)
233233
#define SANITIZER_INTERCEPT_GETITIMER SI_POSIX
234234
#define SANITIZER_INTERCEPT_TIME SI_POSIX
235235
#define SANITIZER_INTERCEPT_GLOB (SI_GLIBC || SI_SOLARIS)
Original file line numberDiff line numberDiff line change
@@ -1,20 +0,0 @@
1-
// RUN: %clang %s -Wl,-as-needed -o %t && %run %t
2-
#include <time.h>
3-
#include <unistd.h>
4-
#include <assert.h>
5-
6-
long cpu_ns() {
7-
clockid_t clk;
8-
struct timespec ts;
9-
int res = clock_getcpuclockid(getpid(), &clk);
10-
assert(!res);
11-
res = clock_gettime(clk, &ts);
12-
assert(!res);
13-
return ts.tv_nsec;
14-
}
15-
16-
int main() {
17-
long cpuns = cpu_ns();
18-
asm volatile ("" :: "r"(cpuns));
19-
return 0;
20-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %clang -pthread %s -Wl,-as-needed -o %t && %run %t
2+
//
3+
// UNSUPPORTED: darwin, netbsd, solaris
4+
5+
#include <time.h>
6+
#include <unistd.h>
7+
#include <assert.h>
8+
#include <pthread.h>
9+
10+
long cpu_ns() {
11+
clockid_t clk;
12+
struct timespec ts;
13+
int res = clock_getcpuclockid(getpid(), &clk);
14+
assert(!res);
15+
res = clock_gettime(clk, &ts);
16+
assert(!res);
17+
return ts.tv_nsec;
18+
}
19+
20+
long th_cpu_ns() {
21+
clockid_t clk;
22+
struct timespec ts;
23+
int res = pthread_getcpuclockid(pthread_self(), &clk);
24+
assert(!res);
25+
res = clock_gettime(clk, &ts);
26+
assert(!res);
27+
return ts.tv_nsec;
28+
}
29+
30+
int main() {
31+
long cpuns = cpu_ns();
32+
asm volatile ("" :: "r"(cpuns));
33+
cpuns = th_cpu_ns();
34+
asm volatile ("" :: "r"(cpuns));
35+
return 0;
36+
}

0 commit comments

Comments
 (0)