[clang] [UBSan][Ignorelist] Expanding =sanitize to fun. (PR #142074)

Qinkun Bao via cfe-commits cfe-commits at lists.llvm.org
Fri May 30 02:28:18 PDT 2025


https://github.com/qinkunbao updated https://github.com/llvm/llvm-project/pull/142074

>From e12e5dfc241bcc9eb28f616e96cc9eebba918b7c Mon Sep 17 00:00:00 2001
From: Qinkun Bao <qinkun at google.com>
Date: Fri, 30 May 2025 01:21:37 +0000
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?=
 =?UTF-8?q?anges=20to=20main=20this=20commit=20is=20based=20on?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.6

[skip ci]
---
 clang/include/clang/Basic/NoSanitizeList.h |  2 ++
 clang/lib/Basic/NoSanitizeList.cpp         | 27 ++++++++++++++--------
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/Basic/NoSanitizeList.h b/clang/include/clang/Basic/NoSanitizeList.h
index 43415859fcd54..266dfc0d217cb 100644
--- a/clang/include/clang/Basic/NoSanitizeList.h
+++ b/clang/include/clang/Basic/NoSanitizeList.h
@@ -29,6 +29,8 @@ class SanitizerSpecialCaseList;
 class NoSanitizeList {
   std::unique_ptr<SanitizerSpecialCaseList> SSCL;
   SourceManager &SM;
+  bool containsPrefix(SanitizerMask Mask, StringRef Prefix, StringRef Name,
+                      StringRef Category = StringRef()) const;
 
 public:
   NoSanitizeList(const std::vector<std::string> &NoSanitizeListPaths,
diff --git a/clang/lib/Basic/NoSanitizeList.cpp b/clang/lib/Basic/NoSanitizeList.cpp
index 9f0f1c64995cb..a1ca31ea0e970 100644
--- a/clang/lib/Basic/NoSanitizeList.cpp
+++ b/clang/lib/Basic/NoSanitizeList.cpp
@@ -27,6 +27,21 @@ NoSanitizeList::NoSanitizeList(const std::vector<std::string> &NoSanitizePaths,
 
 NoSanitizeList::~NoSanitizeList() = default;
 
+bool NoSanitizeList::containsPrefix(SanitizerMask Mask, StringRef Prefix,
+                                    StringRef Name, StringRef Category) const {
+  std::pair<unsigned, unsigned> NoSan =
+      SSCL->inSectionBlame(Mask, Prefix, Name, Category);
+  if (NoSan == llvm::SpecialCaseList::NotFound)
+    return false;
+  std::pair<unsigned, unsigned> San =
+      SSCL->inSectionBlame(Mask, Prefix, Name, "sanitize");
+  // The statement evaluates to true under the following conditions:
+  // 1. The string "prefix:*=sanitize" is absent.
+  // 2. If "prefix:*=sanitize" is present, its (File Index, Line Number) is less
+  // than that of "prefix:*".
+  return San == llvm::SpecialCaseList::NotFound || NoSan > San;
+}
+
 bool NoSanitizeList::containsGlobal(SanitizerMask Mask, StringRef GlobalName,
                                     StringRef Category) const {
   return SSCL->inSection(Mask, "global", GlobalName, Category);
@@ -34,11 +49,7 @@ bool NoSanitizeList::containsGlobal(SanitizerMask Mask, StringRef GlobalName,
 
 bool NoSanitizeList::containsType(SanitizerMask Mask, StringRef MangledTypeName,
                                   StringRef Category) const {
-  auto NoSan = SSCL->inSectionBlame(Mask, "type", MangledTypeName, Category);
-  if (NoSan == llvm::SpecialCaseList::NotFound)
-    return false;
-  auto San = SSCL->inSectionBlame(Mask, "type", MangledTypeName, "sanitize");
-  return San == llvm::SpecialCaseList::NotFound || NoSan > San;
+  return containsPrefix(Mask, "type", MangledTypeName, Category);
 }
 
 bool NoSanitizeList::containsFunction(SanitizerMask Mask,
@@ -48,11 +59,7 @@ bool NoSanitizeList::containsFunction(SanitizerMask Mask,
 
 bool NoSanitizeList::containsFile(SanitizerMask Mask, StringRef FileName,
                                   StringRef Category) const {
-  auto NoSan = SSCL->inSectionBlame(Mask, "src", FileName, Category);
-  if (NoSan == llvm::SpecialCaseList::NotFound)
-    return false;
-  auto San = SSCL->inSectionBlame(Mask, "src", FileName, "sanitize");
-  return San == llvm::SpecialCaseList::NotFound || NoSan > San;
+  return containsPrefix(Mask, "src", FileName, Category);
 }
 
 bool NoSanitizeList::containsMainFile(SanitizerMask Mask, StringRef FileName,

>From bfd9272989f83e9c3eca47c03919335830f1995d Mon Sep 17 00:00:00 2001
From: Qinkun Bao <qinkun at google.com>
Date: Thu, 29 May 2025 21:23:21 -0400
Subject: [PATCH 2/3] Update clang/test/CodeGen/ubsan-function-ignorelist.test

Co-authored-by: Copilot <175728472+Copilot at users.noreply.github.com>
---
 clang/test/CodeGen/ubsan-function-ignorelist.test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CodeGen/ubsan-function-ignorelist.test b/clang/test/CodeGen/ubsan-function-ignorelist.test
index ce8d37253e2fc..0107e761ba029 100644
--- a/clang/test/CodeGen/ubsan-function-ignorelist.test
+++ b/clang/test/CodeGen/ubsan-function-ignorelist.test
@@ -14,7 +14,7 @@
 
 // The same type can appear multiple times within an ignorelist. Any ``=sanitize`` type
 // entries enable sanitizer instrumentation, even if it was ignored by entries before.
-// If multiple entries match the source, than the latest entry takes the
+// If multiple entries match the source, then the latest entry takes the
 // precedence.
 
 

>From 61f995364bdd2c353801e8570aa3dd90afc17c71 Mon Sep 17 00:00:00 2001
From: Qinkun Bao <qinkun at google.com>
Date: Fri, 30 May 2025 02:20:00 +0000
Subject: [PATCH 3/3] Fix the test.

Created using spr 1.3.6
---
 clang/test/CodeGen/ubsan-function-ignorelist.test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CodeGen/ubsan-function-ignorelist.test b/clang/test/CodeGen/ubsan-function-ignorelist.test
index 0107e761ba029..311b2ffd7f737 100644
--- a/clang/test/CodeGen/ubsan-function-ignorelist.test
+++ b/clang/test/CodeGen/ubsan-function-ignorelist.test
@@ -74,7 +74,7 @@ fun:*dd=sanitize
 
 
 //--- test.c
-// CHECK-LABEL: define dso_local void @test
+// CHECK-LABEL: define dso_local void @add
 void add(int A) {
 // IGNORE: %inc = add nsw
 // SANITIZE: @llvm.sadd.with.overflow.i32



More information about the cfe-commits mailing list