Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / tsan / rtl / tsan_platform_linux.cpp
blob369509ed0a604ee350baa2568c96baac99c25ae1
1 //===-- tsan_platform_linux.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 // Linux- and BSD-specific code.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_libc.h"
19 #include "sanitizer_common/sanitizer_linux.h"
20 #include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
21 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
22 #include "sanitizer_common/sanitizer_posix.h"
23 #include "sanitizer_common/sanitizer_procmaps.h"
24 #include "sanitizer_common/sanitizer_stackdepot.h"
25 #include "sanitizer_common/sanitizer_stoptheworld.h"
26 #include "tsan_flags.h"
27 #include "tsan_platform.h"
28 #include "tsan_rtl.h"
30 #include <fcntl.h>
31 #include <pthread.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include <sys/mman.h>
38 #if SANITIZER_LINUX
39 #include <sys/personality.h>
40 #include <setjmp.h>
41 #endif
42 #include <sys/syscall.h>
43 #include <sys/socket.h>
44 #include <sys/time.h>
45 #include <sys/types.h>
46 #include <sys/resource.h>
47 #include <sys/stat.h>
48 #include <unistd.h>
49 #include <sched.h>
50 #include <dlfcn.h>
51 #if SANITIZER_LINUX
52 #define __need_res_state
53 #include <resolv.h>
54 #endif
56 #ifdef sa_handler
57 # undef sa_handler
58 #endif
60 #ifdef sa_sigaction
61 # undef sa_sigaction
62 #endif
64 #if SANITIZER_FREEBSD
65 extern "C" void *__libc_stack_end;
66 void *__libc_stack_end = 0;
67 #endif
69 #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64)) && \
70 !SANITIZER_GO
71 # define INIT_LONGJMP_XOR_KEY 1
72 #else
73 # define INIT_LONGJMP_XOR_KEY 0
74 #endif
76 #if INIT_LONGJMP_XOR_KEY
77 #include "interception/interception.h"
78 // Must be declared outside of other namespaces.
79 DECLARE_REAL(int, _setjmp, void *env)
80 #endif
82 namespace __tsan {
84 #if INIT_LONGJMP_XOR_KEY
85 static void InitializeLongjmpXorKey();
86 static uptr longjmp_xor_key;
87 #endif
89 // Runtime detected VMA size.
90 uptr vmaSize;
92 enum {
93 MemTotal,
94 MemShadow,
95 MemMeta,
96 MemFile,
97 MemMmap,
98 MemHeap,
99 MemOther,
100 MemCount,
103 void FillProfileCallback(uptr p, uptr rss, bool file, uptr *mem) {
104 mem[MemTotal] += rss;
105 if (p >= ShadowBeg() && p < ShadowEnd())
106 mem[MemShadow] += rss;
107 else if (p >= MetaShadowBeg() && p < MetaShadowEnd())
108 mem[MemMeta] += rss;
109 else if ((p >= LoAppMemBeg() && p < LoAppMemEnd()) ||
110 (p >= MidAppMemBeg() && p < MidAppMemEnd()) ||
111 (p >= HiAppMemBeg() && p < HiAppMemEnd()))
112 mem[file ? MemFile : MemMmap] += rss;
113 else if (p >= HeapMemBeg() && p < HeapMemEnd())
114 mem[MemHeap] += rss;
115 else
116 mem[MemOther] += rss;
119 void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns) {
120 uptr mem[MemCount];
121 internal_memset(mem, 0, sizeof(mem));
122 GetMemoryProfile(FillProfileCallback, mem);
123 auto meta = ctx->metamap.GetMemoryStats();
124 StackDepotStats stacks = StackDepotGetStats();
125 uptr nthread, nlive;
126 ctx->thread_registry.GetNumberOfThreads(&nthread, &nlive);
127 uptr trace_mem;
129 Lock l(&ctx->slot_mtx);
130 trace_mem = ctx->trace_part_total_allocated * sizeof(TracePart);
132 uptr internal_stats[AllocatorStatCount];
133 internal_allocator()->GetStats(internal_stats);
134 // All these are allocated from the common mmap region.
135 mem[MemMmap] -= meta.mem_block + meta.sync_obj + trace_mem +
136 stacks.allocated + internal_stats[AllocatorStatMapped];
137 if (s64(mem[MemMmap]) < 0)
138 mem[MemMmap] = 0;
139 internal_snprintf(
140 buf, buf_size,
141 "==%zu== %llus [%zu]: RSS %zd MB: shadow:%zd meta:%zd file:%zd"
142 " mmap:%zd heap:%zd other:%zd intalloc:%zd memblocks:%zd syncobj:%zu"
143 " trace:%zu stacks=%zd threads=%zu/%zu\n",
144 internal_getpid(), uptime_ns / (1000 * 1000 * 1000), ctx->global_epoch,
145 mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20,
146 mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemHeap] >> 20,
147 mem[MemOther] >> 20, internal_stats[AllocatorStatMapped] >> 20,
148 meta.mem_block >> 20, meta.sync_obj >> 20, trace_mem >> 20,
149 stacks.allocated >> 20, nlive, nthread);
152 #if !SANITIZER_GO
153 // Mark shadow for .rodata sections with the special Shadow::kRodata marker.
154 // Accesses to .rodata can't race, so this saves time, memory and trace space.
155 static NOINLINE void MapRodata(char* buffer, uptr size) {
156 // First create temp file.
157 const char *tmpdir = GetEnv("TMPDIR");
158 if (tmpdir == 0)
159 tmpdir = GetEnv("TEST_TMPDIR");
160 #ifdef P_tmpdir
161 if (tmpdir == 0)
162 tmpdir = P_tmpdir;
163 #endif
164 if (tmpdir == 0)
165 return;
166 internal_snprintf(buffer, size, "%s/tsan.rodata.%d",
167 tmpdir, (int)internal_getpid());
168 uptr openrv = internal_open(buffer, O_RDWR | O_CREAT | O_EXCL, 0600);
169 if (internal_iserror(openrv))
170 return;
171 internal_unlink(buffer); // Unlink it now, so that we can reuse the buffer.
172 fd_t fd = openrv;
173 // Fill the file with Shadow::kRodata.
174 const uptr kMarkerSize = 512 * 1024 / sizeof(RawShadow);
175 InternalMmapVector<RawShadow> marker(kMarkerSize);
176 // volatile to prevent insertion of memset
177 for (volatile RawShadow *p = marker.data(); p < marker.data() + kMarkerSize;
178 p++)
179 *p = Shadow::kRodata;
180 internal_write(fd, marker.data(), marker.size() * sizeof(RawShadow));
181 // Map the file into memory.
182 uptr page = internal_mmap(0, GetPageSizeCached(), PROT_READ | PROT_WRITE,
183 MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
184 if (internal_iserror(page)) {
185 internal_close(fd);
186 return;
188 // Map the file into shadow of .rodata sections.
189 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
190 // Reusing the buffer 'buffer'.
191 MemoryMappedSegment segment(buffer, size);
192 while (proc_maps.Next(&segment)) {
193 if (segment.filename[0] != 0 && segment.filename[0] != '[' &&
194 segment.IsReadable() && segment.IsExecutable() &&
195 !segment.IsWritable() && IsAppMem(segment.start)) {
196 // Assume it's .rodata
197 char *shadow_start = (char *)MemToShadow(segment.start);
198 char *shadow_end = (char *)MemToShadow(segment.end);
199 for (char *p = shadow_start; p < shadow_end;
200 p += marker.size() * sizeof(RawShadow)) {
201 internal_mmap(
202 p, Min<uptr>(marker.size() * sizeof(RawShadow), shadow_end - p),
203 PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
207 internal_close(fd);
210 void InitializeShadowMemoryPlatform() {
211 char buffer[256]; // Keep in a different frame.
212 MapRodata(buffer, sizeof(buffer));
215 #endif // #if !SANITIZER_GO
217 void InitializePlatformEarly() {
218 vmaSize =
219 (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1);
220 #if defined(__aarch64__)
221 # if !SANITIZER_GO
222 if (vmaSize != 39 && vmaSize != 42 && vmaSize != 48) {
223 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
224 Printf("FATAL: Found %zd - Supported 39, 42 and 48\n", vmaSize);
225 Die();
227 #else
228 if (vmaSize != 48) {
229 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
230 Printf("FATAL: Found %zd - Supported 48\n", vmaSize);
231 Die();
233 #endif
234 #elif SANITIZER_LOONGARCH64
235 # if !SANITIZER_GO
236 if (vmaSize != 47) {
237 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
238 Printf("FATAL: Found %zd - Supported 47\n", vmaSize);
239 Die();
241 # endif
242 #elif defined(__powerpc64__)
243 # if !SANITIZER_GO
244 if (vmaSize != 44 && vmaSize != 46 && vmaSize != 47) {
245 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
246 Printf("FATAL: Found %zd - Supported 44, 46, and 47\n", vmaSize);
247 Die();
249 # else
250 if (vmaSize != 46 && vmaSize != 47) {
251 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
252 Printf("FATAL: Found %zd - Supported 46, and 47\n", vmaSize);
253 Die();
255 # endif
256 #elif defined(__mips64)
257 # if !SANITIZER_GO
258 if (vmaSize != 40) {
259 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
260 Printf("FATAL: Found %zd - Supported 40\n", vmaSize);
261 Die();
263 # else
264 if (vmaSize != 47) {
265 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
266 Printf("FATAL: Found %zd - Supported 47\n", vmaSize);
267 Die();
269 # endif
270 # elif SANITIZER_RISCV64
271 // the bottom half of vma is allocated for userspace
272 vmaSize = vmaSize + 1;
273 # if !SANITIZER_GO
274 if (vmaSize != 39 && vmaSize != 48) {
275 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
276 Printf("FATAL: Found %zd - Supported 39 and 48\n", vmaSize);
277 Die();
279 # endif
280 # endif
283 void InitializePlatform() {
284 DisableCoreDumperIfNecessary();
286 // Go maps shadow memory lazily and works fine with limited address space.
287 // Unlimited stack is not a problem as well, because the executable
288 // is not compiled with -pie.
289 #if !SANITIZER_GO
291 bool reexec = false;
292 // TSan doesn't play well with unlimited stack size (as stack
293 // overlaps with shadow memory). If we detect unlimited stack size,
294 // we re-exec the program with limited stack size as a best effort.
295 if (StackSizeIsUnlimited()) {
296 const uptr kMaxStackSize = 32 * 1024 * 1024;
297 VReport(1, "Program is run with unlimited stack size, which wouldn't "
298 "work with ThreadSanitizer.\n"
299 "Re-execing with stack size limited to %zd bytes.\n",
300 kMaxStackSize);
301 SetStackSizeLimitInBytes(kMaxStackSize);
302 reexec = true;
305 if (!AddressSpaceIsUnlimited()) {
306 Report("WARNING: Program is run with limited virtual address space,"
307 " which wouldn't work with ThreadSanitizer.\n");
308 Report("Re-execing with unlimited virtual address space.\n");
309 SetAddressSpaceUnlimited();
310 reexec = true;
312 #if SANITIZER_ANDROID && (defined(__aarch64__) || defined(__x86_64__))
313 // After patch "arm64: mm: support ARCH_MMAP_RND_BITS." is introduced in
314 // linux kernel, the random gap between stack and mapped area is increased
315 // from 128M to 36G on 39-bit aarch64. As it is almost impossible to cover
316 // this big range, we should disable randomized virtual space on aarch64.
317 // ASLR personality check.
318 int old_personality = personality(0xffffffff);
319 if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
320 VReport(1, "WARNING: Program is run with randomized virtual address "
321 "space, which wouldn't work with ThreadSanitizer.\n"
322 "Re-execing with fixed virtual address space.\n");
323 CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
324 reexec = true;
327 #endif
328 #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64))
329 // Initialize the xor key used in {sig}{set,long}jump.
330 InitializeLongjmpXorKey();
331 #endif
332 if (reexec)
333 ReExec();
336 CheckAndProtect();
337 InitTlsSize();
338 #endif // !SANITIZER_GO
341 #if !SANITIZER_GO
342 // Extract file descriptors passed to glibc internal __res_iclose function.
343 // This is required to properly "close" the fds, because we do not see internal
344 // closes within glibc. The code is a pure hack.
345 int ExtractResolvFDs(void *state, int *fds, int nfd) {
346 #if SANITIZER_LINUX && !SANITIZER_ANDROID
347 int cnt = 0;
348 struct __res_state *statp = (struct __res_state*)state;
349 for (int i = 0; i < MAXNS && cnt < nfd; i++) {
350 if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1)
351 fds[cnt++] = statp->_u._ext.nssocks[i];
353 return cnt;
354 #else
355 return 0;
356 #endif
359 // Extract file descriptors passed via UNIX domain sockets.
360 // This is required to properly handle "open" of these fds.
361 // see 'man recvmsg' and 'man 3 cmsg'.
362 int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) {
363 int res = 0;
364 msghdr *msg = (msghdr*)msgp;
365 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
366 for (; cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
367 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
368 continue;
369 int n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(fds[0]);
370 for (int i = 0; i < n; i++) {
371 fds[res++] = ((int*)CMSG_DATA(cmsg))[i];
372 if (res == nfd)
373 return res;
376 return res;
379 // Reverse operation of libc stack pointer mangling
380 static uptr UnmangleLongJmpSp(uptr mangled_sp) {
381 #if defined(__x86_64__)
382 # if SANITIZER_LINUX
383 // Reverse of:
384 // xor %fs:0x30, %rsi
385 // rol $0x11, %rsi
386 uptr sp;
387 asm("ror $0x11, %0 \n"
388 "xor %%fs:0x30, %0 \n"
389 : "=r" (sp)
390 : "0" (mangled_sp));
391 return sp;
392 # else
393 return mangled_sp;
394 # endif
395 #elif defined(__aarch64__)
396 # if SANITIZER_LINUX
397 return mangled_sp ^ longjmp_xor_key;
398 # else
399 return mangled_sp;
400 # endif
401 #elif defined(__loongarch_lp64)
402 return mangled_sp ^ longjmp_xor_key;
403 #elif defined(__powerpc64__)
404 // Reverse of:
405 // ld r4, -28696(r13)
406 // xor r4, r3, r4
407 uptr xor_key;
408 asm("ld %0, -28696(%%r13)" : "=r" (xor_key));
409 return mangled_sp ^ xor_key;
410 #elif defined(__mips__)
411 return mangled_sp;
412 # elif SANITIZER_RISCV64
413 return mangled_sp;
414 # elif defined(__s390x__)
415 // tcbhead_t.stack_guard
416 uptr xor_key = ((uptr *)__builtin_thread_pointer())[5];
417 return mangled_sp ^ xor_key;
418 # else
419 # error "Unknown platform"
420 # endif
423 #if SANITIZER_NETBSD
424 # ifdef __x86_64__
425 # define LONG_JMP_SP_ENV_SLOT 6
426 # else
427 # error unsupported
428 # endif
429 #elif defined(__powerpc__)
430 # define LONG_JMP_SP_ENV_SLOT 0
431 #elif SANITIZER_FREEBSD
432 # ifdef __aarch64__
433 # define LONG_JMP_SP_ENV_SLOT 1
434 # else
435 # define LONG_JMP_SP_ENV_SLOT 2
436 # endif
437 #elif SANITIZER_LINUX
438 # ifdef __aarch64__
439 # define LONG_JMP_SP_ENV_SLOT 13
440 # elif defined(__loongarch__)
441 # define LONG_JMP_SP_ENV_SLOT 1
442 # elif defined(__mips64)
443 # define LONG_JMP_SP_ENV_SLOT 1
444 # elif SANITIZER_RISCV64
445 # define LONG_JMP_SP_ENV_SLOT 13
446 # elif defined(__s390x__)
447 # define LONG_JMP_SP_ENV_SLOT 9
448 # else
449 # define LONG_JMP_SP_ENV_SLOT 6
450 # endif
451 #endif
453 uptr ExtractLongJmpSp(uptr *env) {
454 uptr mangled_sp = env[LONG_JMP_SP_ENV_SLOT];
455 return UnmangleLongJmpSp(mangled_sp);
458 #if INIT_LONGJMP_XOR_KEY
459 // GLIBC mangles the function pointers in jmp_buf (used in {set,long}*jmp
460 // functions) by XORing them with a random key. For AArch64 it is a global
461 // variable rather than a TCB one (as for x86_64/powerpc). We obtain the key by
462 // issuing a setjmp and XORing the SP pointer values to derive the key.
463 static void InitializeLongjmpXorKey() {
464 // 1. Call REAL(setjmp), which stores the mangled SP in env.
465 jmp_buf env;
466 REAL(_setjmp)(env);
468 // 2. Retrieve vanilla/mangled SP.
469 uptr sp;
470 #ifdef __loongarch__
471 asm("move %0, $sp" : "=r" (sp));
472 #else
473 asm("mov %0, sp" : "=r" (sp));
474 #endif
475 uptr mangled_sp = ((uptr *)&env)[LONG_JMP_SP_ENV_SLOT];
477 // 3. xor SPs to obtain key.
478 longjmp_xor_key = mangled_sp ^ sp;
480 #endif
482 extern "C" void __tsan_tls_initialization() {}
484 void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size) {
485 // Check that the thr object is in tls;
486 const uptr thr_beg = (uptr)thr;
487 const uptr thr_end = (uptr)thr + sizeof(*thr);
488 CHECK_GE(thr_beg, tls_addr);
489 CHECK_LE(thr_beg, tls_addr + tls_size);
490 CHECK_GE(thr_end, tls_addr);
491 CHECK_LE(thr_end, tls_addr + tls_size);
492 // Since the thr object is huge, skip it.
493 const uptr pc = StackTrace::GetNextInstructionPc(
494 reinterpret_cast<uptr>(__tsan_tls_initialization));
495 MemoryRangeImitateWrite(thr, pc, tls_addr, thr_beg - tls_addr);
496 MemoryRangeImitateWrite(thr, pc, thr_end, tls_addr + tls_size - thr_end);
499 // Note: this function runs with async signals enabled,
500 // so it must not touch any tsan state.
501 int call_pthread_cancel_with_cleanup(int (*fn)(void *arg),
502 void (*cleanup)(void *arg), void *arg) {
503 // pthread_cleanup_push/pop are hardcore macros mess.
504 // We can't intercept nor call them w/o including pthread.h.
505 int res;
506 pthread_cleanup_push(cleanup, arg);
507 res = fn(arg);
508 pthread_cleanup_pop(0);
509 return res;
511 #endif // !SANITIZER_GO
513 #if !SANITIZER_GO
514 void ReplaceSystemMalloc() { }
515 #endif
517 #if !SANITIZER_GO
518 #if SANITIZER_ANDROID
519 // On Android, one thread can call intercepted functions after
520 // DestroyThreadState(), so add a fake thread state for "dead" threads.
521 static ThreadState *dead_thread_state = nullptr;
523 ThreadState *cur_thread() {
524 ThreadState* thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
525 if (thr == nullptr) {
526 __sanitizer_sigset_t emptyset;
527 internal_sigfillset(&emptyset);
528 __sanitizer_sigset_t oldset;
529 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &emptyset, &oldset));
530 thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
531 if (thr == nullptr) {
532 thr = reinterpret_cast<ThreadState*>(MmapOrDie(sizeof(ThreadState),
533 "ThreadState"));
534 *get_android_tls_ptr() = reinterpret_cast<uptr>(thr);
535 if (dead_thread_state == nullptr) {
536 dead_thread_state = reinterpret_cast<ThreadState*>(
537 MmapOrDie(sizeof(ThreadState), "ThreadState"));
538 dead_thread_state->fast_state.SetIgnoreBit();
539 dead_thread_state->ignore_interceptors = 1;
540 dead_thread_state->is_dead = true;
541 *const_cast<u32*>(&dead_thread_state->tid) = -1;
542 CHECK_EQ(0, internal_mprotect(dead_thread_state, sizeof(ThreadState),
543 PROT_READ));
546 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr));
548 return thr;
551 void set_cur_thread(ThreadState *thr) {
552 *get_android_tls_ptr() = reinterpret_cast<uptr>(thr);
555 void cur_thread_finalize() {
556 __sanitizer_sigset_t emptyset;
557 internal_sigfillset(&emptyset);
558 __sanitizer_sigset_t oldset;
559 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &emptyset, &oldset));
560 ThreadState* thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
561 if (thr != dead_thread_state) {
562 *get_android_tls_ptr() = reinterpret_cast<uptr>(dead_thread_state);
563 UnmapOrDie(thr, sizeof(ThreadState));
565 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr));
567 #endif // SANITIZER_ANDROID
568 #endif // if !SANITIZER_GO
570 } // namespace __tsan
572 #endif // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD