From ca5da89cdde6d31218a580dcf331afa3fe4bfdad Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Tue, 22 Jan 2019 05:23:48 +0000 Subject: [safestack] Return syscalls for mmap, munmap and mprotect This function can be already intercepted by instrumented code. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@351783 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/safestack/safestack.cc | 7 +++---- lib/safestack/safestack_platform.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'lib/safestack') diff --git a/lib/safestack/safestack.cc b/lib/safestack/safestack.cc index c5622d226..ff840a5c1 100644 --- a/lib/safestack/safestack.cc +++ b/lib/safestack/safestack.cc @@ -17,7 +17,6 @@ #include "safestack_util.h" #include -#include #include #include "interception/interception.h" @@ -94,10 +93,10 @@ __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, RoundUpTo(size + guard, pageSize), + void *addr = Mmap(nullptr, RoundUpTo(size + guard, pageSize), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); SFS_CHECK(MAP_FAILED != addr); - mprotect(addr, guard, PROT_NONE); + Mprotect(addr, guard, PROT_NONE); return (char *)addr + guard; } @@ -181,7 +180,7 @@ void thread_cleanup_handler(void *_iter) { thread_stack_ll *stack = *stackp; if (stack->pid != pid || (-1 == TgKill(stack->pid, stack->tid, 0) && errno == ESRCH)) { - munmap(stack->stack_base, stack->size); + Munmap(stack->stack_base, stack->size); *stackp = stack->next; free(stack); } else diff --git a/lib/safestack/safestack_platform.h b/lib/safestack/safestack_platform.h index 6b042f4b4..b906d52d5 100644 --- a/lib/safestack/safestack_platform.h +++ b/lib/safestack/safestack_platform.h @@ -16,6 +16,7 @@ #include "sanitizer_common/sanitizer_platform.h" #include +#include #include #include #include @@ -59,6 +60,33 @@ inline int TgKill(pid_t pid, ThreadId tid, int sig) { #endif } +inline void *Mmap(void *addr, size_t length, int prot, int flags, int fd, + off_t offset) { +#if SANITIZER_NETBSD + return mmap(addr, length, prot, flags, fd, offset); +#elif defined(__x86_64__) && (SANITIZER_FREEBSD) + return (void *)__syscall(SYS_mmap, addr, length, prot, flags, fd, offset); +#else + return (void *)syscall(SYS_mmap, addr, length, prot, flags, fd, offset); +#endif +} + +inline int Munmap(void *addr, size_t length) { +#if SANITIZER_NETBSD + return munmap(addr, length); +#else + return syscall(SYS_munmap, addr, length); +#endif +} + +inline int Mprotect(void *addr, size_t length, int prot) { +#if SANITIZER_NETBSD + return mprotect(addr, length, prot); +#else + return syscall(SYS_mprotect, addr, length, prot); +#endif +} + } // namespace safestack #endif // SAFESTACK_PLATFORM_H -- cgit v1.2.1