2 #include <bpf/bpf_helpers.h>
3 #include <bpf/bpf_core_read.h>
5 const char LICENSE[] SEC("license") = "GPL";
8 __uint(type, BPF_MAP_TYPE_ARRAY);
9 __uint(max_entries, 1);
14 __noinline int sub1(int x)
18 bpf_map_lookup_elem(&array, &key);
22 static __noinline int sub5(int v);
24 __noinline int sub2(int y)
29 static __noinline int sub3(int z)
31 return z + 3 + sub1(4);
34 static __noinline int sub4(int w)
38 bpf_map_lookup_elem(&array, &key);
39 return w + sub3(5) + sub1(6);
42 /* sub5() is an identitify function, just to test weirder functions layout and
45 static __noinline int sub5(int v)
47 return sub1(v) - 1; /* compensates sub1()'s + 1 */
50 /* unfortunately verifier rejects `struct task_struct *t` as an unknown pointer
51 * type, so we need to accept pointer as integer and then cast it inside the
54 __noinline int get_task_tgid(uintptr_t t)
56 /* this ensures that CO-RE relocs work in multi-subprogs .text */
57 return BPF_CORE_READ((struct task_struct *)(void *)t, tgid);
65 SEC("raw_tp/sys_enter")
68 /* perform some CO-RE relocations to ensure they work with multi-prog
71 struct task_struct *t = (void *)bpf_get_current_task();
73 if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
76 res1 = sub1(1) + sub3(2); /* (1 + 1) + (2 + 3 + (4 + 1)) = 12 */
80 SEC("raw_tp/sys_exit")
83 struct task_struct *t = (void *)bpf_get_current_task();
85 if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
88 res2 = sub2(3) + sub3(4); /* (3 + 2) + (4 + 3 + (4 + 1)) = 17 */
92 static int empty_callback(__u32 index, void *data)
97 /* prog3 has the same section name as prog1 */
98 SEC("raw_tp/sys_enter")
101 struct task_struct *t = (void *)bpf_get_current_task();
103 if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
106 /* test that ld_imm64 with BPF_PSEUDO_FUNC doesn't get blinded */
107 bpf_loop(1, empty_callback, NULL, 0);
109 res3 = sub3(5) + 6; /* (5 + 3 + (4 + 1)) + 6 = 19 */
113 /* prog4 has the same section name as prog2 */
114 SEC("raw_tp/sys_exit")
117 struct task_struct *t = (void *)bpf_get_current_task();
119 if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
122 res4 = sub4(7) + sub1(8); /* (7 + (5 + 3 + (4 + 1)) + (6 + 1)) + (8 + 1) = 36 */