From 46b936368346f86cd25c9bb5a7b42fe857fa74ec Mon Sep 17 00:00:00 2001 From: Kuba Brecka Date: Thu, 24 Mar 2016 11:50:21 +0000 Subject: [PATCH] [tsan] Use direct syscalls for internal_mmap and internal_munmap on OS X On OS X, internal_mmap just uses mmap, which can invoke callbacks into libmalloc (e.g. when MallocStackLogging is enabled). This can subsequently call other intercepted functions, and this breaks our Darwin-specific ThreadState initialization. Let's use direct syscalls in internal_mmap and internal_munmap. Added a testcase. Differential Revision: http://reviews.llvm.org/D18431 llvm-svn: 264259 --- compiler-rt/lib/sanitizer_common/sanitizer_mac.cc | 9 +++++++-- compiler-rt/test/tsan/Darwin/malloc-stack-logging.cc | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 compiler-rt/test/tsan/Darwin/malloc-stack-logging.cc diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc index 820634a..35cd3c5 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc @@ -82,15 +82,20 @@ namespace __sanitizer { #include "sanitizer_syscall_generic.inc" +// Direct syscalls, don't call libmalloc hooks. +extern "C" void *__mmap(void *addr, size_t len, int prot, int flags, int fildes, + off_t off); +extern "C" int __munmap(void *, size_t); + // ---------------------- sanitizer_libc.h uptr internal_mmap(void *addr, size_t length, int prot, int flags, int fd, u64 offset) { if (fd == -1) fd = VM_MAKE_TAG(VM_MEMORY_ANALYSIS_TOOL); - return (uptr)mmap(addr, length, prot, flags, fd, offset); + return (uptr)__mmap(addr, length, prot, flags, fd, offset); } uptr internal_munmap(void *addr, uptr length) { - return munmap(addr, length); + return __munmap(addr, length); } int internal_mprotect(void *addr, uptr length, int prot) { diff --git a/compiler-rt/test/tsan/Darwin/malloc-stack-logging.cc b/compiler-rt/test/tsan/Darwin/malloc-stack-logging.cc new file mode 100644 index 0000000..447fcd1 --- /dev/null +++ b/compiler-rt/test/tsan/Darwin/malloc-stack-logging.cc @@ -0,0 +1,19 @@ +// RUN: %clangxx_tsan -O1 %s -o %t +// RUN: MallocStackLogging=1 %run %t 2>&1 | FileCheck %s +#include +#include +#include + +void *foo(void *p) { + return NULL; +} + +int main() { + pthread_t t; + pthread_create(&t, NULL, foo, NULL); + pthread_join(t, NULL); + fprintf(stderr, "Done.\n"); + return 0; +} + +// CHECK: Done. -- 2.7.4