tsan_platform_posix.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "sanitizer_common/sanitizer_platform.h"
  14. #if SANITIZER_POSIX
  15. # include <dlfcn.h>
  16. # include "sanitizer_common/sanitizer_common.h"
  17. # include "sanitizer_common/sanitizer_errno.h"
  18. # include "sanitizer_common/sanitizer_libc.h"
  19. # include "sanitizer_common/sanitizer_procmaps.h"
  20. # include "tsan_platform.h"
  21. # include "tsan_rtl.h"
  22. namespace __tsan {
  23. static const char kShadowMemoryMappingWarning[] =
  24. "FATAL: %s can not madvise shadow region [%zx, %zx] with %s (errno: %d)\n";
  25. static const char kShadowMemoryMappingHint[] =
  26. "HINT: if %s is not supported in your environment, you may set "
  27. "TSAN_OPTIONS=%s=0\n";
  28. # if !SANITIZER_GO
  29. void DontDumpShadow(uptr addr, uptr size) {
  30. if (common_flags()->use_madv_dontdump)
  31. if (!DontDumpShadowMemory(addr, size)) {
  32. Printf(kShadowMemoryMappingWarning, SanitizerToolName, addr, addr + size,
  33. "MADV_DONTDUMP", errno);
  34. Printf(kShadowMemoryMappingHint, "MADV_DONTDUMP", "use_madv_dontdump");
  35. Die();
  36. }
  37. }
  38. void InitializeShadowMemory() {
  39. // Map memory shadow.
  40. if (!MmapFixedSuperNoReserve(ShadowBeg(), ShadowEnd() - ShadowBeg(),
  41. "shadow")) {
  42. Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
  43. Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
  44. Die();
  45. }
  46. // This memory range is used for thread stacks and large user mmaps.
  47. // Frequently a thread uses only a small part of stack and similarly
  48. // a program uses a small part of large mmap. On some programs
  49. // we see 20% memory usage reduction without huge pages for this range.
  50. DontDumpShadow(ShadowBeg(), ShadowEnd() - ShadowBeg());
  51. DPrintf("memory shadow: %zx-%zx (%zuGB)\n",
  52. ShadowBeg(), ShadowEnd(),
  53. (ShadowEnd() - ShadowBeg()) >> 30);
  54. // Map meta shadow.
  55. const uptr meta = MetaShadowBeg();
  56. const uptr meta_size = MetaShadowEnd() - meta;
  57. if (!MmapFixedSuperNoReserve(meta, meta_size, "meta shadow")) {
  58. Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
  59. Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
  60. Die();
  61. }
  62. DontDumpShadow(meta, meta_size);
  63. DPrintf("meta shadow: %zx-%zx (%zuGB)\n",
  64. meta, meta + meta_size, meta_size >> 30);
  65. InitializeShadowMemoryPlatform();
  66. on_initialize = reinterpret_cast<void (*)(void)>(
  67. dlsym(RTLD_DEFAULT, "__tsan_on_initialize"));
  68. on_finalize =
  69. reinterpret_cast<int (*)(int)>(dlsym(RTLD_DEFAULT, "__tsan_on_finalize"));
  70. }
  71. static bool TryProtectRange(uptr beg, uptr end) {
  72. CHECK_LE(beg, end);
  73. if (beg == end)
  74. return true;
  75. return beg == (uptr)MmapFixedNoAccess(beg, end - beg);
  76. }
  77. static void ProtectRange(uptr beg, uptr end) {
  78. if (!TryProtectRange(beg, end)) {
  79. Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
  80. Printf("FATAL: Make sure you are not using unlimited stack\n");
  81. Die();
  82. }
  83. }
  84. // CheckAndProtect will check if the memory layout is compatible with TSan.
  85. // Optionally (if 'protect' is true), it will set the memory regions between
  86. // app memory to be inaccessible.
  87. // 'ignore_heap' means it will not consider heap memory allocations to be a
  88. // conflict. Set this based on whether we are calling CheckAndProtect before
  89. // or after the allocator has initialized the heap.
  90. bool CheckAndProtect(bool protect, bool ignore_heap, bool print_warnings) {
  91. // Ensure that the binary is indeed compiled with -pie.
  92. MemoryMappingLayout proc_maps(true);
  93. MemoryMappedSegment segment;
  94. while (proc_maps.Next(&segment)) {
  95. if (segment.start >= HeapMemBeg() && segment.end <= HeapEnd()) {
  96. if (ignore_heap) {
  97. continue;
  98. } else {
  99. return false;
  100. }
  101. }
  102. // Note: IsAppMem includes if it is heap memory, hence we must
  103. // put this check after the heap bounds check.
  104. if (IsAppMem(segment.start) && IsAppMem(segment.end - 1))
  105. continue;
  106. // Guard page after the heap end
  107. if (segment.start >= HeapMemEnd() && segment.start < HeapEnd()) continue;
  108. if (segment.protection == 0) // Zero page or mprotected.
  109. continue;
  110. if (segment.start >= VdsoBeg()) // vdso
  111. break;
  112. // Debug output can break tests. Suppress this message in most cases.
  113. if (print_warnings)
  114. Printf(
  115. "WARNING: ThreadSanitizer: unexpected memory mapping 0x%zx-0x%zx\n",
  116. segment.start, segment.end);
  117. return false;
  118. }
  119. if (!protect)
  120. return true;
  121. # if SANITIZER_IOS && !SANITIZER_IOSSIM
  122. ProtectRange(HeapMemEnd(), ShadowBeg());
  123. ProtectRange(ShadowEnd(), MetaShadowBeg());
  124. ProtectRange(MetaShadowEnd(), HiAppMemBeg());
  125. # else
  126. ProtectRange(LoAppMemEnd(), ShadowBeg());
  127. ProtectRange(ShadowEnd(), MetaShadowBeg());
  128. if (MidAppMemBeg()) {
  129. ProtectRange(MetaShadowEnd(), MidAppMemBeg());
  130. ProtectRange(MidAppMemEnd(), HeapMemBeg());
  131. } else {
  132. ProtectRange(MetaShadowEnd(), HeapMemBeg());
  133. }
  134. ProtectRange(HeapEnd(), HiAppMemBeg());
  135. # endif
  136. # if defined(__s390x__)
  137. // Protect the rest of the address space.
  138. const uptr user_addr_max_l4 = 0x0020000000000000ull;
  139. const uptr user_addr_max_l5 = 0xfffffffffffff000ull;
  140. // All the maintained s390x kernels support at least 4-level page tables.
  141. ProtectRange(HiAppMemEnd(), user_addr_max_l4);
  142. // Older s390x kernels may not support 5-level page tables.
  143. TryProtectRange(user_addr_max_l4, user_addr_max_l5);
  144. #endif
  145. return true;
  146. }
  147. # endif
  148. } // namespace __tsan
  149. #endif // SANITIZER_POSIX