Skip to content

[X86] nocf_check: disable tail call #131487

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
Mar 16, 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
12 changes: 9 additions & 3 deletions llvm/lib/Target/X86/X86ISelLoweringCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2034,11 +2034,17 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
bool HasNCSR = (CB && isa<CallInst>(CB) &&
CB->hasFnAttr("no_caller_saved_registers"));
bool HasNoCfCheck = (CB && CB->doesNoCfCheck());
bool IsIndirectCall = (CB && isa<CallInst>(CB) && CB->isIndirectCall());
bool IsCFICall = IsIndirectCall && CLI.CFIType;
const Module *M = MF.getFunction().getParent();
Metadata *IsCFProtectionSupported = M->getModuleFlag("cf-protection-branch");

// If the indirect call target has the nocf_check attribute, the call needs
// the NOTRACK prefix. For simplicity just disable tail calls as there are
// so many variants.
bool IsNoTrackIndirectCall = IsIndirectCall && CB->doesNoCfCheck() &&
M->getModuleFlag("cf-protection-branch");
if (IsNoTrackIndirectCall)
isTailCall = false;

MachineFunction::CallSiteInfo CSInfo;
if (CallConv == CallingConv::X86_INTR)
Expand Down Expand Up @@ -2549,7 +2555,7 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,

// Returns a chain & a glue for retval copy to use.
SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
if (HasNoCfCheck && IsCFProtectionSupported && IsIndirectCall) {
if (IsNoTrackIndirectCall) {
Chain = DAG.getNode(X86ISD::NT_CALL, dl, NodeTys, Ops);
} else if (CLI.CB && objcarc::hasAttachedCallOpBundle(CLI.CB)) {
// Calls with a "clang.arc.attachedcall" bundle are special. They should be
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/CodeGen/X86/nocf_check.ll
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ entry:
ret void
}

;; NOTRACK tail call is not implemented, so nocf_check just disables tail call.
define void @NoCfCheckTail(ptr %p) #1 {
; CHECK-LABEL: NoCfCheckTail:
; CHECK: notrack callq *%rax
%f = load ptr, ptr %p, align 4
tail call void %f() #2
ret void
}

define void @NoCfCheckTailCond(ptr %f, i1 %x) #1 {
; CHECK-LABEL: NoCfCheckTailCond:
; CHECK: notrack callq *%rdi
; CHECK: notrack callq *%rdi
entry:
br i1 %x, label %bb1, label %bb2
bb1:
tail call void %f() #2
ret void
bb2:
tail call void %f() #2
ret void
}

attributes #0 = { nocf_check noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { nocf_check }
Expand Down
Loading