[compiler-rt] [ubsan-minimal] Switch to weak symbols for callbacks to allow overriding in client code (PR #119242)

Kirill Stoimenov via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 13 15:00:26 PST 2024


https://github.com/kstoimenov updated https://github.com/llvm/llvm-project/pull/119242

>From 6043590c828bb6d19f5d0c0931c48dfc6d462972 Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 6 Dec 2024 22:05:59 +0000
Subject: [PATCH 1/5] [UBSAN-MINRT] Switch to weak symbols for callbacks to
 allow overriding in client code.

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

diff --git a/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp b/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp
index cd0b38618c5cb2..370c3c8ec9608a 100644
--- a/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp
+++ b/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp
@@ -1,5 +1,6 @@
 #include "sanitizer_common/sanitizer_atomic.h"
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>

>From 8885f823f00ecec7a45c20d8312627d1e4b5c57e Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 13 Dec 2024 22:01:25 +0000
Subject: [PATCH 2/5] Repatch.

---
 compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp b/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp
index 370c3c8ec9608a..98662c5881c9f9 100644
--- a/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp
+++ b/compiler-rt/lib/ubsan_minimal/ubsan_minimal_handlers.cpp
@@ -1,6 +1,5 @@
 #include "sanitizer_common/sanitizer_atomic.h"
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
@@ -49,7 +48,8 @@ static void format_msg(const char *kind, uintptr_t caller, char *buf,
   *buf = '\0';
 }
 
-static void report_error(const char *kind, uintptr_t caller) {
+SANITIZER_INTERFACE_WEAK_DEF(void, __ubsan_report_error, const char *kind,
+                             uintptr_t caller) {
   if (caller == 0)
     return;
   while (true) {
@@ -115,13 +115,13 @@ void NORETURN CheckFailed(const char *file, int, const char *cond, u64, u64) {
 
 #define HANDLER_RECOVER(name, kind)                                            \
   INTERFACE void __ubsan_handle_##name##_minimal() {                           \
-    report_error(kind, GET_CALLER_PC());                                       \
+    __ubsan_report_error(kind, GET_CALLER_PC());                               \
   }
 
 #define HANDLER_NORECOVER(name, kind)                                          \
   INTERFACE void __ubsan_handle_##name##_minimal_abort() {                     \
     uintptr_t caller = GET_CALLER_PC();                                        \
-    report_error(kind, caller);                                                \
+    __ubsan_report_error(kind, caller);                                        \
     abort_with_message(kind, caller);                                          \
   }
 

>From 189941c3a83d3259abe916a7790838960ed2606e Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 13 Dec 2024 22:03:00 +0000
Subject: [PATCH 3/5] Added a test.

---
 .../ubsan_minimal/TestCases/override-callback.c   | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 compiler-rt/test/ubsan_minimal/TestCases/override-callback.c

diff --git a/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c b/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c
new file mode 100644
index 00000000000000..52e02f2046c63e
--- /dev/null
+++ b/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c
@@ -0,0 +1,15 @@
+// RUN: %clang -fsanitize=implicit-integer-sign-change %s -o %t && %run %t 0 2>&1 | FileCheck %s
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+static int Result;
+
+void __ubsan_report_error(const char *msg, uintptr_t caller, int abort) {
+  fprintf(stderr, "CUSTOM_CALLBACK: %s %d\n", msg, abort);
+}
+
+int main(int argc, const char** argv) {
+  int32_t t0 = (~((uint32_t)0));
+// CHECK: CUSTOM_CALLBACK: implicit-conversion 0
+}

>From 67a76588066ae945c8a0710dea7c42bd3288d921 Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 13 Dec 2024 22:18:04 +0000
Subject: [PATCH 4/5] Fixed the test.

---
 .../test/ubsan_minimal/TestCases/override-callback.c        | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c b/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c
index 52e02f2046c63e..eaa864efa17f26 100644
--- a/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c
+++ b/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c
@@ -5,11 +5,11 @@
 
 static int Result;
 
-void __ubsan_report_error(const char *msg, uintptr_t caller, int abort) {
-  fprintf(stderr, "CUSTOM_CALLBACK: %s %d\n", msg, abort);
+void __ubsan_report_error(const char *msg, uintptr_t caller) {
+  fprintf(stderr, "CUSTOM_CALLBACK: %s\n", msg);
 }
 
 int main(int argc, const char** argv) {
   int32_t t0 = (~((uint32_t)0));
-// CHECK: CUSTOM_CALLBACK: implicit-conversion 0
+// CHECK: CUSTOM_CALLBACK: implicit-conversion
 }

>From ce65b1c140985f25885ddca3d52cb422d50c9808 Mon Sep 17 00:00:00 2001
From: Kirill Stoimenov <kstoimenov at google.com>
Date: Fri, 13 Dec 2024 22:59:58 +0000
Subject: [PATCH 5/5] clang-format

---
 .../test/ubsan_minimal/TestCases/override-callback.c        | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c b/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c
index eaa864efa17f26..db3ef757e9391a 100644
--- a/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c
+++ b/compiler-rt/test/ubsan_minimal/TestCases/override-callback.c
@@ -1,6 +1,6 @@
 // RUN: %clang -fsanitize=implicit-integer-sign-change %s -o %t && %run %t 0 2>&1 | FileCheck %s
-#include <stdio.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 
 static int Result;
@@ -9,7 +9,7 @@ void __ubsan_report_error(const char *msg, uintptr_t caller) {
   fprintf(stderr, "CUSTOM_CALLBACK: %s\n", msg);
 }
 
-int main(int argc, const char** argv) {
+int main(int argc, const char **argv) {
   int32_t t0 = (~((uint32_t)0));
-// CHECK: CUSTOM_CALLBACK: implicit-conversion
+  // CHECK: CUSTOM_CALLBACK: implicit-conversion
 }



More information about the llvm-commits mailing list