|
| 1 | +/* Reference for passing a map lookup result to a helper. `prev` is a local |
| 2 | + * holding the pointer bpf_map_lookup_elem returned; bpf_map_update_elem gets |
| 3 | + * that pointer (the map value), loaded from prev's slot. It never gets &prev, |
| 4 | + * the slot's own address, which would make the map store a stack address. |
| 5 | + * The clang -O0 IR for this file is the specification for the pointer depth |
| 6 | + * that tests/test_signedness_ir.py checks: |
| 7 | + * copy() mirrors passing_tests/helpers/map_update_from_lookup.py |
| 8 | + * rebind() mirrors passing_tests/vmlinux/named_arg.py, where `prev = prev + 1` |
| 9 | + * rebinds prev to a stack temporary holding the sum; the helper |
| 10 | + * still gets the pointer loaded from prev's slot. */ |
| 11 | +#define SEC(name) __attribute__((section(name), used)) |
| 12 | +#define __uint(name, val) int (*name)[val] |
| 13 | +#define __type(name, val) typeof(val) *name |
| 14 | +typedef long long __s64; |
| 15 | + |
| 16 | +#define BPF_MAP_TYPE_HASH 1 |
| 17 | +#define BPF_ANY 0 |
| 18 | +#define XDP_PASS 2 |
| 19 | + |
| 20 | +static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *)1; |
| 21 | +static long (*bpf_map_update_elem)(void *map, const void *key, |
| 22 | + const void *value, __s64 flags) = (void *)2; |
| 23 | + |
| 24 | +struct { |
| 25 | + __uint(type, BPF_MAP_TYPE_HASH); |
| 26 | + __uint(max_entries, 2); |
| 27 | + __type(key, __s64); |
| 28 | + __type(value, __s64); |
| 29 | +} count SEC(".maps"); |
| 30 | + |
| 31 | +SEC("tracepoint/syscalls/sys_enter_getpid") |
| 32 | +__s64 copy(void *ctx) |
| 33 | +{ |
| 34 | + __s64 key = 0; |
| 35 | + __s64 *prev = bpf_map_lookup_elem(&count, &key); |
| 36 | + if (prev) { |
| 37 | + key = 1; |
| 38 | + bpf_map_update_elem(&count, &key, prev, BPF_ANY); |
| 39 | + } |
| 40 | + return 0; |
| 41 | +} |
| 42 | + |
| 43 | +SEC("xdp") |
| 44 | +__s64 rebind(void *ctx) |
| 45 | +{ |
| 46 | + __s64 key = 0; |
| 47 | + __s64 tmp; |
| 48 | + __s64 *prev = bpf_map_lookup_elem(&count, &key); |
| 49 | + if (prev) { |
| 50 | + tmp = *prev + 1; |
| 51 | + prev = &tmp; |
| 52 | + bpf_map_update_elem(&count, &key, prev, BPF_ANY); |
| 53 | + return XDP_PASS; |
| 54 | + } |
| 55 | + tmp = 1; |
| 56 | + bpf_map_update_elem(&count, &key, &tmp, BPF_ANY); |
| 57 | + return XDP_PASS; |
| 58 | +} |
| 59 | + |
| 60 | +char LICENSE[] SEC("license") = "GPL"; |
0 commit comments