[PATCH] D18431: [tsan] Use direct syscalls for internal_mmap and internal_munmap on OS X

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 24 01:30:57 PDT 2016

kubabrecka created this revision.
kubabrecka added reviewers: dvyukov, samsonov, glider, kcc.
kubabrecka added subscribers: llvm-commits, zaks.anna, dcoughlin.

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.



Index: test/tsan/Darwin/malloc-stack-logging.cc
--- test/tsan/Darwin/malloc-stack-logging.cc
+++ 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.
Index: lib/sanitizer_common/sanitizer_mac.cc
--- lib/sanitizer_common/sanitizer_mac.cc
+++ lib/sanitizer_common/sanitizer_mac.cc
@@ -82,15 +82,20 @@
 #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) {

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18431.51518.patch
Type: text/x-patch
Size: 1597 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160324/e0d8fba3/attachment.bin>

More information about the llvm-commits mailing list