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.
10 //
11 // Linux- and BSD-specific code.
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD
16
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"
29
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
55
56 #ifdef sa_handler
57 # undef sa_handler
58 #endif
59
60 #ifdef sa_sigaction
61 # undef sa_sigaction
62 #endif
63
64 #if SANITIZER_FREEBSD
65 extern "C" void *__libc_stack_end;
66 void *__libc_stack_end = 0;
67 #endif
68
69 #if SANITIZER_LINUX && defined(__aarch64__) && !SANITIZER_GO
70 # define INIT_LONGJMP_XOR_KEY 1
71 #else
72 # define INIT_LONGJMP_XOR_KEY 0
73 #endif
74
75 #if INIT_LONGJMP_XOR_KEY
76 #include "interception/interception.h"
77 // Must be declared outside of other namespaces.
78 DECLARE_REAL(int, _setjmp, void *env)
79 #endif
80
81 namespace __tsan {
82
83 #if INIT_LONGJMP_XOR_KEY
84 static void InitializeLongjmpXorKey();
85 static uptr longjmp_xor_key;
86 #endif
87
88 // Runtime detected VMA size.
89 uptr vmaSize;
90
91 enum {
92 MemTotal,
93 MemShadow,
94 MemMeta,
95 MemFile,
96 MemMmap,
97 MemHeap,
98 MemOther,
99 MemCount,
100 };
101
FillProfileCallback(uptr p,uptr rss,bool file,uptr * mem)102 void FillProfileCallback(uptr p, uptr rss, bool file, uptr *mem) {
103 mem[MemTotal] += rss;
104 if (p >= ShadowBeg() && p < ShadowEnd())
105 mem[MemShadow] += rss;
106 else if (p >= MetaShadowBeg() && p < MetaShadowEnd())
107 mem[MemMeta] += rss;
108 else if ((p >= LoAppMemBeg() && p < LoAppMemEnd()) ||
109 (p >= MidAppMemBeg() && p < MidAppMemEnd()) ||
110 (p >= HiAppMemBeg() && p < HiAppMemEnd()))
111 mem[file ? MemFile : MemMmap] += rss;
112 else if (p >= HeapMemBeg() && p < HeapMemEnd())
113 mem[MemHeap] += rss;
114 else
115 mem[MemOther] += rss;
116 }
117
WriteMemoryProfile(char * buf,uptr buf_size,u64 uptime_ns)118 void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns) {
119 uptr mem[MemCount];
120 internal_memset(mem, 0, sizeof(mem));
121 GetMemoryProfile(FillProfileCallback, mem);
122 auto meta = ctx->metamap.GetMemoryStats();
123 StackDepotStats stacks = StackDepotGetStats();
124 uptr nthread, nlive;
125 ctx->thread_registry.GetNumberOfThreads(&nthread, &nlive);
126 uptr trace_mem;
127 {
128 Lock l(&ctx->slot_mtx);
129 trace_mem = ctx->trace_part_total_allocated * sizeof(TracePart);
130 }
131 uptr internal_stats[AllocatorStatCount];
132 internal_allocator()->GetStats(internal_stats);
133 // All these are allocated from the common mmap region.
134 mem[MemMmap] -= meta.mem_block + meta.sync_obj + trace_mem +
135 stacks.allocated + internal_stats[AllocatorStatMapped];
136 if (s64(mem[MemMmap]) < 0)
137 mem[MemMmap] = 0;
138 internal_snprintf(
139 buf, buf_size,
140 "==%zu== %llus [%zu]: RSS %zd MB: shadow:%zd meta:%zd file:%zd"
141 " mmap:%zd heap:%zd other:%zd intalloc:%zd memblocks:%zd syncobj:%zu"
142 " trace:%zu stacks=%zd threads=%zu/%zu\n",
143 internal_getpid(), uptime_ns / (1000 * 1000 * 1000), ctx->global_epoch,
144 mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20,
145 mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemHeap] >> 20,
146 mem[MemOther] >> 20, internal_stats[AllocatorStatMapped] >> 20,
147 meta.mem_block >> 20, meta.sync_obj >> 20, trace_mem >> 20,
148 stacks.allocated >> 20, nlive, nthread);
149 }
150
151 #if !SANITIZER_GO
152 // Mark shadow for .rodata sections with the special Shadow::kRodata marker.
153 // Accesses to .rodata can't race, so this saves time, memory and trace space.
MapRodata()154 static void MapRodata() {
155 // First create temp file.
156 const char *tmpdir = GetEnv("TMPDIR");
157 if (tmpdir == 0)
158 tmpdir = GetEnv("TEST_TMPDIR");
159 #ifdef P_tmpdir
160 if (tmpdir == 0)
161 tmpdir = P_tmpdir;
162 #endif
163 if (tmpdir == 0)
164 return;
165 char name[256];
166 internal_snprintf(name, sizeof(name), "%s/tsan.rodata.%d",
167 tmpdir, (int)internal_getpid());
168 uptr openrv = internal_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
169 if (internal_iserror(openrv))
170 return;
171 internal_unlink(name); // 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;
187 }
188 // Map the file into shadow of .rodata sections.
189 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
190 // Reusing the buffer 'name'.
191 MemoryMappedSegment segment(name, ARRAY_SIZE(name));
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);
204 }
205 }
206 }
207 internal_close(fd);
208 }
209
InitializeShadowMemoryPlatform()210 void InitializeShadowMemoryPlatform() {
211 MapRodata();
212 }
213
214 #endif // #if !SANITIZER_GO
215
InitializePlatformEarly()216 void InitializePlatformEarly() {
217 vmaSize =
218 (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1);
219 #if defined(__aarch64__)
220 # if !SANITIZER_GO
221 if (vmaSize != 39 && vmaSize != 42 && vmaSize != 48) {
222 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
223 Printf("FATAL: Found %zd - Supported 39, 42 and 48\n", vmaSize);
224 Die();
225 }
226 #else
227 if (vmaSize != 48) {
228 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
229 Printf("FATAL: Found %zd - Supported 48\n", vmaSize);
230 Die();
231 }
232 #endif
233 #elif SANITIZER_LOONGARCH64
234 # if !SANITIZER_GO
235 if (vmaSize != 47) {
236 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
237 Printf("FATAL: Found %zd - Supported 47\n", vmaSize);
238 Die();
239 }
240 # endif
241 #elif defined(__powerpc64__)
242 # if !SANITIZER_GO
243 if (vmaSize != 44 && vmaSize != 46 && vmaSize != 47) {
244 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
245 Printf("FATAL: Found %zd - Supported 44, 46, and 47\n", vmaSize);
246 Die();
247 }
248 # else
249 if (vmaSize != 46 && vmaSize != 47) {
250 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
251 Printf("FATAL: Found %zd - Supported 46, and 47\n", vmaSize);
252 Die();
253 }
254 # endif
255 #elif defined(__mips64)
256 # if !SANITIZER_GO
257 if (vmaSize != 40) {
258 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
259 Printf("FATAL: Found %zd - Supported 40\n", vmaSize);
260 Die();
261 }
262 # else
263 if (vmaSize != 47) {
264 Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
265 Printf("FATAL: Found %zd - Supported 47\n", vmaSize);
266 Die();
267 }
268 # endif
269 #endif
270 }
271
InitializePlatform()272 void InitializePlatform() {
273 DisableCoreDumperIfNecessary();
274
275 // Go maps shadow memory lazily and works fine with limited address space.
276 // Unlimited stack is not a problem as well, because the executable
277 // is not compiled with -pie.
278 #if !SANITIZER_GO
279 {
280 bool reexec = false;
281 // TSan doesn't play well with unlimited stack size (as stack
282 // overlaps with shadow memory). If we detect unlimited stack size,
283 // we re-exec the program with limited stack size as a best effort.
284 if (StackSizeIsUnlimited()) {
285 const uptr kMaxStackSize = 32 * 1024 * 1024;
286 VReport(1, "Program is run with unlimited stack size, which wouldn't "
287 "work with ThreadSanitizer.\n"
288 "Re-execing with stack size limited to %zd bytes.\n",
289 kMaxStackSize);
290 SetStackSizeLimitInBytes(kMaxStackSize);
291 reexec = true;
292 }
293
294 if (!AddressSpaceIsUnlimited()) {
295 Report("WARNING: Program is run with limited virtual address space,"
296 " which wouldn't work with ThreadSanitizer.\n");
297 Report("Re-execing with unlimited virtual address space.\n");
298 SetAddressSpaceUnlimited();
299 reexec = true;
300 }
301 #if SANITIZER_ANDROID && (defined(__aarch64__) || defined(__x86_64__))
302 // After patch "arm64: mm: support ARCH_MMAP_RND_BITS." is introduced in
303 // linux kernel, the random gap between stack and mapped area is increased
304 // from 128M to 36G on 39-bit aarch64. As it is almost impossible to cover
305 // this big range, we should disable randomized virtual space on aarch64.
306 // ASLR personality check.
307 int old_personality = personality(0xffffffff);
308 if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
309 VReport(1, "WARNING: Program is run with randomized virtual address "
310 "space, which wouldn't work with ThreadSanitizer.\n"
311 "Re-execing with fixed virtual address space.\n");
312 CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
313 reexec = true;
314 }
315
316 #endif
317 #if SANITIZER_LINUX && defined(__aarch64__)
318 // Initialize the xor key used in {sig}{set,long}jump.
319 InitializeLongjmpXorKey();
320 #endif
321 if (reexec)
322 ReExec();
323 }
324
325 CheckAndProtect();
326 InitTlsSize();
327 #endif // !SANITIZER_GO
328 }
329
330 #if !SANITIZER_GO
331 // Extract file descriptors passed to glibc internal __res_iclose function.
332 // This is required to properly "close" the fds, because we do not see internal
333 // closes within glibc. The code is a pure hack.
ExtractResolvFDs(void * state,int * fds,int nfd)334 int ExtractResolvFDs(void *state, int *fds, int nfd) {
335 #if SANITIZER_LINUX && !SANITIZER_ANDROID
336 int cnt = 0;
337 struct __res_state *statp = (struct __res_state*)state;
338 for (int i = 0; i < MAXNS && cnt < nfd; i++) {
339 if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1)
340 fds[cnt++] = statp->_u._ext.nssocks[i];
341 }
342 return cnt;
343 #else
344 return 0;
345 #endif
346 }
347
348 // Extract file descriptors passed via UNIX domain sockets.
349 // This is required to properly handle "open" of these fds.
350 // see 'man recvmsg' and 'man 3 cmsg'.
ExtractRecvmsgFDs(void * msgp,int * fds,int nfd)351 int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) {
352 int res = 0;
353 msghdr *msg = (msghdr*)msgp;
354 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
355 for (; cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
356 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
357 continue;
358 int n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(fds[0]);
359 for (int i = 0; i < n; i++) {
360 fds[res++] = ((int*)CMSG_DATA(cmsg))[i];
361 if (res == nfd)
362 return res;
363 }
364 }
365 return res;
366 }
367
368 // Reverse operation of libc stack pointer mangling
UnmangleLongJmpSp(uptr mangled_sp)369 static uptr UnmangleLongJmpSp(uptr mangled_sp) {
370 #if defined(__x86_64__)
371 # if SANITIZER_LINUX
372 // Reverse of:
373 // xor %fs:0x30, %rsi
374 // rol $0x11, %rsi
375 uptr sp;
376 asm("ror $0x11, %0 \n"
377 "xor %%fs:0x30, %0 \n"
378 : "=r" (sp)
379 : "0" (mangled_sp));
380 return sp;
381 # else
382 return mangled_sp;
383 # endif
384 #elif defined(__aarch64__)
385 # if SANITIZER_LINUX
386 return mangled_sp ^ longjmp_xor_key;
387 # else
388 return mangled_sp;
389 # endif
390 #elif defined(__loongarch__)
391 return mangled_sp;
392 #elif defined(__powerpc64__)
393 // Reverse of:
394 // ld r4, -28696(r13)
395 // xor r4, r3, r4
396 uptr xor_key;
397 asm("ld %0, -28696(%%r13)" : "=r" (xor_key));
398 return mangled_sp ^ xor_key;
399 #elif defined(__mips__)
400 return mangled_sp;
401 #elif defined(__s390x__)
402 // tcbhead_t.stack_guard
403 uptr xor_key = ((uptr *)__builtin_thread_pointer())[5];
404 return mangled_sp ^ xor_key;
405 #else
406 #error "Unknown platform"
407 #endif
408 }
409
410 #if SANITIZER_NETBSD
411 # ifdef __x86_64__
412 # define LONG_JMP_SP_ENV_SLOT 6
413 # else
414 # error unsupported
415 # endif
416 #elif defined(__powerpc__)
417 # define LONG_JMP_SP_ENV_SLOT 0
418 #elif SANITIZER_FREEBSD
419 # ifdef __aarch64__
420 # define LONG_JMP_SP_ENV_SLOT 1
421 # else
422 # define LONG_JMP_SP_ENV_SLOT 2
423 # endif
424 #elif SANITIZER_LINUX
425 # ifdef __aarch64__
426 # define LONG_JMP_SP_ENV_SLOT 13
427 # elif defined(__loongarch__)
428 # define LONG_JMP_SP_ENV_SLOT 1
429 # elif defined(__mips64)
430 # define LONG_JMP_SP_ENV_SLOT 1
431 # elif defined(__s390x__)
432 # define LONG_JMP_SP_ENV_SLOT 9
433 # else
434 # define LONG_JMP_SP_ENV_SLOT 6
435 # endif
436 #endif
437
ExtractLongJmpSp(uptr * env)438 uptr ExtractLongJmpSp(uptr *env) {
439 uptr mangled_sp = env[LONG_JMP_SP_ENV_SLOT];
440 return UnmangleLongJmpSp(mangled_sp);
441 }
442
443 #if INIT_LONGJMP_XOR_KEY
444 // GLIBC mangles the function pointers in jmp_buf (used in {set,long}*jmp
445 // functions) by XORing them with a random key. For AArch64 it is a global
446 // variable rather than a TCB one (as for x86_64/powerpc). We obtain the key by
447 // issuing a setjmp and XORing the SP pointer values to derive the key.
InitializeLongjmpXorKey()448 static void InitializeLongjmpXorKey() {
449 // 1. Call REAL(setjmp), which stores the mangled SP in env.
450 jmp_buf env;
451 REAL(_setjmp)(env);
452
453 // 2. Retrieve vanilla/mangled SP.
454 uptr sp;
455 asm("mov %0, sp" : "=r" (sp));
456 uptr mangled_sp = ((uptr *)&env)[LONG_JMP_SP_ENV_SLOT];
457
458 // 3. xor SPs to obtain key.
459 longjmp_xor_key = mangled_sp ^ sp;
460 }
461 #endif
462
__tsan_tls_initialization()463 extern "C" void __tsan_tls_initialization() {}
464
ImitateTlsWrite(ThreadState * thr,uptr tls_addr,uptr tls_size)465 void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size) {
466 // Check that the thr object is in tls;
467 const uptr thr_beg = (uptr)thr;
468 const uptr thr_end = (uptr)thr + sizeof(*thr);
469 CHECK_GE(thr_beg, tls_addr);
470 CHECK_LE(thr_beg, tls_addr + tls_size);
471 CHECK_GE(thr_end, tls_addr);
472 CHECK_LE(thr_end, tls_addr + tls_size);
473 // Since the thr object is huge, skip it.
474 const uptr pc = StackTrace::GetNextInstructionPc(
475 reinterpret_cast<uptr>(__tsan_tls_initialization));
476 MemoryRangeImitateWrite(thr, pc, tls_addr, thr_beg - tls_addr);
477 MemoryRangeImitateWrite(thr, pc, thr_end, tls_addr + tls_size - thr_end);
478 }
479
480 // Note: this function runs with async signals enabled,
481 // so it must not touch any tsan state.
call_pthread_cancel_with_cleanup(int (* fn)(void * arg),void (* cleanup)(void * arg),void * arg)482 int call_pthread_cancel_with_cleanup(int (*fn)(void *arg),
483 void (*cleanup)(void *arg), void *arg) {
484 // pthread_cleanup_push/pop are hardcore macros mess.
485 // We can't intercept nor call them w/o including pthread.h.
486 int res;
487 pthread_cleanup_push(cleanup, arg);
488 res = fn(arg);
489 pthread_cleanup_pop(0);
490 return res;
491 }
492 #endif // !SANITIZER_GO
493
494 #if !SANITIZER_GO
ReplaceSystemMalloc()495 void ReplaceSystemMalloc() { }
496 #endif
497
498 #if !SANITIZER_GO
499 #if SANITIZER_ANDROID
500 // On Android, one thread can call intercepted functions after
501 // DestroyThreadState(), so add a fake thread state for "dead" threads.
502 static ThreadState *dead_thread_state = nullptr;
503
cur_thread()504 ThreadState *cur_thread() {
505 ThreadState* thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
506 if (thr == nullptr) {
507 __sanitizer_sigset_t emptyset;
508 internal_sigfillset(&emptyset);
509 __sanitizer_sigset_t oldset;
510 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &emptyset, &oldset));
511 thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
512 if (thr == nullptr) {
513 thr = reinterpret_cast<ThreadState*>(MmapOrDie(sizeof(ThreadState),
514 "ThreadState"));
515 *get_android_tls_ptr() = reinterpret_cast<uptr>(thr);
516 if (dead_thread_state == nullptr) {
517 dead_thread_state = reinterpret_cast<ThreadState*>(
518 MmapOrDie(sizeof(ThreadState), "ThreadState"));
519 dead_thread_state->fast_state.SetIgnoreBit();
520 dead_thread_state->ignore_interceptors = 1;
521 dead_thread_state->is_dead = true;
522 *const_cast<u32*>(&dead_thread_state->tid) = -1;
523 CHECK_EQ(0, internal_mprotect(dead_thread_state, sizeof(ThreadState),
524 PROT_READ));
525 }
526 }
527 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr));
528 }
529 return thr;
530 }
531
set_cur_thread(ThreadState * thr)532 void set_cur_thread(ThreadState *thr) {
533 *get_android_tls_ptr() = reinterpret_cast<uptr>(thr);
534 }
535
cur_thread_finalize()536 void cur_thread_finalize() {
537 __sanitizer_sigset_t emptyset;
538 internal_sigfillset(&emptyset);
539 __sanitizer_sigset_t oldset;
540 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &emptyset, &oldset));
541 ThreadState* thr = reinterpret_cast<ThreadState*>(*get_android_tls_ptr());
542 if (thr != dead_thread_state) {
543 *get_android_tls_ptr() = reinterpret_cast<uptr>(dead_thread_state);
544 UnmapOrDie(thr, sizeof(ThreadState));
545 }
546 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr));
547 }
548 #endif // SANITIZER_ANDROID
549 #endif // if !SANITIZER_GO
550
551 } // namespace __tsan
552
553 #endif // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD
554