Issue 238191
Summary [X86][CET] -fcf-protection=branch does not put endbr64 at WinEH catch/cleanup funclet entries
Labels
Assignees
Reporter zjy-dev
    On `x86_64-pc-windows-msvc`, the C++ EH runtime calls catch and cleanup funclets indirectly, through the
`# Handler` entries in the EH tables. X86IndirectBranchTracking puts ENDBR on the parent function and on the
catchret target, but not on the funclet entry. AArch64 does handle this case: AArch64BranchTargets emits `bti c`
for the same funclet.

```cpp
extern "C" int ext_may_throw(int);
int walk(int x) {
 try {
    return ext_may_throw(x);
  } catch (int v) {
    return v + 1;
  }
}
```

Compile flag: `clang++ --target=x86_64-pc-windows-msvc -O2 -fcf-protection=branch -fexceptions -fcxx-exceptions -S`

```
"?walk@@YAHH@Z":
	endbr64                                  # parent entry: OK
.LBB0_1:                                # Block address taken
	endbr64                                  # catchret target: OK
"?catch$2@?0??walk@@YAHH@Z@4HA":             # catch funclet, called by __CxxFrameHandler3
.LBB0_2:
	movq	%rdx, 16(%rsp)                   # <-- no endbr64
	pushq	%rbp
	.long	"?catch$2@?0??walk@@YAHH@Z@4HA"@IMGREL # Handler

# aarch64-pc-windows-msvc -mbranch-protection=bti, same source:
"?catch$2@?0??walk@@YAHH@Z@4HA":
.LBB0_2:
	hint	#34 # bti c
```

`llvm/lib/Target/X86/X86IndirectBranchTracking.cpp:174-181`: for non-SjLj EH, an EH pad only gets ENDBR right
after an `EH_LABEL` in that block. Funclet pads do not have one, because `SelectionDAGISel::PrepareEHLandingPad`
returns early for funclet personalities before it emits the `EH_LABEL`. The pass therefore adds nothing to them.
`AArch64BranchTargets.cpp:139-144` handles this explicitly with `isEHFuncletEntry() || isCleanupFuncletEntry()`.
With the module flag `cf-protection-branch` set, an IBT-enforcing runtime would raise #CP when it dispatches an exception
to this funclet. Reproduces with clang trunk (Compiler Explorer, 2026-11-01).

**Suggested fix:** in the non-SjLj branch, if `MBB.isEHFuncletEntry() || MBB.isCleanupFuncletEntry()`, call
`addENDBR(MBB, MBB.begin())`. Add a windows-msvc RUN line to `indirect-branch-tracking-eh.ll`.

Environment: clang 23.1.2 (llvmorg-23.1.2), target x86_64-pc-windows-msvc.

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

Reply via email to