1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_core_read.h>
9 char _license[] SEC("license") = "GPL";
11 /* fields of exactly the same size */
12 struct test_struct___samesize {
14 unsigned long long val1;
18 } __attribute((preserve_access_index));
20 /* unsigned fields that have to be downsized by libbpf */
21 struct test_struct___downsize {
28 } __attribute__((preserve_access_index));
30 /* fields with signed integers of wrong size, should be rejected */
31 struct test_struct___signed {
37 } __attribute((preserve_access_index));
39 /* real layout and sizes according to test's (32-bit) BTF */
40 struct test_struct___real {
41 unsigned int ptr; /* can't use `void *`, it is always 8 byte in BPF target */
43 unsigned long long val1;
50 struct test_struct___real input = {
52 .val1 = 0x1020304050607080,
56 ._pad = 0xff, /* make sure no accidental zeros are present */
59 unsigned long long ptr_samesized = 0;
60 unsigned long long val1_samesized = 0;
61 unsigned long long val2_samesized = 0;
62 unsigned long long val3_samesized = 0;
63 unsigned long long val4_samesized = 0;
64 struct test_struct___real output_samesized = {};
66 unsigned long long ptr_downsized = 0;
67 unsigned long long val1_downsized = 0;
68 unsigned long long val2_downsized = 0;
69 unsigned long long val3_downsized = 0;
70 unsigned long long val4_downsized = 0;
71 struct test_struct___real output_downsized = {};
73 unsigned long long ptr_probed = 0;
74 unsigned long long val1_probed = 0;
75 unsigned long long val2_probed = 0;
76 unsigned long long val3_probed = 0;
77 unsigned long long val4_probed = 0;
79 unsigned long long ptr_signed = 0;
80 unsigned long long val1_signed = 0;
81 unsigned long long val2_signed = 0;
82 unsigned long long val3_signed = 0;
83 unsigned long long val4_signed = 0;
84 struct test_struct___real output_signed = {};
86 SEC("raw_tp/sys_exit")
87 int handle_samesize(void *ctx)
89 struct test_struct___samesize *in = (void *)&input;
90 struct test_struct___samesize *out = (void *)&output_samesized;
92 ptr_samesized = (unsigned long long)in->ptr;
93 val1_samesized = in->val1;
94 val2_samesized = in->val2;
95 val3_samesized = in->val3;
96 val4_samesized = in->val4;
100 out->val2 = in->val2;
101 out->val3 = in->val3;
102 out->val4 = in->val4;
107 SEC("raw_tp/sys_exit")
108 int handle_downsize(void *ctx)
110 struct test_struct___downsize *in = (void *)&input;
111 struct test_struct___downsize *out = (void *)&output_downsized;
113 ptr_downsized = (unsigned long long)in->ptr;
114 val1_downsized = in->val1;
115 val2_downsized = in->val2;
116 val3_downsized = in->val3;
117 val4_downsized = in->val4;
120 out->val1 = in->val1;
121 out->val2 = in->val2;
122 out->val3 = in->val3;
123 out->val4 = in->val4;
128 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
129 #define bpf_core_read_int bpf_core_read
131 #define bpf_core_read_int(dst, sz, src) ({ \
132 /* Prevent "subtraction from stack pointer prohibited" */ \
133 volatile long __off = sizeof(*dst) - (sz); \
134 bpf_core_read((char *)(dst) + __off, sz, src); \
138 SEC("raw_tp/sys_enter")
139 int handle_probed(void *ctx)
141 struct test_struct___downsize *in = (void *)&input;
145 bpf_core_read_int(&tmp, bpf_core_field_size(in->ptr), &in->ptr);
149 bpf_core_read_int(&tmp, bpf_core_field_size(in->val1), &in->val1);
153 bpf_core_read_int(&tmp, bpf_core_field_size(in->val2), &in->val2);
157 bpf_core_read_int(&tmp, bpf_core_field_size(in->val3), &in->val3);
161 bpf_core_read_int(&tmp, bpf_core_field_size(in->val4), &in->val4);
167 SEC("raw_tp/sys_enter")
168 int handle_signed(void *ctx)
170 struct test_struct___signed *in = (void *)&input;
171 struct test_struct___signed *out = (void *)&output_signed;
173 val2_signed = in->val2;
174 val3_signed = in->val3;
175 val4_signed = in->val4;