Fix disassembly of eBPF atomic instructions and semantics of compare-and-exchange #8721
+26
−26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


Hello,
eBPF ISA v3 introduced atomic instructions: https://www.kernel.org/doc/html/v6.0/bpf/instruction-set.html#atomic-operations . These instructions are encoded using
BPF_ATOMIC | BPF_W | BPF_STXandBPF_ATOMIC | BPF_DW | BPF_STXfor 32-bit and 64-bit operations, with:While Ghidra's semantic section is constructed correctly (atomic add uses an addition ; atomic or uses or ; ...), the disassembly always displays
STXXADDWandSTXXADDDW. These mnemonics come from the deprecated nameBPF_XADD = BPF_ATOMIC | BPF_ADD = 0xc0.This Pull Request replaces the confusing mnemonics with the ones used by binutils and documented in
https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gas/doc/c-bpf.texi;h=003cb92a457985038a9abc1ffbf347f636eb0586;hb=2bc7af1ff7732451b6a7b09462a815c3284f9613#l745
While testing this, I found a bug in the semantics of atomic compare-and-exchange instruction, described in the next section.
Testing
I wrote a test C program, atomic.c using gcc's atomic built-ins. This program contains many small functions such as:
I compiled this program in a Debian 13 container with different options:
I then disassembled the programs with LLVM's
llvm-objdumpand binutils'bpf-objdump:While
llvm-objdumpproduces pseudo-code instructions, such as:...
bpf-objdumpproduces usual ASM mnemonics:I analyzed the compiled eBPF programs with Ghidra and confirmed the disassembly listing contains (with this Pull Request) similar atomic mnemonics:
AXCHG,AFADD,AFOR...While testing atomic compare-and-exchange, Ghidra showed an unexpected decompiled code. This function
... got decompiled to:
The fact that
*param_1 = param_3;is always executed is a bug. When the contents of the pointer does not match the expected value,__atomic_compare_exchange_ndoes not modify this content (and only reads the actual content toexpected, which is what the secondifstatement is about). This Pull Request fixes this bug by addinggoto inst_next;where appropriate.With this, the decompilation becomes:
There is a stray statement
lVar2 = lVar1;but at least the code is semantically correct.Attachements
atomic.c
atomic_example.tar.gz