[clang] c7cacb2 - Fix __cfi_check not aligned to 4k on relocatable files with no executable code

Yi Kong via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 3 03:54:11 PDT 2023


Author: Yi Kong
Date: 2023-08-03T19:53:54+09:00
New Revision: c7cacb2f6efe07fa4a1129bb7e2389312670b84a

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

LOG: 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 https://github.com/llvm/llvm-project/issues/45638.

Differential Revision: https://reviews.llvm.org/D155736

Added: 
    clang/test/CodeGen/cfi-cross-dso-align.c

Modified: 
    clang/lib/CodeGen/CGExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index ed6095f7cfeb0d..15aa1b730d268b 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -3445,14 +3445,12 @@ void CodeGenFunction::EmitCfiCheckStub() {
   llvm::Function *F = llvm::Function::Create(
       llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, false),
       llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M);
+  F->setAlignment(llvm::Align(4096));
   CGM.setDSOLocal(F);
   llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F);
-  // FIXME: consider emitting an intrinsic call like
-  // call void @llvm.cfi_check(i64 %0, i8* %1, i8* %2)
-  // which can be lowered in CrossDSOCFI pass to the actual contents of
-  // __cfi_check. This would allow inlining of __cfi_check calls.
-  llvm::CallInst::Create(
-      llvm::Intrinsic::getDeclaration(M, llvm::Intrinsic::trap), "", BB);
+  // CrossDSOCFI pass is not executed if there is no executable code.
+  SmallVector<llvm::Value*> Args{F->getArg(2), F->getArg(1)};
+  llvm::CallInst::Create(M->getFunction("__cfi_check_fail"), Args, "", BB);
   llvm::ReturnInst::Create(Ctx, nullptr, BB);
 }
 
@@ -3546,9 +3544,6 @@ void CodeGenFunction::EmitCfiCheckFail() {
   }
 
   FinishFunction();
-  // The only reference to this function will be created during LTO link.
-  // Make sure it survives until then.
-  CGM.addUsedGlobal(F);
 }
 
 void CodeGenFunction::EmitUnreachable(SourceLocation Loc) {

diff  --git a/clang/test/CodeGen/cfi-cross-dso-align.c b/clang/test/CodeGen/cfi-cross-dso-align.c
new file mode 100644
index 00000000000000..e023601184fe99
--- /dev/null
+++ b/clang/test/CodeGen/cfi-cross-dso-align.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -O0 -fsanitize-cfi-cross-dso \
+// RUN:     -emit-llvm -o - %s | FileCheck %s
+
+int a;
+
+// CHECK: define weak void @__cfi_check(i64 %[[TYPE:.*]], ptr %[[ADDR:.*]], ptr %[[DATA:.*]]) align 4096
+// CHECK-NEXT: entry:
+// CHECK-NEXT: call void @__cfi_check_fail(ptr %[[DATA]], ptr %[[ADDR]])


        


More information about the cfe-commits mailing list