18c2ecf20Sopenharmony_ci#include "vmlinux.h"
28c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h>
38c2ecf20Sopenharmony_ci#include <bpf/bpf_core_read.h>
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciconst char LICENSE[] SEC("license") = "GPL";
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci__noinline int sub1(int x)
88c2ecf20Sopenharmony_ci{
98c2ecf20Sopenharmony_ci	return x + 1;
108c2ecf20Sopenharmony_ci}
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic __noinline int sub5(int v);
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci__noinline int sub2(int y)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	return sub5(y + 2);
178c2ecf20Sopenharmony_ci}
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic __noinline int sub3(int z)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	return z + 3 + sub1(4);
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic __noinline int sub4(int w)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	return w + sub3(5) + sub1(6);
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* sub5() is an identitify function, just to test weirder functions layout and
308c2ecf20Sopenharmony_ci * call patterns
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_cistatic __noinline int sub5(int v)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	return sub1(v) - 1; /* compensates sub1()'s + 1 */
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* unfortunately verifier rejects `struct task_struct *t` as an unkown pointer
388c2ecf20Sopenharmony_ci * type, so we need to accept pointer as integer and then cast it inside the
398c2ecf20Sopenharmony_ci * function
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_ci__noinline int get_task_tgid(uintptr_t t)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	/* this ensures that CO-RE relocs work in multi-subprogs .text */
448c2ecf20Sopenharmony_ci	return BPF_CORE_READ((struct task_struct *)(void *)t, tgid);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciint res1 = 0;
488c2ecf20Sopenharmony_ciint res2 = 0;
498c2ecf20Sopenharmony_ciint res3 = 0;
508c2ecf20Sopenharmony_ciint res4 = 0;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciSEC("raw_tp/sys_enter")
538c2ecf20Sopenharmony_ciint prog1(void *ctx)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	/* perform some CO-RE relocations to ensure they work with multi-prog
568c2ecf20Sopenharmony_ci	 * sections correctly
578c2ecf20Sopenharmony_ci	 */
588c2ecf20Sopenharmony_ci	struct task_struct *t = (void *)bpf_get_current_task();
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
618c2ecf20Sopenharmony_ci		return 1;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	res1 = sub1(1) + sub3(2); /* (1 + 1) + (2 + 3 + (4 + 1)) = 12 */
648c2ecf20Sopenharmony_ci	return 0;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciSEC("raw_tp/sys_exit")
688c2ecf20Sopenharmony_ciint prog2(void *ctx)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct task_struct *t = (void *)bpf_get_current_task();
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
738c2ecf20Sopenharmony_ci		return 1;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	res2 = sub2(3) + sub3(4); /* (3 + 2) + (4 + 3 + (4 + 1)) = 17 */
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* prog3 has the same section name as prog1 */
808c2ecf20Sopenharmony_ciSEC("raw_tp/sys_enter")
818c2ecf20Sopenharmony_ciint prog3(void *ctx)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	struct task_struct *t = (void *)bpf_get_current_task();
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
868c2ecf20Sopenharmony_ci		return 1;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	res3 = sub3(5) + 6; /* (5 + 3 + (4 + 1)) + 6 = 19 */
898c2ecf20Sopenharmony_ci	return 0;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/* prog4 has the same section name as prog2 */
938c2ecf20Sopenharmony_ciSEC("raw_tp/sys_exit")
948c2ecf20Sopenharmony_ciint prog4(void *ctx)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct task_struct *t = (void *)bpf_get_current_task();
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
998c2ecf20Sopenharmony_ci		return 1;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	res4 = sub4(7) + sub1(8); /* (7 + (5 + 3 + (4 + 1)) + (6 + 1)) + (8 + 1) = 36 */
1028c2ecf20Sopenharmony_ci	return 0;
1038c2ecf20Sopenharmony_ci}
104