asan_fake_stack.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //===-- asan_fake_stack.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 AddressSanitizer, an address sanity checker.
  10. //
  11. // FakeStack is used to detect use-after-return bugs.
  12. //===----------------------------------------------------------------------===//
  13. #include "asan_allocator.h"
  14. #include "asan_poisoning.h"
  15. #include "asan_thread.h"
  16. namespace __asan {
  17. static const u64 kMagic1 = kAsanStackAfterReturnMagic;
  18. static const u64 kMagic2 = (kMagic1 << 8) | kMagic1;
  19. static const u64 kMagic4 = (kMagic2 << 16) | kMagic2;
  20. static const u64 kMagic8 = (kMagic4 << 32) | kMagic4;
  21. static const u64 kAllocaRedzoneSize = 32UL;
  22. static const u64 kAllocaRedzoneMask = 31UL;
  23. // For small size classes inline PoisonShadow for better performance.
  24. ALWAYS_INLINE void SetShadow(uptr ptr, uptr size, uptr class_id, u64 magic) {
  25. u64 *shadow = reinterpret_cast<u64*>(MemToShadow(ptr));
  26. if (ASAN_SHADOW_SCALE == 3 && class_id <= 6) {
  27. // This code expects ASAN_SHADOW_SCALE=3.
  28. for (uptr i = 0; i < (((uptr)1) << class_id); i++) {
  29. shadow[i] = magic;
  30. // Make sure this does not become memset.
  31. SanitizerBreakOptimization(nullptr);
  32. }
  33. } else {
  34. // The size class is too big, it's cheaper to poison only size bytes.
  35. PoisonShadow(ptr, size, static_cast<u8>(magic));
  36. }
  37. }
  38. FakeStack *FakeStack::Create(uptr stack_size_log) {
  39. static uptr kMinStackSizeLog = 16;
  40. static uptr kMaxStackSizeLog = FIRST_32_SECOND_64(24, 28);
  41. if (stack_size_log < kMinStackSizeLog)
  42. stack_size_log = kMinStackSizeLog;
  43. if (stack_size_log > kMaxStackSizeLog)
  44. stack_size_log = kMaxStackSizeLog;
  45. uptr size = RequiredSize(stack_size_log);
  46. FakeStack *res = reinterpret_cast<FakeStack *>(
  47. flags()->uar_noreserve ? MmapNoReserveOrDie(size, "FakeStack")
  48. : MmapOrDie(size, "FakeStack"));
  49. res->stack_size_log_ = stack_size_log;
  50. u8 *p = reinterpret_cast<u8 *>(res);
  51. VReport(1,
  52. "T%d: FakeStack created: %p -- %p stack_size_log: %zd; "
  53. "mmapped %zdK, noreserve=%d \n",
  54. GetCurrentTidOrInvalid(), (void *)p,
  55. (void *)(p + FakeStack::RequiredSize(stack_size_log)), stack_size_log,
  56. size >> 10, flags()->uar_noreserve);
  57. return res;
  58. }
  59. void FakeStack::Destroy(int tid) {
  60. PoisonAll(0);
  61. if (Verbosity() >= 2) {
  62. InternalScopedString str;
  63. for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++)
  64. str.append("%zd: %zd/%zd; ", class_id, hint_position_[class_id],
  65. NumberOfFrames(stack_size_log(), class_id));
  66. Report("T%d: FakeStack destroyed: %s\n", tid, str.data());
  67. }
  68. uptr size = RequiredSize(stack_size_log_);
  69. FlushUnneededASanShadowMemory(reinterpret_cast<uptr>(this), size);
  70. UnmapOrDie(this, size);
  71. }
  72. void FakeStack::PoisonAll(u8 magic) {
  73. PoisonShadow(reinterpret_cast<uptr>(this), RequiredSize(stack_size_log()),
  74. magic);
  75. }
  76. #if !defined(_MSC_VER) || defined(__clang__)
  77. ALWAYS_INLINE USED
  78. #endif
  79. FakeFrame *FakeStack::Allocate(uptr stack_size_log, uptr class_id,
  80. uptr real_stack) {
  81. CHECK_LT(class_id, kNumberOfSizeClasses);
  82. if (needs_gc_)
  83. GC(real_stack);
  84. uptr &hint_position = hint_position_[class_id];
  85. const int num_iter = NumberOfFrames(stack_size_log, class_id);
  86. u8 *flags = GetFlags(stack_size_log, class_id);
  87. for (int i = 0; i < num_iter; i++) {
  88. uptr pos = ModuloNumberOfFrames(stack_size_log, class_id, hint_position++);
  89. // This part is tricky. On one hand, checking and setting flags[pos]
  90. // should be atomic to ensure async-signal safety. But on the other hand,
  91. // if the signal arrives between checking and setting flags[pos], the
  92. // signal handler's fake stack will start from a different hint_position
  93. // and so will not touch this particular byte. So, it is safe to do this
  94. // with regular non-atomic load and store (at least I was not able to make
  95. // this code crash).
  96. if (flags[pos]) continue;
  97. flags[pos] = 1;
  98. FakeFrame *res = reinterpret_cast<FakeFrame *>(
  99. GetFrame(stack_size_log, class_id, pos));
  100. res->real_stack = real_stack;
  101. *SavedFlagPtr(reinterpret_cast<uptr>(res), class_id) = &flags[pos];
  102. return res;
  103. }
  104. return nullptr; // We are out of fake stack.
  105. }
  106. uptr FakeStack::AddrIsInFakeStack(uptr ptr, uptr *frame_beg, uptr *frame_end) {
  107. uptr stack_size_log = this->stack_size_log();
  108. uptr beg = reinterpret_cast<uptr>(GetFrame(stack_size_log, 0, 0));
  109. uptr end = reinterpret_cast<uptr>(this) + RequiredSize(stack_size_log);
  110. if (ptr < beg || ptr >= end) return 0;
  111. uptr class_id = (ptr - beg) >> stack_size_log;
  112. uptr base = beg + (class_id << stack_size_log);
  113. CHECK_LE(base, ptr);
  114. CHECK_LT(ptr, base + (((uptr)1) << stack_size_log));
  115. uptr pos = (ptr - base) >> (kMinStackFrameSizeLog + class_id);
  116. uptr res = base + pos * BytesInSizeClass(class_id);
  117. *frame_end = res + BytesInSizeClass(class_id);
  118. *frame_beg = res + sizeof(FakeFrame);
  119. return res;
  120. }
  121. void FakeStack::HandleNoReturn() {
  122. needs_gc_ = true;
  123. }
  124. // When throw, longjmp or some such happens we don't call OnFree() and
  125. // as the result may leak one or more fake frames, but the good news is that
  126. // we are notified about all such events by HandleNoReturn().
  127. // If we recently had such no-return event we need to collect garbage frames.
  128. // We do it based on their 'real_stack' values -- everything that is lower
  129. // than the current real_stack is garbage.
  130. NOINLINE void FakeStack::GC(uptr real_stack) {
  131. uptr collected = 0;
  132. for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++) {
  133. u8 *flags = GetFlags(stack_size_log(), class_id);
  134. for (uptr i = 0, n = NumberOfFrames(stack_size_log(), class_id); i < n;
  135. i++) {
  136. if (flags[i] == 0) continue; // not allocated.
  137. FakeFrame *ff = reinterpret_cast<FakeFrame *>(
  138. GetFrame(stack_size_log(), class_id, i));
  139. if (ff->real_stack < real_stack) {
  140. flags[i] = 0;
  141. collected++;
  142. }
  143. }
  144. }
  145. needs_gc_ = false;
  146. }
  147. void FakeStack::ForEachFakeFrame(RangeIteratorCallback callback, void *arg) {
  148. for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++) {
  149. u8 *flags = GetFlags(stack_size_log(), class_id);
  150. for (uptr i = 0, n = NumberOfFrames(stack_size_log(), class_id); i < n;
  151. i++) {
  152. if (flags[i] == 0) continue; // not allocated.
  153. FakeFrame *ff = reinterpret_cast<FakeFrame *>(
  154. GetFrame(stack_size_log(), class_id, i));
  155. uptr begin = reinterpret_cast<uptr>(ff);
  156. callback(begin, begin + FakeStack::BytesInSizeClass(class_id), arg);
  157. }
  158. }
  159. }
  160. #if (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_FUCHSIA
  161. static THREADLOCAL FakeStack *fake_stack_tls;
  162. FakeStack *GetTLSFakeStack() {
  163. return fake_stack_tls;
  164. }
  165. void SetTLSFakeStack(FakeStack *fs) {
  166. fake_stack_tls = fs;
  167. }
  168. #else
  169. FakeStack *GetTLSFakeStack() { return 0; }
  170. void SetTLSFakeStack(FakeStack *fs) { }
  171. #endif // (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_FUCHSIA
  172. static FakeStack *GetFakeStack() {
  173. AsanThread *t = GetCurrentThread();
  174. if (!t) return nullptr;
  175. return t->get_or_create_fake_stack();
  176. }
  177. static FakeStack *GetFakeStackFast() {
  178. if (FakeStack *fs = GetTLSFakeStack())
  179. return fs;
  180. if (!__asan_option_detect_stack_use_after_return)
  181. return nullptr;
  182. return GetFakeStack();
  183. }
  184. static FakeStack *GetFakeStackFastAlways() {
  185. if (FakeStack *fs = GetTLSFakeStack())
  186. return fs;
  187. return GetFakeStack();
  188. }
  189. static ALWAYS_INLINE uptr OnMalloc(uptr class_id, uptr size) {
  190. FakeStack *fs = GetFakeStackFast();
  191. if (!fs) return 0;
  192. uptr local_stack;
  193. uptr real_stack = reinterpret_cast<uptr>(&local_stack);
  194. FakeFrame *ff = fs->Allocate(fs->stack_size_log(), class_id, real_stack);
  195. if (!ff) return 0; // Out of fake stack.
  196. uptr ptr = reinterpret_cast<uptr>(ff);
  197. SetShadow(ptr, size, class_id, 0);
  198. return ptr;
  199. }
  200. static ALWAYS_INLINE uptr OnMallocAlways(uptr class_id, uptr size) {
  201. FakeStack *fs = GetFakeStackFastAlways();
  202. if (!fs)
  203. return 0;
  204. uptr local_stack;
  205. uptr real_stack = reinterpret_cast<uptr>(&local_stack);
  206. FakeFrame *ff = fs->Allocate(fs->stack_size_log(), class_id, real_stack);
  207. if (!ff)
  208. return 0; // Out of fake stack.
  209. uptr ptr = reinterpret_cast<uptr>(ff);
  210. SetShadow(ptr, size, class_id, 0);
  211. return ptr;
  212. }
  213. static ALWAYS_INLINE void OnFree(uptr ptr, uptr class_id, uptr size) {
  214. FakeStack::Deallocate(ptr, class_id);
  215. SetShadow(ptr, size, class_id, kMagic8);
  216. }
  217. } // namespace __asan
  218. // ---------------------- Interface ---------------- {{{1
  219. using namespace __asan;
  220. #define DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(class_id) \
  221. extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr \
  222. __asan_stack_malloc_##class_id(uptr size) { \
  223. return OnMalloc(class_id, size); \
  224. } \
  225. extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr \
  226. __asan_stack_malloc_always_##class_id(uptr size) { \
  227. return OnMallocAlways(class_id, size); \
  228. } \
  229. extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __asan_stack_free_##class_id( \
  230. uptr ptr, uptr size) { \
  231. OnFree(ptr, class_id, size); \
  232. }
  233. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(0)
  234. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(1)
  235. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(2)
  236. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(3)
  237. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(4)
  238. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(5)
  239. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(6)
  240. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(7)
  241. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(8)
  242. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(9)
  243. DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(10)
  244. extern "C" {
  245. // TODO: remove this method and fix tests that use it by setting
  246. // -asan-use-after-return=never, after modal UAR flag lands
  247. // (https://github.com/google/sanitizers/issues/1394)
  248. SANITIZER_INTERFACE_ATTRIBUTE
  249. void *__asan_get_current_fake_stack() { return GetFakeStackFast(); }
  250. SANITIZER_INTERFACE_ATTRIBUTE
  251. void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
  252. void **end) {
  253. FakeStack *fs = reinterpret_cast<FakeStack*>(fake_stack);
  254. if (!fs) return nullptr;
  255. uptr frame_beg, frame_end;
  256. FakeFrame *frame = reinterpret_cast<FakeFrame *>(fs->AddrIsInFakeStack(
  257. reinterpret_cast<uptr>(addr), &frame_beg, &frame_end));
  258. if (!frame) return nullptr;
  259. if (frame->magic != kCurrentStackFrameMagic)
  260. return nullptr;
  261. if (beg) *beg = reinterpret_cast<void*>(frame_beg);
  262. if (end) *end = reinterpret_cast<void*>(frame_end);
  263. return reinterpret_cast<void*>(frame->real_stack);
  264. }
  265. SANITIZER_INTERFACE_ATTRIBUTE
  266. void __asan_alloca_poison(uptr addr, uptr size) {
  267. uptr LeftRedzoneAddr = addr - kAllocaRedzoneSize;
  268. uptr PartialRzAddr = addr + size;
  269. uptr RightRzAddr = (PartialRzAddr + kAllocaRedzoneMask) & ~kAllocaRedzoneMask;
  270. uptr PartialRzAligned = PartialRzAddr & ~(ASAN_SHADOW_GRANULARITY - 1);
  271. FastPoisonShadow(LeftRedzoneAddr, kAllocaRedzoneSize, kAsanAllocaLeftMagic);
  272. FastPoisonShadowPartialRightRedzone(
  273. PartialRzAligned, PartialRzAddr % ASAN_SHADOW_GRANULARITY,
  274. RightRzAddr - PartialRzAligned, kAsanAllocaRightMagic);
  275. FastPoisonShadow(RightRzAddr, kAllocaRedzoneSize, kAsanAllocaRightMagic);
  276. }
  277. SANITIZER_INTERFACE_ATTRIBUTE
  278. void __asan_allocas_unpoison(uptr top, uptr bottom) {
  279. if ((!top) || (top > bottom)) return;
  280. REAL(memset)
  281. (reinterpret_cast<void *>(MemToShadow(top)), 0,
  282. (bottom - top) / ASAN_SHADOW_GRANULARITY);
  283. }
  284. } // extern "C"