xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_platform_posix.cpp (revision 2eb4d8dc723da3cf7d735a3226ae49da4c8c5dbc)
1 //===-- tsan_platform_posix.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 // POSIX-specific code.
12 //===----------------------------------------------------------------------===//
13 
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if SANITIZER_POSIX
16 
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_errno.h"
19 #include "sanitizer_common/sanitizer_libc.h"
20 #include "sanitizer_common/sanitizer_procmaps.h"
21 #include "tsan_platform.h"
22 #include "tsan_rtl.h"
23 
24 namespace __tsan {
25 
26 static const char kShadowMemoryMappingWarning[] =
27     "FATAL: %s can not madvise shadow region [%zx, %zx] with %s (errno: %d)\n";
28 static const char kShadowMemoryMappingHint[] =
29     "HINT: if %s is not supported in your environment, you may set "
30     "TSAN_OPTIONS=%s=0\n";
31 
32 static void DontDumpShadow(uptr addr, uptr size) {
33   if (common_flags()->use_madv_dontdump)
34     if (!DontDumpShadowMemory(addr, size)) {
35       Printf(kShadowMemoryMappingWarning, SanitizerToolName, addr, addr + size,
36              "MADV_DONTDUMP", errno);
37       Printf(kShadowMemoryMappingHint, "MADV_DONTDUMP", "use_madv_dontdump");
38       Die();
39     }
40 }
41 
42 #if !SANITIZER_GO
43 void InitializeShadowMemory() {
44   // Map memory shadow.
45   if (!MmapFixedSuperNoReserve(ShadowBeg(), ShadowEnd() - ShadowBeg(),
46                                "shadow")) {
47     Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
48     Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
49     Die();
50   }
51   // This memory range is used for thread stacks and large user mmaps.
52   // Frequently a thread uses only a small part of stack and similarly
53   // a program uses a small part of large mmap. On some programs
54   // we see 20% memory usage reduction without huge pages for this range.
55   DontDumpShadow(ShadowBeg(), ShadowEnd() - ShadowBeg());
56   DPrintf("memory shadow: %zx-%zx (%zuGB)\n",
57       ShadowBeg(), ShadowEnd(),
58       (ShadowEnd() - ShadowBeg()) >> 30);
59 
60   // Map meta shadow.
61   const uptr meta = MetaShadowBeg();
62   const uptr meta_size = MetaShadowEnd() - meta;
63   if (!MmapFixedSuperNoReserve(meta, meta_size, "meta shadow")) {
64     Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
65     Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
66     Die();
67   }
68   DontDumpShadow(meta, meta_size);
69   DPrintf("meta shadow: %zx-%zx (%zuGB)\n",
70       meta, meta + meta_size, meta_size >> 30);
71 
72   InitializeShadowMemoryPlatform();
73 }
74 
75 static void ProtectRange(uptr beg, uptr end) {
76   CHECK_LE(beg, end);
77   if (beg == end)
78     return;
79   if (beg != (uptr)MmapFixedNoAccess(beg, end - beg)) {
80     Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
81     Printf("FATAL: Make sure you are not using unlimited stack\n");
82     Die();
83   }
84 }
85 
86 void CheckAndProtect() {
87   // Ensure that the binary is indeed compiled with -pie.
88   MemoryMappingLayout proc_maps(true);
89   MemoryMappedSegment segment;
90   while (proc_maps.Next(&segment)) {
91     if (IsAppMem(segment.start)) continue;
92     if (segment.start >= HeapMemEnd() && segment.start < HeapEnd()) continue;
93     if (segment.protection == 0)  // Zero page or mprotected.
94       continue;
95     if (segment.start >= VdsoBeg())  // vdso
96       break;
97     Printf("FATAL: ThreadSanitizer: unexpected memory mapping %p-%p\n",
98            segment.start, segment.end);
99     Die();
100   }
101 
102 #if defined(__aarch64__) && defined(__APPLE__)
103   ProtectRange(HeapMemEnd(), ShadowBeg());
104   ProtectRange(ShadowEnd(), MetaShadowBeg());
105   ProtectRange(MetaShadowEnd(), TraceMemBeg());
106 #else
107   ProtectRange(LoAppMemEnd(), ShadowBeg());
108   ProtectRange(ShadowEnd(), MetaShadowBeg());
109 #ifdef TSAN_MID_APP_RANGE
110   ProtectRange(MetaShadowEnd(), MidAppMemBeg());
111   ProtectRange(MidAppMemEnd(), TraceMemBeg());
112 #else
113   ProtectRange(MetaShadowEnd(), TraceMemBeg());
114 #endif
115   // Memory for traces is mapped lazily in MapThreadTrace.
116   // Protect the whole range for now, so that user does not map something here.
117   ProtectRange(TraceMemBeg(), TraceMemEnd());
118   ProtectRange(TraceMemEnd(), HeapMemBeg());
119   ProtectRange(HeapEnd(), HiAppMemBeg());
120 #endif
121 }
122 #endif
123 
124 }  // namespace __tsan
125 
126 #endif  // SANITIZER_POSIX
127