]> git.ipfire.org Git - thirdparty/linux.git/commit
selftests/bpf: Implement setting global variables in veristat
authorMykyta Yatsenko <yatsenko@meta.com>
Tue, 25 Feb 2025 16:31:00 +0000 (16:31 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 26 Feb 2025 18:45:00 +0000 (10:45 -0800)
commite3c9abd0d14bce7852dd11ee65d868369d7cc664
tree71ddbd746e8f0c9bf6d6a0189bdff649e12265be
parent0ba0ef012eba63652a50b318a7a3136963c37f74
selftests/bpf: Implement setting global variables in veristat

To better verify some complex BPF programs we'd like to preset global
variables.
This patch introduces CLI argument `--set-global-vars` or `-G` to
veristat, that allows presetting values to global variables defined
in BPF program. For example:

prog.c:
```
enum Enum { ELEMENT1 = 0, ELEMENT2 = 5 };
const volatile __s64 a = 5;
const volatile __u8 b = 5;
const volatile enum Enum c = ELEMENT2;
const volatile bool d = false;

char arr[4] = {0};

SEC("tp_btf/sched_switch")
int BPF_PROG(...)
{
bpf_printk("%c\n", arr[a]);
bpf_printk("%c\n", arr[b]);
bpf_printk("%c\n", arr[c]);
bpf_printk("%c\n", arr[d]);
return 0;
}
```
By default verification of the program fails:
```
./veristat prog.bpf.o
```
By presetting global variables, we can make verification pass:
```
./veristat wq.bpf.o  -G "a = 0" -G "b = 1" -G "c = 2" -G "d = 3"
```

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20250225163101.121043-2-mykyta.yatsenko5@gmail.com
tools/testing/selftests/bpf/veristat.c