[compiler-rt] [WIP] Sanitizer ReportReceiver (PR #224706)
Andrew Haberlandt via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 18 11:56:41 PDT 2026
https://github.com/ndrewh created https://github.com/llvm/llvm-project/pull/224706
Adds a ScopedSanitizerReport / ReportReceiver interface in sanitizer_common that sanitizer runtimes use to notify observers about the structured content of an error report (title, addresses, backtraces) as it is emitted.
Assisted-By: Claude
>From 338e67a8e1699a142d3ce0f93a92e985c2d735c1 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Fri, 18 Sep 2026 11:36:20 -0700
Subject: [PATCH] compiler-rt: add ReportReceiver infrastructure
Adds a ScopedSanitizerReport / ReportReceiver interface in sanitizer_common
that sanitizer runtimes use to notify observers about the structured content
of an error report (title, addresses, backtraces) as it is emitted.
- New sanitizer_report_receiver.{h,cpp} defines the receiver interface,
registration, and a ScopedSanitizerReport whose AddTitleF also drives the
"ERROR: <sanitizer>: ..." stderr header line.
- ASan's Error*::Print methods and the *AddressDescription::Print methods take
a ScopedSanitizerReport reference (or pointer where the caller may be
outside a report, e.g. __asan_describe_address) and emit fault-address and
allocation-extent events alongside their existing printing.
- HeapAddressDescription now stores access_size so its Print can emit it as
the fault size without threading a separate parameter through.
- TSan's PrintReport threads a ScopedSanitizerReport by reference through
PrintMop/PrintLocation/PrintMutex/PrintThread and a new PrintStack
overload; each helper emits the corresponding receiver event next to its
existing Printf output.
---
compiler-rt/lib/asan/asan_descriptions.cpp | 55 +++--
compiler-rt/lib/asan/asan_descriptions.h | 33 +--
compiler-rt/lib/asan/asan_errors.cpp | 222 ++++++++----------
compiler-rt/lib/asan/asan_errors.h | 53 +++--
compiler-rt/lib/asan/asan_report.cpp | 17 +-
.../lib/sanitizer_common/CMakeLists.txt | 2 +
.../sanitizer_report_receiver.cpp | 91 +++++++
.../sanitizer_report_receiver.h | 104 ++++++++
compiler-rt/lib/tsan/rtl/tsan_report.cpp | 188 +++++++++------
9 files changed, 515 insertions(+), 250 deletions(-)
create mode 100644 compiler-rt/lib/sanitizer_common/sanitizer_report_receiver.cpp
create mode 100644 compiler-rt/lib/sanitizer_common/sanitizer_report_receiver.h
diff --git a/compiler-rt/lib/asan/asan_descriptions.cpp b/compiler-rt/lib/asan/asan_descriptions.cpp
index 551b819a4c436..8372807bbe28c 100644
--- a/compiler-rt/lib/asan/asan_descriptions.cpp
+++ b/compiler-rt/lib/asan/asan_descriptions.cpp
@@ -15,6 +15,7 @@
#include "asan_mapping.h"
#include "asan_report.h"
#include "asan_stack.h"
+#include "sanitizer_common/sanitizer_report_receiver.h"
#include "sanitizer_common/sanitizer_stackdepot.h"
namespace __asan {
@@ -90,10 +91,10 @@ static bool GetShadowKind(uptr addr, ShadowKind *shadow_kind) {
return true;
}
-bool DescribeAddressIfShadow(uptr addr) {
+bool DescribeAddressIfShadow(ScopedSanitizerReport *srep, uptr addr) {
ShadowAddressDescription descr;
if (!GetShadowAddressInformation(addr, &descr)) return false;
- descr.Print();
+ descr.Print(srep);
return true;
}
@@ -167,6 +168,7 @@ bool GetHeapAddressInformation(uptr addr, uptr access_size,
return false;
}
descr->addr = addr;
+ descr->access_size = access_size;
GetAccessToHeapChunkInformation(&descr->chunk_access, chunk, addr,
access_size);
CHECK_NE(chunk.AllocTid(), kInvalidTid);
@@ -185,7 +187,8 @@ static StackTrace GetStackTraceFromId(u32 id) {
return res;
}
-bool DescribeAddressIfHeap(uptr addr, uptr access_size) {
+bool DescribeAddressIfHeap(ScopedSanitizerReport *srep, uptr addr,
+ uptr access_size) {
HeapAddressDescription descr;
if (!GetHeapAddressInformation(addr, access_size, &descr)) {
Printf(
@@ -193,7 +196,7 @@ bool DescribeAddressIfHeap(uptr addr, uptr access_size) {
"(wild memory access suspected).\n");
return false;
}
- descr.Print();
+ descr.Print(srep);
return true;
}
@@ -273,10 +276,11 @@ static void PrintAccessAndVarIntersection(const StackVarDescr &var, uptr addr,
Printf("%s", str.data());
}
-bool DescribeAddressIfStack(uptr addr, uptr access_size) {
+bool DescribeAddressIfStack(ScopedSanitizerReport *srep, uptr addr,
+ uptr access_size) {
StackAddressDescription descr;
if (!GetStackAddressInformation(addr, access_size, &descr)) return false;
- descr.Print();
+ descr.Print(srep);
return true;
}
@@ -316,21 +320,25 @@ bool GetGlobalAddressInformation(uptr addr, uptr access_size,
return globals_num != 0;
}
-bool DescribeAddressIfGlobal(uptr addr, uptr access_size,
- const char *bug_type) {
+bool DescribeAddressIfGlobal(ScopedSanitizerReport *srep, uptr addr,
+ uptr access_size, const char *bug_type) {
GlobalAddressDescription descr;
if (!GetGlobalAddressInformation(addr, access_size, &descr)) return false;
- descr.Print(bug_type);
+ descr.Print(srep, bug_type);
return true;
}
-void ShadowAddressDescription::Print() const {
+void ShadowAddressDescription::Print(ScopedSanitizerReport *srep) const {
+ // Shadow addresses have no meaningful access size.
+ if (srep) srep->AddAddress(addr, 0, kReportAddressFault, "fault", 5);
Printf("Address %p is located in the %s area.\n", (void *)addr,
ShadowNames[kind]);
}
-void GlobalAddressDescription::Print(const char *bug_type) const {
+void GlobalAddressDescription::Print(ScopedSanitizerReport *srep,
+ const char *bug_type) const {
+ if (srep) srep->AddAddress(addr, access_size, kReportAddressFault, "fault", 5);
for (int i = 0; i < size; i++) {
DescribeAddressRelativeToGlobal(addr, access_size, globals[i]);
if (bug_type &&
@@ -362,7 +370,8 @@ bool GlobalAddressDescription::PointsInsideTheSameVariable(
return false;
}
-void StackAddressDescription::Print() const {
+void StackAddressDescription::Print(ScopedSanitizerReport *srep) const {
+ if (srep) srep->AddAddress(addr, access_size, kReportAddressFault, "fault", 5);
Decorator d;
Printf("%s", d.Location());
Printf("Address %p is located in stack of thread %s", (void *)addr,
@@ -417,13 +426,17 @@ void StackAddressDescription::Print() const {
DescribeThread(GetThreadContextByTidLocked(tid));
}
-void HeapAddressDescription::Print() const {
+void HeapAddressDescription::Print(ScopedSanitizerReport *srep) const {
+ if (srep) srep->AddAddress(addr, access_size, kReportAddressFault, "fault", 5);
PrintHeapChunkAccess(addr, chunk_access);
asanThreadRegistry().CheckLocked();
AsanThreadContext *alloc_thread = GetThreadContextByTidLocked(alloc_tid);
StackTrace alloc_stack = GetStackTraceFromId(alloc_stack_id);
+ if (srep) srep->AddAddress(chunk_access.chunk_begin, chunk_access.chunk_size,
+ kReportAddressAllocation, "allocation", 10);
+
Decorator d;
AsanThreadContext *free_thread = nullptr;
if (free_tid != kInvalidTid) {
@@ -432,6 +445,7 @@ void HeapAddressDescription::Print() const {
AsanThreadIdAndName(free_thread).c_str(), d.Default());
StackTrace free_stack = GetStackTraceFromId(free_stack_id);
free_stack.Print();
+ if (srep) srep->AddStack(free_stack.trace, free_stack.size, kReportStackDeallocation, "Freed", 5);
Printf("%spreviously allocated by thread %s here:%s\n", d.Allocation(),
AsanThreadIdAndName(alloc_thread).c_str(), d.Default());
} else {
@@ -439,6 +453,7 @@ void HeapAddressDescription::Print() const {
AsanThreadIdAndName(alloc_thread).c_str(), d.Default());
}
alloc_stack.Print();
+ if (srep) srep->AddStack(alloc_stack.trace, alloc_stack.size, kReportStackAllocation, "Allocated", 9);
DescribeThread(GetCurrentThread());
if (free_thread) DescribeThread(free_thread);
DescribeThread(alloc_thread);
@@ -492,34 +507,36 @@ AddressDescription::AddressDescription(uptr addr, uptr access_size,
data.wild.access_size = access_size;
}
-void WildAddressDescription::Print() const {
+void WildAddressDescription::Print(ScopedSanitizerReport *srep) const {
+ if (srep) srep->AddAddress(addr, access_size, kReportAddressFault, "fault", 5);
Printf("Address %p is a wild pointer inside of access range of size %p.\n",
(void *)addr, (void *)access_size);
}
-void PrintAddressDescription(uptr addr, uptr access_size,
+void PrintAddressDescription(ScopedSanitizerReport *srep, uptr addr,
+ uptr access_size,
const char *bug_type) {
ShadowAddressDescription shadow_descr;
if (GetShadowAddressInformation(addr, &shadow_descr)) {
- shadow_descr.Print();
+ shadow_descr.Print(srep);
return;
}
GlobalAddressDescription global_descr;
if (GetGlobalAddressInformation(addr, access_size, &global_descr)) {
- global_descr.Print(bug_type);
+ global_descr.Print(srep, bug_type);
return;
}
StackAddressDescription stack_descr;
if (GetStackAddressInformation(addr, access_size, &stack_descr)) {
- stack_descr.Print();
+ stack_descr.Print(srep);
return;
}
HeapAddressDescription heap_descr;
if (GetHeapAddressInformation(addr, access_size, &heap_descr)) {
- heap_descr.Print();
+ heap_descr.Print(srep);
return;
}
diff --git a/compiler-rt/lib/asan/asan_descriptions.h b/compiler-rt/lib/asan/asan_descriptions.h
index a614f47d461bb..1617717ea4b4f 100644
--- a/compiler-rt/lib/asan/asan_descriptions.h
+++ b/compiler-rt/lib/asan/asan_descriptions.h
@@ -18,6 +18,7 @@
#include "asan_thread.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_report_decorator.h"
+#include "sanitizer_common/sanitizer_report_receiver.h"
namespace __asan {
@@ -92,11 +93,11 @@ struct ShadowAddressDescription {
ShadowKind kind;
u8 shadow_byte;
- void Print() const;
+ void Print(ScopedSanitizerReport *srep) const;
};
bool GetShadowAddressInformation(uptr addr, ShadowAddressDescription *descr);
-bool DescribeAddressIfShadow(uptr addr);
+bool DescribeAddressIfShadow(ScopedSanitizerReport *srep, uptr addr);
enum AccessType {
kAccessTypeLeft,
@@ -117,18 +118,19 @@ struct ChunkAccess {
struct HeapAddressDescription {
uptr addr;
+ uptr access_size;
uptr alloc_tid;
uptr free_tid;
u32 alloc_stack_id;
u32 free_stack_id;
ChunkAccess chunk_access;
- void Print() const;
+ void Print(ScopedSanitizerReport *srep) const;
};
bool GetHeapAddressInformation(uptr addr, uptr access_size,
HeapAddressDescription *descr);
-bool DescribeAddressIfHeap(uptr addr, uptr access_size = 1);
+bool DescribeAddressIfHeap(ScopedSanitizerReport *srep, uptr addr, uptr access_size = 1);
struct StackAddressDescription {
uptr addr;
@@ -138,7 +140,7 @@ struct StackAddressDescription {
uptr access_size;
const char *frame_descr;
- void Print() const;
+ void Print(ScopedSanitizerReport *srep) const;
};
bool GetStackAddressInformation(uptr addr, uptr access_size,
@@ -148,7 +150,7 @@ struct WildAddressDescription {
uptr addr;
uptr access_size;
- void Print() const;
+ void Print(ScopedSanitizerReport *srep) const;
};
struct GlobalAddressDescription {
@@ -160,7 +162,7 @@ struct GlobalAddressDescription {
uptr access_size;
u8 size;
- void Print(const char *bug_type = "") const;
+ void Print(ScopedSanitizerReport *srep, const char *bug_type = "") const;
// Returns true when this descriptions points inside the same global variable
// as other. Descriptions can have different address within the variable
@@ -169,7 +171,7 @@ struct GlobalAddressDescription {
bool GetGlobalAddressInformation(uptr addr, uptr access_size,
GlobalAddressDescription *descr);
-bool DescribeAddressIfGlobal(uptr addr, uptr access_size, const char *bug_type);
+bool DescribeAddressIfGlobal(ScopedSanitizerReport *srep, uptr addr, uptr access_size, const char *bug_type);
// General function to describe an address. Will try to describe the address as
// a shadow, global (variable), stack, or heap address.
@@ -179,7 +181,8 @@ bool DescribeAddressIfGlobal(uptr addr, uptr access_size, const char *bug_type);
// addresses. Defaults to 1.
// Each of the *AddressDescription functions has its own Print() member, which
// may take access_size and bug_type parameters if needed.
-void PrintAddressDescription(uptr addr, uptr access_size = 1,
+void PrintAddressDescription(ScopedSanitizerReport *srep, uptr addr,
+ uptr access_size = 1,
const char *bug_type = "");
enum AddressKind {
@@ -228,20 +231,20 @@ class AddressDescription {
}
UNREACHABLE("AddressInformation kind is invalid");
}
- void Print(const char *bug_descr = nullptr) const {
+ void Print(ScopedSanitizerReport &srep, const char *bug_descr = nullptr) const {
switch (data.kind) {
case kAddressKindWild:
- data.wild.Print();
+ data.wild.Print(&srep);
return;
case kAddressKindShadow:
- return data.shadow.Print();
+ return data.shadow.Print(&srep);
case kAddressKindHeap:
- return data.heap.Print();
+ return data.heap.Print(&srep);
case kAddressKindStack:
- return data.stack.Print();
+ return data.stack.Print(&srep);
case kAddressKindGlobal:
// initialization-order-fiasco has a special Print()
- return data.global.Print(bug_descr);
+ return data.global.Print(&srep, bug_descr);
}
UNREACHABLE("AddressInformation kind is invalid");
}
diff --git a/compiler-rt/lib/asan/asan_errors.cpp b/compiler-rt/lib/asan/asan_errors.cpp
index 6b4d25c4aa3f9..fc44c024f12f6 100644
--- a/compiler-rt/lib/asan/asan_errors.cpp
+++ b/compiler-rt/lib/asan/asan_errors.cpp
@@ -18,6 +18,7 @@
#include "asan_poisoning.h"
#include "asan_report.h"
#include "asan_stack.h"
+#include "sanitizer_common/sanitizer_report_receiver.h"
#include "sanitizer_common/sanitizer_stackdepot.h"
namespace __asan {
@@ -41,29 +42,27 @@ static void OnStackUnwind(const SignalContext &sig,
fast);
}
-void ErrorDeadlySignal::Print() {
+void ErrorDeadlySignal::Print(ScopedSanitizerReport &srep) {
ReportDeadlySignal(signal, tid, &OnStackUnwind, &scariness);
}
-void ErrorDoubleFree::Print() {
+void ErrorDoubleFree::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report("ERROR: AddressSanitizer: attempting %s on %p in thread %s:\n",
+ srep.AddTitleF("ERROR: AddressSanitizer: attempting %s on %p in thread %s:",
scariness.GetDescription(), (void *)addr_description.addr,
AsanThreadIdAndName(tid).c_str());
- Printf("%s", d.Default());
scariness.Print();
GET_STACK_TRACE_FATAL(second_free_stack->trace[0],
second_free_stack->top_frame_bp);
stack.Print();
- addr_description.Print();
+ srep.AddStack(stack.trace, stack.size, kReportStackFault, "fault", 5);
+ addr_description.Print(&srep);
ReportErrorSummary(scariness.GetDescription(), &stack);
}
-void ErrorNewDeleteTypeMismatch::Print() {
+void ErrorNewDeleteTypeMismatch::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report("ERROR: AddressSanitizer: %s on %p in thread %s:\n",
+ srep.AddTitleF("ERROR: AddressSanitizer: %s on %p in thread %s:",
scariness.GetDescription(), (void *)addr_description.addr,
AsanThreadIdAndName(tid).c_str());
Printf("%s object passed to delete has wrong type:\n", d.Default());
@@ -93,17 +92,17 @@ void ErrorNewDeleteTypeMismatch::Print() {
scariness.Print();
GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
stack.Print();
- addr_description.Print();
+ srep.AddStack(stack.trace, stack.size, kReportStackFault, "fault", 5);
+ addr_description.Print(&srep);
ReportErrorSummary(scariness.GetDescription(), &stack);
Report(
"HINT: if you don't care about these errors you may set "
"ASAN_OPTIONS=new_delete_type_mismatch=0\n");
}
-void ErrorFreeSizeMismatch::Print() {
+void ErrorFreeSizeMismatch::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report("ERROR: AddressSanitizer: %s on %p in thread %s:\n",
+ srep.AddTitleF("ERROR: AddressSanitizer: %s on %p in thread %s:",
scariness.GetDescription(), (void*)addr_description.addr,
AsanThreadIdAndName(tid).c_str());
Printf("%s object passed to %s has wrong size or alignment:\n", d.Default(),
@@ -134,234 +133,219 @@ void ErrorFreeSizeMismatch::Print() {
scariness.Print();
GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
stack.Print();
- addr_description.Print();
+ srep.AddStack(stack.trace, stack.size, kReportStackFault, "fault", 5);
+ addr_description.Print(&srep);
ReportErrorSummary(scariness.GetDescription(), &stack);
Report(
"HINT: if you don't care about these errors you may set "
"ASAN_OPTIONS=free_size_mismatch=0\n");
}
-void ErrorFreeNotMalloced::Print() {
+void ErrorFreeNotMalloced::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: attempting free on address "
- "which was not malloc()-ed: %p in thread %s\n",
+ "which was not malloc()-ed: %p in thread %s",
(void *)addr_description.Address(), AsanThreadIdAndName(tid).c_str());
- Printf("%s", d.Default());
CHECK_GT(free_stack->size, 0);
scariness.Print();
GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
stack.Print();
- addr_description.Print();
+ srep.AddStack(stack.trace, stack.size, kReportStackFault, "fault", 5);
+ addr_description.Print(srep);
ReportErrorSummary(scariness.GetDescription(), &stack);
}
-void ErrorAllocTypeMismatch::Print() {
+void ErrorAllocTypeMismatch::Print(ScopedSanitizerReport &srep) {
static const char *alloc_names[] = {"INVALID", "malloc", "operator new",
"operator new []"};
static const char *dealloc_names[] = {"INVALID", "free", "operator delete",
"operator delete []"};
CHECK_NE(alloc_type, dealloc_type);
Decorator d;
- Printf("%s", d.Error());
- Report("ERROR: AddressSanitizer: %s (%s vs %s) on %p\n",
+ srep.AddTitleF("ERROR: AddressSanitizer: %s (%s vs %s) on %p",
scariness.GetDescription(), alloc_names[alloc_type],
dealloc_names[dealloc_type], (void *)addr_description.Address());
- Printf("%s", d.Default());
CHECK_GT(dealloc_stack->size, 0);
scariness.Print();
GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp);
stack.Print();
- addr_description.Print();
+ srep.AddStack(stack.trace, stack.size, kReportStackFault, "fault", 5);
+ addr_description.Print(srep);
ReportErrorSummary(scariness.GetDescription(), &stack);
Report(
"HINT: if you don't care about these errors you may set "
"ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
}
-void ErrorMallocUsableSizeNotOwned::Print() {
+void ErrorMallocUsableSizeNotOwned::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: attempting to call malloc_usable_size() for "
- "pointer which is not owned: %p\n",
+ "pointer which is not owned: %p",
(void *)addr_description.Address());
- Printf("%s", d.Default());
stack->Print();
- addr_description.Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
+ addr_description.Print(srep);
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorSanitizerGetAllocatedSizeNotOwned::Print() {
+void ErrorSanitizerGetAllocatedSizeNotOwned::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: attempting to call "
- "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n",
+ "__sanitizer_get_allocated_size() for pointer which is not owned: %p",
(void *)addr_description.Address());
- Printf("%s", d.Default());
stack->Print();
- addr_description.Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
+ addr_description.Print(srep);
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorCallocOverflow::Print() {
+void ErrorCallocOverflow::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: calloc parameters overflow: count * size "
- "(%zd * %zd) cannot be represented in type size_t (thread %s)\n",
+ "(%zd * %zd) cannot be represented in type size_t (thread %s)",
count, size, AsanThreadIdAndName(tid).c_str());
- Printf("%s", d.Default());
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorReallocArrayOverflow::Print() {
+void ErrorReallocArrayOverflow::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: reallocarray parameters overflow: count * size "
- "(%zd * %zd) cannot be represented in type size_t (thread %s)\n",
+ "(%zd * %zd) cannot be represented in type size_t (thread %s)",
count, size, AsanThreadIdAndName(tid).c_str());
- Printf("%s", d.Default());
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorPvallocOverflow::Print() {
+void ErrorPvallocOverflow::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: pvalloc parameters overflow: size 0x%zx "
"rounded up to system page size 0x%zx cannot be represented in type "
- "size_t (thread %s)\n",
+ "size_t (thread %s)",
size, GetPageSizeCached(), AsanThreadIdAndName(tid).c_str());
- Printf("%s", d.Default());
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorInvalidAllocationAlignment::Print() {
+void ErrorInvalidAllocationAlignment::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: invalid allocation alignment: %zd, "
- "alignment must be a power of two (thread %s)\n",
+ "alignment must be a power of two (thread %s)",
alignment, AsanThreadIdAndName(tid).c_str());
- Printf("%s", d.Default());
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorInvalidAlignedAllocAlignment::Print() {
+void ErrorInvalidAlignedAllocAlignment::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
#if SANITIZER_POSIX
- Report("ERROR: AddressSanitizer: invalid alignment requested in "
- "aligned_alloc: %zd, alignment must be a power of two and the "
- "requested size 0x%zx must be a multiple of alignment "
- "(thread %s)\n", alignment, size, AsanThreadIdAndName(tid).c_str());
+ srep.AddTitleF("ERROR: AddressSanitizer: invalid alignment requested in "
+ "aligned_alloc: %zd, alignment must be a power of two and the "
+ "requested size 0x%zx must be a multiple of alignment "
+ "(thread %s)", alignment, size, AsanThreadIdAndName(tid).c_str());
#else
- Report("ERROR: AddressSanitizer: invalid alignment requested in "
- "aligned_alloc: %zd, the requested size 0x%zx must be a multiple of "
- "alignment (thread %s)\n", alignment, size,
- AsanThreadIdAndName(tid).c_str());
+ srep.AddTitleF("ERROR: AddressSanitizer: invalid alignment requested in "
+ "aligned_alloc: %zd, the requested size 0x%zx must be a multiple "
+ "of alignment (thread %s)", alignment, size,
+ AsanThreadIdAndName(tid).c_str());
#endif
- Printf("%s", d.Default());
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorInvalidPosixMemalignAlignment::Print() {
+void ErrorInvalidPosixMemalignAlignment::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: invalid alignment requested in posix_memalign: "
"%zd, alignment must be a power of two and a multiple of sizeof(void*) "
- "== %zd (thread %s)\n",
+ "== %zd (thread %s)",
alignment, sizeof(void *), AsanThreadIdAndName(tid).c_str());
- Printf("%s", d.Default());
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorAllocationSizeTooBig::Print() {
+void ErrorAllocationSizeTooBig::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: requested allocation size 0x%zx (0x%zx after "
"adjustments for alignment, red zones etc.) exceeds maximum supported "
- "size of 0x%zx (thread %s)\n",
+ "size of 0x%zx (thread %s)",
user_size, total_size, max_size, AsanThreadIdAndName(tid).c_str());
- Printf("%s", d.Default());
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorRssLimitExceeded::Print() {
+void ErrorRssLimitExceeded::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: specified RSS limit exceeded, currently set to "
- "soft_rss_limit_mb=%zd\n", common_flags()->soft_rss_limit_mb);
- Printf("%s", d.Default());
+ "soft_rss_limit_mb=%zd", common_flags()->soft_rss_limit_mb);
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorOutOfMemory::Print() {
- Decorator d;
- Printf("%s", d.Error());
- ERROR_OOM("allocator is trying to allocate 0x%zx bytes\n", requested_size);
- Printf("%s", d.Default());
+void ErrorOutOfMemory::Print(ScopedSanitizerReport &srep) {
+ srep.AddTitleF("ERROR: %s: out of memory: allocator is trying to allocate 0x%zx "
+ "bytes", SanitizerToolName, requested_size);
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
PrintHintAllocatorCannotReturnNull();
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorStringFunctionMemoryRangesOverlap::Print() {
- Decorator d;
+void ErrorStringFunctionMemoryRangesOverlap::Print(ScopedSanitizerReport &srep) {
char bug_type[100];
internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
- Printf("%s", d.Error());
- Report(
+ srep.AddTitleF(
"ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) "
- "overlap\n",
+ "overlap",
bug_type, (void *)addr1_description.Address(),
(void *)(addr1_description.Address() + length1),
(void *)addr2_description.Address(),
(void *)(addr2_description.Address() + length2));
- Printf("%s", d.Default());
scariness.Print();
stack->Print();
- addr1_description.Print();
- addr2_description.Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
+ addr1_description.Print(srep);
+ addr2_description.Print(srep);
ReportErrorSummary(bug_type, stack);
}
-void ErrorStringFunctionSizeOverflow::Print() {
+void ErrorStringFunctionSizeOverflow::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report("ERROR: AddressSanitizer: %s: (size=%zd)\n",
+ srep.AddTitleF("ERROR: AddressSanitizer: %s: (size=%zd)",
scariness.GetDescription(), size);
- Printf("%s", d.Default());
scariness.Print();
stack->Print();
- addr_description.Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
+ addr_description.Print(srep);
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorBadParamsToAnnotateContiguousContainer::Print() {
+void ErrorBadParamsToAnnotateContiguousContainer::Print(ScopedSanitizerReport &srep) {
Report(
"ERROR: AddressSanitizer: bad parameters to "
"__sanitizer_annotate_contiguous_container:\n"
@@ -371,10 +355,11 @@ void ErrorBadParamsToAnnotateContiguousContainer::Print() {
" new_mid : %p\n",
(void *)beg, (void *)end, (void *)old_mid, (void *)new_mid);
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorBadParamsToAnnotateDoubleEndedContiguousContainer::Print() {
+void ErrorBadParamsToAnnotateDoubleEndedContiguousContainer::Print(ScopedSanitizerReport &srep) {
Report(
"ERROR: AddressSanitizer: bad parameters to "
"__sanitizer_annotate_double_ended_contiguous_container:\n"
@@ -388,15 +373,14 @@ void ErrorBadParamsToAnnotateDoubleEndedContiguousContainer::Print() {
(void *)old_container_end, (void *)new_container_beg,
(void *)new_container_end);
stack->Print();
+ srep.AddStack(stack->trace, stack->size, kReportStackFault, "fault", 5);
ReportErrorSummary(scariness.GetDescription(), stack);
}
-void ErrorODRViolation::Print() {
+void ErrorODRViolation::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(),
+ srep.AddTitleF("ERROR: AddressSanitizer: %s (%p):", scariness.GetDescription(),
(void *)global1.beg);
- Printf("%s", d.Default());
InternalScopedString g1_loc;
InternalScopedString g2_loc;
PrintGlobalLocation(&g1_loc, global1, /*print_module_name=*/true);
@@ -421,17 +405,16 @@ void ErrorODRViolation::Print() {
ReportErrorSummary(error_msg.data());
}
-void ErrorInvalidPointerPair::Print() {
+void ErrorInvalidPointerPair::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
- Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(),
+ srep.AddTitleF("ERROR: AddressSanitizer: %s: %p %p", scariness.GetDescription(),
(void *)addr1_description.Address(),
(void *)addr2_description.Address());
- Printf("%s", d.Default());
GET_STACK_TRACE_FATAL(pc, bp);
stack.Print();
- addr1_description.Print();
- addr2_description.Print();
+ srep.AddStack(stack.trace, stack.size, kReportStackFault, "fault", 5);
+ addr1_description.Print(srep);
+ addr2_description.Print(srep);
ReportErrorSummary(scariness.GetDescription(), &stack);
}
@@ -664,13 +647,11 @@ static void CheckPoisonRecords(uptr addr) {
}
}
-void ErrorGeneric::Print() {
+void ErrorGeneric::Print(ScopedSanitizerReport &srep) {
Decorator d;
- Printf("%s", d.Error());
uptr addr = addr_description.Address();
- Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n",
- bug_descr, (void *)addr, (void *)pc, (void *)bp, (void *)sp);
- Printf("%s", d.Default());
+ srep.AddTitleF("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p",
+ bug_descr, (void *)addr, (void *)pc, (void *)bp, (void *)sp);
Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(),
access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size,
@@ -679,10 +660,11 @@ void ErrorGeneric::Print() {
scariness.Print();
GET_STACK_TRACE_FATAL(pc, bp);
stack.Print();
+ srep.AddStack(stack.trace, stack.size, kReportStackFault, "fault", 5);
// Pass bug_descr because we have a special case for
- // initialization-order-fiasco
- addr_description.Print(bug_descr);
+ // initialization-order-fiasco.
+ addr_description.Print(srep, bug_descr);
if (shadow_val == kAsanContiguousContainerOOBMagic)
PrintContainerOverflowHint();
ReportErrorSummary(bug_descr, &stack);
diff --git a/compiler-rt/lib/asan/asan_errors.h b/compiler-rt/lib/asan/asan_errors.h
index d9a626e711282..599546a35f76d 100644
--- a/compiler-rt/lib/asan/asan_errors.h
+++ b/compiler-rt/lib/asan/asan_errors.h
@@ -16,6 +16,7 @@
#include "asan_descriptions.h"
#include "asan_scariness_score.h"
#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_report_receiver.h"
namespace __asan {
@@ -61,7 +62,7 @@ struct ErrorDeadlySignal : ErrorBase {
scariness.Scare(25, "wild-addr");
}
}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorDoubleFree : ErrorBase {
@@ -75,7 +76,7 @@ struct ErrorDoubleFree : ErrorBase {
CHECK_GT(second_free_stack->size, 0);
GetHeapAddressInformation(addr, 1, &addr_description);
}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorNewDeleteTypeMismatch : ErrorBase {
@@ -93,7 +94,7 @@ struct ErrorNewDeleteTypeMismatch : ErrorBase {
delete_alignment(delete_alignment_) {
GetHeapAddressInformation(addr, 1, &addr_description);
}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorFreeSizeMismatch : ErrorBase {
@@ -111,7 +112,7 @@ struct ErrorFreeSizeMismatch : ErrorBase {
delete_alignment(delete_alignment) {
GetHeapAddressInformation(addr, 1, &addr_description);
}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
bool isFreeAlignedSized() const { return delete_alignment != 0; }
};
@@ -124,7 +125,7 @@ struct ErrorFreeNotMalloced : ErrorBase {
: ErrorBase(tid, 40, "bad-free"),
free_stack(stack),
addr_description(addr, /*shouldLockThreadRegistry=*/false) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorAllocTypeMismatch : ErrorBase {
@@ -140,7 +141,7 @@ struct ErrorAllocTypeMismatch : ErrorBase {
alloc_type(alloc_type_),
dealloc_type(dealloc_type_),
addr_description(addr, 1, false) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorMallocUsableSizeNotOwned : ErrorBase {
@@ -152,7 +153,7 @@ struct ErrorMallocUsableSizeNotOwned : ErrorBase {
: ErrorBase(tid, 10, "bad-malloc_usable_size"),
stack(stack_),
addr_description(addr, /*shouldLockThreadRegistry=*/false) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorSanitizerGetAllocatedSizeNotOwned : ErrorBase {
@@ -165,7 +166,7 @@ struct ErrorSanitizerGetAllocatedSizeNotOwned : ErrorBase {
: ErrorBase(tid, 10, "bad-__sanitizer_get_allocated_size"),
stack(stack_),
addr_description(addr, /*shouldLockThreadRegistry=*/false) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorCallocOverflow : ErrorBase {
@@ -180,7 +181,7 @@ struct ErrorCallocOverflow : ErrorBase {
stack(stack_),
count(count_),
size(size_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorReallocArrayOverflow : ErrorBase {
@@ -195,7 +196,7 @@ struct ErrorReallocArrayOverflow : ErrorBase {
stack(stack_),
count(count_),
size(size_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorPvallocOverflow : ErrorBase {
@@ -207,7 +208,7 @@ struct ErrorPvallocOverflow : ErrorBase {
: ErrorBase(tid, 10, "pvalloc-overflow"),
stack(stack_),
size(size_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorInvalidAllocationAlignment : ErrorBase {
@@ -220,7 +221,7 @@ struct ErrorInvalidAllocationAlignment : ErrorBase {
: ErrorBase(tid, 10, "invalid-allocation-alignment"),
stack(stack_),
alignment(alignment_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorInvalidAlignedAllocAlignment : ErrorBase {
@@ -235,7 +236,7 @@ struct ErrorInvalidAlignedAllocAlignment : ErrorBase {
stack(stack_),
size(size_),
alignment(alignment_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorInvalidPosixMemalignAlignment : ErrorBase {
@@ -248,7 +249,7 @@ struct ErrorInvalidPosixMemalignAlignment : ErrorBase {
: ErrorBase(tid, 10, "invalid-posix-memalign-alignment"),
stack(stack_),
alignment(alignment_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorAllocationSizeTooBig : ErrorBase {
@@ -265,7 +266,7 @@ struct ErrorAllocationSizeTooBig : ErrorBase {
user_size(user_size_),
total_size(total_size_),
max_size(max_size_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorRssLimitExceeded : ErrorBase {
@@ -275,7 +276,7 @@ struct ErrorRssLimitExceeded : ErrorBase {
ErrorRssLimitExceeded(u32 tid, BufferedStackTrace *stack_)
: ErrorBase(tid, 10, "rss-limit-exceeded"),
stack(stack_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorOutOfMemory : ErrorBase {
@@ -287,7 +288,7 @@ struct ErrorOutOfMemory : ErrorBase {
: ErrorBase(tid, 10, "out-of-memory"),
stack(stack_),
requested_size(requested_size_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorStringFunctionMemoryRangesOverlap : ErrorBase {
@@ -313,7 +314,7 @@ struct ErrorStringFunctionMemoryRangesOverlap : ErrorBase {
scariness.Clear();
scariness.Scare(10, bug_type);
}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorStringFunctionSizeOverflow : ErrorBase {
@@ -330,7 +331,7 @@ struct ErrorStringFunctionSizeOverflow : ErrorBase {
addr_description(addr, /*shouldLockThreadRegistry=*/false),
size(size),
is_write(is_write) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorBadParamsToAnnotateContiguousContainer : ErrorBase {
@@ -349,7 +350,7 @@ struct ErrorBadParamsToAnnotateContiguousContainer : ErrorBase {
end(end_),
old_mid(old_mid_),
new_mid(new_mid_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorBadParamsToAnnotateDoubleEndedContiguousContainer : ErrorBase {
@@ -371,7 +372,7 @@ struct ErrorBadParamsToAnnotateDoubleEndedContiguousContainer : ErrorBase {
old_container_end(old_container_end_),
new_container_beg(new_container_beg_),
new_container_end(new_container_end_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorODRViolation : ErrorBase {
@@ -386,7 +387,7 @@ struct ErrorODRViolation : ErrorBase {
global2(*g2),
stack_id1(stack_id1_),
stack_id2(stack_id2_) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorInvalidPointerPair : ErrorBase {
@@ -403,7 +404,7 @@ struct ErrorInvalidPointerPair : ErrorBase {
sp(sp_),
addr1_description(p1, 1, /*shouldLockThreadRegistry=*/false),
addr2_description(p2, 1, /*shouldLockThreadRegistry=*/false) {}
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
struct ErrorGeneric : ErrorBase {
@@ -417,7 +418,7 @@ struct ErrorGeneric : ErrorBase {
ErrorGeneric() = default; // (*)
ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr, bool is_write_,
uptr access_size_);
- void Print();
+ void Print(ScopedSanitizerReport &srep);
};
// clang-format off
@@ -456,7 +457,7 @@ struct ErrorGeneric : ErrorBase {
}
#define ASAN_ERROR_DESCRIPTION_PRINT(name) \
case kErrorKind##name: \
- return name.Print();
+ return name.Print(srep);
enum ErrorKind {
kErrorKindInvalid = 0,
@@ -480,7 +481,7 @@ struct ErrorDescription {
ASAN_FOR_EACH_ERROR_KIND(ASAN_ERROR_DESCRIPTION_CONSTRUCTOR)
bool IsValid() { return kind != kErrorKindInvalid; }
- void Print() {
+ void Print(ScopedSanitizerReport &srep) {
switch (kind) {
ASAN_FOR_EACH_ERROR_KIND(ASAN_ERROR_DESCRIPTION_PRINT)
case kErrorKindInvalid:
diff --git a/compiler-rt/lib/asan/asan_report.cpp b/compiler-rt/lib/asan/asan_report.cpp
index df797deaa5dbd..6cde3840a2bd6 100644
--- a/compiler-rt/lib/asan/asan_report.cpp
+++ b/compiler-rt/lib/asan/asan_report.cpp
@@ -27,6 +27,7 @@
#include "sanitizer_common/sanitizer_interface_internal.h"
#include "sanitizer_common/sanitizer_placement_new.h"
#include "sanitizer_common/sanitizer_report_decorator.h"
+#include "sanitizer_common/sanitizer_report_receiver.h"
#include "sanitizer_common/sanitizer_stackdepot.h"
#include "sanitizer_common/sanitizer_symbolizer.h"
@@ -169,7 +170,17 @@ class ScopedInErrorReport {
return;
}
ASAN_ON_ERROR();
- if (current_error_.IsValid()) current_error_.Print();
+ if (current_error_.IsValid()) {
+ // Passed by reference (or pointer) through the Error*::Print /
+ // *AddressDescription::Print methods so they can push receiver events.
+ const char *type_str =
+ current_error_.kind == kErrorKindGeneric
+ ? current_error_.Generic.bug_descr
+ : current_error_.Base.scariness.GetDescription();
+ ScopedSanitizerReport srep("AddressSanitizer",
+ type_str ? type_str : "unknown");
+ current_error_.Print(srep);
+ }
// Make sure the current thread is announced.
DescribeThread(GetCurrentThread());
@@ -492,7 +503,7 @@ void ReportMacMzReallocUnknown(uptr addr, uptr zone_ptr, const char *zone_name,
(void *)addr);
PrintZoneForPointer(addr, zone_ptr, zone_name);
stack->Print();
- DescribeAddressIfHeap(addr);
+ DescribeAddressIfHeap(nullptr, addr);
}
// -------------- SuppressErrorReport -------------- {{{1
@@ -553,7 +564,7 @@ void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
void __asan_describe_address(uptr addr) {
// Thread registry must be locked while we're describing an address.
asanThreadRegistry().Lock();
- PrintAddressDescription(addr, 1, "");
+ PrintAddressDescription(nullptr, addr, 1, "");
asanThreadRegistry().Unlock();
}
diff --git a/compiler-rt/lib/sanitizer_common/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/CMakeLists.txt
index 346b23569a162..8f68b36b9ba12 100644
--- a/compiler-rt/lib/sanitizer_common/CMakeLists.txt
+++ b/compiler-rt/lib/sanitizer_common/CMakeLists.txt
@@ -34,6 +34,7 @@ set(SANITIZER_SOURCES_NOTERMINATION
sanitizer_procmaps_mac.cpp
sanitizer_procmaps_solaris.cpp
sanitizer_range.cpp
+ sanitizer_report_receiver.cpp
sanitizer_solaris.cpp
sanitizer_stoptheworld_fuchsia.cpp
sanitizer_stoptheworld_mac.cpp
@@ -189,6 +190,7 @@ set(SANITIZER_IMPL_HEADERS
sanitizer_range.h
sanitizer_redefine_builtins.h
sanitizer_report_decorator.h
+ sanitizer_report_receiver.h
sanitizer_ring_buffer.h
sanitizer_signal_interceptors.inc
sanitizer_stack_store.h
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_report_receiver.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_report_receiver.cpp
new file mode 100644
index 0000000000000..680bf89c74736
--- /dev/null
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_report_receiver.cpp
@@ -0,0 +1,91 @@
+//===-- sanitizer_report_receiver.cpp -------------------------------------===//
+//
+// 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_report_receiver.h"
+
+#include "sanitizer_common.h"
+#include "sanitizer_libc.h"
+#include "sanitizer_report_decorator.h"
+
+#include <stdarg.h>
+
+namespace __sanitizer {
+
+// Internal in sanitizer_printf.cpp — not in a public header but linked in the
+// same library. Used to format AddTitleF's varargs without re-implementing
+// the sanitizer printf state machine.
+int VSNPrintf(char *buff, int buff_length, const char *format, va_list args);
+
+// Fixed-size list; registration happens once at init.
+static const int kMaxReceivers = 4;
+static ReportReceiver *g_receivers[kMaxReceivers];
+static int g_num_receivers = 0;
+
+void RegisterReportReceiver(ReportReceiver *r) {
+ if (!r)
+ return;
+ if (g_num_receivers >= kMaxReceivers)
+ return;
+ g_receivers[g_num_receivers++] = r;
+}
+
+ScopedSanitizerReport::ScopedSanitizerReport(const char *sanitizer,
+ const char *type)
+ : sanitizer_(sanitizer ? sanitizer : "") {
+ for (int i = 0; i < g_num_receivers; i++)
+ g_receivers[i]->OnStart(sanitizer, type);
+}
+
+ScopedSanitizerReport::~ScopedSanitizerReport() {
+ for (int i = 0; i < g_num_receivers; i++)
+ g_receivers[i]->OnFinish();
+}
+
+void ScopedSanitizerReport::AddTitleF(const char *fmt, ...) {
+ // Format into a heap-owned buffer so we don't blow the -Wframe-larger-than
+ // limit with a stack-resident 1 KiB title buffer.
+ InternalMmapVector<char> buf(1024);
+ va_list ap;
+ va_start(ap, fmt);
+ int len = VSNPrintf(buf.data(), buf.size(), fmt, ap);
+ va_end(ap);
+ if (len < 0)
+ return;
+ if ((uptr)len >= buf.size())
+ len = buf.size() - 1;
+
+ // Print with error decoration + pid prefix. Matches the shape of ASan's
+ // Report("ERROR: AddressSanitizer: ...\n") sandwich.
+ SanitizerCommonDecorator d;
+ Printf("%s", d.Warning());
+ Report("%s\n", buf.data());
+ Printf("%s", d.Default());
+
+ for (int i = 0; i < g_num_receivers; i++)
+ g_receivers[i]->OnTitle(buf.data(), (uptr)len);
+}
+
+void ScopedSanitizerReport::AddAddress(uptr addr, uptr size,
+ ReportAddressKind kind, const char *desc,
+ uptr desc_len) {
+ if (!addr)
+ return;
+ for (int i = 0; i < g_num_receivers; i++)
+ g_receivers[i]->OnAddress(addr, size, kind, desc, desc_len);
+}
+
+void ScopedSanitizerReport::AddStack(const uptr *frames, uptr num_frames,
+ ReportStackKind kind, const char *desc,
+ uptr desc_len) {
+ if (!frames || !num_frames)
+ return;
+ for (int i = 0; i < g_num_receivers; i++)
+ g_receivers[i]->OnStack(frames, num_frames, kind, desc, desc_len);
+}
+
+} // namespace __sanitizer
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_report_receiver.h b/compiler-rt/lib/sanitizer_common/sanitizer_report_receiver.h
new file mode 100644
index 0000000000000..f69f2bbf7f072
--- /dev/null
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_report_receiver.h
@@ -0,0 +1,104 @@
+//===-- sanitizer_report_receiver.h ------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Incremental reporting API for sanitizers. Sanitizer runtimes construct a
+// ScopedSanitizerReport at the start of an error report and push structured
+// events (title, addresses, backtraces) as they become known. Each registered
+// ReportReceiver observes the events in order and can translate them into
+// its own stable format (e.g. the Darwin crash-reporter payload).
+//
+// The ScopedSanitizerReport is threaded by reference (or pointer, where the
+// caller may not be inside a report) through the sanitizer's Print methods.
+// AddTitleF is the one method that also drives stderr output — it formats
+// the "ERROR: <sanitizer>: ..." header line, prints it with decoration, and
+// notifies OnTitle. AddAddress and AddStack are receiver-only; the callers
+// keep their own printing right next to the AddX call so a single site drives
+// both.
+//
+//===----------------------------------------------------------------------===//
+#ifndef SANITIZER_REPORT_RECEIVER_H
+#define SANITIZER_REPORT_RECEIVER_H
+
+#include "sanitizer_internal_defs.h"
+
+namespace __sanitizer {
+
+enum ReportStackKind {
+ kReportStackOther = 0,
+ kReportStackAllocation = 1,
+ kReportStackDeallocation = 2,
+ // The stack of the offending operation itself (the memory access that
+ // faulted, the second-free call, the misbehaving alloc-parameter call, etc).
+ kReportStackFault = 3,
+};
+
+// Optional role hint for OnAddress. Receivers may use this to tell a fault
+// address apart from an associated heap allocation.
+enum ReportAddressKind {
+ kReportAddressOther = 0,
+ kReportAddressFault = 1,
+ kReportAddressAllocation = 2,
+};
+
+// Observer of a sanitizer report as it is emitted. Callbacks are invoked in
+// order between OnStart and OnFinish. All string/frame pointers passed in are
+// valid only for the duration of the call.
+class ReportReceiver {
+ public:
+ virtual ~ReportReceiver() {}
+ virtual void OnStart(const char *sanitizer, const char *type) {}
+ // Fully-formatted "ERROR: <sanitizer>: ..." header line for the report.
+ virtual void OnTitle(const char *title, uptr title_len) {}
+ // A memory address involved in the report. `size` may be 0.
+ virtual void OnAddress(uptr addr, uptr size, ReportAddressKind kind,
+ const char *desc, uptr desc_len) {}
+ // A backtrace involved in the report. `frames` is top-of-stack-first.
+ virtual void OnStack(const uptr *frames, uptr num_frames,
+ ReportStackKind kind, const char *desc,
+ uptr desc_len) {}
+ virtual void OnFinish() {}
+};
+
+// Register a receiver. The pointer must remain valid for the process lifetime.
+// Not thread-safe; call during platform init before any report can fire.
+void RegisterReportReceiver(ReportReceiver *receiver);
+
+// Piped to all registered receivers. Construct at the start of a report,
+// destroy at the end. Passed by reference (or pointer where the caller may
+// be outside a report) through the sanitizer's Print methods.
+class ScopedSanitizerReport {
+ public:
+ ScopedSanitizerReport(const char *sanitizer, const char *type);
+ ~ScopedSanitizerReport();
+
+ // Format printf-style, print the resulting line with error decoration + pid
+ // prefix, and notify OnTitle. Caller writes the full title text (including
+ // the "ERROR: AddressSanitizer:" or "WARNING: ThreadSanitizer:" prefix); no
+ // newline needed — one is appended for printing.
+ void AddTitleF(const char *fmt, ...) FORMAT(2, 3);
+
+ // Notify OnAddress. Does not print. addr == 0 is treated as a no-op.
+ void AddAddress(uptr addr, uptr size, ReportAddressKind kind,
+ const char *desc, uptr desc_len);
+
+ // Notify OnStack. Does not print. Empty stack is treated as a no-op.
+ void AddStack(const uptr *frames, uptr num_frames, ReportStackKind kind,
+ const char *desc, uptr desc_len);
+
+ const char *sanitizer() const { return sanitizer_; }
+
+ private:
+ const char *sanitizer_;
+
+ ScopedSanitizerReport(const ScopedSanitizerReport &) = delete;
+ void operator=(const ScopedSanitizerReport &) = delete;
+};
+
+} // namespace __sanitizer
+
+#endif // SANITIZER_REPORT_RECEIVER_H
diff --git a/compiler-rt/lib/tsan/rtl/tsan_report.cpp b/compiler-rt/lib/tsan/rtl/tsan_report.cpp
index 17fd3064fb85a..83880dfb90adf 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_report.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_report.cpp
@@ -15,6 +15,7 @@
#include "sanitizer_common/sanitizer_file.h"
#include "sanitizer_common/sanitizer_placement_new.h"
#include "sanitizer_common/sanitizer_report_decorator.h"
+#include "sanitizer_common/sanitizer_report_receiver.h"
#include "sanitizer_common/sanitizer_stacktrace_printer.h"
namespace __tsan {
@@ -139,71 +140,117 @@ static const char *ExternalMopDesc(bool first, bool write) {
: (write ? "Previous modifying" : "Previous read-only");
}
-static void PrintMop(const ReportMop *mop, bool first) {
+namespace {
+
+// Flatten a SymbolizedStack (top-of-stack-first linked list of symbolized
+// frames) into a raw-PC array on `out`, capped at `cap`. Returns count.
+uptr FlattenStack(const ReportStack *stack, uptr *out, uptr cap) {
+ if (!stack || !stack->frames)
+ return 0;
+ uptr n = 0;
+ for (SymbolizedStack *f = stack->frames; f && f->info.address && n < cap;
+ f = f->next)
+ out[n++] = f->info.address;
+ return n;
+}
+
+} // namespace
+
+// PrintStack overload that also emits the stack to `srep`. Callers inside a
+// report use this so a single call site drives both the symbolized stderr
+// output and the receiver's OnStack event. `desc` is passed verbatim to
+// OnStack; it is NOT printed (the caller prints the description on the
+// preceding line, as required for the mutex-set-suffix layout).
+void PrintStack(ScopedSanitizerReport &srep, const ReportStack *stack,
+ ReportStackKind kind, const char *desc, uptr desc_len) {
+ PrintStack(stack);
+ // Heap-owned to keep this function (and any caller it gets inlined into)
+ // under the -Wframe-larger-than limit.
+ InternalMmapVector<uptr> frames(128);
+ uptr n = FlattenStack(stack, frames.data(), frames.size());
+ srep.AddStack(frames.data(), n, kind, desc, desc_len);
+}
+
+static void PrintMop(ScopedSanitizerReport &srep, const ReportMop *mop,
+ bool first) {
Decorator d;
char thrbuf[kThreadBufSize];
Printf("%s", d.Access());
+ InternalScopedString s;
if (mop->external_tag == kExternalTagNone) {
- Printf(" %s of size %d at %p by %s",
- MopDesc(first, mop->write, mop->atomic), mop->size,
- (void *)mop->addr, thread_name(thrbuf, mop->tid));
+ s.AppendF("%s of size %d at %p by %s",
+ MopDesc(first, mop->write, mop->atomic), mop->size,
+ (void *)mop->addr, thread_name(thrbuf, mop->tid));
} else {
const char *object_type = GetObjectTypeFromTag(mop->external_tag);
if (object_type == nullptr)
- object_type = "external object";
- Printf(" %s access of %s at %p by %s",
- ExternalMopDesc(first, mop->write), object_type,
- (void *)mop->addr, thread_name(thrbuf, mop->tid));
+ object_type = "external object";
+ s.AppendF("%s access of %s at %p by %s",
+ ExternalMopDesc(first, mop->write), object_type,
+ (void *)mop->addr, thread_name(thrbuf, mop->tid));
}
+ Printf(" %s", s.data());
PrintMutexSet(mop->mset);
Printf(":\n");
Printf("%s", d.Default());
- PrintStack(mop->stack);
+ if (first)
+ srep.AddAddress((uptr)mop->addr, mop->size, kReportAddressFault, "fault",
+ 5);
+ PrintStack(srep, mop->stack,
+ first ? kReportStackFault : kReportStackOther, s.data(),
+ s.length());
}
-static void PrintLocation(const ReportLocation *loc) {
+static void PrintLocation(ScopedSanitizerReport &srep,
+ const ReportLocation *loc) {
Decorator d;
char thrbuf[kThreadBufSize];
bool print_stack = false;
- Printf("%s", d.Location());
+ ReportStackKind stack_kind = kReportStackOther;
+ InternalScopedString s;
if (loc->type == ReportLocationGlobal) {
const DataInfo &global = loc->global;
if (global.size != 0)
- Printf(" Location is global '%s' of size %zu at %p (%s+0x%zx)\n\n",
- global.name, global.size, reinterpret_cast<void *>(global.start),
- StripModuleName(global.module), global.module_offset);
+ s.AppendF("Location is global '%s' of size %zu at %p (%s+0x%zx)",
+ global.name, global.size,
+ reinterpret_cast<void *>(global.start),
+ StripModuleName(global.module), global.module_offset);
else
- Printf(" Location is global '%s' at %p (%s+0x%zx)\n\n", global.name,
- reinterpret_cast<void *>(global.start),
- StripModuleName(global.module), global.module_offset);
+ s.AppendF("Location is global '%s' at %p (%s+0x%zx)", global.name,
+ reinterpret_cast<void *>(global.start),
+ StripModuleName(global.module), global.module_offset);
} else if (loc->type == ReportLocationHeap) {
- char thrbuf[kThreadBufSize];
const char *object_type = GetObjectTypeFromTag(loc->external_tag);
- if (!object_type) {
- Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
- loc->heap_chunk_size,
- reinterpret_cast<void *>(loc->heap_chunk_start),
- thread_name(thrbuf, loc->tid));
- } else {
- Printf(" Location is %s of size %zu at %p allocated by %s:\n",
- object_type, loc->heap_chunk_size,
- reinterpret_cast<void *>(loc->heap_chunk_start),
- thread_name(thrbuf, loc->tid));
- }
+ if (!object_type)
+ s.AppendF("Location is heap block of size %zu at %p allocated by %s:",
+ loc->heap_chunk_size,
+ reinterpret_cast<void *>(loc->heap_chunk_start),
+ thread_name(thrbuf, loc->tid));
+ else
+ s.AppendF("Location is %s of size %zu at %p allocated by %s:",
+ object_type, loc->heap_chunk_size,
+ reinterpret_cast<void *>(loc->heap_chunk_start),
+ thread_name(thrbuf, loc->tid));
print_stack = true;
+ stack_kind = kReportStackAllocation;
+ srep.AddAddress((uptr)loc->heap_chunk_start, loc->heap_chunk_size,
+ kReportAddressAllocation, "allocation", 10);
} else if (loc->type == ReportLocationStack) {
- Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
+ s.AppendF("Location is stack of %s.", thread_name(thrbuf, loc->tid));
} else if (loc->type == ReportLocationTLS) {
- Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
+ s.AppendF("Location is TLS of %s.", thread_name(thrbuf, loc->tid));
} else if (loc->type == ReportLocationFD) {
- Printf(" Location is file descriptor %d %s by %s at:\n", loc->fd,
- loc->fd_closed ? "destroyed" : "created",
- thread_name(thrbuf, loc->tid));
+ s.AppendF("Location is file descriptor %d %s by %s at:", loc->fd,
+ loc->fd_closed ? "destroyed" : "created",
+ thread_name(thrbuf, loc->tid));
print_stack = true;
}
+ Printf("%s", d.Location());
+ Printf(" %s", s.data());
+ Printf(print_stack ? "\n" : "\n\n");
Printf("%s", d.Default());
if (print_stack)
- PrintStack(loc->stack);
+ PrintStack(srep, loc->stack, stack_kind, s.data(), s.length());
}
static void PrintMutexShort(const ReportMutex *rm, const char *after) {
@@ -218,39 +265,44 @@ static void PrintMutexShortWithAddress(const ReportMutex *rm,
reinterpret_cast<void *>(rm->addr), d.Default(), after);
}
-static void PrintMutex(const ReportMutex *rm) {
+static void PrintMutex(ScopedSanitizerReport &srep, const ReportMutex *rm) {
Decorator d;
Printf("%s", d.Mutex());
- Printf(" Mutex M%u (%p) created at:\n", rm->id,
- reinterpret_cast<void *>(rm->addr));
+ InternalScopedString s;
+ s.AppendF("Mutex M%u (%p) created at:", rm->id,
+ reinterpret_cast<void *>(rm->addr));
+ Printf(" %s\n", s.data());
Printf("%s", d.Default());
- PrintStack(rm->stack);
+ PrintStack(srep, rm->stack, kReportStackOther, s.data(), s.length());
}
-static void PrintThread(const ReportThread *rt) {
+static void PrintThread(ScopedSanitizerReport &srep, const ReportThread *rt) {
Decorator d;
if (rt->id == kMainTid) // Little sense in describing the main thread.
return;
- Printf("%s", d.ThreadDescription());
- Printf(" Thread T%d", rt->id);
- if (rt->name && rt->name[0] != '\0')
- Printf(" '%s'", rt->name);
char thrbuf[kThreadBufSize];
+ InternalScopedString s;
+ s.AppendF("Thread T%d", rt->id);
+ if (rt->name && rt->name[0] != '\0')
+ s.AppendF(" '%s'", rt->name);
const char *thread_status = rt->running ? "running" : "finished";
+ bool print_stack = true;
if (rt->thread_type == ThreadType::Worker) {
- Printf(" (tid=%llu, %s) is a GCD worker thread\n", rt->os_id,
- thread_status);
- Printf("\n");
- Printf("%s", d.Default());
- return;
+ s.AppendF(" (tid=%llu, %s) is a GCD worker thread", rt->os_id,
+ thread_status);
+ print_stack = false;
+ } else {
+ s.AppendF(" (tid=%llu, %s) created by %s", rt->os_id, thread_status,
+ thread_name(thrbuf, rt->parent_tid));
+ if (rt->stack)
+ s.Append(" at:");
}
- Printf(" (tid=%llu, %s) created by %s", rt->os_id, thread_status,
- thread_name(thrbuf, rt->parent_tid));
- if (rt->stack)
- Printf(" at:");
- Printf("\n");
+ Printf("%s", d.ThreadDescription());
+ Printf(" %s", s.data());
+ Printf(print_stack ? "\n" : "\n\n");
Printf("%s", d.Default());
- PrintStack(rt->stack);
+ if (print_stack)
+ PrintStack(srep, rt->stack, kReportStackOther, s.data(), s.length());
}
static void PrintSleep(const ReportStack *s) {
@@ -283,10 +335,10 @@ void PrintReport(const ReportDesc *rep) {
Decorator d;
Printf("==================\n");
const char *rep_typ_str = ReportTypeString(rep->typ, rep->tag);
- Printf("%s", d.Warning());
- Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
- (int)internal_getpid());
- Printf("%s", d.Default());
+
+ ScopedSanitizerReport srep("ThreadSanitizer", rep_typ_str);
+ srep.AddTitleF("WARNING: ThreadSanitizer: %s (pid=%d)", rep_typ_str,
+ (int)internal_getpid());
if (rep->typ == ReportTypeErrnoInSignal)
Printf(" Signal %u handler invoked at:\n", rep->signum);
@@ -309,13 +361,13 @@ void PrintReport(const ReportDesc *rep) {
Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
Printf("%s", d.Default());
if (flags()->second_deadlock_stack) {
- PrintStack(rep->stacks[2*i]);
+ PrintStack(srep, rep->stacks[2*i], kReportStackOther, nullptr, 0);
Printf(" Mutex ");
PrintMutexShort(rep->mutexes[i],
" previously acquired by the same thread here:\n");
- PrintStack(rep->stacks[2*i+1]);
+ PrintStack(srep, rep->stacks[2*i+1], kReportStackOther, nullptr, 0);
} else {
- PrintStack(rep->stacks[i]);
+ PrintStack(srep, rep->stacks[i], kReportStackOther, nullptr, 0);
if (i == 0)
Printf(
" HINT: use TSAN_OPTIONS=second_deadlock_stack=1 "
@@ -326,26 +378,27 @@ void PrintReport(const ReportDesc *rep) {
for (uptr i = 0; i < rep->stacks.Size(); i++) {
if (i)
Printf(" and:\n");
- PrintStack(rep->stacks[i]);
+ PrintStack(srep, rep->stacks[i],
+ i == 0 ? kReportStackFault : kReportStackOther, nullptr, 0);
}
}
for (uptr i = 0; i < rep->mops.Size(); i++)
- PrintMop(rep->mops[i], i == 0);
+ PrintMop(srep, rep->mops[i], i == 0);
if (rep->sleep)
PrintSleep(rep->sleep);
for (uptr i = 0; i < rep->locs.Size(); i++)
- PrintLocation(rep->locs[i]);
+ PrintLocation(srep, rep->locs[i]);
if (rep->typ != ReportTypeDeadlock) {
for (uptr i = 0; i < rep->mutexes.Size(); i++)
- PrintMutex(rep->mutexes[i]);
+ PrintMutex(srep, rep->mutexes[i]);
}
for (uptr i = 0; i < rep->threads.Size(); i++)
- PrintThread(rep->threads[i]);
+ PrintThread(srep, rep->threads[i]);
if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
@@ -361,6 +414,7 @@ void PrintReport(const ReportDesc *rep) {
Printf("==================\n");
}
+
#else // #if !SANITIZER_GO
const Tid kMainGoroutineId = 1;
More information about the llvm-commits
mailing list