@@ -32,12 +32,12 @@ struct FakeFrame {
3232// is not popped but remains there for quite some time until gets used again.
3333// So, we poison the objects on the fake stack when function returns.
3434// It helps us find use-after-return bugs.
35- //
3635// The FakeStack objects is allocated by a single mmap call and has no other
3736// pointers. The size of the fake stack depends on the actual thread stack size
3837// and thus can not be a constant.
3938// stack_size is a power of two greater or equal to the thread's stack size;
4039// we store it as its logarithm (stack_size_log).
40+ // FakeStack is padded such that GetFrame() is aligned to BytesInSizeClass().
4141// FakeStack has kNumberOfSizeClasses (11) size classes, each size class
4242// is a power of two, starting from 64 bytes. Each size class occupies
4343// stack_size bytes and thus can allocate
@@ -56,6 +56,9 @@ struct FakeFrame {
5656class FakeStack {
5757 static const uptr kMinStackFrameSizeLog = 6 ; // Min frame is 64B.
5858 static const uptr kMaxStackFrameSizeLog = 16 ; // Max stack frame is 64K.
59+ static_assert (kMaxStackFrameSizeLog >= kMinStackFrameSizeLog );
60+
61+ static const u64 kMaxStackFrameSize = 1 << kMaxStackFrameSizeLog ;
5962
6063 public:
6164 static const uptr kNumberOfSizeClasses =
@@ -66,7 +69,7 @@ class FakeStack {
6669
6770 void Destroy (int tid);
6871
69- // stack_size_log is at least 15 (stack_size >= 32K).
72+ // min_uar_stack_size_log is 16 (stack_size >= 64KB)
7073 static uptr SizeRequiredForFlags (uptr stack_size_log) {
7174 return ((uptr)1 ) << (stack_size_log + 1 - kMinStackFrameSizeLog );
7275 }
@@ -110,6 +113,28 @@ class FakeStack {
110113 }
111114
112115 // Get frame by class_id and pos.
116+ // Return values are guaranteed to be aligned to BytesInSizeClass(class_id),
117+ // which is useful in combination with
118+ // ASanStackFrameLayout::ComputeASanStackFrameLayout().
119+ //
120+ // Note that alignment to 1<<kMaxStackFrameSizeLog (aka
121+ // BytesInSizeClass(max_class_id)) implies alignment to BytesInSizeClass()
122+ // for any class_id, since the class sizes are increasing powers of 2.
123+ //
124+ // 1) (this + kFlagsOffset + SizeRequiredForFlags())) is aligned to
125+ // 1<<kMaxStackFrameSizeLog (see FakeStack::Create)
126+ //
127+ // Note that SizeRequiredForFlags(16) == 2048. If FakeStack::Create() had
128+ // merely returned an address from mmap (4K-aligned), the addition would
129+ // not be 4K-aligned.
130+ // 2) We know that stack_size_log >= kMaxStackFrameSizeLog (otherwise you
131+ // couldn't store a single frame of that size in the entire stack)
132+ // hence (1<<stack_size_log) is aligned to 1<<kMaxStackFrameSizeLog
133+ // and ((1<<stack_size_log) * class_id) is aligned to
134+ // 1<<kMaxStackFrameSizeLog
135+ // 3) BytesInSizeClass(class_id) * pos is aligned to
136+ // BytesInSizeClass(class_id)
137+ // The sum of these is aligned to BytesInSizeClass(class_id).
113138 u8 *GetFrame (uptr stack_size_log, uptr class_id, uptr pos) {
114139 return reinterpret_cast <u8 *>(this ) + kFlagsOffset +
115140 SizeRequiredForFlags (stack_size_log) +
@@ -156,15 +181,18 @@ class FakeStack {
156181
157182 private:
158183 FakeStack () { }
159- static const uptr kFlagsOffset = 4096 ; // This is were the flags begin.
184+ static const uptr kFlagsOffset = 4096 ; // This is where the flags begin.
160185 // Must match the number of uses of DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID
161186 COMPILER_CHECK (kNumberOfSizeClasses == 11 );
162187 static const uptr kMaxStackMallocSize = ((uptr)1 ) << kMaxStackFrameSizeLog ;
163188
164189 uptr hint_position_[kNumberOfSizeClasses ];
165190 uptr stack_size_log_;
166- // a bit is set if something was allocated from the corresponding size class.
167191 bool needs_gc_;
192+ // We allocated more memory than needed to ensure the FakeStack (and, by
193+ // extension, each of the fake stack frames) is aligned. We keep track of the
194+ // true start so that we can unmap it.
195+ void *true_start;
168196};
169197
170198FakeStack *GetTLSFakeStack ();
0 commit comments