Skip to content

Commit

Permalink
[tsan] Use direct syscalls for internal_mmap and internal_munmap on OS X
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kubamracek committed Mar 24, 2016
1 parent f692130 commit 46b9363
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions compiler-rt/lib/sanitizer_common/sanitizer_mac.cc
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions 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 <pthread.h>
#include <stdlib.h>
#include <stdio.h>

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.

0 comments on commit 46b9363

Please sign in to comment.