[PATCH] D81479: [BPF] introduce __builtin_load_u32_to_ptr() intrinsic

Yonghong Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 9 09:52:33 PDT 2020


yonghong-song created this revision.
yonghong-song added a reviewer: ast.
Herald added projects: clang, LLVM.
Herald added a subscriber: llvm-commits.

In current linux BPF uapi

  https://github.com/torvalds/linux/blob/master/include/uapi/linux/bpf.h

`struct __sk_buff/xdp_md` have fields

  __u32 data;
  __u32 data_end;
  __u32 data_meta;

which actually represent pointers. Typically in bpf program, users
write the following:

  void *data = (void *)(long)__sk_buff->data;

The kernel verifier magically copied the address to the target
64bit register for __sk_buff->data and hope nothing is messed up
and it can survive to the variable "data".

In the past, we have seen a few issues with this. For example,
for the above C code, the IR looks like:

  i32_v = load u32
  i64_v = zext i32_v
  ...

The BPF backend has tried, through InstrInfo.td pattern matching or
optimization in MachineInstr SSA analysis and transformation,
to recognize the above pattern to remove zext so the really "addr"
is preserved. But this is still fragile and in the past, we have
to fix multiple bugs due to other changes in BPF backend. The
optimization may not cover all possible cases. Some users may even
use inline assembly to work around potentially missed compiler
zext elimination.

The patch introduced the following builtin function for bpf target:

  void *ptr = __builtin_load_u32_to_ptr(void *base, int offset);

The builtin will perform a 32bit load with address "base + offset"
and the result, with zext, will be returned. This way, user is
guaranteed a correct address.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81479

Files:
  clang/include/clang/Basic/BuiltinsBPF.def
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/test/CodeGen/builtin-bpf-load-u32-to-ptr.c
  clang/test/Sema/builtin-bpf-load-u32-to-ptr.c
  llvm/include/llvm/IR/IntrinsicsBPF.td

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81479.269575.patch
Type: text/x-patch
Size: 5885 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200609/b07e8c65/attachment.bin>


More information about the cfe-commits mailing list