Issue 242844
Summary [X86][FastISel] bitcast double to i64 + inttoptr folded into load address, XMM register used as base
Labels new issue
Assignees
Reporter jasperdew
    At `-O0` with FastISel, `X86SelectAddress` looks through a `bitcast double to i64` feeding an `inttoptr` and uses the XMM register as the base register of the load. The machine verifier rejects it, and without the verifier the emitted `movl (%xmm0), %eax` is encoded with the GPR of the same number.

Reduced IR (`m.ll`):

```llvm
target triple = "x86_64-unknown-linux-gnu"

define i32 @read_through_f64(double %x) {
 %i = bitcast double %x to i64
  %p = inttoptr i64 %i to ptr
  %v = load i32, ptr %p, align 4
  ret i32 %v
}
```

```
llc -O0 -fast-isel -verify-machineinstrs m.ll -o /dev/null
```

```
*** Bad machine code: Illegal virtual register for instruction ***
- function: read_through_f64
- instruction: %3:gr32 = MOV32rm %1:fr64, 1, $noreg, 0, $noreg :: (load (s32) from %ir.p)
Expected a GR64 register, but got a FR64 register
LLVM ERROR: Found 1 machine code errors.
```

Without `-verify-machineinstrs`, `llc -O0 -fast-isel m.ll` prints:

```
read_through_f64:
	movl	(%xmm0), %eax
	retq
```

With `-fast-isel=false` the result is correct:

```
read_through_f64:
	movq	%xmm0, %rax
	movl	(%rax), %eax
	retq
```

Reproduces with Homebrew LLVM 22.1.7 and with LLVM 21.1.8 (rustc 1.94.0).

Cause: in `X86FastISel::X86SelectAddress` (`llvm/lib/Target/X86/X86FastISel.cpp`) the `Instruction::BitCast` case recurses into the operand without checking its type:

```cpp
  case Instruction::BitCast:
    // Look past bitcasts.
    return X86SelectAddress(U->getOperand(0), AM);
```

The recursion reaches the `double` argument and takes its FR64 virtual register as `AM.Base.Reg`. The `IntToPtr` case below it already checks that the operand is an integer. The `BitCast` case probably needs a check that the operand type is a pointer or integer.

Found through Rust, where it crashes real code: https://github.com/rust-lang/rust/issues/165994 (a pointer kept in an `f64`, converted back with `transmute::<f64, usize>(x) as *const u32`, which is valid under exposed provenance). Downstream report: https://github.com/wasmi-labs/wasmi/issues/2061.

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

Reply via email to