Skip to content

[safestack] Switch to sanitizer_common functions #98513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions compiler-rt/lib/safestack/safestack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
//
//===----------------------------------------------------------------------===//

#include "safestack_platform.h"
#include "safestack_util.h"

#include <errno.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <unistd.h>

#include "interception/interception.h"
#include "safestack_util.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_platform.h"
#include "sanitizer_common/sanitizer_posix.h"

using namespace __sanitizer;
using namespace safestack;

// TODO: To make accessing the unsafe stack pointer faster, we plan to
Expand Down Expand Up @@ -90,10 +94,11 @@ __thread size_t unsafe_stack_guard = 0;

inline void *unsafe_stack_alloc(size_t size, size_t guard) {
SFS_CHECK(size + guard >= size);
void *addr = Mmap(nullptr, size + guard, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
void *addr =
(void *)internal_mmap(nullptr, size + guard, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
SFS_CHECK(MAP_FAILED != addr);
Mprotect(addr, guard, PROT_NONE);
internal_mprotect(addr, guard, PROT_NONE);
return (char *)addr + guard;
}

Expand Down Expand Up @@ -146,7 +151,7 @@ struct thread_stack_ll {
void *stack_base;
size_t size;
pid_t pid;
ThreadId tid;
tid_t tid;
};

/// Linked list of unsafe stacks for threads that are exiting. We delay
Expand All @@ -169,15 +174,15 @@ void thread_cleanup_handler(void *_iter) {
pthread_mutex_unlock(&thread_stacks_mutex);

pid_t pid = getpid();
ThreadId tid = GetTid();
tid_t tid = GetTid();

// Free stacks for dead threads
thread_stack_ll **stackp = &temp_stacks;
while (*stackp) {
thread_stack_ll *stack = *stackp;
if (stack->pid != pid ||
(-1 == TgKill(stack->pid, stack->tid, 0) && errno == ESRCH)) {
Munmap(stack->stack_base, stack->size);
internal_munmap(stack->stack_base, stack->size);
*stackp = stack->next;
free(stack);
} else
Expand Down
161 changes: 0 additions & 161 deletions compiler-rt/lib/safestack/safestack_platform.h

This file was deleted.

5 changes: 0 additions & 5 deletions compiler-rt/lib/safestack/safestack_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ namespace safestack {
}; \
} while (false)

inline size_t RoundUpTo(size_t size, size_t boundary) {
SFS_CHECK((boundary & (boundary - 1)) == 0);
return (size + boundary - 1) & ~(boundary - 1);
}

class MutexLock {
public:
explicit MutexLock(pthread_mutex_t &mutex) : mutex_(&mutex) {
Expand Down
11 changes: 0 additions & 11 deletions compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,6 @@ tid_t GetTid() {
return internal_syscall(SYSCALL(gettid));
# endif
}

int TgKill(pid_t pid, tid_t tid, int sig) {
# if SANITIZER_LINUX
return internal_syscall(SYSCALL(tgkill), pid, tid, sig);
# elif SANITIZER_FREEBSD
return internal_syscall(SYSCALL(thr_kill2), pid, tid, sig);
# elif SANITIZER_SOLARIS
(void)pid;
return thr_kill(tid, sig);
# endif
}
# endif

# if SANITIZER_GLIBC
Expand Down
19 changes: 19 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
# endif

# include <dlfcn.h> // for dlsym()
# include <errno.h>
# include <link.h>
# include <pthread.h>
# include <signal.h>
# include <sys/mman.h>
# include <sys/resource.h>
# include <sys/syscall.h>
# include <syslog.h>

# if !defined(ElfW)
Expand Down Expand Up @@ -113,6 +115,23 @@ int internal_sigaction(int signum, const void *act, void *oldact) {
# endif
}

# if !SANITIZER_NETBSD
int TgKill(pid_t pid, tid_t tid, int sig) {
# if SANITIZER_LINUX
// Cannot use internal_syscall here: returns -errno instead of -1 on failure
// on x86_64.
return syscall(SYS_tgkill, pid, (pid_t)tid, sig);
# elif SANITIZER_FREEBSD
return internal_syscall(SYSCALL(thr_kill2), pid, tid, sig);
# elif SANITIZER_SOLARIS
(void)pid;
errno = thr_kill(tid, sig);
// TgKill is expected to return -1 on error, not an errno.
return errno != 0 ? -1 : 0;
# endif
}
# endif

void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
uptr *stack_bottom) {
CHECK(stack_top);
Expand Down
Loading