asan_fake_stack.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //===-- asan_fake_stack.h ---------------------------------------*- C++ -*-===//
  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 AddressSanitizer, an address sanity checker.
  9. //
  10. // ASan-private header for asan_fake_stack.cc, implements FakeStack.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef ASAN_FAKE_STACK_H
  13. #define ASAN_FAKE_STACK_H
  14. #include "sanitizer_common/sanitizer_common.h"
  15. namespace __asan {
  16. // Fake stack frame contains local variables of one function.
  17. struct FakeFrame {
  18. uptr magic; // Modified by the instrumented code.
  19. uptr descr; // Modified by the instrumented code.
  20. uptr pc; // Modified by the instrumented code.
  21. uptr real_stack;
  22. };
  23. // For each thread we create a fake stack and place stack objects on this fake
  24. // stack instead of the real stack. The fake stack is not really a stack but
  25. // a fast malloc-like allocator so that when a function exits the fake stack
  26. // is not popped but remains there for quite some time until gets used again.
  27. // So, we poison the objects on the fake stack when function returns.
  28. // It helps us find use-after-return bugs.
  29. //
  30. // The FakeStack objects is allocated by a single mmap call and has no other
  31. // pointers. The size of the fake stack depends on the actual thread stack size
  32. // and thus can not be a constant.
  33. // stack_size is a power of two greater or equal to the thread's stack size;
  34. // we store it as its logarithm (stack_size_log).
  35. // FakeStack has kNumberOfSizeClasses (11) size classes, each size class
  36. // is a power of two, starting from 64 bytes. Each size class occupies
  37. // stack_size bytes and thus can allocate
  38. // NumberOfFrames=(stack_size/BytesInSizeClass) fake frames (also a power of 2).
  39. // For each size class we have NumberOfFrames allocation flags,
  40. // each flag indicates whether the given frame is currently allocated.
  41. // All flags for size classes 0 .. 10 are stored in a single contiguous region
  42. // followed by another contiguous region which contains the actual memory for
  43. // size classes. The addresses are computed by GetFlags and GetFrame without
  44. // any memory accesses solely based on 'this' and stack_size_log.
  45. // Allocate() flips the appropriate allocation flag atomically, thus achieving
  46. // async-signal safety.
  47. // This allocator does not have quarantine per se, but it tries to allocate the
  48. // frames in round robin fasion to maximize the delay between a deallocation
  49. // and the next allocation.
  50. class FakeStack {
  51. static const uptr kMinStackFrameSizeLog = 6; // Min frame is 64B.
  52. static const uptr kMaxStackFrameSizeLog = 16; // Max stack frame is 64K.
  53. public:
  54. static const uptr kNumberOfSizeClasses =
  55. kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1;
  56. // CTOR: create the FakeStack as a single mmap-ed object.
  57. static FakeStack *Create(uptr stack_size_log);
  58. void Destroy(int tid);
  59. // stack_size_log is at least 15 (stack_size >= 32K).
  60. static uptr SizeRequiredForFlags(uptr stack_size_log) {
  61. return 1UL << (stack_size_log + 1 - kMinStackFrameSizeLog);
  62. }
  63. // Each size class occupies stack_size bytes.
  64. static uptr SizeRequiredForFrames(uptr stack_size_log) {
  65. return (1ULL << stack_size_log) * kNumberOfSizeClasses;
  66. }
  67. // Number of bytes requires for the whole object.
  68. static uptr RequiredSize(uptr stack_size_log) {
  69. return kFlagsOffset + SizeRequiredForFlags(stack_size_log) +
  70. SizeRequiredForFrames(stack_size_log);
  71. }
  72. // Offset of the given flag from the first flag.
  73. // The flags for class 0 begin at offset 000000000
  74. // The flags for class 1 begin at offset 100000000
  75. // ....................2................ 110000000
  76. // ....................3................ 111000000
  77. // and so on.
  78. static uptr FlagsOffset(uptr stack_size_log, uptr class_id) {
  79. uptr t = kNumberOfSizeClasses - 1 - class_id;
  80. const uptr all_ones = (1 << (kNumberOfSizeClasses - 1)) - 1;
  81. return ((all_ones >> t) << t) << (stack_size_log - 15);
  82. }
  83. static uptr NumberOfFrames(uptr stack_size_log, uptr class_id) {
  84. return 1UL << (stack_size_log - kMinStackFrameSizeLog - class_id);
  85. }
  86. // Divide n by the numbe of frames in size class.
  87. static uptr ModuloNumberOfFrames(uptr stack_size_log, uptr class_id, uptr n) {
  88. return n & (NumberOfFrames(stack_size_log, class_id) - 1);
  89. }
  90. // The the pointer to the flags of the given class_id.
  91. u8 *GetFlags(uptr stack_size_log, uptr class_id) {
  92. return reinterpret_cast<u8 *>(this) + kFlagsOffset +
  93. FlagsOffset(stack_size_log, class_id);
  94. }
  95. // Get frame by class_id and pos.
  96. u8 *GetFrame(uptr stack_size_log, uptr class_id, uptr pos) {
  97. return reinterpret_cast<u8 *>(this) + kFlagsOffset +
  98. SizeRequiredForFlags(stack_size_log) +
  99. (1 << stack_size_log) * class_id + BytesInSizeClass(class_id) * pos;
  100. }
  101. // Allocate the fake frame.
  102. FakeFrame *Allocate(uptr stack_size_log, uptr class_id, uptr real_stack);
  103. // Deallocate the fake frame: read the saved flag address and write 0 there.
  104. static void Deallocate(uptr x, uptr class_id) {
  105. **SavedFlagPtr(x, class_id) = 0;
  106. }
  107. // Poison the entire FakeStack's shadow with the magic value.
  108. void PoisonAll(u8 magic);
  109. // Return the beginning of the FakeFrame or 0 if the address is not ours.
  110. uptr AddrIsInFakeStack(uptr addr, uptr *frame_beg, uptr *frame_end);
  111. USED uptr AddrIsInFakeStack(uptr addr) {
  112. uptr t1, t2;
  113. return AddrIsInFakeStack(addr, &t1, &t2);
  114. }
  115. // Number of bytes in a fake frame of this size class.
  116. static uptr BytesInSizeClass(uptr class_id) {
  117. return 1UL << (class_id + kMinStackFrameSizeLog);
  118. }
  119. // The fake frame is guaranteed to have a right redzone.
  120. // We use the last word of that redzone to store the address of the flag
  121. // that corresponds to the current frame to make faster deallocation.
  122. static u8 **SavedFlagPtr(uptr x, uptr class_id) {
  123. return reinterpret_cast<u8 **>(x + BytesInSizeClass(class_id) - sizeof(x));
  124. }
  125. uptr stack_size_log() const { return stack_size_log_; }
  126. void HandleNoReturn();
  127. void GC(uptr real_stack);
  128. void ForEachFakeFrame(RangeIteratorCallback callback, void *arg);
  129. private:
  130. FakeStack() { }
  131. static const uptr kFlagsOffset = 4096; // This is were the flags begin.
  132. // Must match the number of uses of DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID
  133. COMPILER_CHECK(kNumberOfSizeClasses == 11);
  134. static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog;
  135. uptr hint_position_[kNumberOfSizeClasses];
  136. uptr stack_size_log_;
  137. // a bit is set if something was allocated from the corresponding size class.
  138. bool needs_gc_;
  139. };
  140. FakeStack *GetTLSFakeStack();
  141. void SetTLSFakeStack(FakeStack *fs);
  142. } // namespace __asan
  143. #endif // ASAN_FAKE_STACK_H