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