• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2
3set -e
4
5SRCS="
6	tsan_go.cpp
7	../rtl/tsan_clock.cpp
8	../rtl/tsan_external.cpp
9	../rtl/tsan_flags.cpp
10	../rtl/tsan_interface_atomic.cpp
11	../rtl/tsan_md5.cpp
12	../rtl/tsan_mutex.cpp
13	../rtl/tsan_report.cpp
14	../rtl/tsan_rtl.cpp
15	../rtl/tsan_rtl_mutex.cpp
16	../rtl/tsan_rtl_report.cpp
17	../rtl/tsan_rtl_thread.cpp
18	../rtl/tsan_rtl_proc.cpp
19	../rtl/tsan_stack_trace.cpp
20	../rtl/tsan_stat.cpp
21	../rtl/tsan_suppressions.cpp
22	../rtl/tsan_sync.cpp
23	../../sanitizer_common/sanitizer_allocator.cpp
24	../../sanitizer_common/sanitizer_common.cpp
25	../../sanitizer_common/sanitizer_common_libcdep.cpp
26	../../sanitizer_common/sanitizer_deadlock_detector2.cpp
27	../../sanitizer_common/sanitizer_file.cpp
28	../../sanitizer_common/sanitizer_flag_parser.cpp
29	../../sanitizer_common/sanitizer_flags.cpp
30	../../sanitizer_common/sanitizer_libc.cpp
31	../../sanitizer_common/sanitizer_persistent_allocator.cpp
32	../../sanitizer_common/sanitizer_printf.cpp
33	../../sanitizer_common/sanitizer_suppressions.cpp
34	../../sanitizer_common/sanitizer_thread_registry.cpp
35	../../sanitizer_common/sanitizer_stackdepot.cpp
36	../../sanitizer_common/sanitizer_stacktrace.cpp
37	../../sanitizer_common/sanitizer_symbolizer.cpp
38	../../sanitizer_common/sanitizer_symbolizer_report.cpp
39	../../sanitizer_common/sanitizer_termination.cpp
40"
41
42if [ "`uname -a | grep Linux`" != "" ]; then
43	OSCFLAGS="-fPIC -Wno-maybe-uninitialized"
44	OSLDFLAGS="-lpthread -fPIC -fpie"
45	SRCS="
46		$SRCS
47		../rtl/tsan_platform_linux.cpp
48		../../sanitizer_common/sanitizer_posix.cpp
49		../../sanitizer_common/sanitizer_posix_libcdep.cpp
50		../../sanitizer_common/sanitizer_procmaps_common.cpp
51		../../sanitizer_common/sanitizer_procmaps_linux.cpp
52		../../sanitizer_common/sanitizer_linux.cpp
53		../../sanitizer_common/sanitizer_linux_libcdep.cpp
54		../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
55		../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
56		"
57	if [ "`uname -a | grep ppc64le`" != "" ]; then
58		SUFFIX="linux_ppc64le"
59		ARCHCFLAGS="-m64"
60	elif [ "`uname -a | grep x86_64`" != "" ]; then
61		SUFFIX="linux_amd64"
62		ARCHCFLAGS="-m64"
63		OSCFLAGS="$OSCFLAGS -ffreestanding -Wno-unused-const-variable -Werror -Wno-unknown-warning-option"
64	elif [ "`uname -a | grep aarch64`" != "" ]; then
65		SUFFIX="linux_arm64"
66		ARCHCFLAGS=""
67	elif [ "`uname -a | grep -i mips64`" != "" ]; then
68		if [ "`lscpu | grep -i Little`" != "" ]; then
69			SUFFIX="linux_mips64le"
70			ARCHCFLAGS="-mips64 -EL"
71		else
72			SUFFIX="linux_mips64"
73			ARCHCFLAGS="-mips64 -EB"
74		fi
75	fi
76elif [ "`uname -a | grep FreeBSD`" != "" ]; then
77	# The resulting object still depends on libc.
78	# We removed this dependency for Go runtime for other OSes,
79	# and we should remove it for FreeBSD as well, but there is no pressing need.
80	DEPENDS_ON_LIBC=1
81	SUFFIX="freebsd_amd64"
82	OSCFLAGS="-fno-strict-aliasing -fPIC -Werror"
83	ARCHCFLAGS="-m64"
84	OSLDFLAGS="-lpthread -fPIC -fpie"
85	SRCS="
86		$SRCS
87		../rtl/tsan_platform_linux.cpp
88		../../sanitizer_common/sanitizer_posix.cpp
89		../../sanitizer_common/sanitizer_posix_libcdep.cpp
90		../../sanitizer_common/sanitizer_procmaps_bsd.cpp
91		../../sanitizer_common/sanitizer_procmaps_common.cpp
92		../../sanitizer_common/sanitizer_linux.cpp
93		../../sanitizer_common/sanitizer_linux_libcdep.cpp
94		../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
95		../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
96	"
97elif [ "`uname -a | grep NetBSD`" != "" ]; then
98	# The resulting object still depends on libc.
99	# We removed this dependency for Go runtime for other OSes,
100	# and we should remove it for NetBSD as well, but there is no pressing need.
101	DEPENDS_ON_LIBC=1
102	SUFFIX="netbsd_amd64"
103	OSCFLAGS="-fno-strict-aliasing -fPIC -Werror"
104	ARCHCFLAGS="-m64"
105	OSLDFLAGS="-lpthread -fPIC -fpie"
106	SRCS="
107		$SRCS
108		../rtl/tsan_platform_linux.cpp
109		../../sanitizer_common/sanitizer_posix.cpp
110		../../sanitizer_common/sanitizer_posix_libcdep.cpp
111		../../sanitizer_common/sanitizer_procmaps_bsd.cpp
112		../../sanitizer_common/sanitizer_procmaps_common.cpp
113		../../sanitizer_common/sanitizer_linux.cpp
114		../../sanitizer_common/sanitizer_linux_libcdep.cpp
115		../../sanitizer_common/sanitizer_netbsd.cpp
116		../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
117		../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
118	"
119elif [ "`uname -a | grep Darwin`" != "" ]; then
120	OSCFLAGS="-fPIC -Wno-unused-const-variable -Wno-unknown-warning-option -mmacosx-version-min=10.7"
121	OSLDFLAGS="-lpthread -fPIC -fpie -mmacosx-version-min=10.7"
122	SRCS="
123		$SRCS
124		../rtl/tsan_platform_mac.cpp
125		../../sanitizer_common/sanitizer_mac.cpp
126		../../sanitizer_common/sanitizer_mac_libcdep.cpp
127		../../sanitizer_common/sanitizer_posix.cpp
128		../../sanitizer_common/sanitizer_posix_libcdep.cpp
129		../../sanitizer_common/sanitizer_procmaps_mac.cpp
130	"
131	if [ "`uname -a | grep x86_64`" != "" ]; then
132		SUFFIX="darwin_amd64"
133		ARCHCFLAGS="-m64"
134	elif [ "`uname -a | grep arm64`" != "" ]; then
135		SUFFIX="darwin_arm64"
136		ARCHCFLAGS=""
137	fi
138elif [ "`uname -a | grep MINGW`" != "" ]; then
139	SUFFIX="windows_amd64"
140	OSCFLAGS="-Wno-error=attributes -Wno-attributes -Wno-unused-const-variable -Wno-unknown-warning-option"
141	ARCHCFLAGS="-m64"
142	OSLDFLAGS=""
143	SRCS="
144		$SRCS
145		../rtl/tsan_platform_windows.cpp
146		../../sanitizer_common/sanitizer_win.cpp
147	"
148else
149	echo Unknown platform
150	exit 1
151fi
152
153CC=${CC:-gcc}
154IN_TMPDIR=${IN_TMPDIR:-0}
155SILENT=${SILENT:-0}
156
157if [ $IN_TMPDIR != "0" ]; then
158  DIR=$(mktemp -qd /tmp/gotsan.XXXXXXXXXX)
159  cleanup() {
160    rm -rf $DIR
161  }
162  trap cleanup EXIT
163else
164  DIR=.
165fi
166
167SRCS="$SRCS $ADD_SRCS"
168
169rm -f $DIR/gotsan.cpp
170for F in $SRCS; do
171	cat $F >> $DIR/gotsan.cpp
172done
173
174FLAGS=" -I../rtl -I../.. -I../../sanitizer_common -I../../../include -std=c++14 -Wall -fno-exceptions -fno-rtti -DSANITIZER_GO=1 -DSANITIZER_DEADLOCK_DETECTOR_VERSION=2 $OSCFLAGS $ARCHCFLAGS"
175DEBUG_FLAGS="$FLAGS -DSANITIZER_DEBUG=1 -g"
176FLAGS="$FLAGS -DSANITIZER_DEBUG=0 -O3 -fomit-frame-pointer"
177if [ "$SUFFIX" = "linux_ppc64le" ]; then
178	FLAGS="$FLAGS -mcpu=power8 -fno-function-sections"
179elif [ "$SUFFIX" = "linux_amd64" ]; then
180	FLAGS="$FLAGS -msse3"
181fi
182
183if [ "$DEBUG" = "" ]; then
184	# Do a build test with debug flags.
185	$CC $DIR/gotsan.cpp -c -o $DIR/race_debug_$SUFFIX.syso $DEBUG_FLAGS $CFLAGS
186else
187	FLAGS="$DEBUG_FLAGS"
188fi
189
190if [ "$SILENT" != "1" ]; then
191  echo $CC gotsan.cpp -c -o $DIR/race_$SUFFIX.syso $FLAGS $CFLAGS
192fi
193$CC $DIR/gotsan.cpp -c -o $DIR/race_$SUFFIX.syso $FLAGS $CFLAGS
194
195$CC $OSCFLAGS $ARCHCFLAGS test.c $DIR/race_$SUFFIX.syso -g -o $DIR/test $OSLDFLAGS $LDFLAGS
196
197# Verify that no libc specific code is present.
198if [ "$DEPENDS_ON_LIBC" != "1" ]; then
199	if nm $DIR/race_$SUFFIX.syso | grep -q __libc_; then
200		printf -- '%s seems to link to libc\n' "race_$SUFFIX.syso"
201		exit 1
202	fi
203fi
204
205if [ "`uname -a | grep NetBSD`" != "" ]; then
206  # Turn off ASLR in the test binary.
207  /usr/sbin/paxctl +a $DIR/test
208fi
209export GORACE="exitcode=0 atexit_sleep_ms=0"
210if [ "$SILENT" != "1" ]; then
211  $DIR/test
212else
213  $DIR/test 2>/dev/null
214fi
215