Issue 237908
Summary [X86][LVI] X86LoadValueInjectionRetHardening skips XRay PATCHABLE_RET and emits an unhardened retq
Labels new issue
Assignees
Reporter zjy-dev
    When compiling with both `-mlvi-cfi` and `-fxray-instrument` (or `__attribute__((xray_always_instrument))`),
`X86LoadValueInjectionRetHardeningPass` skips XRay-instrumented returns and leaves a bare `retq` in the XRay exit
sled instead of the LVI-hardened `popq %rcx; lfence; jmpq *%rcx` sequence.

```c
__attribute__((xray_always_instrument))
int test_xray_lvi(int a) {
  return a + 1;
}

int test_normal_lvi(int a) {
 return a + 1;
}
```

Compile flag: `clang --target=x86_64-unknown-linux-gnu -O2 -fxray-instrument -mlvi-cfi -fno-omit-frame-pointer -S trigger.c -o -`

```gas
test_xray_lvi:
	...
	popq	%rbp
.Lxray_sled_1:
	retq # bare retq, not hardened by -mlvi-cfi
	nopw	%cs:512(%rax,%rax)

test_normal_lvi:
	...
	popq	%rbp
	popq	%rcx
	lfence
	jmpq	*%rcx # LVI-hardened return
```

**Root cause.** `XRayInstrumentation` runs in `addPreEmitPass` and replaces `X86::RET64` with `X86::PATCHABLE_RET`.
`X86LoadValueInjectionRetHardeningPass::runOnMachineFunction` (`llvm/lib/Target/X86/X86LoadValueInjectionRetHardening.cpp:74`)
only checks `if (MI.getOpcode() != X86::RET64) continue;`, so it skips `PATCHABLE_RET`. During assembly emission,
`X86AsmPrinter::LowerPATCHABLE_RET` (`llvm/lib/Target/X86/X86MCInstLower.cpp:1358-1365`) unconditionally emits a plain `RET64`.

**Suggested fix.** Handle `X86::PATCHABLE_RET` in `X86LoadValueInjectionRetHardeningPass` (or in `LowerPATCHABLE_RET`
when LVI ret-hardening is enabled) so XRay exit sleds do not emit an unhardened `retq`.

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