tsan: port Go runtime to Darwin


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@160266 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/tsan/go/buildgo.sh b/lib/tsan/go/buildgo.sh
index 5382af2..0741790 100755
--- a/lib/tsan/go/buildgo.sh
+++ b/lib/tsan/go/buildgo.sh
@@ -1,13 +1,21 @@
 #!/bin/bash
 set -e
 
+if [ "`uname -a | grep Linux`" != "" ]; then
+	LINUX=1
+elif [ "`uname -a | grep Darwin`" != "" ]; then
+	MAC=1
+else
+	echo Unknown platform
+	exit 1
+fi
+
 SRCS="
 	tsan_go.cc
 	../rtl/tsan_clock.cc
 	../rtl/tsan_flags.cc
 	../rtl/tsan_md5.cc
 	../rtl/tsan_mutex.cc
-	../rtl/tsan_platform_linux.cc
 	../rtl/tsan_printf.cc
 	../rtl/tsan_report.cc
 	../rtl/tsan_rtl.cc
@@ -21,12 +29,23 @@
 	../../sanitizer_common/sanitizer_common.cc
 	../../sanitizer_common/sanitizer_flags.cc
 	../../sanitizer_common/sanitizer_libc.cc
-	../../sanitizer_common/sanitizer_linux.cc
 	../../sanitizer_common/sanitizer_posix.cc
 	../../sanitizer_common/sanitizer_printf.cc
 	../../sanitizer_common/sanitizer_symbolizer.cc
 "
 
+if [ "$LINUX" != "" ]; then
+	SRCS+="
+		../rtl/tsan_platform_linux.cc
+		../../sanitizer_common/sanitizer_linux.cc
+	"
+elif [ "$MAC" != "" ]; then
+        SRCS+="
+                ../rtl/tsan_platform_mac.cc
+                ../../sanitizer_common/sanitizer_mac.cc
+        "
+fi
+
 #ASMS="../rtl/tsan_rtl_amd64.S"
 
 rm -f gotsan.cc
@@ -34,13 +53,17 @@
 	cat $F >> gotsan.cc
 done
 
-FLAGS=" -I../rtl -I../.. -I../../sanitizer_common -fPIC -g -Wall -Werror -ffreestanding -fno-exceptions -DTSAN_GO -DSANITIZER_GO -DTSAN_SHADOW_COUNT=4"
+FLAGS=" -I../rtl -I../.. -I../../sanitizer_common -fPIC -g -Wall -Werror -fno-exceptions -DTSAN_GO -DSANITIZER_GO -DTSAN_SHADOW_COUNT=4"
 if [ "$DEBUG" == "" ]; then
 	FLAGS+=" -DTSAN_DEBUG=0 -O3 -fomit-frame-pointer"
 else
 	FLAGS+=" -DTSAN_DEBUG=1 -g"
 fi
 
+if [ "$LINUX" != "" ]; then
+	FLAGS+=" -ffreestanding"
+fi
+
 echo gcc gotsan.cc -S -o tmp.s $FLAGS $CFLAGS
 gcc gotsan.cc -S -o tmp.s $FLAGS $CFLAGS
 cat tmp.s $ASMS > gotsan.s
diff --git a/lib/tsan/go/tsan_go.cc b/lib/tsan/go/tsan_go.cc
index 11f14da..13cc569 100644
--- a/lib/tsan/go/tsan_go.cc
+++ b/lib/tsan/go/tsan_go.cc
@@ -39,7 +39,7 @@
 
 extern "C" int goCallbackCommentPc(uptr pc, char **img, char **rtn,
                                    char **filename, int *lineno);
-extern "C" void __libc_free(void *p);
+extern "C" void free(void *p);
 
 ReportStack *SymbolizeCode(uptr addr) {
   ReportStack *s = NewReportStackEntry(addr);
@@ -52,9 +52,9 @@
     s->file = internal_strdup(filename);
     s->line = lineno;
     s->col = 0;
-    __libc_free(img);
-    __libc_free(rtn);
-    __libc_free(filename);
+    free(img);
+    free(rtn);
+    free(filename);
   }
   return s;
 }
diff --git a/lib/tsan/rtl/tsan_platform.h b/lib/tsan/rtl/tsan_platform.h
index 3013798..b557fa1 100644
--- a/lib/tsan/rtl/tsan_platform.h
+++ b/lib/tsan/rtl/tsan_platform.h
@@ -12,9 +12,8 @@
 // Platform-specific code.
 //===----------------------------------------------------------------------===//
 
-#ifndef TSAN_LINUX_H
-#define TSAN_LINUX_H
-#ifdef __linux__
+#ifndef TSAN_PLATFORM_H
+#define TSAN_PLATFORM_H
 
 #include "tsan_rtl.h"
 
@@ -99,5 +98,4 @@
 # error "Only 64-bit is supported"
 #endif
 
-#endif  // __linux__
-#endif  // TSAN_LINUX_H
+#endif  // TSAN_PLATFORM_H
diff --git a/lib/tsan/rtl/tsan_platform_mac.cc b/lib/tsan/rtl/tsan_platform_mac.cc
new file mode 100644
index 0000000..e56304c
--- /dev/null
+++ b/lib/tsan/rtl/tsan_platform_mac.cc
@@ -0,0 +1,108 @@
+//===-- tsan_platform_mac.cc ----------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of ThreadSanitizer (TSan), a race detector.
+//
+// Linux-specific code.
+//===----------------------------------------------------------------------===//
+
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_libc.h"
+#include "sanitizer_common/sanitizer_procmaps.h"
+#include "tsan_platform.h"
+#include "tsan_rtl.h"
+#include "tsan_flags.h"
+
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sched.h>
+
+namespace __sanitizer {
+
+void Die() {
+  _exit(1);
+}
+
+}  // namespace __sanitizer
+
+namespace __tsan {
+
+ScopedInRtl::ScopedInRtl() {
+}
+
+ScopedInRtl::~ScopedInRtl() {
+}
+
+uptr GetShadowMemoryConsumption() {
+  return 0;
+}
+
+void FlushShadowMemory() {
+}
+
+void InitializeShadowMemory() {
+  uptr shadow = (uptr)MmapFixedNoReserve(kLinuxShadowBeg,
+    kLinuxShadowEnd - kLinuxShadowBeg);
+  if (shadow != kLinuxShadowBeg) {
+    TsanPrintf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
+    TsanPrintf("FATAL: Make sure to compile with -fPIE and "
+               "to link with -pie.\n");
+    Die();
+  }
+  DPrintf("kLinuxShadow %zx-%zx (%zuGB)\n",
+      kLinuxShadowBeg, kLinuxShadowEnd,
+      (kLinuxShadowEnd - kLinuxShadowBeg) >> 30);
+  DPrintf("kLinuxAppMem %zx-%zx (%zuGB)\n",
+      kLinuxAppMemBeg, kLinuxAppMemEnd,
+      (kLinuxAppMemEnd - kLinuxAppMemBeg) >> 30);
+}
+
+const char *InitializePlatform() {
+  void *p = 0;
+  if (sizeof(p) == 8) {
+    // Disable core dumps, dumping of 16TB usually takes a bit long.
+    // The following magic is to prevent clang from replacing it with memset.
+    volatile rlimit lim;
+    lim.rlim_cur = 0;
+    lim.rlim_max = 0;
+    setrlimit(RLIMIT_CORE, (rlimit*)&lim);
+  }
+
+  return getenv("TSAN_OPTIONS");
+}
+
+void FinalizePlatform() {
+  fflush(0);
+}
+
+uptr GetTlsSize() {
+  return 0;
+}
+
+void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
+                          uptr *tls_addr, uptr *tls_size) {
+  *stk_addr = 0;
+  *stk_size = 0;
+  *tls_addr = 0;
+  *tls_size = 0;
+}
+
+}  // namespace __tsan