tsan_platform_posix.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. static 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. void CheckAndProtect() {
  85. // Ensure that the binary is indeed compiled with -pie.
  86. MemoryMappingLayout proc_maps(true);
  87. MemoryMappedSegment segment;
  88. while (proc_maps.Next(&segment)) {
  89. if (IsAppMem(segment.start)) continue;
  90. if (segment.start >= HeapMemEnd() && segment.start < HeapEnd()) continue;
  91. if (segment.protection == 0) // Zero page or mprotected.
  92. continue;
  93. if (segment.start >= VdsoBeg()) // vdso
  94. break;
  95. Printf("FATAL: ThreadSanitizer: unexpected memory mapping 0x%zx-0x%zx\n",
  96. segment.start, segment.end);
  97. Die();
  98. }
  99. # if defined(__aarch64__) && defined(__APPLE__) && SANITIZER_IOS
  100. ProtectRange(HeapMemEnd(), ShadowBeg());
  101. ProtectRange(ShadowEnd(), MetaShadowBeg());
  102. ProtectRange(MetaShadowEnd(), TraceMemBeg());
  103. #else
  104. ProtectRange(LoAppMemEnd(), ShadowBeg());
  105. ProtectRange(ShadowEnd(), MetaShadowBeg());
  106. if (MidAppMemBeg()) {
  107. ProtectRange(MetaShadowEnd(), MidAppMemBeg());
  108. ProtectRange(MidAppMemEnd(), TraceMemBeg());
  109. } else {
  110. ProtectRange(MetaShadowEnd(), TraceMemBeg());
  111. }
  112. // Memory for traces is mapped lazily in MapThreadTrace.
  113. // Protect the whole range for now, so that user does not map something here.
  114. ProtectRange(TraceMemBeg(), TraceMemEnd());
  115. ProtectRange(TraceMemEnd(), HeapMemBeg());
  116. ProtectRange(HeapEnd(), HiAppMemBeg());
  117. #endif
  118. #if defined(__s390x__)
  119. // Protect the rest of the address space.
  120. const uptr user_addr_max_l4 = 0x0020000000000000ull;
  121. const uptr user_addr_max_l5 = 0xfffffffffffff000ull;
  122. // All the maintained s390x kernels support at least 4-level page tables.
  123. ProtectRange(HiAppMemEnd(), user_addr_max_l4);
  124. // Older s390x kernels may not support 5-level page tables.
  125. TryProtectRange(user_addr_max_l4, user_addr_max_l5);
  126. #endif
  127. }
  128. #endif
  129. } // namespace __tsan
  130. #endif // SANITIZER_POSIX