Issue 235269
Summary [BPF] Assertion `Expr->getKind() == MCExpr::SymbolRef` in BPFMCCodeEmitter::getMachineOpValue for `ld_imm64` with `sym + const`
Labels new issue
Assignees
Reporter I-am-Octavian
    `r2 = tbl + 8 ll` reaches `BPFMCCodeEmitter::getMachineOpValue()` with an
`MCBinaryExpr` operand and trips the `SymbolRef` assertion
(BPFMCCodeEmitter.cpp:106). Hit via inline asm in the Linux BPF selftest
`tools/testing/selftests/bpf/progs/verifier_callx_rodata.c`.

Reproducer:

```asm
 .section .rodata,"a"
tbl:
        .quad 1
        .quad 2
 .text
        r2 = tbl + 8 ll
        r0 = 0
        exit
```

```
$ llvm-mc -triple=bpfel -filetype=obj repro.s -o repro.o
llvm-mc: llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp:106: unsigned int {anonymous}::BPFMCCodeEmitter::getMachineOpValue(const llvm::MCInst&, const llvm::MCOperand&, llvm::SmallVectorImpl<llvm::MCFixup>&, const llvm::MCSubtargetInfo&) const: Assertion `Expr->getKind() == MCExpr::SymbolRef' failed.
```

Reproduces on main (2b6e769) and 22.1.5 with `LLVM_ENABLE_ASSERTIONS=ON`.
Same crash from clang (`--target=bpfel -O2`) with the equivalent inline asm.

<details>
<summary>Backtrace</summary>

```
 #0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int)
 #1 llvm::sys::RunSignalHandlers()
 #2 SignalHandler(int, siginfo_t*, void*)
 ...
 #8 abort
#11 (anonymous namespace)::BPFMCCodeEmitter::getMachineOpValue(llvm::MCInst const&, llvm::MCOperand const&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const
#12 (anonymous namespace)::BPFMCCodeEmitter::getBinaryCodeForInstr(llvm::MCInst const&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const
#13 (anonymous namespace)::BPFMCCodeEmitter::encodeInstruction(llvm::MCInst const&, llvm::SmallVectorImpl<char>&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const
#14 llvm::MCObjectStreamer::emitInstToData(llvm::MCInst const&, llvm::MCSubtargetInfo const&)
#15 (anonymous namespace)::BPFAsmParser::matchAndEmitInstruction(llvm::SMLoc, unsigned int&, llvm::SmallVectorImpl<std::unique_ptr<llvm::MCParsedAsmOperand>>&, llvm::MCStreamer&, unsigned long&, bool)
#16 (anonymous namespace)::AsmParser::parseAndMatchAndEmitTargetInstruction(...)
#17 (anonymous namespace)::AsmParser::parseStatement(...)
#18 (anonymous namespace)::AsmParser::Run(bool, bool)
#19 main
```

</details>

Without assertions (22.1.8) the output is correct, the `FK_SecRel_8` fixup
carries the addend:

```
0:  18 02 00 00 08 00 00 00 00 00 00 00 00 00 00 00   r2 = 0x8 ll
    0000000000000000:  R_BPF_64_64 .rodata
```

(`tbl` at `.rodata+8` gives `imm = 0x10`; a global `tbl` gives
`R_BPF_64_64 tbl` with `imm = 8`.)

Codegen can't produce this since `LowerGlobalAddress` rejects nonzero offsets,
so only the asm parser path hits it. The assert looks too strict for
`LD_imm64` at least. I haven't checked `JAL`/`JMPL`/branch operands with
`sym + const`, which share the same assert.

[bpf-ld-imm64-sym-offset-repro.zip](https://github.com/user-attachments/files/33902885/bpf-ld-imm64-sym-offset-repro.zip)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to