xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_platform_posix.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
168d75effSDimitry Andric //===-- tsan_platform_posix.cpp -------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of ThreadSanitizer (TSan), a race detector.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric // POSIX-specific code.
1268d75effSDimitry Andric //===----------------------------------------------------------------------===//
1368d75effSDimitry Andric 
1468d75effSDimitry Andric #include "sanitizer_common/sanitizer_platform.h"
1568d75effSDimitry Andric #if SANITIZER_POSIX
1668d75effSDimitry Andric 
17349cc55cSDimitry Andric #  include <dlfcn.h>
18349cc55cSDimitry Andric 
1968d75effSDimitry Andric #  include "sanitizer_common/sanitizer_common.h"
2068d75effSDimitry Andric #  include "sanitizer_common/sanitizer_errno.h"
2168d75effSDimitry Andric #  include "sanitizer_common/sanitizer_libc.h"
2268d75effSDimitry Andric #  include "sanitizer_common/sanitizer_procmaps.h"
2368d75effSDimitry Andric #  include "tsan_platform.h"
2468d75effSDimitry Andric #  include "tsan_rtl.h"
2568d75effSDimitry Andric 
2668d75effSDimitry Andric namespace __tsan {
2768d75effSDimitry Andric 
2868d75effSDimitry Andric static const char kShadowMemoryMappingWarning[] =
2968d75effSDimitry Andric     "FATAL: %s can not madvise shadow region [%zx, %zx] with %s (errno: %d)\n";
3068d75effSDimitry Andric static const char kShadowMemoryMappingHint[] =
3168d75effSDimitry Andric     "HINT: if %s is not supported in your environment, you may set "
3268d75effSDimitry Andric     "TSAN_OPTIONS=%s=0\n";
3368d75effSDimitry Andric 
34349cc55cSDimitry Andric #  if !SANITIZER_GO
DontDumpShadow(uptr addr,uptr size)35bdd1243dSDimitry Andric void DontDumpShadow(uptr addr, uptr size) {
3668d75effSDimitry Andric   if (common_flags()->use_madv_dontdump)
3768d75effSDimitry Andric     if (!DontDumpShadowMemory(addr, size)) {
3868d75effSDimitry Andric       Printf(kShadowMemoryMappingWarning, SanitizerToolName, addr, addr + size,
3968d75effSDimitry Andric              "MADV_DONTDUMP", errno);
4068d75effSDimitry Andric       Printf(kShadowMemoryMappingHint, "MADV_DONTDUMP", "use_madv_dontdump");
4168d75effSDimitry Andric       Die();
4268d75effSDimitry Andric     }
4368d75effSDimitry Andric }
4468d75effSDimitry Andric 
InitializeShadowMemory()4568d75effSDimitry Andric void InitializeShadowMemory() {
4668d75effSDimitry Andric   // Map memory shadow.
47e8d8bef9SDimitry Andric   if (!MmapFixedSuperNoReserve(ShadowBeg(), ShadowEnd() - ShadowBeg(),
48e8d8bef9SDimitry Andric                                "shadow")) {
4968d75effSDimitry Andric     Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
5068d75effSDimitry Andric     Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
5168d75effSDimitry Andric     Die();
5268d75effSDimitry Andric   }
5368d75effSDimitry Andric   // This memory range is used for thread stacks and large user mmaps.
5468d75effSDimitry Andric   // Frequently a thread uses only a small part of stack and similarly
5568d75effSDimitry Andric   // a program uses a small part of large mmap. On some programs
5668d75effSDimitry Andric   // we see 20% memory usage reduction without huge pages for this range.
5768d75effSDimitry Andric   DontDumpShadow(ShadowBeg(), ShadowEnd() - ShadowBeg());
5868d75effSDimitry Andric   DPrintf("memory shadow: %zx-%zx (%zuGB)\n",
5968d75effSDimitry Andric       ShadowBeg(), ShadowEnd(),
6068d75effSDimitry Andric       (ShadowEnd() - ShadowBeg()) >> 30);
6168d75effSDimitry Andric 
6268d75effSDimitry Andric   // Map meta shadow.
6368d75effSDimitry Andric   const uptr meta = MetaShadowBeg();
6468d75effSDimitry Andric   const uptr meta_size = MetaShadowEnd() - meta;
65e8d8bef9SDimitry Andric   if (!MmapFixedSuperNoReserve(meta, meta_size, "meta shadow")) {
6668d75effSDimitry Andric     Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
6768d75effSDimitry Andric     Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
6868d75effSDimitry Andric     Die();
6968d75effSDimitry Andric   }
7068d75effSDimitry Andric   DontDumpShadow(meta, meta_size);
7168d75effSDimitry Andric   DPrintf("meta shadow: %zx-%zx (%zuGB)\n",
7268d75effSDimitry Andric       meta, meta + meta_size, meta_size >> 30);
7368d75effSDimitry Andric 
7468d75effSDimitry Andric   InitializeShadowMemoryPlatform();
75349cc55cSDimitry Andric 
76349cc55cSDimitry Andric   on_initialize = reinterpret_cast<void (*)(void)>(
77349cc55cSDimitry Andric       dlsym(RTLD_DEFAULT, "__tsan_on_initialize"));
78349cc55cSDimitry Andric   on_finalize =
79349cc55cSDimitry Andric       reinterpret_cast<int (*)(int)>(dlsym(RTLD_DEFAULT, "__tsan_on_finalize"));
8068d75effSDimitry Andric }
8168d75effSDimitry Andric 
TryProtectRange(uptr beg,uptr end)82fe6060f1SDimitry Andric static bool TryProtectRange(uptr beg, uptr end) {
8368d75effSDimitry Andric   CHECK_LE(beg, end);
8468d75effSDimitry Andric   if (beg == end)
85fe6060f1SDimitry Andric     return true;
86fe6060f1SDimitry Andric   return beg == (uptr)MmapFixedNoAccess(beg, end - beg);
87fe6060f1SDimitry Andric }
88fe6060f1SDimitry Andric 
ProtectRange(uptr beg,uptr end)89fe6060f1SDimitry Andric static void ProtectRange(uptr beg, uptr end) {
90fe6060f1SDimitry Andric   if (!TryProtectRange(beg, end)) {
9168d75effSDimitry Andric     Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
9268d75effSDimitry Andric     Printf("FATAL: Make sure you are not using unlimited stack\n");
9368d75effSDimitry Andric     Die();
9468d75effSDimitry Andric   }
9568d75effSDimitry Andric }
9668d75effSDimitry Andric 
97*7a6dacacSDimitry Andric // CheckAndProtect will check if the memory layout is compatible with TSan.
98*7a6dacacSDimitry Andric // Optionally (if 'protect' is true), it will set the memory regions between
99*7a6dacacSDimitry Andric // app memory to be inaccessible.
100*7a6dacacSDimitry Andric // 'ignore_heap' means it will not consider heap memory allocations to be a
101*7a6dacacSDimitry Andric // conflict. Set this based on whether we are calling CheckAndProtect before
102*7a6dacacSDimitry Andric // or after the allocator has initialized the heap.
CheckAndProtect(bool protect,bool ignore_heap,bool print_warnings)103*7a6dacacSDimitry Andric bool CheckAndProtect(bool protect, bool ignore_heap, bool print_warnings) {
10468d75effSDimitry Andric   // Ensure that the binary is indeed compiled with -pie.
10568d75effSDimitry Andric   MemoryMappingLayout proc_maps(true);
10668d75effSDimitry Andric   MemoryMappedSegment segment;
10768d75effSDimitry Andric   while (proc_maps.Next(&segment)) {
108*7a6dacacSDimitry Andric     if (segment.start >= HeapMemBeg() && segment.end <= HeapEnd()) {
109*7a6dacacSDimitry Andric       if (ignore_heap) {
110*7a6dacacSDimitry Andric         continue;
111*7a6dacacSDimitry Andric       } else {
112*7a6dacacSDimitry Andric         return false;
113*7a6dacacSDimitry Andric       }
114*7a6dacacSDimitry Andric     }
115*7a6dacacSDimitry Andric 
116*7a6dacacSDimitry Andric     // Note: IsAppMem includes if it is heap memory, hence we must
117*7a6dacacSDimitry Andric     // put this check after the heap bounds check.
118*7a6dacacSDimitry Andric     if (IsAppMem(segment.start) && IsAppMem(segment.end - 1))
119*7a6dacacSDimitry Andric       continue;
120*7a6dacacSDimitry Andric 
121*7a6dacacSDimitry Andric     // Guard page after the heap end
12268d75effSDimitry Andric     if (segment.start >= HeapMemEnd() && segment.start < HeapEnd()) continue;
123*7a6dacacSDimitry Andric 
12468d75effSDimitry Andric     if (segment.protection == 0)  // Zero page or mprotected.
12568d75effSDimitry Andric       continue;
126*7a6dacacSDimitry Andric 
12768d75effSDimitry Andric     if (segment.start >= VdsoBeg())  // vdso
12868d75effSDimitry Andric       break;
129*7a6dacacSDimitry Andric 
130*7a6dacacSDimitry Andric     // Debug output can break tests. Suppress this message in most cases.
131*7a6dacacSDimitry Andric     if (print_warnings)
132*7a6dacacSDimitry Andric       Printf(
133*7a6dacacSDimitry Andric           "WARNING: ThreadSanitizer: unexpected memory mapping 0x%zx-0x%zx\n",
13468d75effSDimitry Andric           segment.start, segment.end);
135*7a6dacacSDimitry Andric 
136*7a6dacacSDimitry Andric     return false;
13768d75effSDimitry Andric   }
13868d75effSDimitry Andric 
139*7a6dacacSDimitry Andric   if (!protect)
140*7a6dacacSDimitry Andric     return true;
141*7a6dacacSDimitry Andric 
1420eae32dcSDimitry Andric #    if SANITIZER_IOS && !SANITIZER_IOSSIM
14368d75effSDimitry Andric   ProtectRange(HeapMemEnd(), ShadowBeg());
14468d75effSDimitry Andric   ProtectRange(ShadowEnd(), MetaShadowBeg());
1450eae32dcSDimitry Andric   ProtectRange(MetaShadowEnd(), HiAppMemBeg());
14668d75effSDimitry Andric #    else
14768d75effSDimitry Andric   ProtectRange(LoAppMemEnd(), ShadowBeg());
14868d75effSDimitry Andric   ProtectRange(ShadowEnd(), MetaShadowBeg());
149349cc55cSDimitry Andric   if (MidAppMemBeg()) {
15068d75effSDimitry Andric     ProtectRange(MetaShadowEnd(), MidAppMemBeg());
1510eae32dcSDimitry Andric     ProtectRange(MidAppMemEnd(), HeapMemBeg());
152349cc55cSDimitry Andric   } else {
1530eae32dcSDimitry Andric     ProtectRange(MetaShadowEnd(), HeapMemBeg());
154349cc55cSDimitry Andric   }
15568d75effSDimitry Andric   ProtectRange(HeapEnd(), HiAppMemBeg());
15668d75effSDimitry Andric #    endif
157fe6060f1SDimitry Andric 
158fe6060f1SDimitry Andric #    if defined(__s390x__)
159fe6060f1SDimitry Andric   // Protect the rest of the address space.
160fe6060f1SDimitry Andric   const uptr user_addr_max_l4 = 0x0020000000000000ull;
161fe6060f1SDimitry Andric   const uptr user_addr_max_l5 = 0xfffffffffffff000ull;
162fe6060f1SDimitry Andric   // All the maintained s390x kernels support at least 4-level page tables.
163fe6060f1SDimitry Andric   ProtectRange(HiAppMemEnd(), user_addr_max_l4);
164fe6060f1SDimitry Andric   // Older s390x kernels may not support 5-level page tables.
165fe6060f1SDimitry Andric   TryProtectRange(user_addr_max_l4, user_addr_max_l5);
166fe6060f1SDimitry Andric #endif
167*7a6dacacSDimitry Andric 
168*7a6dacacSDimitry Andric   return true;
16968d75effSDimitry Andric }
17068d75effSDimitry Andric #  endif
17168d75effSDimitry Andric 
17268d75effSDimitry Andric }  // namespace __tsan
17368d75effSDimitry Andric 
17468d75effSDimitry Andric #endif  // SANITIZER_POSIX
175