Skip to content

Commit c7cacb2

Browse files
committedAug 3, 2023
Fix __cfi_check not aligned to 4k on relocatable files with no executable code
CrossDSOCFIPass is supposed to replace this stub function to a properly aligned function. However the pass is not ran if the file has no executable code, thus producing incorrectly aligned __cfi_check. Fixes #45638. Differential Revision: https://reviews.llvm.org/D155736
1 parent f2e7285 commit c7cacb2

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed
 

‎clang/lib/CodeGen/CGExpr.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -3445,14 +3445,12 @@ void CodeGenFunction::EmitCfiCheckStub() {
34453445
llvm::Function *F = llvm::Function::Create(
34463446
llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, false),
34473447
llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M);
3448+
F->setAlignment(llvm::Align(4096));
34483449
CGM.setDSOLocal(F);
34493450
llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F);
3450-
// FIXME: consider emitting an intrinsic call like
3451-
// call void @llvm.cfi_check(i64 %0, i8* %1, i8* %2)
3452-
// which can be lowered in CrossDSOCFI pass to the actual contents of
3453-
// __cfi_check. This would allow inlining of __cfi_check calls.
3454-
llvm::CallInst::Create(
3455-
llvm::Intrinsic::getDeclaration(M, llvm::Intrinsic::trap), "", BB);
3451+
// CrossDSOCFI pass is not executed if there is no executable code.
3452+
SmallVector<llvm::Value*> Args{F->getArg(2), F->getArg(1)};
3453+
llvm::CallInst::Create(M->getFunction("__cfi_check_fail"), Args, "", BB);
34563454
llvm::ReturnInst::Create(Ctx, nullptr, BB);
34573455
}
34583456

@@ -3546,9 +3544,6 @@ void CodeGenFunction::EmitCfiCheckFail() {
35463544
}
35473545

35483546
FinishFunction();
3549-
// The only reference to this function will be created during LTO link.
3550-
// Make sure it survives until then.
3551-
CGM.addUsedGlobal(F);
35523547
}
35533548

35543549
void CodeGenFunction::EmitUnreachable(SourceLocation Loc) {
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux -O0 -fsanitize-cfi-cross-dso \
2+
// RUN: -emit-llvm -o - %s | FileCheck %s
3+
4+
int a;
5+
6+
// CHECK: define weak void @__cfi_check(i64 %[[TYPE:.*]], ptr %[[ADDR:.*]], ptr %[[DATA:.*]]) align 4096
7+
// CHECK-NEXT: entry:
8+
// CHECK-NEXT: call void @__cfi_check_fail(ptr %[[DATA]], ptr %[[ADDR]])

0 commit comments

Comments
 (0)
Please sign in to comment.