tsan_mman.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===-- tsan_mman.cc ------------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of ThreadSanitizer (TSan), a race detector.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "sanitizer_common/sanitizer_allocator_interface.h"
  12. #include "sanitizer_common/sanitizer_common.h"
  13. #include "sanitizer_common/sanitizer_placement_new.h"
  14. #include "tsan_mman.h"
  15. #include "tsan_rtl.h"
  16. #include "tsan_report.h"
  17. #include "tsan_flags.h"
  18. // May be overriden by front-end.
  19. extern "C" void WEAK __sanitizer_malloc_hook(void *ptr, uptr size) {
  20. (void)ptr;
  21. (void)size;
  22. }
  23. extern "C" void WEAK __sanitizer_free_hook(void *ptr) {
  24. (void)ptr;
  25. }
  26. namespace __tsan {
  27. struct MapUnmapCallback {
  28. void OnMap(uptr p, uptr size) const { }
  29. void OnUnmap(uptr p, uptr size) const {
  30. // We are about to unmap a chunk of user memory.
  31. // Mark the corresponding shadow memory as not needed.
  32. DontNeedShadowFor(p, size);
  33. }
  34. };
  35. static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
  36. Allocator *allocator() {
  37. return reinterpret_cast<Allocator*>(&allocator_placeholder);
  38. }
  39. void InitializeAllocator() {
  40. allocator()->Init();
  41. }
  42. void AllocatorThreadStart(ThreadState *thr) {
  43. allocator()->InitCache(&thr->alloc_cache);
  44. internal_allocator()->InitCache(&thr->internal_alloc_cache);
  45. }
  46. void AllocatorThreadFinish(ThreadState *thr) {
  47. allocator()->DestroyCache(&thr->alloc_cache);
  48. internal_allocator()->DestroyCache(&thr->internal_alloc_cache);
  49. }
  50. void AllocatorPrintStats() {
  51. allocator()->PrintStats();
  52. }
  53. static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
  54. if (atomic_load(&thr->in_signal_handler, memory_order_relaxed) == 0 ||
  55. !flags()->report_signal_unsafe)
  56. return;
  57. VarSizeStackTrace stack;
  58. ObtainCurrentStack(thr, pc, &stack);
  59. ThreadRegistryLock l(ctx->thread_registry);
  60. ScopedReport rep(ReportTypeSignalUnsafe);
  61. if (!IsFiredSuppression(ctx, rep, stack)) {
  62. rep.AddStack(stack, true);
  63. OutputReport(thr, rep);
  64. }
  65. }
  66. void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align, bool signal) {
  67. if ((sz >= (1ull << 40)) || (align >= (1ull << 40)))
  68. return AllocatorReturnNull();
  69. void *p = allocator()->Allocate(&thr->alloc_cache, sz, align);
  70. if (p == 0)
  71. return 0;
  72. if (ctx && ctx->initialized)
  73. OnUserAlloc(thr, pc, (uptr)p, sz, true);
  74. if (signal)
  75. SignalUnsafeCall(thr, pc);
  76. return p;
  77. }
  78. void user_free(ThreadState *thr, uptr pc, void *p, bool signal) {
  79. if (ctx && ctx->initialized)
  80. OnUserFree(thr, pc, (uptr)p, true);
  81. allocator()->Deallocate(&thr->alloc_cache, p);
  82. if (signal)
  83. SignalUnsafeCall(thr, pc);
  84. }
  85. void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) {
  86. DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p);
  87. ctx->metamap.AllocBlock(thr, pc, p, sz);
  88. if (write && thr->ignore_reads_and_writes == 0)
  89. MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
  90. else
  91. MemoryResetRange(thr, pc, (uptr)p, sz);
  92. }
  93. void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) {
  94. CHECK_NE(p, (void*)0);
  95. uptr sz = ctx->metamap.FreeBlock(thr, pc, p);
  96. DPrintf("#%d: free(%p, %zu)\n", thr->tid, p, sz);
  97. if (write && thr->ignore_reads_and_writes == 0)
  98. MemoryRangeFreed(thr, pc, (uptr)p, sz);
  99. }
  100. void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
  101. void *p2 = 0;
  102. // FIXME: Handle "shrinking" more efficiently,
  103. // it seems that some software actually does this.
  104. if (sz) {
  105. p2 = user_alloc(thr, pc, sz);
  106. if (p2 == 0)
  107. return 0;
  108. if (p) {
  109. uptr oldsz = user_alloc_usable_size(p);
  110. internal_memcpy(p2, p, min(oldsz, sz));
  111. }
  112. }
  113. if (p)
  114. user_free(thr, pc, p);
  115. return p2;
  116. }
  117. uptr user_alloc_usable_size(const void *p) {
  118. if (p == 0)
  119. return 0;
  120. MBlock *b = ctx->metamap.GetBlock((uptr)p);
  121. return b ? b->siz : 0;
  122. }
  123. void invoke_malloc_hook(void *ptr, uptr size) {
  124. ThreadState *thr = cur_thread();
  125. if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
  126. return;
  127. __sanitizer_malloc_hook(ptr, size);
  128. }
  129. void invoke_free_hook(void *ptr) {
  130. ThreadState *thr = cur_thread();
  131. if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
  132. return;
  133. __sanitizer_free_hook(ptr);
  134. }
  135. void *internal_alloc(MBlockType typ, uptr sz) {
  136. ThreadState *thr = cur_thread();
  137. if (thr->nomalloc) {
  138. thr->nomalloc = 0; // CHECK calls internal_malloc().
  139. CHECK(0);
  140. }
  141. return InternalAlloc(sz, &thr->internal_alloc_cache);
  142. }
  143. void internal_free(void *p) {
  144. ThreadState *thr = cur_thread();
  145. if (thr->nomalloc) {
  146. thr->nomalloc = 0; // CHECK calls internal_malloc().
  147. CHECK(0);
  148. }
  149. InternalFree(p, &thr->internal_alloc_cache);
  150. }
  151. } // namespace __tsan
  152. using namespace __tsan;
  153. extern "C" {
  154. uptr __sanitizer_get_current_allocated_bytes() {
  155. uptr stats[AllocatorStatCount];
  156. allocator()->GetStats(stats);
  157. return stats[AllocatorStatAllocated];
  158. }
  159. uptr __sanitizer_get_heap_size() {
  160. uptr stats[AllocatorStatCount];
  161. allocator()->GetStats(stats);
  162. return stats[AllocatorStatMapped];
  163. }
  164. uptr __sanitizer_get_free_bytes() {
  165. return 1;
  166. }
  167. uptr __sanitizer_get_unmapped_bytes() {
  168. return 1;
  169. }
  170. uptr __sanitizer_get_estimated_allocated_size(uptr size) {
  171. return size;
  172. }
  173. int __sanitizer_get_ownership(const void *p) {
  174. return allocator()->GetBlockBegin(p) != 0;
  175. }
  176. uptr __sanitizer_get_allocated_size(const void *p) {
  177. return user_alloc_usable_size(p);
  178. }
  179. void __tsan_on_thread_idle() {
  180. ThreadState *thr = cur_thread();
  181. allocator()->SwallowCache(&thr->alloc_cache);
  182. internal_allocator()->SwallowCache(&thr->internal_alloc_cache);
  183. ctx->metamap.OnThreadIdle(thr);
  184. }
  185. } // extern "C"