annotate libsanitizer/tsan/tsan_platform_linux.cc @ 138:fc828634a951

merge
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:17:14 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- tsan_platform_linux.cc --------------------------------------------===//
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
kono
parents:
diff changeset
9 //
kono
parents:
diff changeset
10 // Linux- and FreeBSD-specific code.
kono
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 #include "sanitizer_common/sanitizer_platform.h"
kono
parents:
diff changeset
15 #if SANITIZER_LINUX || SANITIZER_FREEBSD
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 #include "sanitizer_common/sanitizer_common.h"
kono
parents:
diff changeset
18 #include "sanitizer_common/sanitizer_libc.h"
kono
parents:
diff changeset
19 #include "sanitizer_common/sanitizer_linux.h"
kono
parents:
diff changeset
20 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
kono
parents:
diff changeset
21 #include "sanitizer_common/sanitizer_posix.h"
kono
parents:
diff changeset
22 #include "sanitizer_common/sanitizer_procmaps.h"
kono
parents:
diff changeset
23 #include "sanitizer_common/sanitizer_stoptheworld.h"
kono
parents:
diff changeset
24 #include "sanitizer_common/sanitizer_stackdepot.h"
kono
parents:
diff changeset
25 #include "tsan_platform.h"
kono
parents:
diff changeset
26 #include "tsan_rtl.h"
kono
parents:
diff changeset
27 #include "tsan_flags.h"
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 #include <fcntl.h>
kono
parents:
diff changeset
30 #include <pthread.h>
kono
parents:
diff changeset
31 #include <signal.h>
kono
parents:
diff changeset
32 #include <stdio.h>
kono
parents:
diff changeset
33 #include <stdlib.h>
kono
parents:
diff changeset
34 #include <string.h>
kono
parents:
diff changeset
35 #include <stdarg.h>
kono
parents:
diff changeset
36 #include <sys/mman.h>
kono
parents:
diff changeset
37 #if SANITIZER_LINUX
kono
parents:
diff changeset
38 #include <sys/personality.h>
kono
parents:
diff changeset
39 #include <setjmp.h>
kono
parents:
diff changeset
40 #endif
kono
parents:
diff changeset
41 #include <sys/syscall.h>
kono
parents:
diff changeset
42 #include <sys/socket.h>
kono
parents:
diff changeset
43 #include <sys/time.h>
kono
parents:
diff changeset
44 #include <sys/types.h>
kono
parents:
diff changeset
45 #include <sys/resource.h>
kono
parents:
diff changeset
46 #include <sys/stat.h>
kono
parents:
diff changeset
47 #include <unistd.h>
kono
parents:
diff changeset
48 #include <sched.h>
kono
parents:
diff changeset
49 #include <dlfcn.h>
kono
parents:
diff changeset
50 #if SANITIZER_LINUX
kono
parents:
diff changeset
51 #define __need_res_state
kono
parents:
diff changeset
52 #include <resolv.h>
kono
parents:
diff changeset
53 #endif
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 #ifdef sa_handler
kono
parents:
diff changeset
56 # undef sa_handler
kono
parents:
diff changeset
57 #endif
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 #ifdef sa_sigaction
kono
parents:
diff changeset
60 # undef sa_sigaction
kono
parents:
diff changeset
61 #endif
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 #if SANITIZER_FREEBSD
kono
parents:
diff changeset
64 extern "C" void *__libc_stack_end;
kono
parents:
diff changeset
65 void *__libc_stack_end = 0;
kono
parents:
diff changeset
66 #endif
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 #if SANITIZER_LINUX && defined(__aarch64__)
kono
parents:
diff changeset
69 void InitializeGuardPtr() __attribute__((visibility("hidden")));
kono
parents:
diff changeset
70 #endif
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 namespace __tsan {
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 #ifdef TSAN_RUNTIME_VMA
kono
parents:
diff changeset
75 // Runtime detected VMA size.
kono
parents:
diff changeset
76 uptr vmaSize;
kono
parents:
diff changeset
77 #endif
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 enum {
kono
parents:
diff changeset
80 MemTotal = 0,
kono
parents:
diff changeset
81 MemShadow = 1,
kono
parents:
diff changeset
82 MemMeta = 2,
kono
parents:
diff changeset
83 MemFile = 3,
kono
parents:
diff changeset
84 MemMmap = 4,
kono
parents:
diff changeset
85 MemTrace = 5,
kono
parents:
diff changeset
86 MemHeap = 6,
kono
parents:
diff changeset
87 MemOther = 7,
kono
parents:
diff changeset
88 MemCount = 8,
kono
parents:
diff changeset
89 };
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 void FillProfileCallback(uptr p, uptr rss, bool file,
kono
parents:
diff changeset
92 uptr *mem, uptr stats_size) {
kono
parents:
diff changeset
93 mem[MemTotal] += rss;
kono
parents:
diff changeset
94 if (p >= ShadowBeg() && p < ShadowEnd())
kono
parents:
diff changeset
95 mem[MemShadow] += rss;
kono
parents:
diff changeset
96 else if (p >= MetaShadowBeg() && p < MetaShadowEnd())
kono
parents:
diff changeset
97 mem[MemMeta] += rss;
kono
parents:
diff changeset
98 #if !SANITIZER_GO
kono
parents:
diff changeset
99 else if (p >= HeapMemBeg() && p < HeapMemEnd())
kono
parents:
diff changeset
100 mem[MemHeap] += rss;
kono
parents:
diff changeset
101 else if (p >= LoAppMemBeg() && p < LoAppMemEnd())
kono
parents:
diff changeset
102 mem[file ? MemFile : MemMmap] += rss;
kono
parents:
diff changeset
103 else if (p >= HiAppMemBeg() && p < HiAppMemEnd())
kono
parents:
diff changeset
104 mem[file ? MemFile : MemMmap] += rss;
kono
parents:
diff changeset
105 #else
kono
parents:
diff changeset
106 else if (p >= AppMemBeg() && p < AppMemEnd())
kono
parents:
diff changeset
107 mem[file ? MemFile : MemMmap] += rss;
kono
parents:
diff changeset
108 #endif
kono
parents:
diff changeset
109 else if (p >= TraceMemBeg() && p < TraceMemEnd())
kono
parents:
diff changeset
110 mem[MemTrace] += rss;
kono
parents:
diff changeset
111 else
kono
parents:
diff changeset
112 mem[MemOther] += rss;
kono
parents:
diff changeset
113 }
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) {
kono
parents:
diff changeset
116 uptr mem[MemCount];
kono
parents:
diff changeset
117 internal_memset(mem, 0, sizeof(mem[0]) * MemCount);
kono
parents:
diff changeset
118 __sanitizer::GetMemoryProfile(FillProfileCallback, mem, 7);
kono
parents:
diff changeset
119 StackDepotStats *stacks = StackDepotGetStats();
kono
parents:
diff changeset
120 internal_snprintf(buf, buf_size,
kono
parents:
diff changeset
121 "RSS %zd MB: shadow:%zd meta:%zd file:%zd mmap:%zd"
kono
parents:
diff changeset
122 " trace:%zd heap:%zd other:%zd stacks=%zd[%zd] nthr=%zd/%zd\n",
kono
parents:
diff changeset
123 mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20,
kono
parents:
diff changeset
124 mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemTrace] >> 20,
kono
parents:
diff changeset
125 mem[MemHeap] >> 20, mem[MemOther] >> 20,
kono
parents:
diff changeset
126 stacks->allocated >> 20, stacks->n_uniq_ids,
kono
parents:
diff changeset
127 nlive, nthread);
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 #if SANITIZER_LINUX
kono
parents:
diff changeset
131 void FlushShadowMemoryCallback(
kono
parents:
diff changeset
132 const SuspendedThreadsList &suspended_threads_list,
kono
parents:
diff changeset
133 void *argument) {
kono
parents:
diff changeset
134 ReleaseMemoryPagesToOS(ShadowBeg(), ShadowEnd());
kono
parents:
diff changeset
135 }
kono
parents:
diff changeset
136 #endif
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 void FlushShadowMemory() {
kono
parents:
diff changeset
139 #if SANITIZER_LINUX
kono
parents:
diff changeset
140 StopTheWorld(FlushShadowMemoryCallback, 0);
kono
parents:
diff changeset
141 #endif
kono
parents:
diff changeset
142 }
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 #if !SANITIZER_GO
kono
parents:
diff changeset
145 // Mark shadow for .rodata sections with the special kShadowRodata marker.
kono
parents:
diff changeset
146 // Accesses to .rodata can't race, so this saves time, memory and trace space.
kono
parents:
diff changeset
147 static void MapRodata() {
kono
parents:
diff changeset
148 // First create temp file.
kono
parents:
diff changeset
149 const char *tmpdir = GetEnv("TMPDIR");
kono
parents:
diff changeset
150 if (tmpdir == 0)
kono
parents:
diff changeset
151 tmpdir = GetEnv("TEST_TMPDIR");
kono
parents:
diff changeset
152 #ifdef P_tmpdir
kono
parents:
diff changeset
153 if (tmpdir == 0)
kono
parents:
diff changeset
154 tmpdir = P_tmpdir;
kono
parents:
diff changeset
155 #endif
kono
parents:
diff changeset
156 if (tmpdir == 0)
kono
parents:
diff changeset
157 return;
kono
parents:
diff changeset
158 char name[256];
kono
parents:
diff changeset
159 internal_snprintf(name, sizeof(name), "%s/tsan.rodata.%d",
kono
parents:
diff changeset
160 tmpdir, (int)internal_getpid());
kono
parents:
diff changeset
161 uptr openrv = internal_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
kono
parents:
diff changeset
162 if (internal_iserror(openrv))
kono
parents:
diff changeset
163 return;
kono
parents:
diff changeset
164 internal_unlink(name); // Unlink it now, so that we can reuse the buffer.
kono
parents:
diff changeset
165 fd_t fd = openrv;
kono
parents:
diff changeset
166 // Fill the file with kShadowRodata.
kono
parents:
diff changeset
167 const uptr kMarkerSize = 512 * 1024 / sizeof(u64);
kono
parents:
diff changeset
168 InternalScopedBuffer<u64> marker(kMarkerSize);
kono
parents:
diff changeset
169 // volatile to prevent insertion of memset
kono
parents:
diff changeset
170 for (volatile u64 *p = marker.data(); p < marker.data() + kMarkerSize; p++)
kono
parents:
diff changeset
171 *p = kShadowRodata;
kono
parents:
diff changeset
172 internal_write(fd, marker.data(), marker.size());
kono
parents:
diff changeset
173 // Map the file into memory.
kono
parents:
diff changeset
174 uptr page = internal_mmap(0, GetPageSizeCached(), PROT_READ | PROT_WRITE,
kono
parents:
diff changeset
175 MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
kono
parents:
diff changeset
176 if (internal_iserror(page)) {
kono
parents:
diff changeset
177 internal_close(fd);
kono
parents:
diff changeset
178 return;
kono
parents:
diff changeset
179 }
kono
parents:
diff changeset
180 // Map the file into shadow of .rodata sections.
kono
parents:
diff changeset
181 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
kono
parents:
diff changeset
182 // Reusing the buffer 'name'.
kono
parents:
diff changeset
183 MemoryMappedSegment segment(name, ARRAY_SIZE(name));
kono
parents:
diff changeset
184 while (proc_maps.Next(&segment)) {
kono
parents:
diff changeset
185 if (segment.filename[0] != 0 && segment.filename[0] != '[' &&
kono
parents:
diff changeset
186 segment.IsReadable() && segment.IsExecutable() &&
kono
parents:
diff changeset
187 !segment.IsWritable() && IsAppMem(segment.start)) {
kono
parents:
diff changeset
188 // Assume it's .rodata
kono
parents:
diff changeset
189 char *shadow_start = (char *)MemToShadow(segment.start);
kono
parents:
diff changeset
190 char *shadow_end = (char *)MemToShadow(segment.end);
kono
parents:
diff changeset
191 for (char *p = shadow_start; p < shadow_end; p += marker.size()) {
kono
parents:
diff changeset
192 internal_mmap(p, Min<uptr>(marker.size(), shadow_end - p),
kono
parents:
diff changeset
193 PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
kono
parents:
diff changeset
194 }
kono
parents:
diff changeset
195 }
kono
parents:
diff changeset
196 }
kono
parents:
diff changeset
197 internal_close(fd);
kono
parents:
diff changeset
198 }
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 void InitializeShadowMemoryPlatform() {
kono
parents:
diff changeset
201 MapRodata();
kono
parents:
diff changeset
202 }
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 #endif // #if !SANITIZER_GO
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 void InitializePlatformEarly() {
kono
parents:
diff changeset
207 #ifdef TSAN_RUNTIME_VMA
kono
parents:
diff changeset
208 vmaSize =
kono
parents:
diff changeset
209 (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1);
kono
parents:
diff changeset
210 #if defined(__aarch64__)
kono
parents:
diff changeset
211 if (vmaSize != 39 && vmaSize != 42 && vmaSize != 48) {
kono
parents:
diff changeset
212 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
kono
parents:
diff changeset
213 Printf("FATAL: Found %d - Supported 39, 42 and 48\n", vmaSize);
kono
parents:
diff changeset
214 Die();
kono
parents:
diff changeset
215 }
kono
parents:
diff changeset
216 #elif defined(__powerpc64__)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
217 if (vmaSize != 44 && vmaSize != 46 && vmaSize != 47) {
111
kono
parents:
diff changeset
218 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
219 Printf("FATAL: Found %d - Supported 44, 46, and 47\n", vmaSize);
111
kono
parents:
diff changeset
220 Die();
kono
parents:
diff changeset
221 }
kono
parents:
diff changeset
222 #endif
kono
parents:
diff changeset
223 #endif
kono
parents:
diff changeset
224 }
kono
parents:
diff changeset
225
kono
parents:
diff changeset
226 void InitializePlatform() {
kono
parents:
diff changeset
227 DisableCoreDumperIfNecessary();
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 // Go maps shadow memory lazily and works fine with limited address space.
kono
parents:
diff changeset
230 // Unlimited stack is not a problem as well, because the executable
kono
parents:
diff changeset
231 // is not compiled with -pie.
kono
parents:
diff changeset
232 if (!SANITIZER_GO) {
kono
parents:
diff changeset
233 bool reexec = false;
kono
parents:
diff changeset
234 // TSan doesn't play well with unlimited stack size (as stack
kono
parents:
diff changeset
235 // overlaps with shadow memory). If we detect unlimited stack size,
kono
parents:
diff changeset
236 // we re-exec the program with limited stack size as a best effort.
kono
parents:
diff changeset
237 if (StackSizeIsUnlimited()) {
kono
parents:
diff changeset
238 const uptr kMaxStackSize = 32 * 1024 * 1024;
kono
parents:
diff changeset
239 VReport(1, "Program is run with unlimited stack size, which wouldn't "
kono
parents:
diff changeset
240 "work with ThreadSanitizer.\n"
kono
parents:
diff changeset
241 "Re-execing with stack size limited to %zd bytes.\n",
kono
parents:
diff changeset
242 kMaxStackSize);
kono
parents:
diff changeset
243 SetStackSizeLimitInBytes(kMaxStackSize);
kono
parents:
diff changeset
244 reexec = true;
kono
parents:
diff changeset
245 }
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 if (!AddressSpaceIsUnlimited()) {
kono
parents:
diff changeset
248 Report("WARNING: Program is run with limited virtual address space,"
kono
parents:
diff changeset
249 " which wouldn't work with ThreadSanitizer.\n");
kono
parents:
diff changeset
250 Report("Re-execing with unlimited virtual address space.\n");
kono
parents:
diff changeset
251 SetAddressSpaceUnlimited();
kono
parents:
diff changeset
252 reexec = true;
kono
parents:
diff changeset
253 }
kono
parents:
diff changeset
254 #if SANITIZER_LINUX && defined(__aarch64__)
kono
parents:
diff changeset
255 // After patch "arm64: mm: support ARCH_MMAP_RND_BITS." is introduced in
kono
parents:
diff changeset
256 // linux kernel, the random gap between stack and mapped area is increased
kono
parents:
diff changeset
257 // from 128M to 36G on 39-bit aarch64. As it is almost impossible to cover
kono
parents:
diff changeset
258 // this big range, we should disable randomized virtual space on aarch64.
kono
parents:
diff changeset
259 int old_personality = personality(0xffffffff);
kono
parents:
diff changeset
260 if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
kono
parents:
diff changeset
261 VReport(1, "WARNING: Program is run with randomized virtual address "
kono
parents:
diff changeset
262 "space, which wouldn't work with ThreadSanitizer.\n"
kono
parents:
diff changeset
263 "Re-execing with fixed virtual address space.\n");
kono
parents:
diff changeset
264 CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
kono
parents:
diff changeset
265 reexec = true;
kono
parents:
diff changeset
266 }
kono
parents:
diff changeset
267 // Initialize the guard pointer used in {sig}{set,long}jump.
kono
parents:
diff changeset
268 InitializeGuardPtr();
kono
parents:
diff changeset
269 #endif
kono
parents:
diff changeset
270 if (reexec)
kono
parents:
diff changeset
271 ReExec();
kono
parents:
diff changeset
272 }
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 #if !SANITIZER_GO
kono
parents:
diff changeset
275 CheckAndProtect();
kono
parents:
diff changeset
276 InitTlsSize();
kono
parents:
diff changeset
277 #endif
kono
parents:
diff changeset
278 }
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 #if !SANITIZER_GO
kono
parents:
diff changeset
281 // Extract file descriptors passed to glibc internal __res_iclose function.
kono
parents:
diff changeset
282 // This is required to properly "close" the fds, because we do not see internal
kono
parents:
diff changeset
283 // closes within glibc. The code is a pure hack.
kono
parents:
diff changeset
284 int ExtractResolvFDs(void *state, int *fds, int nfd) {
kono
parents:
diff changeset
285 #if SANITIZER_LINUX && !SANITIZER_ANDROID
kono
parents:
diff changeset
286 int cnt = 0;
kono
parents:
diff changeset
287 struct __res_state *statp = (struct __res_state*)state;
kono
parents:
diff changeset
288 for (int i = 0; i < MAXNS && cnt < nfd; i++) {
kono
parents:
diff changeset
289 if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1)
kono
parents:
diff changeset
290 fds[cnt++] = statp->_u._ext.nssocks[i];
kono
parents:
diff changeset
291 }
kono
parents:
diff changeset
292 return cnt;
kono
parents:
diff changeset
293 #else
kono
parents:
diff changeset
294 return 0;
kono
parents:
diff changeset
295 #endif
kono
parents:
diff changeset
296 }
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 // Extract file descriptors passed via UNIX domain sockets.
kono
parents:
diff changeset
299 // This is requried to properly handle "open" of these fds.
kono
parents:
diff changeset
300 // see 'man recvmsg' and 'man 3 cmsg'.
kono
parents:
diff changeset
301 int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) {
kono
parents:
diff changeset
302 int res = 0;
kono
parents:
diff changeset
303 msghdr *msg = (msghdr*)msgp;
kono
parents:
diff changeset
304 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
kono
parents:
diff changeset
305 for (; cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
kono
parents:
diff changeset
306 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
kono
parents:
diff changeset
307 continue;
kono
parents:
diff changeset
308 int n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(fds[0]);
kono
parents:
diff changeset
309 for (int i = 0; i < n; i++) {
kono
parents:
diff changeset
310 fds[res++] = ((int*)CMSG_DATA(cmsg))[i];
kono
parents:
diff changeset
311 if (res == nfd)
kono
parents:
diff changeset
312 return res;
kono
parents:
diff changeset
313 }
kono
parents:
diff changeset
314 }
kono
parents:
diff changeset
315 return res;
kono
parents:
diff changeset
316 }
kono
parents:
diff changeset
317
kono
parents:
diff changeset
318 void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size) {
kono
parents:
diff changeset
319 // Check that the thr object is in tls;
kono
parents:
diff changeset
320 const uptr thr_beg = (uptr)thr;
kono
parents:
diff changeset
321 const uptr thr_end = (uptr)thr + sizeof(*thr);
kono
parents:
diff changeset
322 CHECK_GE(thr_beg, tls_addr);
kono
parents:
diff changeset
323 CHECK_LE(thr_beg, tls_addr + tls_size);
kono
parents:
diff changeset
324 CHECK_GE(thr_end, tls_addr);
kono
parents:
diff changeset
325 CHECK_LE(thr_end, tls_addr + tls_size);
kono
parents:
diff changeset
326 // Since the thr object is huge, skip it.
kono
parents:
diff changeset
327 MemoryRangeImitateWrite(thr, /*pc=*/2, tls_addr, thr_beg - tls_addr);
kono
parents:
diff changeset
328 MemoryRangeImitateWrite(thr, /*pc=*/2, thr_end,
kono
parents:
diff changeset
329 tls_addr + tls_size - thr_end);
kono
parents:
diff changeset
330 }
kono
parents:
diff changeset
331
kono
parents:
diff changeset
332 // Note: this function runs with async signals enabled,
kono
parents:
diff changeset
333 // so it must not touch any tsan state.
kono
parents:
diff changeset
334 int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
kono
parents:
diff changeset
335 void *abstime), void *c, void *m, void *abstime,
kono
parents:
diff changeset
336 void(*cleanup)(void *arg), void *arg) {
kono
parents:
diff changeset
337 // pthread_cleanup_push/pop are hardcore macros mess.
kono
parents:
diff changeset
338 // We can't intercept nor call them w/o including pthread.h.
kono
parents:
diff changeset
339 int res;
kono
parents:
diff changeset
340 pthread_cleanup_push(cleanup, arg);
kono
parents:
diff changeset
341 res = fn(c, m, abstime);
kono
parents:
diff changeset
342 pthread_cleanup_pop(0);
kono
parents:
diff changeset
343 return res;
kono
parents:
diff changeset
344 }
kono
parents:
diff changeset
345 #endif
kono
parents:
diff changeset
346
kono
parents:
diff changeset
347 #if !SANITIZER_GO
kono
parents:
diff changeset
348 void ReplaceSystemMalloc() { }
kono
parents:
diff changeset
349 #endif
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 #if !SANITIZER_GO
kono
parents:
diff changeset
352 #if SANITIZER_ANDROID
kono
parents:
diff changeset
353 // On Android, one thread can call intercepted functions after
kono
parents:
diff changeset
354 // DestroyThreadState(), so add a fake thread state for "dead" threads.
kono
parents:
diff changeset
355 static ThreadState *dead_thread_state = nullptr;
kono
parents:
diff changeset
356
kono
parents:
diff changeset
357 ThreadState *cur_thread() {
kono
parents:
diff changeset
358 ThreadState* thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
kono
parents:
diff changeset
359 if (thr == nullptr) {
kono
parents:
diff changeset
360 __sanitizer_sigset_t emptyset;
kono
parents:
diff changeset
361 internal_sigfillset(&emptyset);
kono
parents:
diff changeset
362 __sanitizer_sigset_t oldset;
kono
parents:
diff changeset
363 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &emptyset, &oldset));
kono
parents:
diff changeset
364 thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
kono
parents:
diff changeset
365 if (thr == nullptr) {
kono
parents:
diff changeset
366 thr = reinterpret_cast<ThreadState*>(MmapOrDie(sizeof(ThreadState),
kono
parents:
diff changeset
367 "ThreadState"));
kono
parents:
diff changeset
368 *get_android_tls_ptr() = reinterpret_cast<uptr>(thr);
kono
parents:
diff changeset
369 if (dead_thread_state == nullptr) {
kono
parents:
diff changeset
370 dead_thread_state = reinterpret_cast<ThreadState*>(
kono
parents:
diff changeset
371 MmapOrDie(sizeof(ThreadState), "ThreadState"));
kono
parents:
diff changeset
372 dead_thread_state->fast_state.SetIgnoreBit();
kono
parents:
diff changeset
373 dead_thread_state->ignore_interceptors = 1;
kono
parents:
diff changeset
374 dead_thread_state->is_dead = true;
kono
parents:
diff changeset
375 *const_cast<int*>(&dead_thread_state->tid) = -1;
kono
parents:
diff changeset
376 CHECK_EQ(0, internal_mprotect(dead_thread_state, sizeof(ThreadState),
kono
parents:
diff changeset
377 PROT_READ));
kono
parents:
diff changeset
378 }
kono
parents:
diff changeset
379 }
kono
parents:
diff changeset
380 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr));
kono
parents:
diff changeset
381 }
kono
parents:
diff changeset
382 return thr;
kono
parents:
diff changeset
383 }
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 void cur_thread_finalize() {
kono
parents:
diff changeset
386 __sanitizer_sigset_t emptyset;
kono
parents:
diff changeset
387 internal_sigfillset(&emptyset);
kono
parents:
diff changeset
388 __sanitizer_sigset_t oldset;
kono
parents:
diff changeset
389 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &emptyset, &oldset));
kono
parents:
diff changeset
390 ThreadState* thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
kono
parents:
diff changeset
391 if (thr != dead_thread_state) {
kono
parents:
diff changeset
392 *get_android_tls_ptr() = reinterpret_cast<uptr>(dead_thread_state);
kono
parents:
diff changeset
393 UnmapOrDie(thr, sizeof(ThreadState));
kono
parents:
diff changeset
394 }
kono
parents:
diff changeset
395 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr));
kono
parents:
diff changeset
396 }
kono
parents:
diff changeset
397 #endif // SANITIZER_ANDROID
kono
parents:
diff changeset
398 #endif // if !SANITIZER_GO
kono
parents:
diff changeset
399
kono
parents:
diff changeset
400 } // namespace __tsan
kono
parents:
diff changeset
401
kono
parents:
diff changeset
402 #endif // SANITIZER_LINUX || SANITIZER_FREEBSD