This is an archive of the discontinued LLVM Phabricator instance.

[BPF] Prevent disassembly segfault for NOP insn
ClosedPublic

Authored by yonghong-song on May 18 2020, 1:38 PM.

Details

Summary

For a simple program like below:

-bash-4.4$ cat t.c 
int test() {
  asm volatile("r0 = r0" ::);
  return 0;
}

compiled with

clang -target bpf -O2 -c t.c

the following llvm-objdump command will segfault.

llvm-objdump -d t.o 

0:       bf 00 00 00 00 00 00 00 nop 
llvm-objdump: ../include/llvm/ADT/SmallVector.h:180
... 
Assertion `idx < size()' failed
... 
abort
... 
llvm::BPFInstPrinter::printOperand
llvm::BPFInstPrinter::printInstruction
...

The reason is both NOP and MOV_rr (r0 = r0) having the same encoding.
The disassembly getInstruction() decodes to be a NOP instruciton but
during printInstruction() the same encoding is interpreted as
a MOV_rr instruction. Such a mismatcch caused the segfault.

The fix is to make NOP instruction as CodeGen only so disassembler
will skip it.

Note that instruction "r0 = r0" should not appear in non inline
asm codes, we BPF Machine Instruction Peephole optimization will
remove it.

Diff Detail

Event Timeline

yonghong-song created this revision.May 18 2020, 1:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 18 2020, 1:38 PM
ast accepted this revision.May 18 2020, 2:25 PM
This revision is now accepted and ready to land.May 18 2020, 2:25 PM
This revision was automatically updated to reflect the committed changes.