[compiler-rt] [compiler-rt] Expose TSAN report strings via __tsan_describe_{mop,loc,mutex,thread} (PR #200736)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 2 19:00:35 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-compiler-rt-sanitizer
Author: Andrew Haberlandt (ndrewh)
<details>
<summary>Changes</summary>
This refactors the various functions for printing TSAN report components into PrintX and DescribeX, where DescribeX can be used to obtain a (unformatted) string description of a report component e.g. the string `Location is heap block of size %zu at %p allocated by %s:`.
These are analogous to `__tsan_get_report_{mop,loc,mutex,thread}`, but return the exact string that would have appeared in the report.
This will make it easier to do something like #<!-- -->200526 downstream.
Assisted-by: Claude
---
Patch is 25.75 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/200736.diff
8 Files Affected:
- (modified) compiler-rt/include/sanitizer/tsan_interface.h (+55)
- (modified) compiler-rt/lib/tsan/rtl/tsan_debugging.cpp (+53)
- (modified) compiler-rt/lib/tsan/rtl/tsan_interface.h (+14)
- (modified) compiler-rt/lib/tsan/rtl/tsan_report.cpp (+85-52)
- (modified) compiler-rt/lib/tsan/rtl/tsan_report.h (+13)
- (modified) compiler-rt/lib/tsan/tests/unit/CMakeLists.txt (+1)
- (added) compiler-rt/lib/tsan/tests/unit/tsan_report_test.cpp (+152)
- (added) compiler-rt/test/tsan/debug_describe.cpp (+113)
``````````diff
diff --git a/compiler-rt/include/sanitizer/tsan_interface.h b/compiler-rt/include/sanitizer/tsan_interface.h
index e11a4175cd8ed..aa47d30659d2c 100644
--- a/compiler-rt/include/sanitizer/tsan_interface.h
+++ b/compiler-rt/include/sanitizer/tsan_interface.h
@@ -309,6 +309,61 @@ int SANITIZER_CDECL __tsan_get_report_unique_tid(void *report,
/// \returns An opaque pointer to the current report. Otherwise returns NULL.
void *SANITIZER_CDECL __tsan_get_current_report();
+/// Writes a single-line, uncolored description of a memory operation in the
+/// report into <c>out</c> (e.g. <c>" Write of size 4 at 0x... by thread
+/// T1"</c>).
+///
+/// The output is null-terminated and truncated to fit <c>outlen</c> bytes
+/// (including the terminator).
+///
+/// \param report Opaque pointer to the current report.
+/// \param idx Index of the memory operation (same indexing as
+/// __tsan_get_report_mop).
+/// \param first Non-zero if this is the first / primary memory operation
+/// in the report; controls "Write" vs "Previous write" wording.
+/// \param out Buffer to write the description into.
+/// \param outlen Capacity of <c>out</c> in bytes.
+/// \returns Returns 1 on success, 0 on failure.
+int SANITIZER_CDECL __tsan_describe_mop(void *report, unsigned long idx,
+ int first, char *out,
+ unsigned long outlen);
+
+/// Writes a single-line, uncolored description of a location in the report
+/// into <c>out</c>.
+///
+/// \param report Opaque pointer to the current report.
+/// \param idx Index of the location (same indexing as __tsan_get_report_loc).
+/// \param out Buffer to write the description into.
+/// \param outlen Capacity of <c>out</c> in bytes.
+/// \returns Returns 1 if a stack trace logically follows this description
+/// (true for heap and file-descriptor locations), 0 otherwise.
+int SANITIZER_CDECL __tsan_describe_loc(void *report, unsigned long idx,
+ char *out, unsigned long outlen);
+
+/// Writes a single-line, uncolored description of a mutex in the report
+/// into <c>out</c>.
+///
+/// \param report Opaque pointer to the current report.
+/// \param idx Index of the mutex (same indexing as __tsan_get_report_mutex).
+/// \param out Buffer to write the description into.
+/// \param outlen Capacity of <c>out</c> in bytes.
+/// \returns Returns 1 on success, 0 on failure.
+int SANITIZER_CDECL __tsan_describe_mutex(void *report, unsigned long idx,
+ char *out, unsigned long outlen);
+
+/// Writes a single-line, uncolored description of a thread in the report
+/// into <c>out</c>.
+///
+/// \param report Opaque pointer to the current report.
+/// \param idx Index of the thread (same indexing as __tsan_get_report_thread).
+/// \param out Buffer to write the description into.
+/// \param outlen Capacity of <c>out</c> in bytes.
+/// \returns Returns 1 if a stack trace logically follows this description
+/// (creation stack of the thread), 0 otherwise (e.g. main thread,
+/// GCD worker thread).
+int SANITIZER_CDECL __tsan_describe_thread(void *report, unsigned long idx,
+ char *out, unsigned long outlen);
+
#ifdef __cplusplus
} // extern "C"
#endif
diff --git a/compiler-rt/lib/tsan/rtl/tsan_debugging.cpp b/compiler-rt/lib/tsan/rtl/tsan_debugging.cpp
index b3422af756067..b33b56b63af0e 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_debugging.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_debugging.cpp
@@ -188,6 +188,59 @@ int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
return 1;
}
+namespace {
+// Copy `s` (null-terminated) to `out`, truncating to `outlen-1` bytes plus
+// a null terminator. No-op when outlen is zero.
+void CopyDescription(const InternalScopedString& s, char* out, uptr outlen) {
+ if (!out || !outlen)
+ return;
+ uptr n = Min(s.length(), outlen - 1);
+ internal_memcpy(out, s.data(), n);
+ out[n] = '\0';
+}
+} // namespace
+
+SANITIZER_INTERFACE_ATTRIBUTE
+int __tsan_describe_mop(void* report, uptr idx, int first, char* out,
+ uptr outlen) {
+ const ReportDesc* rep = (ReportDesc*)report;
+ CHECK_LT(idx, rep->mops.Size());
+ InternalScopedString s;
+ DescribeMop(rep->mops[idx], first != 0, s);
+ CopyDescription(s, out, outlen);
+ return 1;
+}
+
+SANITIZER_INTERFACE_ATTRIBUTE
+int __tsan_describe_loc(void* report, uptr idx, char* out, uptr outlen) {
+ const ReportDesc* rep = (ReportDesc*)report;
+ CHECK_LT(idx, rep->locs.Size());
+ InternalScopedString s;
+ bool stack_follows = DescribeLocation(rep->locs[idx], s);
+ CopyDescription(s, out, outlen);
+ return stack_follows ? 1 : 0;
+}
+
+SANITIZER_INTERFACE_ATTRIBUTE
+int __tsan_describe_mutex(void* report, uptr idx, char* out, uptr outlen) {
+ const ReportDesc* rep = (ReportDesc*)report;
+ CHECK_LT(idx, rep->mutexes.Size());
+ InternalScopedString s;
+ DescribeMutex(rep->mutexes[idx], s);
+ CopyDescription(s, out, outlen);
+ return 1;
+}
+
+SANITIZER_INTERFACE_ATTRIBUTE
+int __tsan_describe_thread(void* report, uptr idx, char* out, uptr outlen) {
+ const ReportDesc* rep = (ReportDesc*)report;
+ CHECK_LT(idx, rep->threads.Size());
+ InternalScopedString s;
+ bool stack_follows = DescribeThread(rep->threads[idx], s);
+ CopyDescription(s, out, outlen);
+ return stack_follows ? 1 : 0;
+}
+
SANITIZER_INTERFACE_ATTRIBUTE
const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
uptr *region_address_ptr,
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interface.h b/compiler-rt/lib/tsan/rtl/tsan_interface.h
index db94cf48f9c2d..313162947e62e 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interface.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_interface.h
@@ -183,6 +183,20 @@ int __tsan_get_report_thread(void *report, uptr idx, int *tid, ThreadID *os_id,
SANITIZER_INTERFACE_ATTRIBUTE
int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid);
+// Writes uncolored, single-line descriptions of report components into a
+// caller-provided buffer. Output is truncated to fit. The describe_loc and
+// describe_thread variants return whether a stack trace logically follows
+// the description; describe_mop and describe_mutex return 1 on success.
+SANITIZER_INTERFACE_ATTRIBUTE
+int __tsan_describe_mop(void* report, uptr idx, int first, char* out,
+ uptr outlen);
+SANITIZER_INTERFACE_ATTRIBUTE
+int __tsan_describe_loc(void* report, uptr idx, char* out, uptr outlen);
+SANITIZER_INTERFACE_ATTRIBUTE
+int __tsan_describe_mutex(void* report, uptr idx, char* out, uptr outlen);
+SANITIZER_INTERFACE_ATTRIBUTE
+int __tsan_describe_thread(void* report, uptr idx, char* out, uptr outlen);
+
// Returns the type of the pointer (heap, stack, global, ...) and if possible
// also the starting address (e.g. of a heap allocation) and size.
SANITIZER_INTERFACE_ATTRIBUTE
diff --git a/compiler-rt/lib/tsan/rtl/tsan_report.cpp b/compiler-rt/lib/tsan/rtl/tsan_report.cpp
index 17fd3064fb85a..19d39886aa7b6 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_report.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_report.cpp
@@ -139,68 +139,85 @@ static const char *ExternalMopDesc(bool first, bool write) {
: (write ? "Previous modifying" : "Previous read-only");
}
-static void PrintMop(const ReportMop *mop, bool first) {
- Decorator d;
+void DescribeMop(const ReportMop* mop, bool first, InternalScopedString& s) {
char thrbuf[kThreadBufSize];
- Printf("%s", d.Access());
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));
+ s.AppendF(" %s access of %s at %p by %s",
+ ExternalMopDesc(first, mop->write), object_type, (void*)mop->addr,
+ thread_name(thrbuf, mop->tid));
}
+}
+
+static void PrintMop(const ReportMop* mop, bool first) {
+ Decorator d;
+ Printf("%s", d.Access());
+ InternalScopedString s;
+ DescribeMop(mop, first, s);
+ Printf("%s", s.data());
PrintMutexSet(mop->mset);
Printf(":\n");
Printf("%s", d.Default());
PrintStack(mop->stack);
}
-static void PrintLocation(const ReportLocation *loc) {
- Decorator d;
+// Writes the (uncolored) location description and returns whether a stack
+// trace should follow (true only for Heap and FD locations).
+bool DescribeLocation(const ReportLocation* loc, InternalScopedString& s) {
char thrbuf[kThreadBufSize];
bool print_stack = false;
- Printf("%s", d.Location());
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));
+ 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 {
- 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));
+ 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;
} 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;
}
+ return print_stack;
+}
+
+static void PrintLocation(const ReportLocation* loc) {
+ Decorator d;
+ Printf("%s", d.Location());
+ InternalScopedString s;
+ bool print_stack = DescribeLocation(loc, s);
+ Printf("%s", s.data());
+ // Locations with a following stack are separated from it by a single
+ // newline; the others provide their own trailing blank line.
+ Printf(print_stack ? "\n" : "\n\n");
Printf("%s", d.Default());
if (print_stack)
PrintStack(loc->stack);
@@ -218,39 +235,55 @@ static void PrintMutexShortWithAddress(const ReportMutex *rm,
reinterpret_cast<void *>(rm->addr), d.Default(), after);
}
+void DescribeMutex(const ReportMutex* rm, InternalScopedString& s) {
+ s.AppendF(" Mutex M%u (%p) created at:", rm->id,
+ reinterpret_cast<void*>(rm->addr));
+}
+
static void PrintMutex(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;
+ DescribeMutex(rm, s);
+ Printf("%s\n", s.data());
Printf("%s", d.Default());
PrintStack(rm->stack);
}
-static void PrintThread(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);
+// Writes the (uncolored) description for a non-main thread and returns whether
+// a stack trace should follow (false for GCD worker threads).
+bool DescribeThread(const ReportThread* rt, InternalScopedString& s) {
char thrbuf[kThreadBufSize];
+ 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";
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);
+ return false;
}
- Printf(" (tid=%llu, %s) created by %s", rt->os_id, thread_status,
- thread_name(thrbuf, rt->parent_tid));
+ s.AppendF(" (tid=%llu, %s) created by %s", rt->os_id, thread_status,
+ thread_name(thrbuf, rt->parent_tid));
if (rt->stack)
- Printf(" at:");
- Printf("\n");
+ s.Append(" at:");
+ return true;
+}
+
+static void PrintThread(const ReportThread* rt) {
+ Decorator d;
+ if (rt->id == kMainTid) // Little sense in describing the main thread.
+ return;
+ Printf("%s", d.ThreadDescription());
+ InternalScopedString s;
+ bool print_stack = DescribeThread(rt, s);
+ Printf("%s", s.data());
+ // A thread with a following stack is separated from it by a single newline;
+ // otherwise we emit the trailing blank line here.
+ Printf(print_stack ? "\n" : "\n\n");
Printf("%s", d.Default());
- PrintStack(rt->stack);
+ if (print_stack)
+ PrintStack(rt->stack);
}
static void PrintSleep(const ReportStack *s) {
diff --git a/compiler-rt/lib/tsan/rtl/tsan_report.h b/compiler-rt/lib/tsan/rtl/tsan_report.h
index 53bb21964dbba..b3321894ea2cd 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_report.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_report.h
@@ -137,6 +137,19 @@ class ReportDesc {
void PrintReport(const ReportDesc *rep);
void PrintStack(const ReportStack *stack);
+// Write an uncolored, single-line (newline-free) description of a report
+// component into `s`. These are the formatting cores shared with the PrintX
+// functions; they perform no decoration, add no trailing newline, and print no
+// stack trace, so they can be reused to obtain the raw description string.
+//
+// DescribeLocation and DescribeThread return whether a stack trace logically
+// follows the description (e.g. false for non-heap locations and GCD worker
+// threads).
+void DescribeMop(const ReportMop* mop, bool first, InternalScopedString& s);
+bool DescribeLocation(const ReportLocation* loc, InternalScopedString& s);
+void DescribeMutex(const ReportMutex* rm, InternalScopedString& s);
+bool DescribeThread(const ReportThread* rt, InternalScopedString& s);
+
} // namespace __tsan
#endif // TSAN_REPORT_H
diff --git a/compiler-rt/lib/tsan/tests/unit/CMakeLists.txt b/compiler-rt/lib/tsan/tests/unit/CMakeLists.txt
index d183c3071cff8..cc8055c603e3c 100644
--- a/compiler-rt/lib/tsan/tests/unit/CMakeLists.txt
+++ b/compiler-rt/lib/tsan/tests/unit/CMakeLists.txt
@@ -4,6 +4,7 @@ set(TSAN_UNIT_TEST_SOURCES
tsan_ilist_test.cpp
tsan_mman_test.cpp
tsan_percent_test.cpp
+ tsan_report_test.cpp
tsan_shadow_test.cpp
tsan_stack_test.cpp
tsan_sync_test.cpp
diff --git a/compiler-rt/lib/tsan/tests/unit/tsan_report_test.cpp b/compiler-rt/lib/tsan/tests/unit/tsan_report_test.cpp
new file mode 100644
index 0000000000000..2150f16261859
--- /dev/null
+++ b/compiler-rt/lib/tsan/tests/unit/tsan_report_test.cpp
@@ -0,0 +1,152 @@
+//===-- tsan_report_test.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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of ThreadSanitizer (TSan), a race detector.
+//
+// Tests for the DescribeX functions in tsan_report.cpp, which produce the
+// uncolored, newline-free description strings shared with the PrintX path.
+//
+// NOTE: The exact string format asserted in these tests is not a stable
+// contract. These checks just pin down the current output so the DescribeX /
+// PrintX split stays in sync; if the report wording changes, update the
+// expected strings here to match.
+//
+//===----------------------------------------------------------------------===//
+#include "tsan_report.h"
+
+#include "gtest/gtest.h"
+
+namespace __tsan {
+
+TEST(Report, DescribeMop) {
+ ReportMop mop;
+ mop.tid = 1;
+ mop.addr = 0x1234;
+ mop.size = 4;
+ mop.write = true;
+ mop.atomic = false;
+ mop.external_tag = kExternalTagNone;
+
+ InternalScopedString s;
+ DescribeMop(&mop, /*first=*/true, s);
+ EXPECT_STREQ(" Write of size 4 at 0x1234 by thread T1", s.data());
+
+ s.clear();
+ mop.write = false;
+ DescribeMop(&mop, /*first=*/false, s);
+ EXPECT_STREQ(" Previous read of size 4 at 0x1234 by thread T1", s.data());
+
+ s.clear();
+ mop.write = true;
+ mop.atomic = true;
+ mop.tid = kMainTid;
+ DescribeMop(&mop, /*first=*/true, s);
+ EXPECT_STREQ(" Atomic write of size 4 at 0x1234 by main thread", s.data());
+}
+
+TEST(Report, DescribeLocationStackAndTls) {
+ ReportLocation loc;
+ loc.tid = 2;
+
+ loc.type = ReportLocationStack;
+ InternalScopedString s;
+ // Stack/TLS locations carry no following stack trace.
+ EXPECT_FALSE(DescribeLocation(&loc, s));
+ EXPECT_STREQ(" Location is stack of thread T2.", s.data());
+
+ loc.type = ReportLocationTLS;
+ s.clear();
+ EXPECT_FALSE(DescribeLocation(&loc, s));
+ EXPECT_STREQ(" Location is TLS of thread T2.", s.data());
+}
+
+TEST(Report, DescribeLocationHeap) {
+ ReportLocation loc;
+ loc.type = ReportLocationHeap;
+ loc.tid = 3;
+ loc.heap_chunk_start = 0x4000;
+ loc.heap_chunk_size = 16;
+ loc.external_tag = kExternalTagNone;
+
+ InternalScopedString s;
+ // Heap locations are followed by the allocation stack.
+ EXPECT_TRUE(DescribeLocation(&loc, s));
+ EXPECT_STREQ(
+ " Location is heap block of size 16 at 0x4000 allocated by thread T3:",
+ s.data());
+}
+
+TEST(Report, DescribeLocationFD) {
+ ReportLocation loc;
+ loc.type = ReportLocationFD;
+ loc.tid = kMainTid;
+ loc.fd = 7;
+
+ InternalScopedString s;
+ loc.fd_closed = false;
+ EXPECT_TRUE(DescribeLocation(&loc, s));
+ EXPECT_STREQ(" Location is file descriptor 7 created by main thread at:",
+ s.data());
+
+ s.clear();
+ loc.fd_closed = true;
+ EXPECT_TRUE(DescribeLocation(&loc, s));
+ EXPECT_STREQ(" Location is file descriptor 7 destroyed by main thread at:",
+ s.data());
+}
+
+TEST(Report, Des...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/200736
More information about the llvm-commits
mailing list