Skip to content

[ARM64EC] Avoid emitting unnecessary symbol references with /guard:cf. #123235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions llvm/lib/CodeGen/AsmPrinter/WinCFGuard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,32 @@ static bool isPossibleIndirectCallTarget(const Function *F) {
const Value *FnOrCast = Users.pop_back_val();
for (const Use &U : FnOrCast->uses()) {
const User *FnUser = U.getUser();
if (isa<BlockAddress>(FnUser))
if (isa<BlockAddress>(FnUser)) {
// Block addresses are illegal to call.
continue;
}
if (const auto *Call = dyn_cast<CallBase>(FnUser)) {
if (!Call->isCallee(&U))
if ((!Call->isCallee(&U) || U.get() != F) &&
!Call->getFunction()->getName().ends_with("$exit_thunk")) {
// Passing a function pointer to a call may lead to an indirect
// call. As an exception, ignore ARM64EC exit thunks.
return true;
}
} else if (isa<Instruction>(FnUser)) {
// Consider any other instruction to be an escape. This has some weird
// consequences like no-op intrinsics being an escape or a store *to* a
// function address being an escape.
return true;
} else if (const auto *C = dyn_cast<Constant>(FnUser)) {
// If this is a constant pointer cast of the function, don't consider
// this escape. Analyze the uses of the cast as well. This ensures that
// direct calls with mismatched prototypes don't end up in the CFG
// table. Consider other constants, such as vtable initializers, to
// escape the function.
if (C->stripPointerCasts() == F)
Users.push_back(FnUser);
else
return true;
} else if (const auto *G = dyn_cast<GlobalValue>(FnUser)) {
// Ignore llvm.arm64ec.symbolmap; it doesn't lower to an actual address.
if (G->getName() == "llvm.arm64ec.symbolmap")
continue;
// Globals (for example, vtables) are escapes.
return true;
} else if (isa<Constant>(FnUser)) {
// Constants which aren't a global are intermediate values; recursively
// analyze the users to see if they actually escape.
Users.push_back(FnUser);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions llvm/test/CodeGen/AArch64/cfguard-arm64ec.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; RUN: llc < %s -mtriple=arm64ec-pc-windows-msvc | FileCheck %s

declare void @called()
declare void @escaped()
define void @f(ptr %dst) {
call void @called()
store ptr @escaped, ptr %dst
ret void
}

!llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 1}

; CHECK-LABEL: .section .gfids$y,"dr"
; CHECK-NEXT: .symidx escaped
; CHECK-NOT: .symidx
Loading