Back to home page

OSCL-LXR

 
 

    


0001 #include "vmlinux.h"
0002 #include <bpf/bpf_helpers.h>
0003 #include <bpf/bpf_core_read.h>
0004 
0005 const char LICENSE[] SEC("license") = "GPL";
0006 
0007 struct {
0008     __uint(type, BPF_MAP_TYPE_ARRAY);
0009     __uint(max_entries, 1);
0010     __type(key, __u32);
0011     __type(value, __u64);
0012 } array SEC(".maps");
0013 
0014 __noinline int sub1(int x)
0015 {
0016     int key = 0;
0017 
0018     bpf_map_lookup_elem(&array, &key);
0019     return x + 1;
0020 }
0021 
0022 static __noinline int sub5(int v);
0023 
0024 __noinline int sub2(int y)
0025 {
0026     return sub5(y + 2);
0027 }
0028 
0029 static __noinline int sub3(int z)
0030 {
0031     return z + 3 + sub1(4);
0032 }
0033 
0034 static __noinline int sub4(int w)
0035 {
0036     int key = 0;
0037 
0038     bpf_map_lookup_elem(&array, &key);
0039     return w + sub3(5) + sub1(6);
0040 }
0041 
0042 /* sub5() is an identitify function, just to test weirder functions layout and
0043  * call patterns
0044  */
0045 static __noinline int sub5(int v)
0046 {
0047     return sub1(v) - 1; /* compensates sub1()'s + 1 */
0048 }
0049 
0050 /* unfortunately verifier rejects `struct task_struct *t` as an unkown pointer
0051  * type, so we need to accept pointer as integer and then cast it inside the
0052  * function
0053  */
0054 __noinline int get_task_tgid(uintptr_t t)
0055 {
0056     /* this ensures that CO-RE relocs work in multi-subprogs .text */
0057     return BPF_CORE_READ((struct task_struct *)(void *)t, tgid);
0058 }
0059 
0060 int res1 = 0;
0061 int res2 = 0;
0062 int res3 = 0;
0063 int res4 = 0;
0064 
0065 SEC("raw_tp/sys_enter")
0066 int prog1(void *ctx)
0067 {
0068     /* perform some CO-RE relocations to ensure they work with multi-prog
0069      * sections correctly
0070      */
0071     struct task_struct *t = (void *)bpf_get_current_task();
0072 
0073     if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
0074         return 1;
0075 
0076     res1 = sub1(1) + sub3(2); /* (1 + 1) + (2 + 3 + (4 + 1)) = 12 */
0077     return 0;
0078 }
0079 
0080 SEC("raw_tp/sys_exit")
0081 int prog2(void *ctx)
0082 {
0083     struct task_struct *t = (void *)bpf_get_current_task();
0084 
0085     if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
0086         return 1;
0087 
0088     res2 = sub2(3) + sub3(4); /* (3 + 2) + (4 + 3 + (4 + 1)) = 17 */
0089     return 0;
0090 }
0091 
0092 static int empty_callback(__u32 index, void *data)
0093 {
0094     return 0;
0095 }
0096 
0097 /* prog3 has the same section name as prog1 */
0098 SEC("raw_tp/sys_enter")
0099 int prog3(void *ctx)
0100 {
0101     struct task_struct *t = (void *)bpf_get_current_task();
0102 
0103     if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
0104         return 1;
0105 
0106     /* test that ld_imm64 with BPF_PSEUDO_FUNC doesn't get blinded */
0107     bpf_loop(1, empty_callback, NULL, 0);
0108 
0109     res3 = sub3(5) + 6; /* (5 + 3 + (4 + 1)) + 6 = 19 */
0110     return 0;
0111 }
0112 
0113 /* prog4 has the same section name as prog2 */
0114 SEC("raw_tp/sys_exit")
0115 int prog4(void *ctx)
0116 {
0117     struct task_struct *t = (void *)bpf_get_current_task();
0118 
0119     if (!BPF_CORE_READ(t, pid) || !get_task_tgid((uintptr_t)t))
0120         return 1;
0121 
0122     res4 = sub4(7) + sub1(8); /* (7 + (5 + 3 + (4 + 1)) + (6 + 1)) + (8 + 1) = 36 */
0123     return 0;
0124 }