1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
5 #include <linux/errno.h>
7 #include <bpf/bpf_helpers.h>
10 const volatile __s64 exp_empty_zero = 0 + 1;
13 SEC("raw_tp/sys_enter")
14 int num_empty_zero(const void *ctx)
18 bpf_for(i, 0, 0) sum += i;
19 res_empty_zero = 1 + sum;
24 const volatile __s64 exp_empty_int_min = 0 + 2;
25 __s64 res_empty_int_min;
27 SEC("raw_tp/sys_enter")
28 int num_empty_int_min(const void *ctx)
32 bpf_for(i, INT_MIN, INT_MIN) sum += i;
33 res_empty_int_min = 2 + sum;
38 const volatile __s64 exp_empty_int_max = 0 + 3;
39 __s64 res_empty_int_max;
41 SEC("raw_tp/sys_enter")
42 int num_empty_int_max(const void *ctx)
46 bpf_for(i, INT_MAX, INT_MAX) sum += i;
47 res_empty_int_max = 3 + sum;
52 const volatile __s64 exp_empty_minus_one = 0 + 4;
53 __s64 res_empty_minus_one;
55 SEC("raw_tp/sys_enter")
56 int num_empty_minus_one(const void *ctx)
60 bpf_for(i, -1, -1) sum += i;
61 res_empty_minus_one = 4 + sum;
66 const volatile __s64 exp_simple_sum = 9 * 10 / 2;
69 SEC("raw_tp/sys_enter")
70 int num_simple_sum(const void *ctx)
74 bpf_for(i, 0, 10) sum += i;
80 const volatile __s64 exp_neg_sum = -11 * 10 / 2;
83 SEC("raw_tp/sys_enter")
84 int num_neg_sum(const void *ctx)
88 bpf_for(i, -10, 0) sum += i;
94 const volatile __s64 exp_very_neg_sum = INT_MIN + (__s64)(INT_MIN + 1);
95 __s64 res_very_neg_sum;
97 SEC("raw_tp/sys_enter")
98 int num_very_neg_sum(const void *ctx)
102 bpf_for(i, INT_MIN, INT_MIN + 2) sum += i;
103 res_very_neg_sum = sum;
108 const volatile __s64 exp_very_big_sum = (__s64)(INT_MAX - 1) + (__s64)(INT_MAX - 2);
109 __s64 res_very_big_sum;
111 SEC("raw_tp/sys_enter")
112 int num_very_big_sum(const void *ctx)
116 bpf_for(i, INT_MAX - 2, INT_MAX) sum += i;
117 res_very_big_sum = sum;
122 const volatile __s64 exp_neg_pos_sum = -3;
123 __s64 res_neg_pos_sum;
125 SEC("raw_tp/sys_enter")
126 int num_neg_pos_sum(const void *ctx)
130 bpf_for(i, -3, 3) sum += i;
131 res_neg_pos_sum = sum;
136 const volatile __s64 exp_invalid_range = -EINVAL;
137 __s64 res_invalid_range;
139 SEC("raw_tp/sys_enter")
140 int num_invalid_range(const void *ctx)
142 struct bpf_iter_num it;
144 res_invalid_range = bpf_iter_num_new(&it, 1, 0);
145 bpf_iter_num_destroy(&it);
150 const volatile __s64 exp_max_range = 0 + 10;
153 SEC("raw_tp/sys_enter")
154 int num_max_range(const void *ctx)
156 struct bpf_iter_num it;
158 res_max_range = 10 + bpf_iter_num_new(&it, 0, BPF_MAX_LOOPS);
159 bpf_iter_num_destroy(&it);
164 const volatile __s64 exp_e2big_range = -E2BIG;
165 __s64 res_e2big_range;
167 SEC("raw_tp/sys_enter")
168 int num_e2big_range(const void *ctx)
170 struct bpf_iter_num it;
172 res_e2big_range = bpf_iter_num_new(&it, -1, BPF_MAX_LOOPS);
173 bpf_iter_num_destroy(&it);
178 const volatile __s64 exp_succ_elem_cnt = 10;
179 __s64 res_succ_elem_cnt;
181 SEC("raw_tp/sys_enter")
182 int num_succ_elem_cnt(const void *ctx)
184 struct bpf_iter_num it;
187 bpf_iter_num_new(&it, 0, 10);
188 while ((v = bpf_iter_num_next(&it))) {
191 bpf_iter_num_destroy(&it);
193 res_succ_elem_cnt = cnt;
198 const volatile __s64 exp_overfetched_elem_cnt = 5;
199 __s64 res_overfetched_elem_cnt;
201 SEC("raw_tp/sys_enter")
202 int num_overfetched_elem_cnt(const void *ctx)
204 struct bpf_iter_num it;
207 bpf_iter_num_new(&it, 0, 5);
208 for (i = 0; i < 10; i++) {
209 v = bpf_iter_num_next(&it);
213 bpf_iter_num_destroy(&it);
215 res_overfetched_elem_cnt = cnt;
220 const volatile __s64 exp_fail_elem_cnt = 20 + 0;
221 __s64 res_fail_elem_cnt;
223 SEC("raw_tp/sys_enter")
224 int num_fail_elem_cnt(const void *ctx)
226 struct bpf_iter_num it;
229 bpf_iter_num_new(&it, 100, 10);
230 for (i = 0; i < 10; i++) {
231 v = bpf_iter_num_next(&it);
235 bpf_iter_num_destroy(&it);
237 res_fail_elem_cnt = 20 + cnt;
242 char _license[] SEC("license") = "GPL";