[clang] aec3ce0 - [compiler-rt][UBSan] Add __ubsan_default_suppressions() hook (#194862)

via cfe-commits cfe-commits at lists.llvm.org
Sun May 3 11:26:46 PDT 2026


Author: Kleis Auke Wolthuizen
Date: 2026-05-03T11:26:40-07:00
New Revision: aec3ce0ac5bdfa5db75db07e91220a1c374022e7

URL: https://github.com/llvm/llvm-project/commit/aec3ce0ac5bdfa5db75db07e91220a1c374022e7
DIFF: https://github.com/llvm/llvm-project/commit/aec3ce0ac5bdfa5db75db07e91220a1c374022e7.diff

LOG: [compiler-rt][UBSan] Add __ubsan_default_suppressions() hook (#194862)

In line with commit 5c62af5 and 83566da.

Assisted-by: Gemini

---------

Co-authored-by: Vitaly Buka <vitalybuka at google.com>

Added: 
    compiler-rt/test/ubsan/TestCases/Misc/Posix/ubsan_suppressions.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/docs/UndefinedBehaviorSanitizer.rst
    compiler-rt/include/sanitizer/ubsan_interface.h
    compiler-rt/lib/ubsan/ubsan_diag.cpp
    compiler-rt/lib/ubsan/ubsan_flags.h
    compiler-rt/lib/ubsan/ubsan_interface.inc
    compiler-rt/lib/ubsan/weak_symbols.txt
    llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn
    llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2c1d0d22dfa10..7027a3d836cd6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -749,6 +749,7 @@ Static Analyzer
 
 Sanitizers
 ----------
+- UndefinedBehaviorSanitizer now supports ``__ubsan_default_suppressions``.
 
 Python Binding Changes
 ----------------------

diff  --git a/clang/docs/UndefinedBehaviorSanitizer.rst b/clang/docs/UndefinedBehaviorSanitizer.rst
index bb0dc00609688..3ff1a77e33a6c 100644
--- a/clang/docs/UndefinedBehaviorSanitizer.rst
+++ b/clang/docs/UndefinedBehaviorSanitizer.rst
@@ -448,6 +448,16 @@ suppression file in a ``UBSAN_OPTIONS`` environment variable.
 
     UBSAN_OPTIONS=suppressions=MyUBSan.supp
 
+Alternatively, you can provide default suppressions by defining a
+``__ubsan_default_suppressions`` function:
+
+.. code-block:: c++
+
+    extern "C" const char *__ubsan_default_suppressions() {
+      return "signed-integer-overflow:file-with-known-overflow.cpp\n"
+             "alignment:function_doing_unaligned_access\n";
+    }
+
 You need to specify a :ref:`check <ubsan-checks>` you are suppressing and the
 bug location. For example:
 

diff  --git a/compiler-rt/include/sanitizer/ubsan_interface.h b/compiler-rt/include/sanitizer/ubsan_interface.h
index b53d6013c71cd..a6cad89bfcedc 100644
--- a/compiler-rt/include/sanitizer/ubsan_interface.h
+++ b/compiler-rt/include/sanitizer/ubsan_interface.h
@@ -27,6 +27,14 @@ extern "C" {
 /// \returns Default options string.
 const char *SANITIZER_CDECL __ubsan_default_options(void);
 
+/// User-provided default suppressions.
+///
+/// You can provide your own implementation of this function to return a string
+/// containing UBSan suppressions.
+///
+/// \returns Default suppressions string.
+const char *SANITIZER_CDECL __ubsan_default_suppressions(void);
+
 /// Set up an interval timer and install a SIGPROF signal handler that crashes
 /// the program if it is stuck in a trap loop generated by -fsanitize-trap-loop.
 ///

diff  --git a/compiler-rt/lib/ubsan/ubsan_diag.cpp b/compiler-rt/lib/ubsan/ubsan_diag.cpp
index d883fe7d8f681..6efd082a89934 100644
--- a/compiler-rt/lib/ubsan/ubsan_diag.cpp
+++ b/compiler-rt/lib/ubsan/ubsan_diag.cpp
@@ -17,6 +17,7 @@
 #include "ubsan_init.h"
 #include "ubsan_monitor.h"
 
+#include "sanitizer_common/sanitizer_internal_defs.h"
 #include "sanitizer_common/sanitizer_placement_new.h"
 #include "sanitizer_common/sanitizer_report_decorator.h"
 #include "sanitizer_common/sanitizer_stacktrace.h"
@@ -28,6 +29,11 @@
 
 using namespace __ubsan;
 
+// Can be overriden in frontend.
+SANITIZER_INTERFACE_WEAK_DEF(const char *, __ubsan_default_suppressions, void) {
+  return "";
+}
+
 // UBSan is combined with runtimes that already provide this functionality
 // (e.g., ASan) as well as runtimes that lack it (e.g., scudo). Tried to use
 // weak linkage to resolve this issue which is not portable and breaks on
@@ -419,6 +425,7 @@ void __ubsan::InitializeSuppressions() {
   suppression_ctx = new (suppression_placeholder)
       SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
   suppression_ctx->ParseFromFile(flags()->suppressions);
+  suppression_ctx->Parse(__ubsan_default_suppressions());
 }
 
 bool __ubsan::IsVptrCheckSuppressed(const char *TypeName) {

diff  --git a/compiler-rt/lib/ubsan/ubsan_flags.h b/compiler-rt/lib/ubsan/ubsan_flags.h
index c47009bafe539..1e4a6dc7bd65a 100644
--- a/compiler-rt/lib/ubsan/ubsan_flags.h
+++ b/compiler-rt/lib/ubsan/ubsan_flags.h
@@ -41,6 +41,10 @@ extern "C" {
 // override the default flag values.
 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
 const char *__ubsan_default_options();
+// Users may provide their own implementation of __ubsan_default_suppressions to
+// override the default suppression values.
+SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE const char *
+__ubsan_default_suppressions();
 }  // extern "C"
 
 #endif  // UBSAN_FLAGS_H

diff  --git a/compiler-rt/lib/ubsan/ubsan_interface.inc b/compiler-rt/lib/ubsan/ubsan_interface.inc
index 0eb109f37d445..7a9cb9b336f6e 100644
--- a/compiler-rt/lib/ubsan/ubsan_interface.inc
+++ b/compiler-rt/lib/ubsan/ubsan_interface.inc
@@ -59,5 +59,6 @@ INTERFACE_FUNCTION(__ubsan_handle_type_mismatch_v1_abort)
 INTERFACE_FUNCTION(__ubsan_handle_vla_bound_not_positive)
 INTERFACE_FUNCTION(__ubsan_handle_vla_bound_not_positive_abort)
 INTERFACE_WEAK_FUNCTION(__ubsan_default_options)
+INTERFACE_WEAK_FUNCTION(__ubsan_default_suppressions)
 INTERFACE_FUNCTION(__ubsan_on_report)
 INTERFACE_FUNCTION(__ubsan_get_current_report_data)

diff  --git a/compiler-rt/lib/ubsan/weak_symbols.txt b/compiler-rt/lib/ubsan/weak_symbols.txt
index 69e1bc1857ed6..587a6aee18dff 100644
--- a/compiler-rt/lib/ubsan/weak_symbols.txt
+++ b/compiler-rt/lib/ubsan/weak_symbols.txt
@@ -1 +1,2 @@
 ___ubsan_default_options
+___ubsan_default_suppressions

diff  --git a/compiler-rt/test/ubsan/TestCases/Misc/Posix/ubsan_suppressions.cpp b/compiler-rt/test/ubsan/TestCases/Misc/Posix/ubsan_suppressions.cpp
new file mode 100644
index 0000000000000..2f3c74736a561
--- /dev/null
+++ b/compiler-rt/test/ubsan/TestCases/Misc/Posix/ubsan_suppressions.cpp
@@ -0,0 +1,24 @@
+// Test that we use the suppressions from __ubsan_default_suppressions.
+// RUN: %clangxx -fsanitize=integer -fsanitize-recover=integer -g0 %s -o %t
+// RUN: %env_ubsan_opts=halt_on_error=1 %run %t
+
+// Suppression by symbol name requires the compiler-rt runtime to be able to
+// symbolize stack addresses.
+// REQUIRES: can-symbolize
+// UNSUPPORTED: android
+
+#include <sanitizer/ubsan_interface.h>
+#include <stdint.h>
+
+extern "C" const char *__ubsan_default_suppressions() {
+  return "unsigned-integer-overflow:do_overflow";
+}
+
+extern "C" void do_overflow() {
+  (void)(uint64_t(10000000000000000000ull) + uint64_t(9000000000000000000ull));
+}
+
+int main() {
+  do_overflow();
+  return 0;
+}

diff  --git a/llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn
index ab8ea1ffb56b0..290aa3f77a7d9 100644
--- a/llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn
@@ -109,6 +109,7 @@ if (current_toolchain == host_toolchain) {
 
         # ubsan
         "-Wl,-U,___ubsan_default_options",
+        "-Wl,-U,___ubsan_default_suppressions",
 
         # sanitizer_common
         "-Wl,-U,___sanitizer_free_hook",

diff  --git a/llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn
index 541b86ad0970e..c6a511c3c714c 100644
--- a/llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn
@@ -176,6 +176,7 @@ shared_library("ubsan_shared") {
 
       # ubsan
       "-Wl,-U,___ubsan_default_options",
+      "-Wl,-U,___ubsan_default_suppressions",
 
       # sanitizer_common
       "-Wl,-U,___sanitizer_free_hook",


        


More information about the cfe-commits mailing list