[clang] [ubsan] Allow -ubsan-unique-traps option for ubsan min-rt (PR #119302)
Thurston Dang via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 9 18:51:06 PST 2024
https://github.com/thurstond updated https://github.com/llvm/llvm-project/pull/119302
>From 0b45356af23201081e00ea278513520b8b0a7b49 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Tue, 10 Dec 2024 00:23:26 +0000
Subject: [PATCH 1/5] [ubsan] Allow -ubsan-unique-traps option for ubsan min-rt
UBSan handlers can sometimes be merged by the backend, which complicates
debugging when a backtrace is not available (traps or min-rt handlers).
-ubsan-unique-traps prevents merging for trap mode, but not
for min-rt's (non-trap) handlers. This patch extends -ubsan-unique-traps to work
for min-rt handlers.
It does not change the behavior when using the regular ubsan runtime;
the backtrace provides enough information for debugging, hence no-merge would
simply increase code size with little benefit.
---
clang/lib/CodeGen/CGExpr.cpp | 6 ++++++
clang/test/CodeGen/ubsan-trap-merge.c | 2 +-
clang/test/CodeGenCXX/catch-undef-behavior.cpp | 2 +-
3 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 0d16408aa4de9d..9a3f573fec7725 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -3581,6 +3581,12 @@ static void emitCheckHandlerCall(CodeGenFunction &CGF,
llvm::AttributeList::FunctionIndex, B),
/*Local=*/true);
llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs);
+ bool NoMerge = ClSanitizeDebugDeoptimization ||
+ !CGF.CGM.getCodeGenOpts().OptimizationLevel ||
+ (CGF.CurCodeDecl && CGF.CurCodeDecl->hasAttr<OptimizeNoneAttr>());
+ // Regular runtime provides a backtrace, making NoMerge a waste of space
+ if (NoMerge && MinimalRuntime)
+ HandlerCall->addFnAttr(llvm::Attribute::NoMerge);
if (!MayReturn) {
HandlerCall->setDoesNotReturn();
CGF.Builder.CreateUnreachable();
diff --git a/clang/test/CodeGen/ubsan-trap-merge.c b/clang/test/CodeGen/ubsan-trap-merge.c
index e48681ce09377d..5c0760a539ec6a 100644
--- a/clang/test/CodeGen/ubsan-trap-merge.c
+++ b/clang/test/CodeGen/ubsan-trap-merge.c
@@ -266,4 +266,4 @@ int m(int x, int y) {
}
// TRAP: attributes #[[ATTR4]] = { nomerge noreturn nounwind }
// HANDLER: attributes #[[ATTR4]] = { noreturn nounwind }
-// MINRT: attributes #[[ATTR4]] = { noreturn nounwind }
+// MINRT: attributes #[[ATTR4]] = { nomerge noreturn nounwind }
diff --git a/clang/test/CodeGenCXX/catch-undef-behavior.cpp b/clang/test/CodeGenCXX/catch-undef-behavior.cpp
index a985183129911c..419d1292551a08 100644
--- a/clang/test/CodeGenCXX/catch-undef-behavior.cpp
+++ b/clang/test/CodeGenCXX/catch-undef-behavior.cpp
@@ -734,6 +734,6 @@ void ThisAlign::this_align_lambda_2() {
p();
}
-// CHECK: attributes [[NR_NUW]] = { noreturn nounwind }
+// CHECK: attributes [[NR_NUW]] = { nomerge noreturn nounwind }
// CHECK-FUNCSAN: ![[FUNCSAN]] = !{i32 -1056584962, i32 -1000226989}
>From 1167d264a616cbabe42fc5953c50fea2af06d834 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Tue, 10 Dec 2024 00:36:04 +0000
Subject: [PATCH 2/5] Revert change for non-min-rt
---
clang/test/CodeGenCXX/catch-undef-behavior.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CodeGenCXX/catch-undef-behavior.cpp b/clang/test/CodeGenCXX/catch-undef-behavior.cpp
index 419d1292551a08..a985183129911c 100644
--- a/clang/test/CodeGenCXX/catch-undef-behavior.cpp
+++ b/clang/test/CodeGenCXX/catch-undef-behavior.cpp
@@ -734,6 +734,6 @@ void ThisAlign::this_align_lambda_2() {
p();
}
-// CHECK: attributes [[NR_NUW]] = { nomerge noreturn nounwind }
+// CHECK: attributes [[NR_NUW]] = { noreturn nounwind }
// CHECK-FUNCSAN: ![[FUNCSAN]] = !{i32 -1056584962, i32 -1000226989}
>From a78aac36d5196a78e6d073a2c7137df238d5d5c2 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Tue, 10 Dec 2024 01:28:37 +0000
Subject: [PATCH 3/5] clang-format
---
clang/lib/CodeGen/CGExpr.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 9a3f573fec7725..943621436d6921 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -3581,9 +3581,10 @@ static void emitCheckHandlerCall(CodeGenFunction &CGF,
llvm::AttributeList::FunctionIndex, B),
/*Local=*/true);
llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs);
- bool NoMerge = ClSanitizeDebugDeoptimization ||
- !CGF.CGM.getCodeGenOpts().OptimizationLevel ||
- (CGF.CurCodeDecl && CGF.CurCodeDecl->hasAttr<OptimizeNoneAttr>());
+ bool NoMerge =
+ ClSanitizeDebugDeoptimization ||
+ !CGF.CGM.getCodeGenOpts().OptimizationLevel ||
+ (CGF.CurCodeDecl && CGF.CurCodeDecl->hasAttr<OptimizeNoneAttr>());
// Regular runtime provides a backtrace, making NoMerge a waste of space
if (NoMerge && MinimalRuntime)
HandlerCall->addFnAttr(llvm::Attribute::NoMerge);
>From b5f5194fd8619172dfd75f8b3ade98b123845c58 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Tue, 10 Dec 2024 02:28:19 +0000
Subject: [PATCH 4/5] Revert "Revert change for non-min-rt"
This reverts commit 1167d264a616cbabe42fc5953c50fea2af06d834.
---
clang/test/CodeGenCXX/catch-undef-behavior.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CodeGenCXX/catch-undef-behavior.cpp b/clang/test/CodeGenCXX/catch-undef-behavior.cpp
index a985183129911c..419d1292551a08 100644
--- a/clang/test/CodeGenCXX/catch-undef-behavior.cpp
+++ b/clang/test/CodeGenCXX/catch-undef-behavior.cpp
@@ -734,6 +734,6 @@ void ThisAlign::this_align_lambda_2() {
p();
}
-// CHECK: attributes [[NR_NUW]] = { noreturn nounwind }
+// CHECK: attributes [[NR_NUW]] = { nomerge noreturn nounwind }
// CHECK-FUNCSAN: ![[FUNCSAN]] = !{i32 -1056584962, i32 -1000226989}
>From d6ae380cc3ed1c6b3276849bca967294277e1d64 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Tue, 10 Dec 2024 02:32:13 +0000
Subject: [PATCH 5/5] Apply to non-min-rt as well
---
clang/lib/CodeGen/CGExpr.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 943621436d6921..79955f55714164 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -3585,8 +3585,7 @@ static void emitCheckHandlerCall(CodeGenFunction &CGF,
ClSanitizeDebugDeoptimization ||
!CGF.CGM.getCodeGenOpts().OptimizationLevel ||
(CGF.CurCodeDecl && CGF.CurCodeDecl->hasAttr<OptimizeNoneAttr>());
- // Regular runtime provides a backtrace, making NoMerge a waste of space
- if (NoMerge && MinimalRuntime)
+ if (NoMerge)
HandlerCall->addFnAttr(llvm::Attribute::NoMerge);
if (!MayReturn) {
HandlerCall->setDoesNotReturn();
More information about the cfe-commits
mailing list