Skip to content

Commit 20c4999

Browse files
committed
Revert "[hwasan] Record and display stack history in stack-based reports."
This reverts commit r342921: test failures on clang-cmake-arm* bots. llvm-svn: 342922
1 parent 9043e17 commit 20c4999

21 files changed

+228
-908
lines changed

compiler-rt/lib/hwasan/CMakeLists.txt

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ set(HWASAN_RTL_SOURCES
1010
hwasan_poisoning.cc
1111
hwasan_report.cc
1212
hwasan_thread.cc
13-
hwasan_thread_list.cc
1413
)
1514

1615
set(HWASAN_RTL_CXX_SOURCES
@@ -26,9 +25,8 @@ set(HWASAN_RTL_HEADERS
2625
hwasan_mapping.h
2726
hwasan_poisoning.h
2827
hwasan_report.h
29-
hwasan_thread.h
30-
hwasan_thread_list.h
31-
)
28+
hwasan_thread.h)
29+
3230

3331
set(HWASAN_DEFINITIONS)
3432
append_list_if(COMPILER_RT_HWASAN_WITH_INTERCEPTORS HWASAN_WITH_INTERCEPTORS=1 HWASAN_DEFINITIONS)

compiler-rt/lib/hwasan/hwasan.cc

+4-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "hwasan_poisoning.h"
1818
#include "hwasan_report.h"
1919
#include "hwasan_thread.h"
20-
#include "hwasan_thread_list.h"
2120
#include "sanitizer_common/sanitizer_atomic.h"
2221
#include "sanitizer_common/sanitizer_common.h"
2322
#include "sanitizer_common/sanitizer_flags.h"
@@ -175,8 +174,7 @@ static void HWAsanCheckFailed(const char *file, int line, const char *cond,
175174
static constexpr uptr kMemoryUsageBufferSize = 4096;
176175

177176
static void HwasanFormatMemoryUsage(InternalScopedString &s) {
178-
HwasanThreadList &thread_list = hwasanThreadList();
179-
auto thread_stats = thread_list.GetThreadStats();
177+
auto thread_stats = Thread::GetThreadStats();
180178
auto *sds = StackDepotGetStats();
181179
AllocatorStatCounters asc;
182180
GetAllocatorStats(asc);
@@ -186,7 +184,7 @@ static void HwasanFormatMemoryUsage(InternalScopedString &s) {
186184
" heap: %zd",
187185
internal_getpid(), GetRSS(), thread_stats.n_live_threads,
188186
thread_stats.total_stack_size,
189-
thread_stats.n_live_threads * thread_list.MemoryUsedPerThread(),
187+
thread_stats.n_live_threads * Thread::MemoryUsedPerThread(),
190188
sds->allocated, sds->n_uniq_ids, asc[AllocatorStatMapped]);
191189
}
192190

@@ -255,12 +253,7 @@ void __hwasan_init() {
255253
__sanitizer_set_report_path(common_flags()->log_path);
256254

257255
DisableCoreDumperIfNecessary();
258-
259256
__hwasan_shadow_init();
260-
261-
InitThreads();
262-
hwasanThreadList().CreateCurrentThread();
263-
264257
MadviseShadow();
265258

266259
// This may call libc -> needs initialized shadow.
@@ -275,10 +268,11 @@ void __hwasan_init() {
275268
InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
276269

277270
HwasanTSDInit();
278-
HwasanTSDThreadInit();
279271

280272
HwasanAllocatorInit();
281273

274+
Thread::Create();
275+
282276
#if HWASAN_CONTAINS_UBSAN
283277
__ubsan::InitAsPlugin();
284278
#endif

compiler-rt/lib/hwasan/hwasan.h

-6
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ typedef u8 tag_t;
4141
const unsigned kAddressTagShift = 56;
4242
const uptr kAddressTagMask = 0xFFUL << kAddressTagShift;
4343

44-
// Minimal alignment of the shadow base address. Determines the space available
45-
// for threads and stack histories. This is an ABI constant.
46-
const unsigned kShadowBaseAlignment = 32;
47-
4844
static inline tag_t GetTagFromPointer(uptr p) {
4945
return p >> kAddressTagShift;
5046
}
@@ -70,7 +66,6 @@ extern int hwasan_report_count;
7066

7167
bool ProtectRange(uptr beg, uptr end);
7268
bool InitShadow();
73-
void InitThreads();
7469
void MadviseShadow();
7570
char *GetProcSelfMaps();
7671
void InitializeInterceptors();
@@ -147,7 +142,6 @@ class ScopedThreadLocalStateBackup {
147142
};
148143

149144
void HwasanTSDInit();
150-
void HwasanTSDThreadInit();
151145

152146
void HwasanOnDeadlySignal(int signo, void *info, void *context);
153147

compiler-rt/lib/hwasan/hwasan_dynamic_shadow.cc

+4-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
///
1414
//===----------------------------------------------------------------------===//
1515

16-
#include "hwasan.h"
1716
#include "hwasan_dynamic_shadow.h"
1817
#include "hwasan_mapping.h"
1918
#include "sanitizer_common/sanitizer_common.h"
@@ -36,16 +35,12 @@ static void UnmapFromTo(uptr from, uptr to) {
3635
}
3736
}
3837

39-
// Returns an address aligned to kShadowBaseAlignment, such that
40-
// 2**kShadowBaseAlingment on the left and shadow_size_bytes bytes on the right
41-
// of it are mapped no access.
38+
// Returns an address aligned to 8 pages, such that one page on the left and
39+
// shadow_size_bytes bytes on the right of it are mapped r/o.
4240
static uptr MapDynamicShadow(uptr shadow_size_bytes) {
4341
const uptr granularity = GetMmapGranularity();
44-
const uptr min_alignment = granularity << kShadowScale;
45-
const uptr alignment = 1ULL << kShadowBaseAlignment;
46-
CHECK_GE(alignment, min_alignment);
47-
48-
const uptr left_padding = 1ULL << kShadowBaseAlignment;
42+
const uptr alignment = granularity << kShadowScale;
43+
const uptr left_padding = granularity;
4944
const uptr shadow_size =
5045
RoundUpTo(shadow_size_bytes, granularity);
5146
const uptr map_size = shadow_size + left_padding + alignment;

compiler-rt/lib/hwasan/hwasan_flags.inc

-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,3 @@ HWASAN_FLAG(int, heap_history_size, 1023,
5151
"to find bugs.")
5252
HWASAN_FLAG(bool, export_memory_stats, true,
5353
"Export up-to-date memory stats through /proc")
54-
HWASAN_FLAG(int, stack_history_size, 1024,
55-
"The number of stack frames remembered per thread. "
56-
"Affects the quality of stack-related reports, but not the ability "
57-
"to find bugs.")

compiler-rt/lib/hwasan/hwasan_linux.cc

+26-44
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "hwasan_mapping.h"
2323
#include "hwasan_report.h"
2424
#include "hwasan_thread.h"
25-
#include "hwasan_thread_list.h"
2625

2726
#include <elf.h>
2827
#include <link.h>
@@ -38,10 +37,6 @@
3837
#include "sanitizer_common/sanitizer_common.h"
3938
#include "sanitizer_common/sanitizer_procmaps.h"
4039

41-
#if HWASAN_WITH_INTERCEPTORS && !SANITIZER_ANDROID
42-
THREADLOCAL uptr __hwasan_tls;
43-
#endif
44-
4540
namespace __hwasan {
4641

4742
static void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name) {
@@ -184,20 +179,6 @@ bool InitShadow() {
184179
return true;
185180
}
186181

187-
void InitThreads() {
188-
CHECK(__hwasan_shadow_memory_dynamic_address);
189-
uptr guard_page_size = GetMmapGranularity();
190-
uptr thread_space_start =
191-
__hwasan_shadow_memory_dynamic_address - (1ULL << kShadowBaseAlignment);
192-
uptr thread_space_end =
193-
__hwasan_shadow_memory_dynamic_address - guard_page_size;
194-
ReserveShadowMemoryRange(thread_space_start, thread_space_end - 1,
195-
"hwasan threads");
196-
ProtectGap(thread_space_end,
197-
__hwasan_shadow_memory_dynamic_address - thread_space_end);
198-
InitThreadList(thread_space_start, thread_space_end - thread_space_start);
199-
}
200-
201182
static void MadviseShadowRegion(uptr beg, uptr end) {
202183
uptr size = end - beg + 1;
203184
if (common_flags()->no_huge_pages_for_shadow)
@@ -233,33 +214,29 @@ void InstallAtExitHandler() {
233214
// ---------------------- TSD ---------------- {{{1
234215

235216
extern "C" void __hwasan_thread_enter() {
236-
hwasanThreadList().CreateCurrentThread();
217+
Thread::Create();
237218
}
238219

239220
extern "C" void __hwasan_thread_exit() {
240221
Thread *t = GetCurrentThread();
241222
// Make sure that signal handler can not see a stale current thread pointer.
242223
atomic_signal_fence(memory_order_seq_cst);
243224
if (t)
244-
hwasanThreadList().ReleaseThread(t);
225+
t->Destroy();
245226
}
246227

247228
#if HWASAN_WITH_INTERCEPTORS
248229
static pthread_key_t tsd_key;
249230
static bool tsd_key_inited = false;
250231

251-
void HwasanTSDThreadInit() {
252-
if (tsd_key_inited)
253-
CHECK_EQ(0, pthread_setspecific(tsd_key,
254-
(void *)GetPthreadDestructorIterations()));
255-
}
256-
257232
void HwasanTSDDtor(void *tsd) {
258-
uptr iterations = (uptr)tsd;
259-
if (iterations > 1) {
260-
CHECK_EQ(0, pthread_setspecific(tsd_key, (void *)(iterations - 1)));
233+
Thread *t = (Thread*)tsd;
234+
if (t->destructor_iterations_ > 1) {
235+
t->destructor_iterations_--;
236+
CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
261237
return;
262238
}
239+
t->Destroy();
263240
__hwasan_thread_exit();
264241
}
265242

@@ -268,26 +245,31 @@ void HwasanTSDInit() {
268245
tsd_key_inited = true;
269246
CHECK_EQ(0, pthread_key_create(&tsd_key, HwasanTSDDtor));
270247
}
271-
#else
272-
void HwasanTSDInit() {}
273-
void HwasanTSDThreadInit() {}
274-
#endif
275248

276-
#if SANITIZER_ANDROID
277-
uptr *GetCurrentThreadLongPtr() {
278-
return (uptr *)get_android_tls_ptr();
279-
}
280-
#else
281-
uptr *GetCurrentThreadLongPtr() {
282-
return &__hwasan_tls;
249+
Thread *GetCurrentThread() {
250+
return (Thread *)pthread_getspecific(tsd_key);
283251
}
284-
#endif
285252

253+
void SetCurrentThread(Thread *t) {
254+
// Make sure that HwasanTSDDtor gets called at the end.
255+
CHECK(tsd_key_inited);
256+
// Make sure we do not reset the current Thread.
257+
CHECK_EQ(0, pthread_getspecific(tsd_key));
258+
pthread_setspecific(tsd_key, (void *)t);
259+
}
260+
#elif SANITIZER_ANDROID
261+
void HwasanTSDInit() {}
286262
Thread *GetCurrentThread() {
287-
auto *R = (StackAllocationsRingBuffer*)GetCurrentThreadLongPtr();
288-
return hwasanThreadList().GetThreadByBufferAddress((uptr)(R->Next()));
263+
return (Thread*)*get_android_tls_ptr();
289264
}
290265

266+
void SetCurrentThread(Thread *t) {
267+
*get_android_tls_ptr() = (uptr)t;
268+
}
269+
#else
270+
#error unsupported configuration !HWASAN_WITH_INTERCEPTORS && !SANITIZER_ANDROID
271+
#endif
272+
291273
struct AccessInfo {
292274
uptr addr;
293275
uptr size;

compiler-rt/lib/hwasan/hwasan_report.cc

+5-58
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "hwasan_allocator.h"
1717
#include "hwasan_mapping.h"
1818
#include "hwasan_thread.h"
19-
#include "hwasan_thread_list.h"
2019
#include "sanitizer_common/sanitizer_allocator_internal.h"
2120
#include "sanitizer_common/sanitizer_common.h"
2221
#include "sanitizer_common/sanitizer_flags.h"
@@ -36,31 +35,6 @@ static StackTrace GetStackTraceFromId(u32 id) {
3635
return res;
3736
}
3837

39-
// A RAII object that holds a copy of the current thread stack ring buffer.
40-
// The actual stack buffer may change while we are iterating over it (for
41-
// example, Printf may call syslog() which can itself be built with hwasan).
42-
class SavedStackAllocations {
43-
public:
44-
SavedStackAllocations(StackAllocationsRingBuffer *rb) {
45-
uptr size = rb->size() * sizeof(uptr);
46-
void *storage =
47-
MmapAlignedOrDieOnFatalError(size, size * 2, "saved stack allocations");
48-
new (&rb_) StackAllocationsRingBuffer(*rb, storage);
49-
}
50-
51-
~SavedStackAllocations() {
52-
StackAllocationsRingBuffer *rb = get();
53-
UnmapOrDie(rb->StartOfStorage(), rb->size() * sizeof(uptr));
54-
}
55-
56-
StackAllocationsRingBuffer *get() {
57-
return (StackAllocationsRingBuffer *)&rb_;
58-
}
59-
60-
private:
61-
uptr rb_;
62-
};
63-
6438
class Decorator: public __sanitizer::SanitizerCommonDecorator {
6539
public:
6640
Decorator() : SanitizerCommonDecorator() { }
@@ -89,9 +63,7 @@ uptr FindHeapAllocation(HeapAllocationsRingBuffer *rb,
8963
return 0;
9064
}
9165

92-
void PrintAddressDescription(
93-
uptr tagged_addr, uptr access_size,
94-
StackAllocationsRingBuffer *current_stack_allocations) {
66+
void PrintAddressDescription(uptr tagged_addr, uptr access_size) {
9567
Decorator d;
9668
int num_descriptions_printed = 0;
9769
uptr untagged_addr = UntagAddr(tagged_addr);
@@ -137,7 +109,7 @@ void PrintAddressDescription(
137109
}
138110
}
139111

140-
hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {
112+
Thread::VisitAllLiveThreads([&](Thread *t) {
141113
// Scan all threads' ring buffers to find if it's a heap-use-after-free.
142114
HeapAllocationRecord har;
143115
if (uptr D = FindHeapAllocation(t->heap_allocations(), tagged_addr, &har)) {
@@ -173,25 +145,6 @@ void PrintAddressDescription(
173145
Printf("%s", d.Default());
174146
t->Announce();
175147

176-
// Temporary report section, needs to be improved.
177-
Printf("Previosly allocated frames:\n");
178-
auto *sa = (t == GetCurrentThread() && current_stack_allocations)
179-
? current_stack_allocations
180-
: t->stack_allocations();
181-
uptr frames = Min((uptr)flags()->stack_history_size, sa->size());
182-
for (uptr i = 0; i < frames; i++) {
183-
uptr record = (*sa)[i];
184-
if (!record)
185-
break;
186-
uptr sp = (record >> 48) << 4;
187-
uptr pc_mask = (1ULL << 48) - 1;
188-
uptr pc = record & pc_mask;
189-
uptr fixed_pc = StackTrace::GetNextInstructionPc(pc);
190-
StackTrace stack(&fixed_pc, 1);
191-
Printf("record: %p pc: %p sp: %p", record, pc, sp);
192-
stack.Print();
193-
}
194-
195148
num_descriptions_printed++;
196149
}
197150
});
@@ -217,16 +170,13 @@ void ReportStats() {}
217170
void ReportInvalidAccessInsideAddressRange(const char *what, const void *start,
218171
uptr size, uptr offset) {
219172
ScopedErrorReportLock l;
220-
SavedStackAllocations current_stack_allocations(
221-
GetCurrentThread()->stack_allocations());
222173

223174
Decorator d;
224175
Printf("%s", d.Warning());
225176
Printf("%sTag mismatch in %s%s%s at offset %zu inside [%p, %zu)%s\n",
226177
d.Warning(), d.Name(), what, d.Warning(), offset, start, size,
227178
d.Default());
228-
PrintAddressDescription((uptr)start + offset, 1,
229-
current_stack_allocations.get());
179+
PrintAddressDescription((uptr)start + offset, 1);
230180
// if (__sanitizer::Verbosity())
231181
// DescribeMemoryRange(start, size);
232182
}
@@ -274,7 +224,7 @@ void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) {
274224

275225
stack->Print();
276226

277-
PrintAddressDescription(tagged_addr, 0, nullptr);
227+
PrintAddressDescription(tagged_addr, 0);
278228

279229
PrintTagsAroundAddr(tag_ptr);
280230

@@ -285,8 +235,6 @@ void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) {
285235
void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size,
286236
bool is_store) {
287237
ScopedErrorReportLock l;
288-
SavedStackAllocations current_stack_allocations(
289-
GetCurrentThread()->stack_allocations());
290238

291239
Decorator d;
292240
Printf("%s", d.Error());
@@ -310,8 +258,7 @@ void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size,
310258

311259
stack->Print();
312260

313-
PrintAddressDescription(tagged_addr, access_size,
314-
current_stack_allocations.get());
261+
PrintAddressDescription(tagged_addr, access_size);
315262
t->Announce();
316263

317264
PrintTagsAroundAddr(tag_ptr);

0 commit comments

Comments
 (0)