2019-02-27 23:44:03 +08:00
|
|
|
//===-- hwasan_report.cpp -------------------------------------------------===//
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of HWAddressSanitizer.
|
|
|
|
//
|
|
|
|
// Error reporting.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "hwasan.h"
|
|
|
|
#include "hwasan_allocator.h"
|
2018-04-24 02:19:23 +08:00
|
|
|
#include "hwasan_mapping.h"
|
[HWASan] Save + print registers when tag mismatch occurs in AArch64.
Summary:
This change change the instrumentation to allow users to view the registers at the point at which tag mismatch occured. Most of the heavy lifting is done in the runtime library, where we save the registers to the stack and emit unwind information. This allows us to reduce the overhead, as very little additional work needs to be done in each __hwasan_check instance.
In this implementation, the fast path of __hwasan_check is unmodified. There are an additional 4 instructions (16B) emitted in the slow path in every __hwasan_check instance. This may increase binary size somewhat, but as most of the work is done in the runtime library, it's manageable.
The failure trace now contains a list of registers at the point of which the failure occured, in a format similar to that of Android's tombstones. It currently has the following format:
Registers where the failure occurred (pc 0x0055555561b4):
x0 0000000000000014 x1 0000007ffffff6c0 x2 1100007ffffff6d0 x3 12000056ffffe025
x4 0000007fff800000 x5 0000000000000014 x6 0000007fff800000 x7 0000000000000001
x8 12000056ffffe020 x9 0200007700000000 x10 0200007700000000 x11 0000000000000000
x12 0000007fffffdde0 x13 0000000000000000 x14 02b65b01f7a97490 x15 0000000000000000
x16 0000007fb77376b8 x17 0000000000000012 x18 0000007fb7ed6000 x19 0000005555556078
x20 0000007ffffff768 x21 0000007ffffff778 x22 0000000000000001 x23 0000000000000000
x24 0000000000000000 x25 0000000000000000 x26 0000000000000000 x27 0000000000000000
x28 0000000000000000 x29 0000007ffffff6f0 x30 00000055555561b4
... and prints after the dump of memory tags around the buggy address.
Every register is saved exactly as it was at the point where the tag mismatch occurs, with the exception of x16/x17. These registers are used in the tag mismatch calculation as scratch registers during __hwasan_check, and cannot be saved without affecting the fast path. As these registers are designated as scratch registers for linking, there should be no important information in them that could aid in debugging.
Reviewers: pcc, eugenis
Reviewed By: pcc, eugenis
Subscribers: srhines, kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, hiraditya, jdoerfert, llvm-commits, #sanitizers
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58857
llvm-svn: 355738
2019-03-09 05:22:35 +08:00
|
|
|
#include "hwasan_report.h"
|
2018-08-30 05:07:07 +08:00
|
|
|
#include "hwasan_thread.h"
|
2018-09-25 07:03:34 +08:00
|
|
|
#include "hwasan_thread_list.h"
|
2017-12-09 09:31:51 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_internal.h"
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
|
|
|
#include "sanitizer_common/sanitizer_mutex.h"
|
|
|
|
#include "sanitizer_common/sanitizer_report_decorator.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
2018-10-11 08:34:20 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stacktrace_printer.h"
|
2017-12-09 09:31:51 +08:00
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
|
|
|
|
|
|
|
using namespace __sanitizer;
|
|
|
|
|
|
|
|
namespace __hwasan {
|
|
|
|
|
2018-11-10 05:54:03 +08:00
|
|
|
class ScopedReport {
|
|
|
|
public:
|
|
|
|
ScopedReport(bool fatal = false) : error_message_(1), fatal(fatal) {
|
|
|
|
BlockingMutexLock lock(&error_message_lock_);
|
|
|
|
error_message_ptr_ = fatal ? &error_message_ : nullptr;
|
2019-01-25 10:05:25 +08:00
|
|
|
++hwasan_report_count;
|
2018-11-10 05:54:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedReport() {
|
2019-01-25 10:05:25 +08:00
|
|
|
{
|
|
|
|
BlockingMutexLock lock(&error_message_lock_);
|
|
|
|
if (fatal)
|
|
|
|
SetAbortMessage(error_message_.data());
|
|
|
|
error_message_ptr_ = nullptr;
|
2018-11-10 05:54:03 +08:00
|
|
|
}
|
2019-01-25 10:05:25 +08:00
|
|
|
if (common_flags()->print_module_map >= 2 ||
|
|
|
|
(fatal && common_flags()->print_module_map))
|
|
|
|
DumpProcessMap();
|
|
|
|
if (fatal)
|
|
|
|
Die();
|
2018-11-10 05:54:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MaybeAppendToErrorMessage(const char *msg) {
|
|
|
|
BlockingMutexLock lock(&error_message_lock_);
|
|
|
|
if (!error_message_ptr_)
|
|
|
|
return;
|
|
|
|
uptr len = internal_strlen(msg);
|
|
|
|
uptr old_size = error_message_ptr_->size();
|
|
|
|
error_message_ptr_->resize(old_size + len);
|
|
|
|
// overwrite old trailing '\0', keep new trailing '\0' untouched.
|
|
|
|
internal_memcpy(&(*error_message_ptr_)[old_size - 1], msg, len);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
ScopedErrorReportLock error_report_lock_;
|
|
|
|
InternalMmapVector<char> error_message_;
|
|
|
|
bool fatal;
|
|
|
|
|
|
|
|
static InternalMmapVector<char> *error_message_ptr_;
|
|
|
|
static BlockingMutex error_message_lock_;
|
|
|
|
};
|
|
|
|
|
|
|
|
InternalMmapVector<char> *ScopedReport::error_message_ptr_;
|
|
|
|
BlockingMutex ScopedReport::error_message_lock_;
|
|
|
|
|
|
|
|
// If there is an active ScopedReport, append to its error message.
|
|
|
|
void AppendToErrorMessageBuffer(const char *buffer) {
|
|
|
|
ScopedReport::MaybeAppendToErrorMessage(buffer);
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
static StackTrace GetStackTraceFromId(u32 id) {
|
|
|
|
CHECK(id);
|
|
|
|
StackTrace res = StackDepotGet(id);
|
|
|
|
CHECK(res.trace);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-09-25 07:03:34 +08:00
|
|
|
// A RAII object that holds a copy of the current thread stack ring buffer.
|
|
|
|
// The actual stack buffer may change while we are iterating over it (for
|
|
|
|
// example, Printf may call syslog() which can itself be built with hwasan).
|
|
|
|
class SavedStackAllocations {
|
|
|
|
public:
|
|
|
|
SavedStackAllocations(StackAllocationsRingBuffer *rb) {
|
|
|
|
uptr size = rb->size() * sizeof(uptr);
|
|
|
|
void *storage =
|
|
|
|
MmapAlignedOrDieOnFatalError(size, size * 2, "saved stack allocations");
|
|
|
|
new (&rb_) StackAllocationsRingBuffer(*rb, storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SavedStackAllocations() {
|
|
|
|
StackAllocationsRingBuffer *rb = get();
|
|
|
|
UnmapOrDie(rb->StartOfStorage(), rb->size() * sizeof(uptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
StackAllocationsRingBuffer *get() {
|
|
|
|
return (StackAllocationsRingBuffer *)&rb_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uptr rb_;
|
|
|
|
};
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
class Decorator: public __sanitizer::SanitizerCommonDecorator {
|
|
|
|
public:
|
|
|
|
Decorator() : SanitizerCommonDecorator() { }
|
2018-08-23 06:55:16 +08:00
|
|
|
const char *Access() { return Blue(); }
|
2018-06-08 07:33:33 +08:00
|
|
|
const char *Allocation() const { return Magenta(); }
|
|
|
|
const char *Origin() const { return Magenta(); }
|
|
|
|
const char *Name() const { return Green(); }
|
2018-08-31 06:11:56 +08:00
|
|
|
const char *Location() { return Green(); }
|
2018-09-05 09:16:50 +08:00
|
|
|
const char *Thread() { return Green(); }
|
2017-12-09 09:31:51 +08:00
|
|
|
};
|
|
|
|
|
2018-09-12 08:58:15 +08:00
|
|
|
// Returns the index of the rb element that matches tagged_addr (plus one),
|
|
|
|
// or zero if found nothing.
|
|
|
|
uptr FindHeapAllocation(HeapAllocationsRingBuffer *rb,
|
2018-08-30 05:07:07 +08:00
|
|
|
uptr tagged_addr,
|
|
|
|
HeapAllocationRecord *har) {
|
2018-09-12 08:58:15 +08:00
|
|
|
if (!rb) return 0;
|
2018-08-30 05:07:07 +08:00
|
|
|
for (uptr i = 0, size = rb->size(); i < size; i++) {
|
|
|
|
auto h = (*rb)[i];
|
|
|
|
if (h.tagged_addr <= tagged_addr &&
|
|
|
|
h.tagged_addr + h.requested_size > tagged_addr) {
|
|
|
|
*har = h;
|
2018-09-12 08:58:15 +08:00
|
|
|
return i + 1;
|
2018-08-30 05:07:07 +08:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 08:58:15 +08:00
|
|
|
return 0;
|
2018-08-30 05:07:07 +08:00
|
|
|
}
|
|
|
|
|
2018-09-25 07:03:34 +08:00
|
|
|
void PrintAddressDescription(
|
|
|
|
uptr tagged_addr, uptr access_size,
|
|
|
|
StackAllocationsRingBuffer *current_stack_allocations) {
|
2018-08-31 11:29:09 +08:00
|
|
|
Decorator d;
|
2018-08-31 06:11:56 +08:00
|
|
|
int num_descriptions_printed = 0;
|
|
|
|
uptr untagged_addr = UntagAddr(tagged_addr);
|
2018-10-11 06:24:44 +08:00
|
|
|
|
|
|
|
// Print some very basic information about the address, if it's a heap.
|
|
|
|
HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr);
|
|
|
|
if (uptr beg = chunk.Beg()) {
|
|
|
|
uptr size = chunk.ActualSize();
|
|
|
|
Printf("%s[%p,%p) is a %s %s heap chunk; "
|
|
|
|
"size: %zd offset: %zd\n%s",
|
|
|
|
d.Location(),
|
|
|
|
beg, beg + size,
|
|
|
|
chunk.FromSmallHeap() ? "small" : "large",
|
|
|
|
chunk.IsAllocated() ? "allocated" : "unallocated",
|
|
|
|
size, untagged_addr - beg,
|
|
|
|
d.Default());
|
|
|
|
}
|
|
|
|
|
2018-08-31 11:29:09 +08:00
|
|
|
// Check if this looks like a heap buffer overflow by scanning
|
|
|
|
// the shadow left and right and looking for the first adjacent
|
|
|
|
// object with a different memory tag. If that tag matches addr_tag,
|
|
|
|
// check the allocator if it has a live chunk there.
|
|
|
|
tag_t addr_tag = GetTagFromPointer(tagged_addr);
|
|
|
|
tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr));
|
|
|
|
if (*tag_ptr != addr_tag) { // should be true usually.
|
|
|
|
tag_t *left = tag_ptr, *right = tag_ptr;
|
|
|
|
// scan left.
|
|
|
|
for (int i = 0; i < 1000 && *left == *tag_ptr; i++, left--){}
|
|
|
|
// scan right.
|
|
|
|
for (int i = 0; i < 1000 && *right == *tag_ptr; i++, right++){}
|
|
|
|
// Chose the object that has addr_tag and that is closer to addr.
|
|
|
|
tag_t *candidate = nullptr;
|
|
|
|
if (*right == addr_tag && *left == addr_tag)
|
|
|
|
candidate = right - tag_ptr < tag_ptr - left ? right : left;
|
|
|
|
else if (*right == addr_tag)
|
|
|
|
candidate = right;
|
|
|
|
else if (*left == addr_tag)
|
|
|
|
candidate = left;
|
2018-08-31 11:18:31 +08:00
|
|
|
|
2018-08-31 11:29:09 +08:00
|
|
|
if (candidate) {
|
|
|
|
uptr mem = ShadowToMem(reinterpret_cast<uptr>(candidate));
|
|
|
|
HwasanChunkView chunk = FindHeapChunkByAddress(mem);
|
|
|
|
if (chunk.IsAllocated()) {
|
|
|
|
Printf("%s", d.Location());
|
|
|
|
Printf(
|
|
|
|
"%p is located %zd bytes to the %s of %zd-byte region [%p,%p)\n",
|
|
|
|
untagged_addr,
|
|
|
|
candidate == left ? untagged_addr - chunk.End()
|
|
|
|
: chunk.Beg() - untagged_addr,
|
|
|
|
candidate == right ? "left" : "right", chunk.UsedSize(),
|
|
|
|
chunk.Beg(), chunk.End());
|
|
|
|
Printf("%s", d.Allocation());
|
|
|
|
Printf("allocated here:\n");
|
|
|
|
Printf("%s", d.Default());
|
|
|
|
GetStackTraceFromId(chunk.GetAllocStackId()).Print();
|
|
|
|
num_descriptions_printed++;
|
2018-08-31 11:18:31 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 11:29:09 +08:00
|
|
|
}
|
2018-08-31 11:18:31 +08:00
|
|
|
|
2018-09-25 07:03:34 +08:00
|
|
|
hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {
|
2018-08-31 11:18:31 +08:00
|
|
|
// Scan all threads' ring buffers to find if it's a heap-use-after-free.
|
2018-08-31 06:11:56 +08:00
|
|
|
HeapAllocationRecord har;
|
2018-09-12 08:58:15 +08:00
|
|
|
if (uptr D = FindHeapAllocation(t->heap_allocations(), tagged_addr, &har)) {
|
2018-08-31 06:11:56 +08:00
|
|
|
Printf("%s", d.Location());
|
|
|
|
Printf("%p is located %zd bytes inside of %zd-byte region [%p,%p)\n",
|
|
|
|
untagged_addr, untagged_addr - UntagAddr(har.tagged_addr),
|
|
|
|
har.requested_size, UntagAddr(har.tagged_addr),
|
|
|
|
UntagAddr(har.tagged_addr) + har.requested_size);
|
|
|
|
Printf("%s", d.Allocation());
|
2018-09-05 09:16:50 +08:00
|
|
|
Printf("freed by thread T%zd here:\n", t->unique_id());
|
2018-08-31 06:11:56 +08:00
|
|
|
Printf("%s", d.Default());
|
|
|
|
GetStackTraceFromId(har.free_context_id).Print();
|
|
|
|
|
|
|
|
Printf("%s", d.Allocation());
|
|
|
|
Printf("previously allocated here:\n", t);
|
|
|
|
Printf("%s", d.Default());
|
|
|
|
GetStackTraceFromId(har.alloc_context_id).Print();
|
|
|
|
|
2018-09-12 08:58:15 +08:00
|
|
|
// Print a developer note: the index of this heap object
|
|
|
|
// in the thread's deallocation ring buffer.
|
|
|
|
Printf("hwasan_dev_note_heap_rb_distance: %zd %zd\n", D,
|
|
|
|
flags()->heap_history_size);
|
|
|
|
|
2018-10-11 02:56:31 +08:00
|
|
|
t->Announce();
|
2018-08-31 06:11:56 +08:00
|
|
|
num_descriptions_printed++;
|
|
|
|
}
|
2018-08-31 11:18:31 +08:00
|
|
|
|
|
|
|
// Very basic check for stack memory.
|
2018-08-31 09:38:00 +08:00
|
|
|
if (t->AddrIsInStack(untagged_addr)) {
|
|
|
|
Printf("%s", d.Location());
|
2018-09-05 09:16:50 +08:00
|
|
|
Printf("Address %p is located in stack of thread T%zd\n", untagged_addr,
|
|
|
|
t->unique_id());
|
2018-09-05 09:27:48 +08:00
|
|
|
Printf("%s", d.Default());
|
2018-09-05 09:16:50 +08:00
|
|
|
t->Announce();
|
|
|
|
|
2018-09-25 07:03:34 +08:00
|
|
|
// Temporary report section, needs to be improved.
|
2018-12-15 15:06:24 +08:00
|
|
|
Printf("Previously allocated frames:\n");
|
2018-09-25 07:03:34 +08:00
|
|
|
auto *sa = (t == GetCurrentThread() && current_stack_allocations)
|
|
|
|
? current_stack_allocations
|
|
|
|
: t->stack_allocations();
|
|
|
|
uptr frames = Min((uptr)flags()->stack_history_size, sa->size());
|
2018-10-11 08:34:20 +08:00
|
|
|
InternalScopedString frame_desc(GetPageSizeCached() * 2);
|
2018-09-25 07:03:34 +08:00
|
|
|
for (uptr i = 0; i < frames; i++) {
|
|
|
|
uptr record = (*sa)[i];
|
|
|
|
if (!record)
|
|
|
|
break;
|
|
|
|
uptr sp = (record >> 48) << 4;
|
|
|
|
uptr pc_mask = (1ULL << 48) - 1;
|
|
|
|
uptr pc = record & pc_mask;
|
2018-10-11 08:34:20 +08:00
|
|
|
if (SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc)) {
|
2019-02-16 02:38:23 +08:00
|
|
|
frame_desc.append(" sp: 0x%zx ", sp);
|
|
|
|
RenderFrame(&frame_desc, "#%n %p %F %L\n", 0, frame->info,
|
2018-10-11 08:34:20 +08:00
|
|
|
common_flags()->symbolize_vs_style,
|
|
|
|
common_flags()->strip_path_prefix);
|
|
|
|
frame->ClearAll();
|
2018-10-24 09:35:50 +08:00
|
|
|
if (auto Descr = GetStackFrameDescr(pc))
|
|
|
|
frame_desc.append(" %s\n", Descr);
|
2018-10-11 08:34:20 +08:00
|
|
|
}
|
|
|
|
Printf("%s", frame_desc.data());
|
|
|
|
frame_desc.clear();
|
2018-09-25 07:03:34 +08:00
|
|
|
}
|
|
|
|
|
2018-08-31 09:38:00 +08:00
|
|
|
num_descriptions_printed++;
|
|
|
|
}
|
2018-08-31 06:11:56 +08:00
|
|
|
});
|
2017-12-09 09:31:51 +08:00
|
|
|
|
2018-10-11 02:56:31 +08:00
|
|
|
// Print the remaining threads, as an extra information, 1 line per thread.
|
|
|
|
hwasanThreadList().VisitAllLiveThreads([&](Thread *t) { t->Announce(); });
|
|
|
|
|
2018-08-31 06:11:56 +08:00
|
|
|
if (!num_descriptions_printed)
|
|
|
|
// We exhausted our possibilities. Bail out.
|
|
|
|
Printf("HWAddressSanitizer can not describe address in more detail.\n");
|
2017-12-09 09:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReportStats() {}
|
|
|
|
|
2018-08-24 09:12:26 +08:00
|
|
|
static void PrintTagsAroundAddr(tag_t *tag_ptr) {
|
|
|
|
Printf(
|
|
|
|
"Memory tags around the buggy address (one tag corresponds to %zd "
|
|
|
|
"bytes):\n", kShadowAlignment);
|
|
|
|
|
|
|
|
const uptr row_len = 16; // better be power of two.
|
2018-10-11 02:32:31 +08:00
|
|
|
const uptr num_rows = 17;
|
2018-08-24 09:12:26 +08:00
|
|
|
tag_t *center_row_beg = reinterpret_cast<tag_t *>(
|
|
|
|
RoundDownTo(reinterpret_cast<uptr>(tag_ptr), row_len));
|
|
|
|
tag_t *beg_row = center_row_beg - row_len * (num_rows / 2);
|
|
|
|
tag_t *end_row = center_row_beg + row_len * (num_rows / 2);
|
2018-10-11 02:32:31 +08:00
|
|
|
InternalScopedString s(GetPageSizeCached() * 8);
|
2018-08-24 09:12:26 +08:00
|
|
|
for (tag_t *row = beg_row; row < end_row; row += row_len) {
|
2018-09-14 03:14:22 +08:00
|
|
|
s.append("%s", row == center_row_beg ? "=>" : " ");
|
2018-08-24 09:12:26 +08:00
|
|
|
for (uptr i = 0; i < row_len; i++) {
|
2018-09-14 03:14:22 +08:00
|
|
|
s.append("%s", row + i == tag_ptr ? "[" : " ");
|
|
|
|
s.append("%02x", row[i]);
|
|
|
|
s.append("%s", row + i == tag_ptr ? "]" : " ");
|
2018-08-24 09:12:26 +08:00
|
|
|
}
|
2018-09-14 03:14:22 +08:00
|
|
|
s.append("%s\n", row == center_row_beg ? "<=" : " ");
|
2018-08-24 09:12:26 +08:00
|
|
|
}
|
2018-10-11 02:32:31 +08:00
|
|
|
Printf("%s", s.data());
|
2018-08-24 09:12:26 +08:00
|
|
|
}
|
|
|
|
|
2018-08-30 06:21:22 +08:00
|
|
|
void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) {
|
2018-11-10 05:54:03 +08:00
|
|
|
ScopedReport R(flags()->halt_on_error);
|
|
|
|
|
2018-08-30 06:21:22 +08:00
|
|
|
uptr untagged_addr = UntagAddr(tagged_addr);
|
|
|
|
tag_t ptr_tag = GetTagFromPointer(tagged_addr);
|
2018-08-30 06:42:16 +08:00
|
|
|
tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr));
|
2018-08-24 09:12:26 +08:00
|
|
|
tag_t mem_tag = *tag_ptr;
|
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Error());
|
|
|
|
uptr pc = stack->size ? stack->trace[0] : 0;
|
|
|
|
const char *bug_type = "invalid-free";
|
|
|
|
Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,
|
2018-08-30 06:21:22 +08:00
|
|
|
untagged_addr, pc);
|
2018-08-24 09:12:26 +08:00
|
|
|
Printf("%s", d.Access());
|
|
|
|
Printf("tags: %02x/%02x (ptr/mem)\n", ptr_tag, mem_tag);
|
|
|
|
Printf("%s", d.Default());
|
|
|
|
|
|
|
|
stack->Print();
|
|
|
|
|
2018-09-25 07:03:34 +08:00
|
|
|
PrintAddressDescription(tagged_addr, 0, nullptr);
|
2018-08-24 09:12:26 +08:00
|
|
|
|
|
|
|
PrintTagsAroundAddr(tag_ptr);
|
|
|
|
|
|
|
|
ReportErrorSummary(bug_type, stack);
|
|
|
|
}
|
|
|
|
|
2018-11-17 08:25:17 +08:00
|
|
|
void ReportTailOverwritten(StackTrace *stack, uptr tagged_addr, uptr orig_size,
|
|
|
|
uptr tail_size, const u8 *expected) {
|
|
|
|
ScopedReport R(flags()->halt_on_error);
|
|
|
|
Decorator d;
|
|
|
|
uptr untagged_addr = UntagAddr(tagged_addr);
|
|
|
|
Printf("%s", d.Error());
|
|
|
|
const char *bug_type = "alocation-tail-overwritten";
|
|
|
|
Report("ERROR: %s: %s; heap object [%p,%p) of size %zd\n", SanitizerToolName,
|
|
|
|
bug_type, untagged_addr, untagged_addr + orig_size, orig_size);
|
|
|
|
Printf("\n%s", d.Default());
|
|
|
|
stack->Print();
|
|
|
|
HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr);
|
|
|
|
if (chunk.Beg()) {
|
|
|
|
Printf("%s", d.Allocation());
|
|
|
|
Printf("allocated here:\n");
|
|
|
|
Printf("%s", d.Default());
|
|
|
|
GetStackTraceFromId(chunk.GetAllocStackId()).Print();
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalScopedString s(GetPageSizeCached() * 8);
|
|
|
|
CHECK_GT(tail_size, 0U);
|
|
|
|
CHECK_LT(tail_size, kShadowAlignment);
|
|
|
|
u8 *tail = reinterpret_cast<u8*>(untagged_addr + orig_size);
|
|
|
|
s.append("Tail contains: ");
|
|
|
|
for (uptr i = 0; i < kShadowAlignment - tail_size; i++)
|
|
|
|
s.append(".. ");
|
|
|
|
for (uptr i = 0; i < tail_size; i++)
|
|
|
|
s.append("%02x ", tail[i]);
|
|
|
|
s.append("\n");
|
|
|
|
s.append("Expected: ");
|
|
|
|
for (uptr i = 0; i < kShadowAlignment - tail_size; i++)
|
|
|
|
s.append(".. ");
|
|
|
|
for (uptr i = 0; i < tail_size; i++)
|
|
|
|
s.append("%02x ", expected[i]);
|
|
|
|
s.append("\n");
|
|
|
|
s.append(" ");
|
|
|
|
for (uptr i = 0; i < kShadowAlignment - tail_size; i++)
|
|
|
|
s.append(" ");
|
|
|
|
for (uptr i = 0; i < tail_size; i++)
|
|
|
|
s.append("%s ", expected[i] != tail[i] ? "^^" : " ");
|
|
|
|
|
|
|
|
s.append("\nThis error occurs when a buffer overflow overwrites memory\n"
|
|
|
|
"to the right of a heap object, but within the %zd-byte granule, e.g.\n"
|
|
|
|
" char *x = new char[20];\n"
|
|
|
|
" x[25] = 42;\n"
|
|
|
|
"By default %s does not detect such bugs at the time of write,\n"
|
|
|
|
"but can detect them at the time of free/delete.\n"
|
|
|
|
"To disable this feature set HWASAN_OPTIONS=free_checks_tail_magic=0;\n"
|
|
|
|
"To enable checking at the time of access, set "
|
|
|
|
"HWASAN_OPTIONS=malloc_align_right to non-zero\n\n",
|
|
|
|
kShadowAlignment, SanitizerToolName);
|
|
|
|
Printf("%s", s.data());
|
|
|
|
GetCurrentThread()->Announce();
|
|
|
|
|
|
|
|
tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr));
|
|
|
|
PrintTagsAroundAddr(tag_ptr);
|
|
|
|
|
|
|
|
ReportErrorSummary(bug_type, stack);
|
|
|
|
}
|
|
|
|
|
2018-08-30 06:21:22 +08:00
|
|
|
void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size,
|
[HWASan] Save + print registers when tag mismatch occurs in AArch64.
Summary:
This change change the instrumentation to allow users to view the registers at the point at which tag mismatch occured. Most of the heavy lifting is done in the runtime library, where we save the registers to the stack and emit unwind information. This allows us to reduce the overhead, as very little additional work needs to be done in each __hwasan_check instance.
In this implementation, the fast path of __hwasan_check is unmodified. There are an additional 4 instructions (16B) emitted in the slow path in every __hwasan_check instance. This may increase binary size somewhat, but as most of the work is done in the runtime library, it's manageable.
The failure trace now contains a list of registers at the point of which the failure occured, in a format similar to that of Android's tombstones. It currently has the following format:
Registers where the failure occurred (pc 0x0055555561b4):
x0 0000000000000014 x1 0000007ffffff6c0 x2 1100007ffffff6d0 x3 12000056ffffe025
x4 0000007fff800000 x5 0000000000000014 x6 0000007fff800000 x7 0000000000000001
x8 12000056ffffe020 x9 0200007700000000 x10 0200007700000000 x11 0000000000000000
x12 0000007fffffdde0 x13 0000000000000000 x14 02b65b01f7a97490 x15 0000000000000000
x16 0000007fb77376b8 x17 0000000000000012 x18 0000007fb7ed6000 x19 0000005555556078
x20 0000007ffffff768 x21 0000007ffffff778 x22 0000000000000001 x23 0000000000000000
x24 0000000000000000 x25 0000000000000000 x26 0000000000000000 x27 0000000000000000
x28 0000000000000000 x29 0000007ffffff6f0 x30 00000055555561b4
... and prints after the dump of memory tags around the buggy address.
Every register is saved exactly as it was at the point where the tag mismatch occurs, with the exception of x16/x17. These registers are used in the tag mismatch calculation as scratch registers during __hwasan_check, and cannot be saved without affecting the fast path. As these registers are designated as scratch registers for linking, there should be no important information in them that could aid in debugging.
Reviewers: pcc, eugenis
Reviewed By: pcc, eugenis
Subscribers: srhines, kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, hiraditya, jdoerfert, llvm-commits, #sanitizers
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58857
llvm-svn: 355738
2019-03-09 05:22:35 +08:00
|
|
|
bool is_store, bool fatal, uptr *registers_frame) {
|
2018-11-10 05:54:03 +08:00
|
|
|
ScopedReport R(fatal);
|
2018-09-25 07:03:34 +08:00
|
|
|
SavedStackAllocations current_stack_allocations(
|
|
|
|
GetCurrentThread()->stack_allocations());
|
2017-12-09 09:31:51 +08:00
|
|
|
|
|
|
|
Decorator d;
|
2018-08-23 06:55:16 +08:00
|
|
|
Printf("%s", d.Error());
|
2018-08-30 06:21:22 +08:00
|
|
|
uptr untagged_addr = UntagAddr(tagged_addr);
|
2018-08-23 06:55:16 +08:00
|
|
|
// TODO: when possible, try to print heap-use-after-free, etc.
|
|
|
|
const char *bug_type = "tag-mismatch";
|
|
|
|
uptr pc = stack->size ? stack->trace[0] : 0;
|
2018-08-24 09:12:26 +08:00
|
|
|
Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,
|
2018-08-30 06:21:22 +08:00
|
|
|
untagged_addr, pc);
|
2017-12-09 09:31:51 +08:00
|
|
|
|
2018-09-05 09:16:50 +08:00
|
|
|
Thread *t = GetCurrentThread();
|
|
|
|
|
2019-01-21 17:51:10 +08:00
|
|
|
sptr offset =
|
|
|
|
__hwasan_test_shadow(reinterpret_cast<void *>(tagged_addr), access_size);
|
|
|
|
CHECK(offset >= 0 && offset < static_cast<sptr>(access_size));
|
2018-08-30 06:21:22 +08:00
|
|
|
tag_t ptr_tag = GetTagFromPointer(tagged_addr);
|
2019-01-21 17:51:10 +08:00
|
|
|
tag_t *tag_ptr =
|
|
|
|
reinterpret_cast<tag_t *>(MemToShadow(untagged_addr + offset));
|
2018-08-23 06:55:16 +08:00
|
|
|
tag_t mem_tag = *tag_ptr;
|
2019-01-21 17:51:10 +08:00
|
|
|
|
2018-08-23 06:55:16 +08:00
|
|
|
Printf("%s", d.Access());
|
2018-09-05 09:16:50 +08:00
|
|
|
Printf("%s of size %zu at %p tags: %02x/%02x (ptr/mem) in thread T%zd\n",
|
2018-08-30 06:23:34 +08:00
|
|
|
is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag,
|
2018-09-05 09:16:50 +08:00
|
|
|
mem_tag, t->unique_id());
|
2019-01-21 17:51:10 +08:00
|
|
|
if (offset != 0)
|
|
|
|
Printf("Invalid access starting at offset [%zu, %zu)\n", offset,
|
|
|
|
Min(access_size, static_cast<uptr>(offset) + (1 << kShadowScale)));
|
2017-12-09 09:31:51 +08:00
|
|
|
Printf("%s", d.Default());
|
|
|
|
|
|
|
|
stack->Print();
|
|
|
|
|
2018-09-25 07:03:34 +08:00
|
|
|
PrintAddressDescription(tagged_addr, access_size,
|
|
|
|
current_stack_allocations.get());
|
2018-09-05 09:16:50 +08:00
|
|
|
t->Announce();
|
2018-08-30 05:07:07 +08:00
|
|
|
|
2018-08-24 09:12:26 +08:00
|
|
|
PrintTagsAroundAddr(tag_ptr);
|
2018-08-23 06:55:16 +08:00
|
|
|
|
[HWASan] Save + print registers when tag mismatch occurs in AArch64.
Summary:
This change change the instrumentation to allow users to view the registers at the point at which tag mismatch occured. Most of the heavy lifting is done in the runtime library, where we save the registers to the stack and emit unwind information. This allows us to reduce the overhead, as very little additional work needs to be done in each __hwasan_check instance.
In this implementation, the fast path of __hwasan_check is unmodified. There are an additional 4 instructions (16B) emitted in the slow path in every __hwasan_check instance. This may increase binary size somewhat, but as most of the work is done in the runtime library, it's manageable.
The failure trace now contains a list of registers at the point of which the failure occured, in a format similar to that of Android's tombstones. It currently has the following format:
Registers where the failure occurred (pc 0x0055555561b4):
x0 0000000000000014 x1 0000007ffffff6c0 x2 1100007ffffff6d0 x3 12000056ffffe025
x4 0000007fff800000 x5 0000000000000014 x6 0000007fff800000 x7 0000000000000001
x8 12000056ffffe020 x9 0200007700000000 x10 0200007700000000 x11 0000000000000000
x12 0000007fffffdde0 x13 0000000000000000 x14 02b65b01f7a97490 x15 0000000000000000
x16 0000007fb77376b8 x17 0000000000000012 x18 0000007fb7ed6000 x19 0000005555556078
x20 0000007ffffff768 x21 0000007ffffff778 x22 0000000000000001 x23 0000000000000000
x24 0000000000000000 x25 0000000000000000 x26 0000000000000000 x27 0000000000000000
x28 0000000000000000 x29 0000007ffffff6f0 x30 00000055555561b4
... and prints after the dump of memory tags around the buggy address.
Every register is saved exactly as it was at the point where the tag mismatch occurs, with the exception of x16/x17. These registers are used in the tag mismatch calculation as scratch registers during __hwasan_check, and cannot be saved without affecting the fast path. As these registers are designated as scratch registers for linking, there should be no important information in them that could aid in debugging.
Reviewers: pcc, eugenis
Reviewed By: pcc, eugenis
Subscribers: srhines, kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, hiraditya, jdoerfert, llvm-commits, #sanitizers
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58857
llvm-svn: 355738
2019-03-09 05:22:35 +08:00
|
|
|
if (registers_frame)
|
|
|
|
ReportRegisters(registers_frame, pc);
|
|
|
|
|
2018-08-23 06:55:16 +08:00
|
|
|
ReportErrorSummary(bug_type, stack);
|
2017-12-09 09:31:51 +08:00
|
|
|
}
|
|
|
|
|
[HWASan] Save + print registers when tag mismatch occurs in AArch64.
Summary:
This change change the instrumentation to allow users to view the registers at the point at which tag mismatch occured. Most of the heavy lifting is done in the runtime library, where we save the registers to the stack and emit unwind information. This allows us to reduce the overhead, as very little additional work needs to be done in each __hwasan_check instance.
In this implementation, the fast path of __hwasan_check is unmodified. There are an additional 4 instructions (16B) emitted in the slow path in every __hwasan_check instance. This may increase binary size somewhat, but as most of the work is done in the runtime library, it's manageable.
The failure trace now contains a list of registers at the point of which the failure occured, in a format similar to that of Android's tombstones. It currently has the following format:
Registers where the failure occurred (pc 0x0055555561b4):
x0 0000000000000014 x1 0000007ffffff6c0 x2 1100007ffffff6d0 x3 12000056ffffe025
x4 0000007fff800000 x5 0000000000000014 x6 0000007fff800000 x7 0000000000000001
x8 12000056ffffe020 x9 0200007700000000 x10 0200007700000000 x11 0000000000000000
x12 0000007fffffdde0 x13 0000000000000000 x14 02b65b01f7a97490 x15 0000000000000000
x16 0000007fb77376b8 x17 0000000000000012 x18 0000007fb7ed6000 x19 0000005555556078
x20 0000007ffffff768 x21 0000007ffffff778 x22 0000000000000001 x23 0000000000000000
x24 0000000000000000 x25 0000000000000000 x26 0000000000000000 x27 0000000000000000
x28 0000000000000000 x29 0000007ffffff6f0 x30 00000055555561b4
... and prints after the dump of memory tags around the buggy address.
Every register is saved exactly as it was at the point where the tag mismatch occurs, with the exception of x16/x17. These registers are used in the tag mismatch calculation as scratch registers during __hwasan_check, and cannot be saved without affecting the fast path. As these registers are designated as scratch registers for linking, there should be no important information in them that could aid in debugging.
Reviewers: pcc, eugenis
Reviewed By: pcc, eugenis
Subscribers: srhines, kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, hiraditya, jdoerfert, llvm-commits, #sanitizers
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58857
llvm-svn: 355738
2019-03-09 05:22:35 +08:00
|
|
|
// See the frame breakdown defined in __hwasan_tag_mismatch (from
|
|
|
|
// hwasan_tag_mismatch_aarch64.S).
|
|
|
|
static const char *kDoubleSpace = " ";
|
|
|
|
static const char *kSingleSpace = " ";
|
|
|
|
void ReportRegisters(uptr *frame, uptr pc) {
|
|
|
|
Printf("Registers where the failure occurred (pc %p):", pc);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i <= 30; i++) {
|
|
|
|
if (i % 4 == 0)
|
|
|
|
Printf("\n ");
|
|
|
|
|
|
|
|
// Note - manually inserting a double or single space here based on the
|
|
|
|
// number of digits in the register name, as our sanitizer Printf does not
|
|
|
|
// support padding where the content is left aligned (i.e. the format
|
|
|
|
// specifier "%-2d" will CHECK fail).
|
|
|
|
Printf(" x%d%s%016llx", i, (i < 10) ? kDoubleSpace : kSingleSpace,
|
|
|
|
frame[i]);
|
|
|
|
}
|
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
} // namespace __hwasan
|