Issue 237861
Summary [X86][KCFI] Indirect invoke checks isa<CallInst> in LowerCall, dropping the KCFI type-hash check and nocf_check NOTRACK prefix
Labels new issue
Assignees
Reporter zjy-dev
    On x86_64, `X86TargetLowering::LowerCall` tests `isa<CallInst>(CB)` when computing `IsIndirectCall`.
Because `InvokeInst` derives from `CallBase` rather than `CallInst`, `IsIndirectCall` is false for every
indirect `invoke` (any indirect call inside a C++ `try` block or in a scope with active destructors).
As a result, indirect `invoke` instructions drop their `-fsanitize=kcfi` type-hash check and omit the
`notrack` prefix requested by `__attribute__((nocf_check))` under `-fcf-protection=branch`.

```cpp
extern void may_throw();
using fn_t = void (*)();
typedef void (*nocf_fn_t)() __attribute__((nocf_check));

void test_call(fn_t f, nocf_fn_t nf) {
  f();
  nf();
}

void test_invoke(fn_t f, nocf_fn_t nf) {
  try {
    f();
    nf();
 may_throw();
  } catch (...) {
  }
}
```

Compile flag: `clang++ --target=x86_64-unknown-linux-gnu -O2 -fsanitize=kcfi -fcf-protection=branch -fexceptions -S trigger.cpp -o -`

```gas
_Z9test_callPFvvES0_:
	movq	%rsi, %rbx
	cmpl	$12345678, -4(%rdi)      # KCFI type-hash check emitted
	jne	.LBB0_2
	callq	*%rdi
	movq	%rbx, %rax
	popq	%rbx
	notrack jmpq	*%rax            # notrack emitted for nocf_check

_Z11test_invokePFvvES0_:
.Ltmp0:
	callq	*%rdi # no KCFI check
.Ltmp1:
.Ltmp3:
	callq	*%rbx                    # no notrack prefix
.Ltmp4:
```

**Root cause.** `llvm/lib/Target/X86/X86ISelLoweringCall.cpp:2111-2120` writes:
```cpp
bool IsIndirectCall = (CB && isa<CallInst>(CB) && CB->isIndirectCall());
bool IsCFICall = IsIndirectCall && CLI.CFIType;
bool IsNoTrackIndirectCall = IsIndirectCall && CB->doesNoCfCheck() &&
 M->getModuleFlag("cf-protection-branch");
```
Even though the frontend attaches the `"kcfi"` operand bundle to the `invoke` and `SelectionDAGBuilder::lowerInvokable`
populates `CLI.CFIType`, `isa<CallInst>(CB)` rejects `InvokeInst`. By contrast, AArch64 uses
`bool IsCFICall = CB && CB->isIndirectCall();` without restricting `CB` to `CallInst`.

**Suggested fix.** Drop `isa<CallInst>(CB)` in `X86ISelLoweringCall.cpp:2111`:
`bool IsIndirectCall = (CB && CB->isIndirectCall());`.

Environment: clang 23.1.2 (`85ac560262434c9ccfc0c183ec22d4138ed647fb`), `x86_64-unknown-linux-gnu`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to