[llvm] BPF: Emit an error for illegal LD_imm64 insn when LLVM_ENABLE_ASSERTI… (PR #74035)

via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 2 10:18:17 PST 2023


eddyz87 wrote:

Form me when assertions are enabled the error is reported not in `printExpr`, but in `BPFMCCodeEmitter::getMachineOpValue`:

```
$ clang -O2 --target=bpf -c test-asm-ll.c -o /dev/null 
clang: /home/eddy/work/llvm-project/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp:92: unsigned int (anonymous namespace)::BPFMCCodeEmitter::getMachineOpValue(const MCInst &, const MCOperand &, SmallVectorImpl<MCFixup> &, const MCSubtargetInfo &) const: Assertion `Expr->getKind() == MCExpr::SymbolRef' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: clang -O2 --target=bpf -c test-asm-ll.c -o /dev/null
1.	<eof> parser at end of file
2.	Code generation
3.	Running pass 'Function Pass Manager' on module 'test-asm-ll.c'.
4.	Running pass 'BPF Assembly Printer' on function '@foo'
 #0 0x000055ef0b9e3954 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/eddy/work/llvm-project/build/bin/clang-18+0x3aba954)
 #1 0x000055ef0b9e331d llvm::sys::CleanupOnSignal(unsigned long) (/home/eddy/work/llvm-project/build/bin/clang-18+0x3aba31d)
 #2 0x000055ef0b982f39 (anonymous namespace)::CrashRecoveryContextImpl::HandleCrash(int, unsigned long) CrashRecoveryContext.cpp:0:0
 #3 0x000055ef0b9830cf CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #4 0x00007fa04003f190 __restore_rt (/lib64/libc.so.6+0x3f190)
 #5 0x00007fa040091dec __pthread_kill_implementation (/lib64/libc.so.6+0x91dec)
 #6 0x00007fa04003f0c6 gsignal (/lib64/libc.so.6+0x3f0c6)
 #7 0x00007fa0400268d7 abort (/lib64/libc.so.6+0x268d7)
 #8 0x00007fa0400267eb _nl_load_domain.cold (/lib64/libc.so.6+0x267eb)
 #9 0x00007fa040037016 (/lib64/libc.so.6+0x37016)
#10 0x000055ef0ad9285d (/home/eddy/work/llvm-project/build/bin/clang-18+0x2e6985d)
#11 0x000055ef0ad92305 (anonymous namespace)::BPFMCCodeEmitter::getBinaryCodeForInstr(llvm::MCInst const&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const BPFMCCodeEmitter.cpp:0:0
#12 0x000055ef0ad92061 (anonymous namespace)::BPFMCCodeEmitter::encodeInstruction(llvm::MCInst const&, llvm::SmallVectorImpl<char>&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const BPFMCCodeEmitter.cpp:0:0
#13 0x000055ef0b81eef4 llvm::MCELFStreamer::emitInstToData(llvm::MCInst const&, llvm::MCSubtargetInfo const&) (/home/eddy/work/llvm-project/build/bin/clang-18+0x38f5ef4)
#14 0x000055ef0b82f480 llvm::MCObjectStreamer::emitInstruction(llvm::MCInst const&, llvm::MCSubtargetInfo const&) (/home/eddy/work/llvm-project/build/bin/clang-18+0x3906480)
#15 0x000055ef0ad8f39c (anonymous namespace)::BPFAsmParser::MatchAndEmitInstruction(llvm::SMLoc, unsigned int&, llvm::SmallVectorImpl<std::unique_ptr<llvm::MCParsedAsmOperand, std::default_delete<llvm::MCParsedAsmOperand>>>&, llvm::MCStreamer&, unsigned long&, bool) BPFAsmParser.cpp:0:0
#16 0x000055ef0b87b159 (anonymous namespace)::AsmParser::parseAndMatchAndEmitTargetInstruction((anonymous namespace)::ParseStatementInfo&, llvm::StringRef, llvm::AsmToken, llvm::SMLoc) AsmParser.cpp:0:0
#17 0x000055ef0b870550 (anonymous namespace)::AsmParser::parseStatement((anonymous namespace)::ParseStatementInfo&, llvm::MCAsmParserSemaCallback*) AsmParser.cpp:0:0
#18 0x000055ef0b86bc7a (anonymous namespace)::AsmParser::Run(bool, bool) AsmParser.cpp:0:0
#19 0x000055ef0c37f8e4 llvm::AsmPrinter::emitInlineAsm(llvm::StringRef, llvm::MCSubtargetInfo const&, llvm::MCTargetOptions const&, llvm::MDNode const*, llvm::InlineAsm::AsmDialect) const (/home/eddy/work/llvm-project/build/bin/clang-18+0x44568e4)
#20 0x000055ef0c380842 llvm::AsmPrinter::emitInlineAsm(llvm::MachineInstr const*) const (/home/eddy/work/llvm-project/build/bin/clang-18+0x4457842)
#21 0x000055ef0c36e5a1 llvm::AsmPrinter::emitFunctionBody() (/home/eddy/work/llvm-project/build/bin/clang-18+0x44455a1)
```

Subsequently, the following modification works with both assertions on and off:

```diff
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
index b807d6904004..4f4ebdabf810 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
@@ -89,6 +89,14 @@ unsigned BPFMCCodeEmitter::getMachineOpValue(const MCInst &MI,
 
   const MCExpr *Expr = MO.getExpr();
 
+  if (Expr->getKind() != MCExpr::SymbolRef) {
+    std::string Msg;
+    llvm::raw_string_ostream S(Msg);
+    S << "getMachineOpValue: unexpected Expr->getKind() == "
+      << (unsigned)Expr->getKind() << ": "
+      << "'" << *Expr << "'" << "\n";
+    report_fatal_error(StringRef(Msg));
+  }
   assert(Expr->getKind() == MCExpr::SymbolRef);
 
   if (MI.getOpcode() == BPF::JAL)
```

Still, I think that error should be reported earlier, when expressions are parsed.

https://github.com/llvm/llvm-project/pull/74035


More information about the llvm-commits mailing list