Issue 240689
Summary [Driver][X86][Retpoline] -mretpoline-external-thunk Implies +retpoline-indirect-calls via m_x86_Features_Group After -mno-retpoline and Bypasses SpectreOpt LVI/SESES Conflict Diagnostics
Labels new issue
Assignees
Reporter zjy-dev
    In `x86::getX86TargetFeatures` (clang/lib/Driver/ToolChains/Arch/X86.cpp:170-194), the `else if (Args.hasFlag(OPT_mretpoline_external_thunk, ...))` block is gated behind `if (Args.hasArgNoClaim(OPT_mretpoline, OPT_mno_retpoline, ...))`. Furthermore, `mretpoline_external_thunk` belongs to `m_x86_Features_Group` (Options.td:7400), whereas `mretpoline`/`mno_retpoline` belongs to `m_Group` (Options.td:5617), and in `X86.td:448`, `FeatureRetpolineExternalThunk` carries `[FeatureRetpolineIndirectCalls]` as an implied feature (contradicting its own description: `"Only has effect when combined with some other retpoline feature"`). This creates two concrete defects when `-mretpoline -mretpoline-external-thunk -mno-retpoline` (or `-mno-retpoline -mretpoline-external-thunk`) is passed: (1) `-mno-retpoline` sets `SpectreOpt = OPT_INVALID` and pushes no negative `-retpoline-*` features at lines 173-185, and then the generic `m_x86_Features_Group` loop at line 236 pushes `+retpoline-external-thunk`, which via `X86.td:448` re-enables `UseRetpolineIndirectCalls` (so indirect calls still emit `jmp __x86_indirect_thunk_r11` despite trailing `-mno-retpoline`, while jump tables emit raw `jmpq *%rax`); and (2) because `SpectreOpt` remains `OPT_INVALID`, combining `-mno-retpoline -mretpoline-external-thunk -mlvi-hardening` bypasses the conflict check at `X86.cpp:225-230` (`if (SpectreOpt != OPT_INVALID && LVIOpt != OPT_INVALID)`).

### Reproducer (`trigger.c`)

```c
typedef int (*fn_t)(int);

static int add1(int x) { return x + 1; }

int call_indirect(fn_t f, int x) {
  return f(x);
}

int branch_indirect(int k, fn_t f) {
  switch (k) {
  case 0: return add1(k);
  case 1: return f(k + 1);
  case 2: return f(k - 1);
 case 3: return f(k * 2);
  default: return f(k);
  }
}

int entry(void) { return call_indirect(add1, 41) + branch_indirect(3, add1); }
```

Compile / reproduction command (`['--target=x86_64-unknown-linux-gnu', '-O2', '-mretpoline', '-mretpoline-external-thunk', '-mno-retpoline', '-S']`):

```bash
podman exec -i -e TMPDIR=/dev/shm defuzz-toolchain-daemon /opt/llvm23/bin/clang --target=x86_64-unknown-linux-gnu -O2 -mretpoline -mretpoline-external-thunk -mno-retpoline -x c - -S -o -
```

Observed output on LLVM 23.1.2:

```text
call_indirect:                          # @call_indirect
# %bb.0:
	movq	%rdi, %r11
	movl	%esi, %edi
	jmp	__x86_indirect_thunk_r11        # TAILCALL
...
branch_indirect:                        # @branch_indirect
...
	movslq	(%rcx,%rax,4), %rax
	addq	%rcx, %rax
	jmpq	*%rax
```

### Expected behavior

A trailing `-mno-retpoline` must disable retpolines even if `-mretpoline-external-thunk` appeared earlier on the command line (or `FeatureRetpolineExternalThunk` in `X86.td:448` must not imply `FeatureRetpolineIndirectCalls` on its own), and `SpectreOpt` must reflect active retpoline state for `-mlvi-hardening` / `-mseses` conflict diagnostics.

### Root cause & impact

- **Source location**: `clang/lib/Driver/ToolChains/Arch/X86.cpp:170-194, 225-240; llvm/lib/Target/X86/X86.td:442-448; clang/include/clang/Options/Options.td:5617-5619, 7400-7401`
- **Impact**: Inconsistent Spectre v2 mitigation state and broken flag-override semantics: build systems appending `-mno-retpoline` after `-mretpoline -mretpoline-external-thunk` still get external retpoline thunks for calls, while `-mno-retpoline -mretpoline-external-thunk` silently leaves indirect branches (`jmpq *%rax`) unhardened and bypasses `-mlvi-hardening` conflict checks.

### Suggested fix

Remove `[FeatureRetpolineIndirectCalls]` from `FeatureRetpolineExternalThunk`'s implied feature list in `llvm/lib/Target/X86/X86.td:448` (matching its comment that it only takes effect when combined with a retpoline feature), and handle `-mretpoline-external-thunk` consistently with `-mretpoline`/`-mno-retpoline` in `clang/lib/Driver/ToolChains/Arch/X86.cpp:170-194`.

Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `x86_64`.

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

Reply via email to