bpf: check illegal usage of XADD insn return value
authorYonghong Song <yhs@fb.com>
Thu, 20 Sep 2018 22:24:27 +0000 (22:24 +0000)
committerYonghong Song <yhs@fb.com>
Thu, 20 Sep 2018 22:24:27 +0000 (22:24 +0000)
commit150ca5143be586e81befc7ab689e1c058ab31710
treec50ffdbc836adab4235b004535bc5272aa30d7aa
parentbb993db080391241b390f8408ff12c16b1e29a84
bpf: check illegal usage of XADD insn return value

Currently, BPF has XADD (locked add) insn support and the
asm looks like:
  lock *(u32 *)(r1 + 0) += r2
  lock *(u64 *)(r1 + 0) += r2
The instruction itself does not have a return value.

At the source code level, users often use
  __sync_fetch_and_add()
which eventually translates to XADD. The return value of
__sync_fetch_and_add() is supposed to be the old value
in the xadd memory location. Since BPF::XADD insn does not
support such a return value, this patch added a PreEmit
phase to check such a usage. If such an illegal usage
pattern is detected, a fatal error will be reported like
  line 4: Invalid usage of the XADD return value
if compiled with -g, or
  Invalid usage of the XADD return value
if compiled without -g.

Signed-off-by: Yonghong Song <yhs@fb.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
llvm-svn: 342692
llvm/lib/Target/BPF/BPF.h
llvm/lib/Target/BPF/BPFMIChecking.cpp [new file with mode: 0644]
llvm/lib/Target/BPF/BPFTargetMachine.cpp
llvm/lib/Target/BPF/CMakeLists.txt
llvm/test/CodeGen/BPF/xadd.ll [new file with mode: 0644]