[compiler-rt] d043b9e - [TySan][Sanitizer Common] Make TySan compatible with sanitizer common… (#183310)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 13 03:03:07 PDT 2026
Author: Matthew Nagy
Date: 2026-04-13T11:03:02+01:00
New Revision: d043b9e38dd9494c3c56018b2cbaf44fe205b110
URL: https://github.com/llvm/llvm-project/commit/d043b9e38dd9494c3c56018b2cbaf44fe205b110
DIFF: https://github.com/llvm/llvm-project/commit/d043b9e38dd9494c3c56018b2cbaf44fe205b110.diff
LOG: [TySan][Sanitizer Common] Make TySan compatible with sanitizer common… (#183310)
… features and test suite
This involved:
- Implementing the `__sanitizer_print_stack_trace` interface
- Adding the common signal handlers
- Correctly set the tool name
- Cache the binary name before running
Added:
Modified:
compiler-rt/lib/tysan/tysan.cpp
compiler-rt/lib/tysan/tysan_interceptors.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 52f941180b8eb..c02e80dfea441 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -15,6 +15,7 @@
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_flag_parser.h"
#include "sanitizer_common/sanitizer_flags.h"
+#include "sanitizer_common/sanitizer_interface_internal.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_report_decorator.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
@@ -40,6 +41,30 @@ tysan_copy_types(const void *daddr, const void *saddr, uptr size) {
internal_memmove(shadow_for(daddr), shadow_for(saddr), size * sizeof(uptr));
}
+static void getStackTrace(bool fullStacktrace, uptr pc, uptr bp,
+ BufferedStackTrace *ST) {
+ uptr top = 0;
+ uptr bottom = 0;
+ if (fullStacktrace)
+ GetThreadStackTopAndBottom(false, &top, &bottom);
+ bool request_fast = StackTrace::WillUseFastUnwind(true);
+ ST->Unwind(kStackTraceMax, pc, bp, 0, top, bottom, request_fast);
+}
+
+namespace __tysan {
+void OnStackUnwind(const SignalContext &sig, const void *,
+ BufferedStackTrace *stack) {
+ getStackTrace(true, StackTrace::GetNextInstructionPc(sig.pc), sig.bp, stack);
+}
+} // namespace __tysan
+
+extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_print_stack_trace() {
+ GET_CURRENT_PC_BP;
+ UNINITIALIZED BufferedStackTrace stack;
+ getStackTrace(true, pc, bp, &stack);
+ stack.Print();
+}
+
static const char *getDisplayName(const char *Name) {
if (Name[0] == '\0')
return "<anonymous type>";
@@ -241,14 +266,8 @@ static void reportError(void *Addr, int Size, tysan_type_descriptor *TD,
Printf("\n");
if (pc) {
- uptr top = 0;
- uptr bottom = 0;
- if (flags().print_stacktrace)
- GetThreadStackTopAndBottom(false, &top, &bottom);
-
- bool request_fast = StackTrace::WillUseFastUnwind(true);
BufferedStackTrace ST;
- ST.Unwind(kStackTraceMax, pc, bp, 0, top, bottom, request_fast);
+ getStackTrace(flags().print_stacktrace, pc, bp, &ST);
ST.Print();
} else {
Printf("\n");
@@ -465,6 +484,8 @@ static void InitializeFlags() {
ReportUnrecognizedFlags();
if (common_flags()->help)
parser.PrintFlagDescriptions();
+
+ __sanitizer_set_report_path(common_flags()->log_path);
}
static void TySanInitializePlatformEarly() {
@@ -489,9 +510,13 @@ static void TySanInitializePlatformEarly() {
namespace __tysan {
bool tysan_inited = false;
bool tysan_init_is_running;
+void InitializeDeadlySignals();
} // namespace __tysan
extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __tysan_init() {
+ SanitizerToolName = "TypeSanitizer";
+ CacheBinaryName();
+
CHECK(!tysan_init_is_running);
if (tysan_inited)
return;
@@ -501,6 +526,7 @@ extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __tysan_init() {
TySanInitializePlatformEarly();
InitializeInterceptors();
+ InitializeDeadlySignals();
if (!MmapFixedNoReserve(ShadowAddr(), AppAddr() - ShadowAddr()))
Die();
diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp
index a9c55a3ae0cf0..0361c794cb10f 100644
--- a/compiler-rt/lib/tysan/tysan_interceptors.cpp
+++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp
@@ -22,12 +22,44 @@
#define TYSAN_INTERCEPT___STRDUP 0
#endif
+#define TYSAN_INTERCEPT_FUNC(name) \
+ do { \
+ if (!INTERCEPT_FUNCTION(name)) \
+ VReport(1, "TypeSanitizer: failed to intercept '%s'\n", #name); \
+ } while (0)
+
#if SANITIZER_LINUX
extern "C" int mallopt(int param, int value);
#endif
using namespace __sanitizer;
using namespace __tysan;
+namespace __tysan {
+// Defined in tysan.cpp
+void OnStackUnwind(const SignalContext &sig, const void *,
+ BufferedStackTrace *stack);
+
+static void TysanOnDeadlySignal(int signo, void *siginfo, void *context) {
+ HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr);
+}
+
+static bool tysanSignalsInitialized = false;
+void InitializeDeadlySignals();
+} // namespace __tysan
+
+#define SIGNAL_INTERCEPTOR_ENTER() __tysan::InitializeDeadlySignals()
+#define COMMON_INTERCEPT_FUNCTION(name) TYSAN_INTERCEPT_FUNC(name)
+#include "sanitizer_common/sanitizer_signal_interceptors.inc"
+
+namespace __tysan {
+void InitializeDeadlySignals() {
+ if (tysanSignalsInitialized)
+ return;
+ InitializeSignalInterceptors();
+ InstallDeadlySignalHandlers(&TysanOnDeadlySignal);
+ tysanSignalsInitialized = true;
+}
+} // namespace __tysan
namespace {
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
@@ -233,7 +265,7 @@ void InitializeInterceptors() {
TYSAN_MAYBE_INTERCEPT_MEMALIGN;
TYSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
TYSAN_MAYBE_INTERCEPT_PVALLOC;
- TYSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
+ TYSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
INTERCEPT_FUNCTION(posix_memalign);
INTERCEPT_FUNCTION(memset);
More information about the llvm-commits
mailing list