Skip to content

Commit aeaf705

Browse files
committed
[sanitizer] Change NanoTime to use clock_gettime on non-glibc
This avoids the `__NR_gettimeofday` syscall number, which does not exist on 32-bit musl (it has `__NR_gettimeofday_time32`). This switched Android to `clock_gettime` as well, which should work according to the old code before D96925. Tested on Alpine Linux x86-64 (musl) and FreeBSD x86-64. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D98121
1 parent c771208 commit aeaf705

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,24 +489,24 @@ int TgKill(pid_t pid, tid_t tid, int sig) {
489489
}
490490
#endif
491491

492-
#if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
492+
#if SANITIZER_GLIBC
493493
u64 NanoTime() {
494-
#if SANITIZER_FREEBSD
495-
timeval tv;
496-
#else
497494
kernel_timeval tv;
498-
#endif
499495
internal_memset(&tv, 0, sizeof(tv));
500496
internal_syscall(SYSCALL(gettimeofday), &tv, 0);
501-
return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
497+
return (u64)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000;
502498
}
503-
#endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
504-
505-
#if SANITIZER_GLIBC
499+
// Used by real_clock_gettime.
506500
uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
507501
return internal_syscall(SYSCALL(clock_gettime), clk_id, tp);
508502
}
509-
#endif // SANITIZER_GLIBC
503+
#elif !SANITIZER_SOLARIS && !SANITIZER_NETBSD
504+
u64 NanoTime() {
505+
struct timespec ts;
506+
clock_gettime(CLOCK_REALTIME, &ts);
507+
return (u64)ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
508+
}
509+
#endif
510510

511511
// Like getenv, but reads env directly from /proc (on Linux) or parses the
512512
// 'environ' array (on some others) and does not use libc. This function

compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,13 @@ u64 MonotonicNanoTime() {
829829
return (u64)ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
830830
}
831831
#else
832-
// Non-Linux & Go always use the regular function.
832+
// Non-glibc & Go always use the regular function.
833833
u64 MonotonicNanoTime() {
834834
timespec ts;
835835
clock_gettime(CLOCK_MONOTONIC, &ts);
836836
return (u64)ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
837837
}
838-
#endif // SANITIZER_LINUX && !SANITIZER_GO
838+
#endif // SANITIZER_GLIBC && !SANITIZER_GO
839839

840840
void ReExec() {
841841
const char *pathname = "/proc/self/exe";

0 commit comments

Comments
 (0)