Skip to content

Commit 9850957

Browse files
author
Nikolai Kholiavin
committed
[cfi][CodeGen] Call SetLLVMFunctionAttributes{,ForDefinition} on __cfi_check
This causes __cfi_check, just as __cfi_check_fail, to get the proper target-specific attributes, in particular uwtable for unwind table generation. Previously, nounwind attribute could be inferred for __cfi_check, which caused it to lose its unwind table even with -funwind-table option.
1 parent caca8d3 commit 9850957

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3682,12 +3682,29 @@ void CodeGenFunction::EmitCfiSlowPathCheck(
36823682
// symbol in LTO mode.
36833683
void CodeGenFunction::EmitCfiCheckStub() {
36843684
llvm::Module *M = &CGM.getModule();
3685-
auto &Ctx = M->getContext();
3685+
ASTContext &C = getContext();
3686+
QualType QInt64Ty = C.getIntTypeForBitwidth(64, false);
3687+
3688+
FunctionArgList FnArgs;
3689+
ImplicitParamDecl ArgCallsiteTypeId(C, QInt64Ty, ImplicitParamKind::Other);
3690+
ImplicitParamDecl ArgAddr(C, C.VoidPtrTy, ImplicitParamKind::Other);
3691+
ImplicitParamDecl ArgCFICheckFailData(C, C.VoidPtrTy,
3692+
ImplicitParamKind::Other);
3693+
FnArgs.push_back(&ArgCallsiteTypeId);
3694+
FnArgs.push_back(&ArgAddr);
3695+
FnArgs.push_back(&ArgCFICheckFailData);
3696+
const CGFunctionInfo &FI =
3697+
CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, FnArgs);
3698+
36863699
llvm::Function *F = llvm::Function::Create(
3687-
llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, false),
3700+
llvm::FunctionType::get(VoidTy, {Int64Ty, VoidPtrTy, VoidPtrTy}, false),
36883701
llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M);
3702+
CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false);
3703+
CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F);
36893704
F->setAlignment(llvm::Align(4096));
36903705
CGM.setDSOLocal(F);
3706+
3707+
llvm::LLVMContext &Ctx = M->getContext();
36913708
llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F);
36923709
// CrossDSOCFI pass is not executed if there is no executable code.
36933710
SmallVector<llvm::Value*> Args{F->getArg(2), F->getArg(1)};

clang/test/CodeGen/cfi-check-attrs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %clang_cc1 -triple arm-unknown-linux -funwind-tables=1 -fsanitize-cfi-cross-dso -emit-llvm -o - %s | FileCheck %s
2+
3+
// CHECK: define weak {{.*}}void @__cfi_check({{.*}} [[ATTR:#[0-9]*]]
4+
5+
// CHECK: attributes [[ATTR]] = {{.*}} uwtable(sync)

clang/test/CodeGen/cfi-check-fail.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void caller(void (*f)(void)) {
7272
// CHECK: [[CONT5]]:
7373
// CHECK: ret void
7474

75-
// CHECK: define weak void @__cfi_check(i64 %[[TYPE:.*]], ptr %[[ADDR:.*]], ptr %[[DATA:.*]]) align 4096
75+
// CHECK: define weak void @__cfi_check(i64 noundef %[[TYPE:.*]], ptr noundef %[[ADDR:.*]], ptr noundef %[[DATA:.*]]){{.*}} align 4096
7676
// CHECK-NOT: }
7777
// CHECK: call void @__cfi_check_fail(ptr %[[DATA]], ptr %[[ADDR]])
7878
// CHECK-NEXT: ret void

0 commit comments

Comments
 (0)