
This commit adds a new BPF specific structure attribte `__attribute__((preserve_static_offset))` and a pass to deal with it. This attribute may be attached to a struct or union declaration, where it notifies the compiler that this structure is a "context" structure. The following limitations apply to context structures: - runtime environment might patch access to the fields of this type by updating the field offset; BPF verifier limits access patterns allowed for certain data types. E.g. `struct __sk_buff` and `struct bpf_sock_ops`. For these types only `LD/ST <reg> <static-offset>` memory loads and stores are allowed. This is so because offsets of the fields of these structures do not match real offsets in the running kernel. During BPF program load/verification loads and stores to the fields of these types are rewritten so that offsets match real offsets. For this rewrite to happen static offsets have to be encoded in the instructions. See `kernel/bpf/verifier.c:convert_ctx_access` function in the Linux kernel source tree for details. - runtime environment might disallow access to the field of the type through modified pointers. During BPF program verification a tag `PTR_TO_CTX` is tracked for register values. In case if register with such tag is modified BPF programs are not allowed to read or write memory using register. See kernel/bpf/verifier.c:check_mem_access function in the Linux kernel source tree for details. Access to the structure fields is translated to IR as a sequence: - `(load (getelementptr %ptr %offset))` or - `(store (getelementptr %ptr %offset))` During instruction selection phase such sequences are translated as a single load instruction with embedded offset, e.g. `LDW %ptr, %offset`, which matches access pattern necessary for the restricted set of types described above (when `%offset` is static). Multiple optimizer passes might separate these instructions, this includes: - SimplifyCFGPass (sinking) - InstCombine (sinking) - GVN (hoisting) The `preserve_static_offset` attribute marks structures for which the following transformations happen: - at the early IR processing stage: - `(load (getelementptr ...))` replaced by call to intrinsic `llvm.bpf.getelementptr.and.load`; - `(store (getelementptr ...))` replaced by call to intrinsic `llvm.bpf.getelementptr.and.store`; - at the late IR processing stage this modification is undone. Such handling prevents various optimizer passes from generating sequences of instructions that would be rejected by BPF verifier. The __attribute__((preserve_static_offset)) has a priority over __attribute__((preserve_access_index)). When preserve_access_index attribute is present preserve access index transformations are not applied. This addresses the issue reported by the following thread: https://lore.kernel.org/bpf/CAA-VZPmxh8o8EBcJ=m-DH4ytcxDFmo0JKsm1p1gf40kS0CE3NQ@mail.gmail.com/T/#m4b9ce2ce73b34f34172328f975235fc6f19841b6 This is a second attempt to commit this change, previous reverted commit is: cb13e9286b6d4e384b5d4203e853d44e2eff0f0f. The following items had been fixed: - test case bpf-preserve-static-offset-bitfield.c now uses `-triple bpfel` to avoid different codegen for little/big endian targets. - BPFPreserveStaticOffset.cpp:removePAICalls() modified to avoid use after free for `WorkList` elements `V`. Differential Revision: https://reviews.llvm.org/D133361
74 lines
2.3 KiB
LLVM
74 lines
2.3 KiB
LLVM
; RUN: opt --bpf-check-and-opt-ir -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
|
|
;
|
|
; Check unroll of getelementptr.and.load when several field indexes
|
|
; are specified in a chain.
|
|
;
|
|
; Source:
|
|
; #define __ctx __attribute__((preserve_static_offset))
|
|
;
|
|
; struct bar {
|
|
; int aa;
|
|
; int bb;
|
|
; };
|
|
;
|
|
; struct foo {
|
|
; int a;
|
|
; struct bar b;
|
|
; } __ctx;
|
|
;
|
|
; extern void consume(int);
|
|
;
|
|
; void buz(struct foo *p) {
|
|
; consume(p->b.bb);
|
|
; }
|
|
;
|
|
; Compilation flag:
|
|
; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
|
|
; | opt -passes=sroa,bpf-preserve-static-offset -S -o -
|
|
|
|
%struct.foo = type { i32, %struct.bar }
|
|
%struct.bar = type { i32, i32 }
|
|
|
|
; Function Attrs: nounwind
|
|
define dso_local void @buz(ptr noundef %p) #0 {
|
|
entry:
|
|
%bb1 = call i32 (ptr, i1, i8, i8, i8, i1, ...)
|
|
@llvm.bpf.getelementptr.and.load.i32
|
|
(ptr readonly elementtype(%struct.foo) %p,
|
|
i1 false, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1, i32 immarg 1)
|
|
#4, !tbaa !2
|
|
call void @consume(i32 noundef %bb1)
|
|
ret void
|
|
}
|
|
|
|
; CHECK: define dso_local void @buz(ptr noundef %[[p:.*]])
|
|
; CHECK: %[[bb11:.*]] = getelementptr inbounds %struct.foo, ptr %[[p]], i32 0, i32 1, i32 1
|
|
; CHECK: %[[v2:.*]] = load i32, ptr %[[bb11]], align 4
|
|
; CHECK: call void @consume(i32 noundef %[[v2]])
|
|
|
|
declare void @consume(i32 noundef) #1
|
|
|
|
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
|
|
declare ptr @llvm.preserve.static.offset(ptr readnone) #2
|
|
|
|
; Function Attrs: nocallback nofree nounwind willreturn
|
|
declare i32 @llvm.bpf.getelementptr.and.load.i32(ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #3
|
|
|
|
attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
|
|
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
|
|
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
|
|
attributes #3 = { nocallback nofree nounwind willreturn }
|
|
attributes #4 = { memory(argmem: read) }
|
|
|
|
!llvm.module.flags = !{!0}
|
|
!llvm.ident = !{!1}
|
|
|
|
!0 = !{i32 1, !"wchar_size", i32 4}
|
|
!1 = !{!"clang"}
|
|
!2 = !{!3, !4, i64 8}
|
|
!3 = !{!"foo", !4, i64 0, !7, i64 4}
|
|
!4 = !{!"int", !5, i64 0}
|
|
!5 = !{!"omnipotent char", !6, i64 0}
|
|
!6 = !{!"Simple C/C++ TBAA"}
|
|
!7 = !{!"bar", !4, i64 0, !4, i64 4}
|