Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <stddef.h>
0003 #include <linux/bpf.h>
0004 #include <bpf/bpf_helpers.h>
0005 
0006 struct S {
0007     int x;
0008 };
0009 
0010 struct C {
0011     int x;
0012     int y;
0013 };
0014 
0015 struct {
0016     __uint(type, BPF_MAP_TYPE_ARRAY);
0017     __uint(max_entries, 1);
0018     __type(key, __u32);
0019     __type(value, struct S);
0020 } map SEC(".maps");
0021 
0022 enum E {
0023     E_ITEM
0024 };
0025 
0026 static int global_data_x = 100;
0027 static int volatile global_data_y = 500;
0028 
0029 __noinline int foo(const struct S *s)
0030 {
0031     if (s)
0032         return bpf_get_prandom_u32() < s->x;
0033 
0034     return 0;
0035 }
0036 
0037 __noinline int bar(int *x)
0038 {
0039     if (x)
0040         *x &= bpf_get_prandom_u32();
0041 
0042     return 0;
0043 }
0044 __noinline int baz(volatile int *x)
0045 {
0046     if (x)
0047         *x &= bpf_get_prandom_u32();
0048 
0049     return 0;
0050 }
0051 
0052 __noinline int qux(enum E *e)
0053 {
0054     if (e)
0055         return *e;
0056 
0057     return 0;
0058 }
0059 
0060 __noinline int quux(int (*arr)[10])
0061 {
0062     if (arr)
0063         return (*arr)[9];
0064 
0065     return 0;
0066 }
0067 
0068 __noinline int quuz(int **p)
0069 {
0070     if (p)
0071         *p = NULL;
0072 
0073     return 0;
0074 }
0075 
0076 SEC("cgroup_skb/ingress")
0077 int test_cls(struct __sk_buff *skb)
0078 {
0079     int result = 0;
0080 
0081     {
0082         const struct S s = {.x = skb->len };
0083 
0084         result |= foo(&s);
0085     }
0086 
0087     {
0088         const __u32 key = 1;
0089         const struct S *s = bpf_map_lookup_elem(&map, &key);
0090 
0091         result |= foo(s);
0092     }
0093 
0094     {
0095         const struct C c = {.x = skb->len, .y = skb->family };
0096 
0097         result |= foo((const struct S *)&c);
0098     }
0099 
0100     {
0101         result |= foo(NULL);
0102     }
0103 
0104     {
0105         bar(&result);
0106         bar(&global_data_x);
0107     }
0108 
0109     {
0110         result |= baz(&global_data_y);
0111     }
0112 
0113     {
0114         enum E e = E_ITEM;
0115 
0116         result |= qux(&e);
0117     }
0118 
0119     {
0120         int array[10] = {0};
0121 
0122         result |= quux(&array);
0123     }
0124 
0125     {
0126         int *p;
0127 
0128         result |= quuz(&p);
0129     }
0130 
0131     return result ? 1 : 0;
0132 }