1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
//===-- sanitizer_stack_store.cpp -------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "sanitizer_stack_store.h"
#include "sanitizer_atomic.h"
#include "sanitizer_common.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_leb128.h"
#include "sanitizer_lzw.h"
#include "sanitizer_placement_new.h"
#include "sanitizer_stacktrace.h"
namespace __sanitizer {
namespace {
struct StackTraceHeader {
static constexpr u32 kStackSizeBits = 8;
u8 size;
u8 tag;
explicit StackTraceHeader(const StackTrace &trace)
: size(Min<uptr>(trace.size, (1u << 8) - 1)), tag(trace.tag) {
CHECK_EQ(trace.tag, static_cast<uptr>(tag));
}
explicit StackTraceHeader(uptr h)
: size(h & ((1 << kStackSizeBits) - 1)), tag(h >> kStackSizeBits) {}
uptr ToUptr() const {
return static_cast<uptr>(size) | (static_cast<uptr>(tag) << kStackSizeBits);
}
};
} // namespace
StackStore::Id StackStore::Store(const StackTrace &trace, uptr *pack) {
if (!trace.size && !trace.tag)
return 0;
StackTraceHeader h(trace);
uptr idx = 0;
*pack = 0;
uptr *stack_trace = Alloc(h.size + 1, &idx, pack);
// No more space.
if (stack_trace == nullptr)
return 0;
*stack_trace = h.ToUptr();
internal_memcpy(stack_trace + 1, trace.trace, h.size * sizeof(uptr));
*pack += blocks_[GetBlockIdx(idx)].Stored(h.size + 1);
return OffsetToId(idx);
}
StackTrace StackStore::Load(Id id) {
if (!id)
return {};
uptr idx = IdToOffset(id);
uptr block_idx = GetBlockIdx(idx);
CHECK_LT(block_idx, ARRAY_SIZE(blocks_));
const uptr *stack_trace = blocks_[block_idx].GetOrUnpack(this);
if (!stack_trace)
return {};
stack_trace += GetInBlockIdx(idx);
StackTraceHeader h(*stack_trace);
return StackTrace(stack_trace + 1, h.size, h.tag);
}
uptr StackStore::Allocated() const {
return atomic_load_relaxed(&allocated_) + sizeof(*this);
}
uptr *StackStore::Alloc(uptr count, uptr *idx, uptr *pack) {
for (;;) {
// Optimisic lock-free allocation, essentially try to bump the
// total_frames_.
uptr start = atomic_fetch_add(&total_frames_, count, memory_order_relaxed);
uptr block_idx = GetBlockIdx(start);
uptr last_idx = GetBlockIdx(start + count - 1);
if (LIKELY(block_idx == last_idx)) {
// Fits into a single block.
// No more available blocks. Indicate inability to allocate more memory.
if (block_idx >= ARRAY_SIZE(blocks_))
return nullptr;
*idx = start;
return blocks_[block_idx].GetOrCreate(this) + GetInBlockIdx(start);
}
// Retry. We can't use range allocated in two different blocks.
CHECK_LE(count, kBlockSizeFrames);
uptr in_first = kBlockSizeFrames - GetInBlockIdx(start);
// Mark tail/head of these blocks as "stored".to avoid waiting before we can
// Pack().
*pack += blocks_[block_idx].Stored(in_first);
*pack += blocks_[last_idx].Stored(count - in_first);
}
}
void *StackStore::Map(uptr size, const char *mem_type) {
atomic_fetch_add(&allocated_, size, memory_order_relaxed);
return MmapNoReserveOrDie(size, mem_type);
}
void StackStore::Unmap(void *addr, uptr size) {
atomic_fetch_sub(&allocated_, size, memory_order_relaxed);
UnmapOrDie(addr, size);
}
uptr StackStore::Pack(Compression type) {
uptr res = 0;
for (BlockInfo &b : blocks_) res += b.Pack(type, this);
return res;
}
void StackStore::LockAll() {
for (BlockInfo &b : blocks_) b.Lock();
}
void StackStore::UnlockAll() {
for (BlockInfo &b : blocks_) b.Unlock();
}
void StackStore::TestOnlyUnmap() {
for (BlockInfo &b : blocks_) b.TestOnlyUnmap(this);
internal_memset(this, 0, sizeof(*this));
}
uptr *StackStore::BlockInfo::Get() const {
// Idiomatic double-checked locking uses memory_order_acquire here. But
// relaxed is fine for us, justification is similar to
// TwoLevelMap::GetOrCreate.
return reinterpret_cast<uptr *>(atomic_load_relaxed(&data_));
}
uptr *StackStore::BlockInfo::Create(StackStore *store) {
SpinMutexLock l(&mtx_);
uptr *ptr = Get();
if (!ptr) {
ptr = reinterpret_cast<uptr *>(store->Map(kBlockSizeBytes, "StackStore"));
atomic_store(&data_, reinterpret_cast<uptr>(ptr), memory_order_release);
}
return ptr;
}
uptr *StackStore::BlockInfo::GetOrCreate(StackStore *store) {
uptr *ptr = Get();
if (LIKELY(ptr))
return ptr;
return Create(store);
}
class SLeb128Encoder {
public:
SLeb128Encoder(u8 *begin, u8 *end) : begin(begin), end(end) {}
bool operator==(const SLeb128Encoder &other) const {
return begin == other.begin;
}
bool operator!=(const SLeb128Encoder &other) const {
return begin != other.begin;
}
SLeb128Encoder &operator=(uptr v) {
sptr diff = v - previous;
begin = EncodeSLEB128(diff, begin, end);
previous = v;
return *this;
}
SLeb128Encoder &operator*() { return *this; }
SLeb128Encoder &operator++() { return *this; }
u8 *base() const { return begin; }
private:
u8 *begin;
u8 *end;
uptr previous = 0;
};
class SLeb128Decoder {
public:
SLeb128Decoder(const u8 *begin, const u8 *end) : begin(begin), end(end) {}
bool operator==(const SLeb128Decoder &other) const {
return begin == other.begin;
}
bool operator!=(const SLeb128Decoder &other) const {
return begin != other.begin;
}
uptr operator*() {
sptr diff;
begin = DecodeSLEB128(begin, end, &diff);
previous += diff;
return previous;
}
SLeb128Decoder &operator++() { return *this; }
SLeb128Decoder operator++(int) { return *this; }
private:
const u8 *begin;
const u8 *end;
uptr previous = 0;
};
static u8 *CompressDelta(const uptr *from, const uptr *from_end, u8 *to,
u8 *to_end) {
SLeb128Encoder encoder(to, to_end);
for (; from != from_end; ++from, ++encoder) *encoder = *from;
return encoder.base();
}
static uptr *UncompressDelta(const u8 *from, const u8 *from_end, uptr *to,
uptr *to_end) {
SLeb128Decoder decoder(from, from_end);
SLeb128Decoder end(from_end, from_end);
for (; decoder != end; ++to, ++decoder) *to = *decoder;
CHECK_EQ(to, to_end);
return to;
}
static u8 *CompressLzw(const uptr *from, const uptr *from_end, u8 *to,
u8 *to_end) {
SLeb128Encoder encoder(to, to_end);
encoder = LzwEncode<uptr>(from, from_end, encoder);
return encoder.base();
}
static uptr *UncompressLzw(const u8 *from, const u8 *from_end, uptr *to,
uptr *to_end) {
SLeb128Decoder decoder(from, from_end);
SLeb128Decoder end(from_end, from_end);
to = LzwDecode<uptr>(decoder, end, to);
CHECK_EQ(to, to_end);
return to;
}
#if defined(_MSC_VER) && !defined(__clang__)
# pragma warning(push)
// Disable 'nonstandard extension used: zero-sized array in struct/union'.
# pragma warning(disable : 4200)
#endif
namespace {
struct PackedHeader {
uptr size;
StackStore::Compression type;
u8 data[];
};
} // namespace
#if defined(_MSC_VER) && !defined(__clang__)
# pragma warning(pop)
#endif
uptr *StackStore::BlockInfo::GetOrUnpack(StackStore *store) {
SpinMutexLock l(&mtx_);
switch (state) {
case State::Storing:
state = State::Unpacked;
FALLTHROUGH;
case State::Unpacked:
return Get();
case State::Packed:
break;
}
u8 *ptr = reinterpret_cast<u8 *>(Get());
CHECK_NE(nullptr, ptr);
const PackedHeader *header = reinterpret_cast<const PackedHeader *>(ptr);
CHECK_LE(header->size, kBlockSizeBytes);
CHECK_GE(header->size, sizeof(PackedHeader));
uptr packed_size_aligned = RoundUpTo(header->size, GetPageSizeCached());
uptr *unpacked =
reinterpret_cast<uptr *>(store->Map(kBlockSizeBytes, "StackStoreUnpack"));
uptr *unpacked_end;
switch (header->type) {
case Compression::Delta:
unpacked_end = UncompressDelta(header->data, ptr + header->size, unpacked,
unpacked + kBlockSizeFrames);
break;
case Compression::LZW:
unpacked_end = UncompressLzw(header->data, ptr + header->size, unpacked,
unpacked + kBlockSizeFrames);
break;
default:
UNREACHABLE("Unexpected type");
break;
}
CHECK_EQ(kBlockSizeFrames, unpacked_end - unpacked);
MprotectReadOnly(reinterpret_cast<uptr>(unpacked), kBlockSizeBytes);
atomic_store(&data_, reinterpret_cast<uptr>(unpacked), memory_order_release);
store->Unmap(ptr, packed_size_aligned);
state = State::Unpacked;
return Get();
}
uptr StackStore::BlockInfo::Pack(Compression type, StackStore *store) {
if (type == Compression::None)
return 0;
SpinMutexLock l(&mtx_);
switch (state) {
case State::Unpacked:
case State::Packed:
return 0;
case State::Storing:
break;
}
uptr *ptr = Get();
if (!ptr || !Stored(0))
return 0;
u8 *packed =
reinterpret_cast<u8 *>(store->Map(kBlockSizeBytes, "StackStorePack"));
PackedHeader *header = reinterpret_cast<PackedHeader *>(packed);
u8 *alloc_end = packed + kBlockSizeBytes;
u8 *packed_end = nullptr;
switch (type) {
case Compression::Delta:
packed_end =
CompressDelta(ptr, ptr + kBlockSizeFrames, header->data, alloc_end);
break;
case Compression::LZW:
packed_end =
CompressLzw(ptr, ptr + kBlockSizeFrames, header->data, alloc_end);
break;
default:
UNREACHABLE("Unexpected type");
break;
}
header->type = type;
header->size = packed_end - packed;
VPrintf(1, "Packed block of %zu KiB to %zu KiB\n", kBlockSizeBytes >> 10,
header->size >> 10);
if (kBlockSizeBytes - header->size < kBlockSizeBytes / 8) {
VPrintf(1, "Undo and keep block unpacked\n");
MprotectReadOnly(reinterpret_cast<uptr>(ptr), kBlockSizeBytes);
store->Unmap(packed, kBlockSizeBytes);
state = State::Unpacked;
return 0;
}
uptr packed_size_aligned = RoundUpTo(header->size, GetPageSizeCached());
store->Unmap(packed + packed_size_aligned,
kBlockSizeBytes - packed_size_aligned);
MprotectReadOnly(reinterpret_cast<uptr>(packed), packed_size_aligned);
atomic_store(&data_, reinterpret_cast<uptr>(packed), memory_order_release);
store->Unmap(ptr, kBlockSizeBytes);
state = State::Packed;
return kBlockSizeBytes - packed_size_aligned;
}
void StackStore::BlockInfo::TestOnlyUnmap(StackStore *store) {
if (uptr *ptr = Get())
store->Unmap(ptr, kBlockSizeBytes);
}
bool StackStore::BlockInfo::Stored(uptr n) {
return n + atomic_fetch_add(&stored_, n, memory_order_release) ==
kBlockSizeFrames;
}
bool StackStore::BlockInfo::IsPacked() const {
SpinMutexLock l(&mtx_);
return state == State::Packed;
}
} // namespace __sanitizer
|