[clang] [compiler-rt] [llvm] [TySan] Let TypeSanitizer know about conservative path TBAA data (PR #227782)

Matthew Nagy via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 02:56:29 PST 2026


https://github.com/gbMattN updated https://github.com/llvm/llvm-project/pull/227782

>From 35ec688fb374c9f66b0762af99117977fe4f9fff Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Wed, 30 Sep 2026 17:07:22 +0100
Subject: [PATCH 01/12] [TySan] Let TypeSanitizer know about conservative path
 TBAA data

---
 clang/lib/CodeGen/CodeGenTBAA.cpp             |  3 ++
 compiler-rt/lib/tysan/tysan.cpp               | 22 +++++++++++++--
 .../tysan/no-false-positive-issue208646.cpp   | 11 ++++++++
 .../tysan/no-false-positive-issue208647.cpp   | 10 +++++++
 .../tysan/no-false-positive-issue208655.cpp   | 15 ++++++++++
 .../tysan/no-false-positive-issue210643.cpp   | 11 ++++++++
 .../Instrumentation/TypeSanitizer.cpp         | 28 ++++++++++++++++++-
 7 files changed, 96 insertions(+), 4 deletions(-)
 create mode 100644 compiler-rt/test/tysan/no-false-positive-issue208646.cpp
 create mode 100644 compiler-rt/test/tysan/no-false-positive-issue208647.cpp
 create mode 100644 compiler-rt/test/tysan/no-false-positive-issue208655.cpp
 create mode 100644 compiler-rt/test/tysan/no-false-positive-issue210643.cpp

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 3b2dc799102b96..0ec312154a595f 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -372,6 +372,9 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
   }
 
   // For now, handle any other kind of type conservatively.
+  if(Features.Sanitize.has(SanitizerKind::Type)){
+    return createScalarTypeNode("TysanConservativeTBAA", getChar(), 1);
+  }
   return getChar();
 }
 
diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 6b2adda10c6250..da34d5fe27898e 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -128,12 +128,27 @@ static tysan_type_descriptor *getRootTD(tysan_type_descriptor *TD) {
   return RootTD;
 }
 
+// Currently, Clang's TBAA system does not correctly describe every possible
+// type. For unhandled types, it makes the most conservative choice, emitting
+// omnipotent char. TySan needs to handle this seperately to a real omnipotent
+// char otherwise the user may get false positives. When compiling with TySan
+// enabled, clang will emit a special TBAA type to show that the conservative
+// path has been taken. When the transformation pass finds this TBAA, it will
+// set this global variable. This then allows quick comparison of TDs at
+// runtime.
+static tysan_type_descriptor *__tysan_conservative_tbaa_descriptor = nullptr;
+extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
+__tysan_set_conservative_tbaa_descriptor(
+    tysan_type_descriptor *conservativeTBAATD) {
+  __tysan_conservative_tbaa_descriptor = conservativeTBAATD;
+}
+
 // Walk up TDA to see if it reaches TDB.
 static bool walkAliasTree(tysan_type_descriptor *TDA,
                           tysan_type_descriptor *TDB, uptr OffsetA,
                           uptr OffsetB) {
   do {
-    if (TDA == TDB)
+    if (TDA == TDB || TDA == __tysan_conservative_tbaa_descriptor)
       return OffsetA == OffsetB;
 
     if (TDA->Tag == TYSAN_STRUCT_TD) {
@@ -182,7 +197,6 @@ static bool isAliasingLegalUp(tysan_type_descriptor *TDA,
     OffsetA = TDA->Member.Offset;
     TDA = TDA->Member.Base;
   }
-
   return walkAliasTree(TDA, TDB, OffsetA, OffsetB);
 }
 
@@ -213,7 +227,9 @@ static bool isAliasingLegalWithOffset(tysan_type_descriptor *TDA,
 
 static bool isAliasingLegal(tysan_type_descriptor *TDA,
                             tysan_type_descriptor *TDB, uptr OffsetB = 0) {
-  if (TDA == TDB || !TDB || !TDA)
+  if (TDA == TDB || !TDB || !TDA ||
+      TDA == __tysan_conservative_tbaa_descriptor ||
+      TDB == __tysan_conservative_tbaa_descriptor)
     return true;
 
   // Aliasing is legal is the two types have different root nodes.
diff --git a/compiler-rt/test/tysan/no-false-positive-issue208646.cpp b/compiler-rt/test/tysan/no-false-positive-issue208646.cpp
new file mode 100644
index 00000000000000..236b4a86d0a2ba
--- /dev/null
+++ b/compiler-rt/test/tysan/no-false-positive-issue208646.cpp
@@ -0,0 +1,11 @@
+// RUN: %clangxx_tysan -O0 %s -o %t && %run %t
+
+#include <string>
+#include <optional>
+
+static std::optional<std::string> optional_var = std::nullopt;
+
+int main() { 
+  optional_var = "this is a random long string (short one does not reproduce)";
+  return 0;
+}
diff --git a/compiler-rt/test/tysan/no-false-positive-issue208647.cpp b/compiler-rt/test/tysan/no-false-positive-issue208647.cpp
new file mode 100644
index 00000000000000..ef8b004496a16c
--- /dev/null
+++ b/compiler-rt/test/tysan/no-false-positive-issue208647.cpp
@@ -0,0 +1,10 @@
+// RUN: %clangxx_tysan -O0 %s -o %t && %run %t
+
+#include <variant>
+
+int main() {
+  std::variant<int, double> v;
+  v = 1;
+  v = 3.5;
+  return 0;
+}
diff --git a/compiler-rt/test/tysan/no-false-positive-issue208655.cpp b/compiler-rt/test/tysan/no-false-positive-issue208655.cpp
new file mode 100644
index 00000000000000..7518b244513cda
--- /dev/null
+++ b/compiler-rt/test/tysan/no-false-positive-issue208655.cpp
@@ -0,0 +1,15 @@
+// RUN: %clangxx_tysan -O0 %s -o %t && %run %t
+
+#include <vector>
+
+struct Registry {
+    std::vector<int> arr;
+    char temp_byte;
+    bool bool_var = false;
+};
+
+int main() {
+    static Registry r;
+    r.arr.push_back(0);
+    r.bool_var = true;
+}
diff --git a/compiler-rt/test/tysan/no-false-positive-issue210643.cpp b/compiler-rt/test/tysan/no-false-positive-issue210643.cpp
new file mode 100644
index 00000000000000..d667f75646cfdf
--- /dev/null
+++ b/compiler-rt/test/tysan/no-false-positive-issue210643.cpp
@@ -0,0 +1,11 @@
+// RUN: %clangxx_tysan -O0 %s -o %t && %run %t
+
+struct A {
+    int elems[3];
+};
+
+A a;
+
+int main() {
+    a.elems[0] = 1;
+}
diff --git a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
index a82102b913acc0..3852deb30b2f95 100644
--- a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
@@ -63,6 +63,7 @@ struct TypeSanitizer {
   TypeSanitizer(const InstrumentationOptions &Opts, Module &M);
   bool sanitizeFunction(Function &F, const TargetLibraryInfo &TLI);
   void instrumentGlobals(Module &M);
+  void setConservativeTBAA(Module &M);
 
   const InstrumentationOptions &Opts;
   bool OutlineInstrumentation;
@@ -113,6 +114,9 @@ struct TypeSanitizer {
   FunctionCallee TysanInstrumentWithShadowUpdate;
   FunctionCallee TysanSetShadowType;
 
+  FunctionCallee TysanSetConservativeTBAADescriptor;
+  GlobalVariable *ConservativeTBAADescriptor;
+
   /// Callback to set types for gloabls.
   Function *TysanGlobalsSetTypeFunction;
 };
@@ -121,7 +125,8 @@ struct TypeSanitizer {
 TypeSanitizer::TypeSanitizer(const InstrumentationOptions &Opts, Module &M)
     : Opts(Opts), OutlineInstrumentation(Opts.tysan_outline_instrumentation),
       TargetTriple(M.getTargetTriple()),
-      AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N") {
+      AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N"),
+      ConservativeTBAADescriptor(nullptr) {
   const DataLayout &DL = M.getDataLayout();
   IntptrTy = DL.getIntPtrType(M.getContext());
   PtrShift = countr_zero(IntptrTy->getPrimitiveSizeInBits() / 8);
@@ -186,6 +191,12 @@ void TypeSanitizer::initializeCallbacks(Module &M) {
       IRB.getPtrTy(), // Pointer to the new type descriptor
       U64Ty           // Size of data we access in bytes
   );
+
+  TysanSetConservativeTBAADescriptor = M.getOrInsertFunction(
+      "__tysan_set_conservative_tbaa_descriptor", Attr, IRB.getVoidTy(),
+      IRB.getPtrTy() // Pointer to the type descriptor that describes the TBAA
+                     // clang generates under the conservative TBAA path
+  );
 }
 
 void TypeSanitizer::instrumentGlobals(Module &M) {
@@ -234,6 +245,16 @@ void TypeSanitizer::instrumentGlobals(Module &M) {
   }
 }
 
+void TypeSanitizer::setConservativeTBAA(Module &M) {
+  if (ConservativeTBAADescriptor) {
+    IRBuilder<> IRB(cast<Function>(TysanCtorFunction.getCallee())
+                        ->getEntryBlock()
+                        .getTerminator());
+    IRB.CreateCall(TysanSetConservativeTBAADescriptor,
+                   {ConservativeTBAADescriptor});
+  }
+}
+
 static const char LUT[] = "0123456789abcdef";
 
 static std::string encodeName(StringRef Name) {
@@ -399,6 +420,9 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
                          TD, EncodedName);
   M.insertGlobalVariable(TDGV);
 
+  if (Name == "TysanConservativeTBAA") {
+    ConservativeTBAADescriptor = TDGV;
+  }
   if (ShouldBeComdat) {
     if (TargetTriple.isOSBinFormatELF()) {
       Comdat *TDComdat = M.getOrInsertComdat(EncodedName);
@@ -988,5 +1012,7 @@ PreservedAnalyses TypeSanitizerPass::run(Module &M,
     }
   }
 
+  TySan.setConservativeTBAA(M);
+
   return PreservedAnalyses::none();
 }

>From bae31bab97496dfd0e2d03a36253217f74acbc4f Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Wed, 30 Sep 2026 17:52:42 +0100
Subject: [PATCH 02/12] Format repro's copied from issues

---
 .../test/tysan/no-false-positive-issue208646.cpp     |  4 ++--
 .../test/tysan/no-false-positive-issue208655.cpp     | 12 ++++++------
 .../test/tysan/no-false-positive-issue210643.cpp     |  6 ++----
 3 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/compiler-rt/test/tysan/no-false-positive-issue208646.cpp b/compiler-rt/test/tysan/no-false-positive-issue208646.cpp
index 236b4a86d0a2ba..fc8a2da6e8b11e 100644
--- a/compiler-rt/test/tysan/no-false-positive-issue208646.cpp
+++ b/compiler-rt/test/tysan/no-false-positive-issue208646.cpp
@@ -1,11 +1,11 @@
 // RUN: %clangxx_tysan -O0 %s -o %t && %run %t
 
-#include <string>
 #include <optional>
+#include <string>
 
 static std::optional<std::string> optional_var = std::nullopt;
 
-int main() { 
+int main() {
   optional_var = "this is a random long string (short one does not reproduce)";
   return 0;
 }
diff --git a/compiler-rt/test/tysan/no-false-positive-issue208655.cpp b/compiler-rt/test/tysan/no-false-positive-issue208655.cpp
index 7518b244513cda..cba71c5b5be499 100644
--- a/compiler-rt/test/tysan/no-false-positive-issue208655.cpp
+++ b/compiler-rt/test/tysan/no-false-positive-issue208655.cpp
@@ -3,13 +3,13 @@
 #include <vector>
 
 struct Registry {
-    std::vector<int> arr;
-    char temp_byte;
-    bool bool_var = false;
+  std::vector<int> arr;
+  char temp_byte;
+  bool bool_var = false;
 };
 
 int main() {
-    static Registry r;
-    r.arr.push_back(0);
-    r.bool_var = true;
+  static Registry r;
+  r.arr.push_back(0);
+  r.bool_var = true;
 }
diff --git a/compiler-rt/test/tysan/no-false-positive-issue210643.cpp b/compiler-rt/test/tysan/no-false-positive-issue210643.cpp
index d667f75646cfdf..4a74abd43d02de 100644
--- a/compiler-rt/test/tysan/no-false-positive-issue210643.cpp
+++ b/compiler-rt/test/tysan/no-false-positive-issue210643.cpp
@@ -1,11 +1,9 @@
 // RUN: %clangxx_tysan -O0 %s -o %t && %run %t
 
 struct A {
-    int elems[3];
+  int elems[3];
 };
 
 A a;
 
-int main() {
-    a.elems[0] = 1;
-}
+int main() { a.elems[0] = 1; }

>From 0e4c37690ca84840b3697e27ad436a798bfcccc1 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Thu, 1 Oct 2026 11:17:59 +0100
Subject: [PATCH 03/12] Replace global sentinal with new TD tag

---
 compiler-rt/lib/tysan/tysan.cpp               | 23 +++---------
 compiler-rt/lib/tysan/tysan.h                 |  2 +-
 .../Instrumentation/TypeSanitizer.cpp         | 36 ++++++-------------
 3 files changed, 16 insertions(+), 45 deletions(-)

diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index da34d5fe27898e..c2f78fd9c44fa3 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -119,6 +119,8 @@ static tysan_type_descriptor *getRootTD(tysan_type_descriptor *TD) {
         TD = nullptr;
     } else if (TD->Tag == TYSAN_MEMBER_TD) {
       TD = TD->Member.Access;
+    } else if (TD->Tag == TYSAN_CONSERVATIVE_ALIAS_TD) {
+      return RootTD;
     } else {
       CHECK(false && "invalid enum value");
       break;
@@ -128,27 +130,12 @@ static tysan_type_descriptor *getRootTD(tysan_type_descriptor *TD) {
   return RootTD;
 }
 
-// Currently, Clang's TBAA system does not correctly describe every possible
-// type. For unhandled types, it makes the most conservative choice, emitting
-// omnipotent char. TySan needs to handle this seperately to a real omnipotent
-// char otherwise the user may get false positives. When compiling with TySan
-// enabled, clang will emit a special TBAA type to show that the conservative
-// path has been taken. When the transformation pass finds this TBAA, it will
-// set this global variable. This then allows quick comparison of TDs at
-// runtime.
-static tysan_type_descriptor *__tysan_conservative_tbaa_descriptor = nullptr;
-extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
-__tysan_set_conservative_tbaa_descriptor(
-    tysan_type_descriptor *conservativeTBAATD) {
-  __tysan_conservative_tbaa_descriptor = conservativeTBAATD;
-}
-
 // Walk up TDA to see if it reaches TDB.
 static bool walkAliasTree(tysan_type_descriptor *TDA,
                           tysan_type_descriptor *TDB, uptr OffsetA,
                           uptr OffsetB) {
   do {
-    if (TDA == TDB || TDA == __tysan_conservative_tbaa_descriptor)
+    if (TDA == TDB || TDA->Tag == TYSAN_CONSERVATIVE_ALIAS_TD)
       return OffsetA == OffsetB;
 
     if (TDA->Tag == TYSAN_STRUCT_TD) {
@@ -228,8 +215,8 @@ static bool isAliasingLegalWithOffset(tysan_type_descriptor *TDA,
 static bool isAliasingLegal(tysan_type_descriptor *TDA,
                             tysan_type_descriptor *TDB, uptr OffsetB = 0) {
   if (TDA == TDB || !TDB || !TDA ||
-      TDA == __tysan_conservative_tbaa_descriptor ||
-      TDB == __tysan_conservative_tbaa_descriptor)
+      TDA->Tag == TYSAN_CONSERVATIVE_ALIAS_TD ||
+      TDB->Tag == TYSAN_CONSERVATIVE_ALIAS_TD)
     return true;
 
   // Aliasing is legal is the two types have different root nodes.
diff --git a/compiler-rt/lib/tysan/tysan.h b/compiler-rt/lib/tysan/tysan.h
index 791c6a47ce5f86..8ac957250b0027 100644
--- a/compiler-rt/lib/tysan/tysan.h
+++ b/compiler-rt/lib/tysan/tysan.h
@@ -33,7 +33,7 @@ extern bool tysan_init_is_running;
 
 void InitializeInterceptors();
 
-enum { TYSAN_MEMBER_TD = 1, TYSAN_STRUCT_TD = 2 };
+enum { TYSAN_MEMBER_TD = 1, TYSAN_STRUCT_TD = 2, TYSAN_CONSERVATIVE_ALIAS_TD = 3 };
 
 struct tysan_member_type_descriptor {
   struct tysan_type_descriptor *Base;
diff --git a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
index 3852deb30b2f95..7a9eee426f60e1 100644
--- a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
@@ -63,7 +63,6 @@ struct TypeSanitizer {
   TypeSanitizer(const InstrumentationOptions &Opts, Module &M);
   bool sanitizeFunction(Function &F, const TargetLibraryInfo &TLI);
   void instrumentGlobals(Module &M);
-  void setConservativeTBAA(Module &M);
 
   const InstrumentationOptions &Opts;
   bool OutlineInstrumentation;
@@ -114,19 +113,22 @@ struct TypeSanitizer {
   FunctionCallee TysanInstrumentWithShadowUpdate;
   FunctionCallee TysanSetShadowType;
 
-  FunctionCallee TysanSetConservativeTBAADescriptor;
-  GlobalVariable *ConservativeTBAADescriptor;
-
   /// Callback to set types for gloabls.
   Function *TysanGlobalsSetTypeFunction;
 };
 } // namespace
 
+<<<<<<< HEAD
 TypeSanitizer::TypeSanitizer(const InstrumentationOptions &Opts, Module &M)
     : Opts(Opts), OutlineInstrumentation(Opts.tysan_outline_instrumentation),
       TargetTriple(M.getTargetTriple()),
       AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N"),
       ConservativeTBAADescriptor(nullptr) {
+=======
+TypeSanitizer::TypeSanitizer(Module &M)
+    : TargetTriple(M.getTargetTriple()),
+      AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N") {
+>>>>>>> 8678792148af (Replace global sentinal with new TD tag)
   const DataLayout &DL = M.getDataLayout();
   IntptrTy = DL.getIntPtrType(M.getContext());
   PtrShift = countr_zero(IntptrTy->getPrimitiveSizeInBits() / 8);
@@ -191,12 +193,6 @@ void TypeSanitizer::initializeCallbacks(Module &M) {
       IRB.getPtrTy(), // Pointer to the new type descriptor
       U64Ty           // Size of data we access in bytes
   );
-
-  TysanSetConservativeTBAADescriptor = M.getOrInsertFunction(
-      "__tysan_set_conservative_tbaa_descriptor", Attr, IRB.getVoidTy(),
-      IRB.getPtrTy() // Pointer to the type descriptor that describes the TBAA
-                     // clang generates under the conservative TBAA path
-  );
 }
 
 void TypeSanitizer::instrumentGlobals(Module &M) {
@@ -245,16 +241,6 @@ void TypeSanitizer::instrumentGlobals(Module &M) {
   }
 }
 
-void TypeSanitizer::setConservativeTBAA(Module &M) {
-  if (ConservativeTBAADescriptor) {
-    IRBuilder<> IRB(cast<Function>(TysanCtorFunction.getCallee())
-                        ->getEntryBlock()
-                        .getTerminator());
-    IRB.CreateCall(TysanSetConservativeTBAADescriptor,
-                   {ConservativeTBAADescriptor});
-  }
-}
-
 static const char LUT[] = "0123456789abcdef";
 
 static std::string encodeName(StringRef Name) {
@@ -393,7 +379,10 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
     TDSubData.push_back(C);
   };
 
-  PushTDSub(ConstantInt::get(IntptrTy, 2));
+  int TDTag = 2; // 2 is for structs
+  if (Name == "TysanConservativeTBAA")
+    TDTag = 3; // 3 is for TDs that conservatively alias with everything
+  PushTDSub(ConstantInt::get(IntptrTy, TDTag));
   PushTDSub(ConstantInt::get(IntptrTy, Members.size()));
 
   // Types that are in an anonymous namespace are local to this module.
@@ -420,9 +409,6 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
                          TD, EncodedName);
   M.insertGlobalVariable(TDGV);
 
-  if (Name == "TysanConservativeTBAA") {
-    ConservativeTBAADescriptor = TDGV;
-  }
   if (ShouldBeComdat) {
     if (TargetTriple.isOSBinFormatELF()) {
       Comdat *TDComdat = M.getOrInsertComdat(EncodedName);
@@ -1012,7 +998,5 @@ PreservedAnalyses TypeSanitizerPass::run(Module &M,
     }
   }
 
-  TySan.setConservativeTBAA(M);
-
   return PreservedAnalyses::none();
 }

>From b627608262a4dbe09916f61d977847fa6479edc9 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Thu, 1 Oct 2026 11:38:55 +0100
Subject: [PATCH 04/12] Formatting

---
 clang/lib/CodeGen/CodeGenTBAA.cpp | 2 +-
 compiler-rt/lib/tysan/tysan.cpp   | 3 +--
 compiler-rt/lib/tysan/tysan.h     | 6 +++++-
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 0ec312154a595f..2cf62092351bb4 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -372,7 +372,7 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
   }
 
   // For now, handle any other kind of type conservatively.
-  if(Features.Sanitize.has(SanitizerKind::Type)){
+  if (Features.Sanitize.has(SanitizerKind::Type)) {
     return createScalarTypeNode("TysanConservativeTBAA", getChar(), 1);
   }
   return getChar();
diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index c2f78fd9c44fa3..8c9d8d291c4d7c 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -214,8 +214,7 @@ static bool isAliasingLegalWithOffset(tysan_type_descriptor *TDA,
 
 static bool isAliasingLegal(tysan_type_descriptor *TDA,
                             tysan_type_descriptor *TDB, uptr OffsetB = 0) {
-  if (TDA == TDB || !TDB || !TDA ||
-      TDA->Tag == TYSAN_CONSERVATIVE_ALIAS_TD ||
+  if (TDA == TDB || !TDB || !TDA || TDA->Tag == TYSAN_CONSERVATIVE_ALIAS_TD ||
       TDB->Tag == TYSAN_CONSERVATIVE_ALIAS_TD)
     return true;
 
diff --git a/compiler-rt/lib/tysan/tysan.h b/compiler-rt/lib/tysan/tysan.h
index 8ac957250b0027..f279ad795a2ffb 100644
--- a/compiler-rt/lib/tysan/tysan.h
+++ b/compiler-rt/lib/tysan/tysan.h
@@ -33,7 +33,11 @@ extern bool tysan_init_is_running;
 
 void InitializeInterceptors();
 
-enum { TYSAN_MEMBER_TD = 1, TYSAN_STRUCT_TD = 2, TYSAN_CONSERVATIVE_ALIAS_TD = 3 };
+enum {
+  TYSAN_MEMBER_TD = 1,
+  TYSAN_STRUCT_TD = 2,
+  TYSAN_CONSERVATIVE_ALIAS_TD = 3
+};
 
 struct tysan_member_type_descriptor {
   struct tysan_type_descriptor *Base;

>From 887f596e5ae69bd1ec1447894bf2caf041d959c6 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Thu, 1 Oct 2026 11:43:42 +0100
Subject: [PATCH 05/12] nit. line

---
 compiler-rt/lib/tysan/tysan.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 8c9d8d291c4d7c..db719c3b7457ab 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -184,6 +184,7 @@ static bool isAliasingLegalUp(tysan_type_descriptor *TDA,
     OffsetA = TDA->Member.Offset;
     TDA = TDA->Member.Base;
   }
+
   return walkAliasTree(TDA, TDB, OffsetA, OffsetB);
 }
 

>From 8acbf27431319a7900c135a9d21efc22e98116df Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Fri, 2 Oct 2026 16:08:09 +0100
Subject: [PATCH 06/12] Add clang test

---
 .../CodeGen/sanitize-type-conservative-tbaa.cpp | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp

diff --git a/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp b/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp
new file mode 100644
index 00000000000000..c3ef72ae1f33cb
--- /dev/null
+++ b/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang --target=x86_64-linux-gnu -S -fsanitize=type -emit-llvm -o - %s \
+// RUN:     | FileCheck %s
+
+// With TySan enabled, when "omnipotent char" TBAA would be emitted,
+// instead it should emit TysanConservativeTBAA. This gets recorded
+// by TySan, and it emits a specially tagged type descriptor.
+
+// CHECK: @__tysan_v1_TysanConservativeTBAA = linkonce_odr constant { i64, i64, ptr, i64, [22 x i8] } { i64 3, i64 1, ptr @__tysan_v1_omnipotent_20char, i64 0, [22 x i8] c"TysanConservativeTBAA\00" }, comdat
+
+struct S{
+    float forceConservativeTBAAPath[1];
+};
+
+S s;
+
+// CHECK: !{!"TysanConservativeTBAA",
+// CHECK-NOT: !{!"float",

>From 13f1fbc87cb26cd985f72396595652108c378eaa Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Fri, 2 Oct 2026 16:32:22 +0100
Subject: [PATCH 07/12] Replace CHECK-NOT with implicit check not flag

---
 clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp b/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp
index c3ef72ae1f33cb..b3e7ac80711787 100644
--- a/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp
+++ b/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp
@@ -1,5 +1,6 @@
 // RUN: %clang --target=x86_64-linux-gnu -S -fsanitize=type -emit-llvm -o - %s \
-// RUN:     | FileCheck %s
+// RUN:     | FileCheck %s --implicit-check-not='!{!"float"'
+
 
 // With TySan enabled, when "omnipotent char" TBAA would be emitted,
 // instead it should emit TysanConservativeTBAA. This gets recorded
@@ -14,4 +15,3 @@ struct S{
 S s;
 
 // CHECK: !{!"TysanConservativeTBAA",
-// CHECK-NOT: !{!"float",

>From 2504f5cf8a79ae2d8e5436ded13c056dd7163342 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Mon, 26 Oct 2026 22:26:20 +0000
Subject: [PATCH 08/12] TysanConservativeTBAA -> Tysan Conservative TBAA

---
 clang/lib/CodeGen/CodeGenTBAA.cpp                      | 2 +-
 clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp | 6 ++++--
 llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp  | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 2cf62092351bb4..7ac77d2dcaeb64 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -373,7 +373,7 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
 
   // For now, handle any other kind of type conservatively.
   if (Features.Sanitize.has(SanitizerKind::Type)) {
-    return createScalarTypeNode("TysanConservativeTBAA", getChar(), 1);
+    return createScalarTypeNode("Tysan Conservative TBAA", getChar(), 1);
   }
   return getChar();
 }
diff --git a/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp b/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp
index b3e7ac80711787..1803a4519a64b9 100644
--- a/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp
+++ b/clang/test/CodeGen/sanitize-type-conservative-tbaa.cpp
@@ -6,7 +6,7 @@
 // instead it should emit TysanConservativeTBAA. This gets recorded
 // by TySan, and it emits a specially tagged type descriptor.
 
-// CHECK: @__tysan_v1_TysanConservativeTBAA = linkonce_odr constant { i64, i64, ptr, i64, [22 x i8] } { i64 3, i64 1, ptr @__tysan_v1_omnipotent_20char, i64 0, [22 x i8] c"TysanConservativeTBAA\00" }, comdat
+// CHECK: @__tysan_v1_Tysan_20Conservative_20TBAA = linkonce_odr constant { i64, i64, ptr, i64, [24 x i8] } { i64 3, i64 1, ptr @__tysan_v1_omnipotent_20char, i64 0, [24 x i8] c"Tysan Conservative TBAA\00" }, comdat
 
 struct S{
     float forceConservativeTBAAPath[1];
@@ -14,4 +14,6 @@ struct S{
 
 S s;
 
-// CHECK: !{!"TysanConservativeTBAA",
+// CHECK: !{!"Tysan Conservative TBAA", [[META1:![0-9]+]], i64 0}
+// CHECK: [[META1]] = !{!"omnipotent char", [[META2:![0-9]+]], i64 0}
+// CHECK: [[META2]] = !{!"Simple C++ TBAA"}
diff --git a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
index 7a9eee426f60e1..1b97ce994714bf 100644
--- a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
@@ -380,7 +380,7 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
   };
 
   int TDTag = 2; // 2 is for structs
-  if (Name == "TysanConservativeTBAA")
+  if (Name == "Tysan Conservative TBAA")
     TDTag = 3; // 3 is for TDs that conservatively alias with everything
   PushTDSub(ConstantInt::get(IntptrTy, TDTag));
   PushTDSub(ConstantInt::get(IntptrTy, Members.size()));

>From 9cc4125b6c4495f66bee44cddec9051d45e99ce5 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Tue, 27 Oct 2026 01:07:55 +0000
Subject: [PATCH 09/12] Update the enum, and remove a false negative

---
 compiler-rt/lib/tysan/tysan.cpp | 29 +++++++++++++++--------------
 compiler-rt/lib/tysan/tysan.h   | 13 ++++++++-----
 2 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index db719c3b7457ab..1e17c22b56c742 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -87,11 +87,11 @@ static void printTDName(tysan_type_descriptor *td) {
     return;
   }
 
-  switch (td->Tag) {
+  switch (td->Kind) {
   default:
     CHECK(false && "invalid enum value");
     break;
-  case TYSAN_MEMBER_TD:
+  case TysanTDKind::MEMBER:
     printTDName(td->Member.Access);
     if (td->Member.Access != td->Member.Base) {
       Printf(" (in ");
@@ -99,10 +99,12 @@ static void printTDName(tysan_type_descriptor *td) {
       Printf(" at offset %zu)", td->Member.Offset);
     }
     break;
-  case TYSAN_STRUCT_TD:
+  case TysanTDKind::STRUCT:
     Printf("%s", getDisplayName(
                      (char *)(td->Struct.Members + td->Struct.MemberCount)));
     break;
+  case TysanTDKind::CONSERVATIVE_ALIAS:
+    Printf("<Unsupported Type>");
   }
 }
 
@@ -112,14 +114,14 @@ static tysan_type_descriptor *getRootTD(tysan_type_descriptor *TD) {
   do {
     RootTD = TD;
 
-    if (TD->Tag == TYSAN_STRUCT_TD) {
+    if (TD->Kind == TysanTDKind::STRUCT) {
       if (TD->Struct.MemberCount > 0)
         TD = TD->Struct.Members[0].Type;
       else
         TD = nullptr;
-    } else if (TD->Tag == TYSAN_MEMBER_TD) {
+    } else if (TD->Kind == TysanTDKind::MEMBER) {
       TD = TD->Member.Access;
-    } else if (TD->Tag == TYSAN_CONSERVATIVE_ALIAS_TD) {
+    } else if (TD->Kind == TysanTDKind::CONSERVATIVE_ALIAS) {
       return RootTD;
     } else {
       CHECK(false && "invalid enum value");
@@ -135,10 +137,10 @@ static bool walkAliasTree(tysan_type_descriptor *TDA,
                           tysan_type_descriptor *TDB, uptr OffsetA,
                           uptr OffsetB) {
   do {
-    if (TDA == TDB || TDA->Tag == TYSAN_CONSERVATIVE_ALIAS_TD)
+    if (TDA == TDB || TDA->Kind == TysanTDKind::CONSERVATIVE_ALIAS)
       return OffsetA == OffsetB;
 
-    if (TDA->Tag == TYSAN_STRUCT_TD) {
+    if (TDA->Kind == TysanTDKind::STRUCT) {
       // Reached root type descriptor.
       if (!TDA->Struct.MemberCount)
         break;
@@ -175,12 +177,12 @@ static bool walkAliasTree(tysan_type_descriptor *TDA,
 static bool isAliasingLegalUp(tysan_type_descriptor *TDA,
                               tysan_type_descriptor *TDB) {
   uptr OffsetA = 0, OffsetB = 0;
-  if (TDB->Tag == TYSAN_MEMBER_TD) {
+  if (TDB->Kind == TysanTDKind::MEMBER) {
     OffsetB = TDB->Member.Offset;
     TDB = TDB->Member.Base;
   }
 
-  if (TDA->Tag == TYSAN_MEMBER_TD) {
+  if (TDA->Kind == TysanTDKind::MEMBER) {
     OffsetA = TDA->Member.Offset;
     TDA = TDA->Member.Base;
   }
@@ -196,11 +198,11 @@ static bool isAliasingLegalWithOffset(tysan_type_descriptor *TDA,
     return false;
 
   // You can't have an offset into a member.
-  if (TDB->Tag == TYSAN_MEMBER_TD)
+  if (TDB->Kind == TysanTDKind::MEMBER)
     return false;
 
   uptr OffsetA = 0;
-  if (TDA->Tag == TYSAN_MEMBER_TD) {
+  if (TDA->Kind == TysanTDKind::MEMBER) {
     OffsetA = TDA->Member.Offset;
     TDA = TDA->Member.Base;
   }
@@ -215,8 +217,7 @@ static bool isAliasingLegalWithOffset(tysan_type_descriptor *TDA,
 
 static bool isAliasingLegal(tysan_type_descriptor *TDA,
                             tysan_type_descriptor *TDB, uptr OffsetB = 0) {
-  if (TDA == TDB || !TDB || !TDA || TDA->Tag == TYSAN_CONSERVATIVE_ALIAS_TD ||
-      TDB->Tag == TYSAN_CONSERVATIVE_ALIAS_TD)
+  if (TDA == TDB || !TDB || !TDA)
     return true;
 
   // Aliasing is legal is the two types have different root nodes.
diff --git a/compiler-rt/lib/tysan/tysan.h b/compiler-rt/lib/tysan/tysan.h
index f279ad795a2ffb..26c6277fb5ae31 100644
--- a/compiler-rt/lib/tysan/tysan.h
+++ b/compiler-rt/lib/tysan/tysan.h
@@ -33,10 +33,13 @@ extern bool tysan_init_is_running;
 
 void InitializeInterceptors();
 
-enum {
-  TYSAN_MEMBER_TD = 1,
-  TYSAN_STRUCT_TD = 2,
-  TYSAN_CONSERVATIVE_ALIAS_TD = 3
+enum class TysanTDKind : char{
+  // TD that represents an access of a member in a struct
+  MEMBER = 1,
+  // TD that represents a struct containing values
+  STRUCT = 2,
+  // Special TD that tells the runtime to ignore this type
+  CONSERVATIVE_ALIAS = 3
 };
 
 struct tysan_member_type_descriptor {
@@ -54,7 +57,7 @@ struct tysan_struct_type_descriptor {
 };
 
 struct tysan_type_descriptor {
-  uptr Tag;
+  TysanTDKind Kind;
   union {
     tysan_member_type_descriptor Member;
     tysan_struct_type_descriptor Struct;

>From be2348cec0aa0d5add2cf1b9bfefd1894ff1181d Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Tue, 27 Oct 2026 01:12:27 +0000
Subject: [PATCH 10/12] fix the formatting

---
 compiler-rt/lib/tysan/tysan.h                         | 2 +-
 llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp | 7 -------
 2 files changed, 1 insertion(+), 8 deletions(-)

diff --git a/compiler-rt/lib/tysan/tysan.h b/compiler-rt/lib/tysan/tysan.h
index 26c6277fb5ae31..c07740a1d4e88a 100644
--- a/compiler-rt/lib/tysan/tysan.h
+++ b/compiler-rt/lib/tysan/tysan.h
@@ -33,7 +33,7 @@ extern bool tysan_init_is_running;
 
 void InitializeInterceptors();
 
-enum class TysanTDKind : char{
+enum class TysanTDKind : char {
   // TD that represents an access of a member in a struct
   MEMBER = 1,
   // TD that represents a struct containing values
diff --git a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
index 1b97ce994714bf..4bdf1b54c57f2f 100644
--- a/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp
@@ -118,17 +118,10 @@ struct TypeSanitizer {
 };
 } // namespace
 
-<<<<<<< HEAD
 TypeSanitizer::TypeSanitizer(const InstrumentationOptions &Opts, Module &M)
     : Opts(Opts), OutlineInstrumentation(Opts.tysan_outline_instrumentation),
       TargetTriple(M.getTargetTriple()),
-      AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N"),
-      ConservativeTBAADescriptor(nullptr) {
-=======
-TypeSanitizer::TypeSanitizer(Module &M)
-    : TargetTriple(M.getTargetTriple()),
       AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N") {
->>>>>>> 8678792148af (Replace global sentinal with new TD tag)
   const DataLayout &DL = M.getDataLayout();
   IntptrTy = DL.getIntPtrType(M.getContext());
   PtrShift = countr_zero(IntptrTy->getPrimitiveSizeInBits() / 8);

>From 05ad09d65e642d4055a520afa5cc33ff8a6cf773 Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Tue, 24 Nov 2026 16:55:06 +0000
Subject: [PATCH 11/12] enum class causes errors for default value checking

---
 compiler-rt/lib/tysan/tysan.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/lib/tysan/tysan.h b/compiler-rt/lib/tysan/tysan.h
index c07740a1d4e88a..3354560c1f7a99 100644
--- a/compiler-rt/lib/tysan/tysan.h
+++ b/compiler-rt/lib/tysan/tysan.h
@@ -33,7 +33,7 @@ extern bool tysan_init_is_running;
 
 void InitializeInterceptors();
 
-enum class TysanTDKind : char {
+enum TysanTDKind : char {
   // TD that represents an access of a member in a struct
   MEMBER = 1,
   // TD that represents a struct containing values

>From 45add56408ed0d8a78bd3ff879e64ec7065f2e9e Mon Sep 17 00:00:00 2001
From: gbMattN <matthew.nagy at sony.com>
Date: Wed, 25 Nov 2026 10:56:10 +0000
Subject: [PATCH 12/12] enum tomfoolery

---
 compiler-rt/lib/tysan/tysan.cpp | 9 ++++-----
 compiler-rt/lib/tysan/tysan.h   | 2 +-
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/compiler-rt/lib/tysan/tysan.cpp b/compiler-rt/lib/tysan/tysan.cpp
index 1e17c22b56c742..ba5659b5e62b24 100644
--- a/compiler-rt/lib/tysan/tysan.cpp
+++ b/compiler-rt/lib/tysan/tysan.cpp
@@ -88,9 +88,6 @@ static void printTDName(tysan_type_descriptor *td) {
   }
 
   switch (td->Kind) {
-  default:
-    CHECK(false && "invalid enum value");
-    break;
   case TysanTDKind::MEMBER:
     printTDName(td->Member.Access);
     if (td->Member.Access != td->Member.Base) {
@@ -98,14 +95,16 @@ static void printTDName(tysan_type_descriptor *td) {
       printTDName(td->Member.Base);
       Printf(" at offset %zu)", td->Member.Offset);
     }
-    break;
+    return;
   case TysanTDKind::STRUCT:
     Printf("%s", getDisplayName(
                      (char *)(td->Struct.Members + td->Struct.MemberCount)));
-    break;
+    return;
   case TysanTDKind::CONSERVATIVE_ALIAS:
     Printf("<Unsupported Type>");
+    return;
   }
+  CHECK(false && "invalid enum value");
 }
 
 static tysan_type_descriptor *getRootTD(tysan_type_descriptor *TD) {
diff --git a/compiler-rt/lib/tysan/tysan.h b/compiler-rt/lib/tysan/tysan.h
index 3354560c1f7a99..c07740a1d4e88a 100644
--- a/compiler-rt/lib/tysan/tysan.h
+++ b/compiler-rt/lib/tysan/tysan.h
@@ -33,7 +33,7 @@ extern bool tysan_init_is_running;
 
 void InitializeInterceptors();
 
-enum TysanTDKind : char {
+enum class TysanTDKind : char {
   // TD that represents an access of a member in a struct
   MEMBER = 1,
   // TD that represents a struct containing values



More information about the cfe-commits mailing list