History

selftests/bpf: Add -Wuninitialized flag to bpf prog flags

Per C99 standard [0], Section 6.7.8, Paragraph 10: If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. And in the same document, in appendix "J.2 Undefined behavior": The behavior is undefined in the following circumstances: [...] The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.8, 6.8). This means that use of an uninitialized stack variable is undefined behavior, and therefore that clang can choose to do a variety of scary things, such as not generating bytecode for "bunch of useful code" in the below example: void some_func() { int i; if (!i) return; // bunch of useful code } To add insult to injury, if some_func above is a helper function for some BPF program, clang can choose to not generate an "exit" insn, causing verifier to fail with "last insn is not an exit or jmp". Going from that verification failure to the root cause of uninitialized use is certain to be frustrating. This patch adds -Wuninitialized to the cflags for selftest BPF progs and fixes up existing instances of uninitialized use. [0]: https://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

Dave Marchevsky committed 3 years ago

Commit ec97a761 parent c501bf56 files · +14 −11Browse files

Unified · Split

6 files
  1. tools/testing/selftests/bpf/Makefile +1 −1
  2. tools/testing/selftests/bpf/progs/rbtree.c +1 −1
  3. tools/testing/selftests/bpf/progs/rbtree_fail.c +5 −2
  4. tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c +1 −1
  5. tools/testing/selftests/bpf/progs/test_sk_lookup_kern.c +1 −1
  6. tools/testing/selftests/bpf/progs/test_tunnel_kern.c +5 −5
tools/testing/selftests/bpf/Makefile+1−1
@@ -357,7 +357,7 @@ BPF_CFLAGS = -g -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ · 356 unmodified lines
357 -I$(abspath $(OUTPUT)/../usr/include)357 -I$(abspath $(OUTPUT)/../usr/include)
358 358
359CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \359CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
360 -Wno-compare-distinct-pointer-types360 -Wno-compare-distinct-pointer-types -Wuninitialized
361 361
362$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline362$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
363$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline363$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
tools/testing/selftests/bpf/progs/rbtree.c+1−1
@@ -75,7 +75,7 @@ SEC("tc") · 74 unmodified lines
75long rbtree_add_and_remove(void *ctx)75long rbtree_add_and_remove(void *ctx)
76{76{
77 struct bpf_rb_node *res = NULL;77 struct bpf_rb_node *res = NULL;
78 struct node_data *n, *m;78 struct node_data *n, *m = NULL;
79 79
80 n = bpf_obj_new(typeof(*n));80 n = bpf_obj_new(typeof(*n));
81 if (!n)81 if (!n)
tools/testing/selftests/bpf/progs/rbtree_fail.c+5−2
@@ -232,8 +232,11 @@ long rbtree_api_first_release_unlock_escape(void *ctx) · 231 unmodified lines
232 232
233 bpf_spin_lock(&glock);233 bpf_spin_lock(&glock);
234 res = bpf_rbtree_first(&groot);234 res = bpf_rbtree_first(&groot);
235 if (res)235 if (!res) {
236 n = container_of(res, struct node_data, node);236 bpf_spin_unlock(&glock);
237 return 1;
238 }
239 n = container_of(res, struct node_data, node);
237 bpf_spin_unlock(&glock);240 bpf_spin_unlock(&glock);
238 241
239 bpf_spin_lock(&glock);242 bpf_spin_lock(&glock);
tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c+1−1
@@ -48,7 +48,7 @@ SEC("?lsm.s/bpf") · 47 unmodified lines
48__failure __msg("arg#0 expected pointer to stack or dynptr_ptr")48__failure __msg("arg#0 expected pointer to stack or dynptr_ptr")
49int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size)49int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size)
50{50{
51 unsigned long val;51 unsigned long val = 0;
52 52
53 return bpf_verify_pkcs7_signature((struct bpf_dynptr *)val,53 return bpf_verify_pkcs7_signature((struct bpf_dynptr *)val,
54 (struct bpf_dynptr *)val, NULL);54 (struct bpf_dynptr *)val, NULL);
tools/testing/selftests/bpf/progs/test_sk_lookup_kern.c+1−1
@@ -23,8 +23,8 @@ static struct bpf_sock_tuple *get_tuple(void *data, __u64 nh_off, · 22 unmodified lines
23 bool *ipv4)23 bool *ipv4)
24{24{
25 struct bpf_sock_tuple *result;25 struct bpf_sock_tuple *result;
26 __u64 ihl_len = 0;
26 __u8 proto = 0;27 __u8 proto = 0;
27 __u64 ihl_len;
28 28
29 if (eth_proto == bpf_htons(ETH_P_IP)) {29 if (eth_proto == bpf_htons(ETH_P_IP)) {
30 struct iphdr *iph = (struct iphdr *)(data + nh_off);30 struct iphdr *iph = (struct iphdr *)(data + nh_off);
tools/testing/selftests/bpf/progs/test_tunnel_kern.c+5−5
@@ -324,11 +324,11 @@ int ip4ip6erspan_get_tunnel(struct __sk_buff *skb) · 323 unmodified lines
324SEC("tc")324SEC("tc")
325int vxlan_set_tunnel_dst(struct __sk_buff *skb)325int vxlan_set_tunnel_dst(struct __sk_buff *skb)
326{326{
327 int ret;
328 struct bpf_tunnel_key key;327 struct bpf_tunnel_key key;
329 struct vxlan_metadata md;328 struct vxlan_metadata md;
330 __u32 index = 0;329 __u32 index = 0;
331 __u32 *local_ip = NULL;330 __u32 *local_ip = NULL;
331 int ret = 0;
332 332
333 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);333 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
334 if (!local_ip) {334 if (!local_ip) {
@@ -363,11 +363,11 @@ int vxlan_set_tunnel_dst(struct __sk_buff *skb) · 28 unmodified lines
363SEC("tc")363SEC("tc")
364int vxlan_set_tunnel_src(struct __sk_buff *skb)364int vxlan_set_tunnel_src(struct __sk_buff *skb)
365{365{
366 int ret;
367 struct bpf_tunnel_key key;366 struct bpf_tunnel_key key;
368 struct vxlan_metadata md;367 struct vxlan_metadata md;
369 __u32 index = 0;368 __u32 index = 0;
370 __u32 *local_ip = NULL;369 __u32 *local_ip = NULL;
370 int ret = 0;
371 371
372 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);372 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
373 if (!local_ip) {373 if (!local_ip) {
@@ -494,9 +494,9 @@ SEC("tc") · 120 unmodified lines
494int ip6vxlan_set_tunnel_dst(struct __sk_buff *skb)494int ip6vxlan_set_tunnel_dst(struct __sk_buff *skb)
495{495{
496 struct bpf_tunnel_key key;496 struct bpf_tunnel_key key;
497 int ret;
498 __u32 index = 0;497 __u32 index = 0;
499 __u32 *local_ip;498 __u32 *local_ip;
499 int ret = 0;
500 500
501 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);501 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
502 if (!local_ip) {502 if (!local_ip) {
@@ -525,9 +525,9 @@ SEC("tc") · 22 unmodified lines
525int ip6vxlan_set_tunnel_src(struct __sk_buff *skb)525int ip6vxlan_set_tunnel_src(struct __sk_buff *skb)
526{526{
527 struct bpf_tunnel_key key;527 struct bpf_tunnel_key key;
528 int ret;
529 __u32 index = 0;528 __u32 index = 0;
530 __u32 *local_ip;529 __u32 *local_ip;
530 int ret = 0;
531 531
532 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);532 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
533 if (!local_ip) {533 if (!local_ip) {
@@ -556,9 +556,9 @@ SEC("tc") · 22 unmodified lines
556int ip6vxlan_get_tunnel_src(struct __sk_buff *skb)556int ip6vxlan_get_tunnel_src(struct __sk_buff *skb)
557{557{
558 struct bpf_tunnel_key key;558 struct bpf_tunnel_key key;
559 int ret;
560 __u32 index = 0;559 __u32 index = 0;
561 __u32 *local_ip;560 __u32 *local_ip;
561 int ret = 0;
562 562
563 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);563 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
564 if (!local_ip) {564 if (!local_ip) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t