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
357357 -I$(abspath $(OUTPUT)/../usr/include)
358358
359359 CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
360− -Wno-compare-distinct-pointer-types
360+ -Wno-compare-distinct-pointer-types -Wuninitialized
361361
362362 $(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
363363 $(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
7575 long rbtree_add_and_remove(void *ctx)
7676 {
7777 struct bpf_rb_node *res = NULL;
78− struct node_data *n, *m;
78+ struct node_data *n, *m = NULL;
7979
8080 n = bpf_obj_new(typeof(*n));
8181 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
232232
233233 bpf_spin_lock(&glock);
234234 res = bpf_rbtree_first(&groot);
235− if (res)
236− n = container_of(res, struct node_data, node);
235+ if (!res) {
236+ bpf_spin_unlock(&glock);
237+ return 1;
238+ }
239+ n = container_of(res, struct node_data, node);
237240 bpf_spin_unlock(&glock);
238241
239242 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
4848 __failure __msg("arg#0 expected pointer to stack or dynptr_ptr")
4949 int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size)
5050 {
51− unsigned long val;
51+ unsigned long val = 0;
5252
5353 return bpf_verify_pkcs7_signature((struct bpf_dynptr *)val,
5454 (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
2323 bool *ipv4)
2424 {
2525 struct bpf_sock_tuple *result;
26+ __u64 ihl_len = 0;
2627 __u8 proto = 0;
27− __u64 ihl_len;
2828
2929 if (eth_proto == bpf_htons(ETH_P_IP)) {
3030 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
324324 SEC("tc")
325325 int vxlan_set_tunnel_dst(struct __sk_buff *skb)
326326 {
327− int ret;
328327 struct bpf_tunnel_key key;
329328 struct vxlan_metadata md;
330329 __u32 index = 0;
331330 __u32 *local_ip = NULL;
331+ int ret = 0;
332332
333333 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
334334 if (!local_ip) {
@@ -363,11 +363,11 @@ int vxlan_set_tunnel_dst(struct __sk_buff *skb) · 28 unmodified lines
363363 SEC("tc")
364364 int vxlan_set_tunnel_src(struct __sk_buff *skb)
365365 {
366− int ret;
367366 struct bpf_tunnel_key key;
368367 struct vxlan_metadata md;
369368 __u32 index = 0;
370369 __u32 *local_ip = NULL;
370+ int ret = 0;
371371
372372 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
373373 if (!local_ip) {
@@ -494,9 +494,9 @@ SEC("tc") · 120 unmodified lines
494494 int ip6vxlan_set_tunnel_dst(struct __sk_buff *skb)
495495 {
496496 struct bpf_tunnel_key key;
497− int ret;
498497 __u32 index = 0;
499498 __u32 *local_ip;
499+ int ret = 0;
500500
501501 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
502502 if (!local_ip) {
@@ -525,9 +525,9 @@ SEC("tc") · 22 unmodified lines
525525 int ip6vxlan_set_tunnel_src(struct __sk_buff *skb)
526526 {
527527 struct bpf_tunnel_key key;
528− int ret;
529528 __u32 index = 0;
530529 __u32 *local_ip;
530+ int ret = 0;
531531
532532 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
533533 if (!local_ip) {
@@ -556,9 +556,9 @@ SEC("tc") · 22 unmodified lines
556556 int ip6vxlan_get_tunnel_src(struct __sk_buff *skb)
557557 {
558558 struct bpf_tunnel_key key;
559− int ret;
560559 __u32 index = 0;
561560 __u32 *local_ip;
561+ int ret = 0;
562562
563563 local_ip = bpf_map_lookup_elem(&local_ip_map, &index);
564564 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