[compiler-rt] [TySan] Make TySan compatible with sanitizer common interceptors (par… (PR #197688)
Matthew Nagy via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 2 05:28:10 PDT 2026
https://github.com/gbMattN updated https://github.com/llvm/llvm-project/pull/197688
>From 0da91e7e84482b39eedf4aa7e57999b85f09b3f9 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Thu, 14 May 2026 15:05:29 +0100
Subject: [PATCH 1/4] [TySan] Make TySan compatible with sanitizer common
interceptors (partial #183310 reland)
---
compiler-rt/lib/tysan/tysan.cpp | 3 ++
compiler-rt/lib/tysan/tysan_interceptors.cpp | 39 +++++++++++++++++++-
2 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 6cbcafb79ebfe..884ac0bef22bd 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"
@@ -488,6 +489,7 @@ 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() {
@@ -503,6 +505,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..b54bdde173f8f 100644
--- a/compiler-rt/lib/tysan/tysan_interceptors.cpp
+++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp
@@ -22,6 +22,16 @@
#define TYSAN_INTERCEPT___STRDUP 0
#endif
+#if !SANITIZER_APPLE
+ #define TYSAN_INTERCEPT_FUNC(name) \
+ do { \
+ if (!INTERCEPT_FUNCTION(name)) \
+ VReport(1, "TypeSanitizer: failed to intercept '%s'\n", #name); \
+ } while (0)
+#else
+ #define TYSAN_INTERCEPT_FUNC(name)
+#endif
+
#if SANITIZER_LINUX
extern "C" int mallopt(int param, int value);
#endif
@@ -29,6 +39,33 @@ extern "C" int mallopt(int param, int value);
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> {
static bool UseImpl() { return !tysan_inited; }
@@ -233,7 +270,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);
>From c6e6fc37c671746887ff291f7b97ea9ac0098188 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Thu, 14 May 2026 15:10:09 +0100
Subject: [PATCH 2/4] unindent macro
---
compiler-rt/lib/tysan/tysan_interceptors.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp
index b54bdde173f8f..70a1a918cba39 100644
--- a/compiler-rt/lib/tysan/tysan_interceptors.cpp
+++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp
@@ -23,13 +23,13 @@
#endif
#if !SANITIZER_APPLE
- #define TYSAN_INTERCEPT_FUNC(name) \
- do { \
- if (!INTERCEPT_FUNCTION(name)) \
- VReport(1, "TypeSanitizer: failed to intercept '%s'\n", #name); \
- } while (0)
+#define TYSAN_INTERCEPT_FUNC(name) \
+ do { \
+ if (!INTERCEPT_FUNCTION(name)) \
+ VReport(1, "TypeSanitizer: failed to intercept '%s'\n", #name); \
+ } while (0)
#else
- #define TYSAN_INTERCEPT_FUNC(name)
+#define TYSAN_INTERCEPT_FUNC(name)
#endif
#if SANITIZER_LINUX
>From e98543594925a9d874d7fb3f00148895da6761e6 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Fri, 15 May 2026 14:21:10 +0100
Subject: [PATCH 3/4] Copied ASan's interceptor explaination to TySan
---
compiler-rt/lib/tysan/tysan_interceptors.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp
index 70a1a918cba39..118567ed4264a 100644
--- a/compiler-rt/lib/tysan/tysan_interceptors.cpp
+++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp
@@ -28,7 +28,7 @@
if (!INTERCEPT_FUNCTION(name)) \
VReport(1, "TypeSanitizer: failed to intercept '%s'\n", #name); \
} while (0)
-#else
+#else // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
#define TYSAN_INTERCEPT_FUNC(name)
#endif
>From 04f3add2831c15049dae15ed95c2ab57487dde6f Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Tue, 2 Jun 2026 13:27:56 +0100
Subject: [PATCH 4/4] Move static var into function
---
compiler-rt/lib/tysan/tysan_interceptors.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/compiler-rt/lib/tysan/tysan_interceptors.cpp b/compiler-rt/lib/tysan/tysan_interceptors.cpp
index 118567ed4264a..46765cc1b77e1 100644
--- a/compiler-rt/lib/tysan/tysan_interceptors.cpp
+++ b/compiler-rt/lib/tysan/tysan_interceptors.cpp
@@ -48,7 +48,6 @@ static void TysanOnDeadlySignal(int signo, void *siginfo, void *context) {
HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr);
}
-static bool tysanSignalsInitialized = false;
void InitializeDeadlySignals();
} // namespace __tysan
@@ -58,6 +57,7 @@ void InitializeDeadlySignals();
namespace __tysan {
void InitializeDeadlySignals() {
+ static bool tysanSignalsInitialized = false;
if (tysanSignalsInitialized)
return;
InitializeSignalInterceptors();
More information about the llvm-commits
mailing list