sanitizer_common: sanitize time functions

We have SleepForSeconds, SleepForMillis and internal_sleep.
Some are implemented in terms of libc functions, some -- in terms
of syscalls. Some are implemented in per OS files,
some -- in libc/nolibc files. That's unnecessary complex
and libc functions cause crashes in some contexts because
we intercept them. There is no single reason to have calls to libc
when we have syscalls (and we have them anyway).

Add internal_usleep that is implemented in terms of syscalls per OS.
Make SleepForSeconds/SleepForMillis/internal_sleep a wrapper
around internal_usleep that is implemented in sanitizer_common.cpp once.

Also remove return values for internal_sleep, it's not used anywhere.

Eventually it would be nice to remove SleepForSeconds/SleepForMillis/internal_sleep.
There is no point in having that many different names for the same thing.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D105718
This commit is contained in:
Dmitry Vyukov 2021-07-09 20:21:52 +02:00
parent 1e1f752027
commit 8df3c7ded2
10 changed files with 24 additions and 46 deletions

View File

@ -330,6 +330,14 @@ static int InstallMallocFreeHooks(void (*malloc_hook)(const void *, uptr),
return 0;
}
void internal_sleep(unsigned seconds) {
internal_usleep((u64)seconds * 1000 * 1000);
}
void SleepForSeconds(unsigned seconds) {
internal_usleep((u64)seconds * 1000 * 1000);
}
void SleepForMillis(unsigned millis) { internal_usleep((u64)millis * 1000); }
} // namespace __sanitizer
using namespace __sanitizer;

View File

@ -288,8 +288,8 @@ void InitTlsSize();
uptr GetTlsSize();
// Other
void SleepForSeconds(int seconds);
void SleepForMillis(int millis);
void SleepForSeconds(unsigned seconds);
void SleepForMillis(unsigned millis);
u64 NanoTime();
u64 MonotonicNanoTime();
int Atexit(void (*function)(void));

View File

@ -25,7 +25,6 @@ void LogMessageOnPrintf(const char *str) {}
#endif
void WriteToSyslog(const char *buffer) {}
void Abort() { internal__exit(1); }
void SleepForSeconds(int seconds) { internal_sleep(seconds); }
#endif // !SANITIZER_WINDOWS
#if !SANITIZER_WINDOWS && !SANITIZER_MAC

View File

@ -36,16 +36,11 @@ uptr internal_sched_yield() {
return 0; // Why doesn't this return void?
}
static void internal_nanosleep(zx_time_t ns) {
zx_status_t status = _zx_nanosleep(_zx_deadline_after(ns));
void internal_usleep(u64 useconds) {
zx_status_t status = _zx_nanosleep(_zx_deadline_after(ZX_USEC(useconds)));
CHECK_EQ(status, ZX_OK);
}
unsigned int internal_sleep(unsigned int seconds) {
internal_nanosleep(ZX_SEC(seconds));
return 0;
}
u64 NanoTime() {
zx_handle_t utc_clock = _zx_utc_reference_get();
CHECK_NE(utc_clock, ZX_HANDLE_INVALID);
@ -78,10 +73,6 @@ void Abort() { abort(); }
int Atexit(void (*function)(void)) { return atexit(function); }
void SleepForSeconds(int seconds) { internal_sleep(seconds); }
void SleepForMillis(int millis) { internal_nanosleep(ZX_MSEC(millis)); }
void GetThreadStackTopAndBottom(bool, uptr *stack_top, uptr *stack_bottom) {
pthread_attr_t attr;
CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);

View File

@ -67,7 +67,8 @@ uptr internal_ftruncate(fd_t fd, uptr size);
// OS
void NORETURN internal__exit(int exitcode);
unsigned int internal_sleep(unsigned int seconds);
void internal_sleep(unsigned seconds);
void internal_usleep(u64 useconds);
uptr internal_getpid();
uptr internal_getppid();

View File

@ -430,13 +430,11 @@ uptr internal_sched_yield() {
return internal_syscall(SYSCALL(sched_yield));
}
unsigned int internal_sleep(unsigned int seconds) {
void internal_usleep(u64 useconds) {
struct timespec ts;
ts.tv_sec = seconds;
ts.tv_nsec = 0;
int res = internal_syscall(SYSCALL(nanosleep), &ts, &ts);
if (res) return ts.tv_sec;
return 0;
ts.tv_sec = useconds / 1000000;
ts.tv_nsec = (useconds % 1000000) * 1000;
internal_syscall(SYSCALL(nanosleep), &ts, &ts);
}
uptr internal_execve(const char *filename, char *const argv[],

View File

@ -219,9 +219,7 @@ void internal__exit(int exitcode) {
_exit(exitcode);
}
unsigned int internal_sleep(unsigned int seconds) {
return sleep(seconds);
}
void internal_usleep(u64 useconds) { usleep(useconds); }
uptr internal_getpid() {
return getpid();

View File

@ -215,15 +215,12 @@ void internal__exit(int exitcode) {
Die(); // Unreachable.
}
unsigned int internal_sleep(unsigned int seconds) {
void internal_usleep(u64 useconds) {
struct timespec ts;
ts.tv_sec = seconds;
ts.tv_nsec = 0;
ts.tv_sec = useconds / 1000000;
ts.tv_nsec = (useconds % 1000000) * 1000;
CHECK(&_sys___nanosleep50);
int res = _sys___nanosleep50(&ts, &ts);
if (res)
return ts.tv_sec;
return 0;
_sys___nanosleep50(&ts, &ts);
}
uptr internal_execve(const char *filename, char *const argv[],

View File

@ -128,14 +128,6 @@ void SetAddressSpaceUnlimited() {
CHECK(AddressSpaceIsUnlimited());
}
void SleepForSeconds(int seconds) {
sleep(seconds);
}
void SleepForMillis(int millis) {
usleep(millis * 1000);
}
void Abort() {
#if !SANITIZER_GO
// If we are handling SIGABRT, unhandle it first.

View File

@ -541,13 +541,7 @@ bool IsAbsolutePath(const char *path) {
IsPathSeparator(path[2]);
}
void SleepForSeconds(int seconds) {
Sleep(seconds * 1000);
}
void SleepForMillis(int millis) {
Sleep(millis);
}
void internal_usleep(u64 useconds) { Sleep(useconds / 1000); }
u64 NanoTime() {
static LARGE_INTEGER frequency = {};