Skip to content

Commit 81ba006

Browse files
authored
[X86] nocf_check: disable tail call
When a function pointer is annotated with `void (*fptr)(void) __attribute__((nocf_check));`, calling it should use the NOTRACK prefix, as the callee may not contain an ENDBR. https://reviews.llvm.org/D41879 implemented NOTRACK variants for X86ISD::CALL and ISD::BRIND but not for TCRETURN. Given that there are so many tail call variants (e.g. conditional tailcall https://reviews.llvm.org/D29856), let's just disable tailcall. While nocf_check has some uses within the Linux kernel, it isn't a popular attribute. Fix #91228 Pull Request: #131487
1 parent 91328db commit 81ba006

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

llvm/lib/Target/X86/X86ISelLoweringCall.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -2034,11 +2034,17 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
20342034
X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
20352035
bool HasNCSR = (CB && isa<CallInst>(CB) &&
20362036
CB->hasFnAttr("no_caller_saved_registers"));
2037-
bool HasNoCfCheck = (CB && CB->doesNoCfCheck());
20382037
bool IsIndirectCall = (CB && isa<CallInst>(CB) && CB->isIndirectCall());
20392038
bool IsCFICall = IsIndirectCall && CLI.CFIType;
20402039
const Module *M = MF.getFunction().getParent();
2041-
Metadata *IsCFProtectionSupported = M->getModuleFlag("cf-protection-branch");
2040+
2041+
// If the indirect call target has the nocf_check attribute, the call needs
2042+
// the NOTRACK prefix. For simplicity just disable tail calls as there are
2043+
// so many variants.
2044+
bool IsNoTrackIndirectCall = IsIndirectCall && CB->doesNoCfCheck() &&
2045+
M->getModuleFlag("cf-protection-branch");
2046+
if (IsNoTrackIndirectCall)
2047+
isTailCall = false;
20422048

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

25502556
// Returns a chain & a glue for retval copy to use.
25512557
SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2552-
if (HasNoCfCheck && IsCFProtectionSupported && IsIndirectCall) {
2558+
if (IsNoTrackIndirectCall) {
25532559
Chain = DAG.getNode(X86ISD::NT_CALL, dl, NodeTys, Ops);
25542560
} else if (CLI.CB && objcarc::hasAttachedCallOpBundle(CLI.CB)) {
25552561
// Calls with a "clang.arc.attachedcall" bundle are special. They should be

llvm/test/CodeGen/X86/nocf_check.ll

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ entry:
4343
ret void
4444
}
4545

46+
;; NOTRACK tail call is not implemented, so nocf_check just disables tail call.
47+
define void @NoCfCheckTail(ptr %p) #1 {
48+
; CHECK-LABEL: NoCfCheckTail:
49+
; CHECK: notrack callq *%rax
50+
%f = load ptr, ptr %p, align 4
51+
tail call void %f() #2
52+
ret void
53+
}
54+
55+
define void @NoCfCheckTailCond(ptr %f, i1 %x) #1 {
56+
; CHECK-LABEL: NoCfCheckTailCond:
57+
; CHECK: notrack callq *%rdi
58+
; CHECK: notrack callq *%rdi
59+
entry:
60+
br i1 %x, label %bb1, label %bb2
61+
bb1:
62+
tail call void %f() #2
63+
ret void
64+
bb2:
65+
tail call void %f() #2
66+
ret void
67+
}
68+
4669
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" }
4770
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" }
4871
attributes #2 = { nocf_check }

0 commit comments

Comments
 (0)