@@ -32,12 +32,12 @@ struct FakeFrame {
32
32
// is not popped but remains there for quite some time until gets used again.
33
33
// So, we poison the objects on the fake stack when function returns.
34
34
// It helps us find use-after-return bugs.
35
- //
36
35
// The FakeStack objects is allocated by a single mmap call and has no other
37
36
// pointers. The size of the fake stack depends on the actual thread stack size
38
37
// and thus can not be a constant.
39
38
// stack_size is a power of two greater or equal to the thread's stack size;
40
39
// we store it as its logarithm (stack_size_log).
40
+ // FakeStack is padded such that GetFrame() is aligned to BytesInSizeClass().
41
41
// FakeStack has kNumberOfSizeClasses (11) size classes, each size class
42
42
// is a power of two, starting from 64 bytes. Each size class occupies
43
43
// stack_size bytes and thus can allocate
@@ -56,6 +56,9 @@ struct FakeFrame {
56
56
class FakeStack {
57
57
static const uptr kMinStackFrameSizeLog = 6 ; // Min frame is 64B.
58
58
static const uptr kMaxStackFrameSizeLog = 16 ; // Max stack frame is 64K.
59
+ static_assert (kMaxStackFrameSizeLog >= kMinStackFrameSizeLog );
60
+
61
+ static const u64 kMaxStackFrameSize = 1 << kMaxStackFrameSizeLog ;
59
62
60
63
public:
61
64
static const uptr kNumberOfSizeClasses =
@@ -66,7 +69,7 @@ class FakeStack {
66
69
67
70
void Destroy (int tid);
68
71
69
- // stack_size_log is at least 15 (stack_size >= 32K).
72
+ // min_uar_stack_size_log is 16 (stack_size >= 64KB)
70
73
static uptr SizeRequiredForFlags (uptr stack_size_log) {
71
74
return ((uptr)1 ) << (stack_size_log + 1 - kMinStackFrameSizeLog );
72
75
}
@@ -110,6 +113,28 @@ class FakeStack {
110
113
}
111
114
112
115
// 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).
113
138
u8 *GetFrame (uptr stack_size_log, uptr class_id, uptr pos) {
114
139
return reinterpret_cast <u8 *>(this ) + kFlagsOffset +
115
140
SizeRequiredForFlags (stack_size_log) +
@@ -156,15 +181,18 @@ class FakeStack {
156
181
157
182
private:
158
183
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.
160
185
// Must match the number of uses of DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID
161
186
COMPILER_CHECK (kNumberOfSizeClasses == 11 );
162
187
static const uptr kMaxStackMallocSize = ((uptr)1 ) << kMaxStackFrameSizeLog ;
163
188
164
189
uptr hint_position_[kNumberOfSizeClasses ];
165
190
uptr stack_size_log_;
166
- // a bit is set if something was allocated from the corresponding size class.
167
191
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;
168
196
};
169
197
170
198
FakeStack *GetTLSFakeStack ();
0 commit comments