1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3
4#include <limits.h>
5#include <linux/errno.h>
6#include "vmlinux.h"
7#include <bpf/bpf_helpers.h>
8#include "bpf_misc.h"
9
10const volatile __s64 exp_empty_zero = 0 + 1;
11__s64 res_empty_zero;
12
13SEC("raw_tp/sys_enter")
14int num_empty_zero(const void *ctx)
15{
16	__s64 sum = 0, i;
17
18	bpf_for(i, 0, 0) sum += i;
19	res_empty_zero = 1 + sum;
20
21	return 0;
22}
23
24const volatile __s64 exp_empty_int_min = 0 + 2;
25__s64 res_empty_int_min;
26
27SEC("raw_tp/sys_enter")
28int num_empty_int_min(const void *ctx)
29{
30	__s64 sum = 0, i;
31
32	bpf_for(i, INT_MIN, INT_MIN) sum += i;
33	res_empty_int_min = 2 + sum;
34
35	return 0;
36}
37
38const volatile __s64 exp_empty_int_max = 0 + 3;
39__s64 res_empty_int_max;
40
41SEC("raw_tp/sys_enter")
42int num_empty_int_max(const void *ctx)
43{
44	__s64 sum = 0, i;
45
46	bpf_for(i, INT_MAX, INT_MAX) sum += i;
47	res_empty_int_max = 3 + sum;
48
49	return 0;
50}
51
52const volatile __s64 exp_empty_minus_one = 0 + 4;
53__s64 res_empty_minus_one;
54
55SEC("raw_tp/sys_enter")
56int num_empty_minus_one(const void *ctx)
57{
58	__s64 sum = 0, i;
59
60	bpf_for(i, -1, -1) sum += i;
61	res_empty_minus_one = 4 + sum;
62
63	return 0;
64}
65
66const volatile __s64 exp_simple_sum = 9 * 10 / 2;
67__s64 res_simple_sum;
68
69SEC("raw_tp/sys_enter")
70int num_simple_sum(const void *ctx)
71{
72	__s64 sum = 0, i;
73
74	bpf_for(i, 0, 10) sum += i;
75	res_simple_sum = sum;
76
77	return 0;
78}
79
80const volatile __s64 exp_neg_sum = -11 * 10 / 2;
81__s64 res_neg_sum;
82
83SEC("raw_tp/sys_enter")
84int num_neg_sum(const void *ctx)
85{
86	__s64 sum = 0, i;
87
88	bpf_for(i, -10, 0) sum += i;
89	res_neg_sum = sum;
90
91	return 0;
92}
93
94const volatile __s64 exp_very_neg_sum = INT_MIN + (__s64)(INT_MIN + 1);
95__s64 res_very_neg_sum;
96
97SEC("raw_tp/sys_enter")
98int num_very_neg_sum(const void *ctx)
99{
100	__s64 sum = 0, i;
101
102	bpf_for(i, INT_MIN, INT_MIN + 2) sum += i;
103	res_very_neg_sum = sum;
104
105	return 0;
106}
107
108const volatile __s64 exp_very_big_sum = (__s64)(INT_MAX - 1) + (__s64)(INT_MAX - 2);
109__s64 res_very_big_sum;
110
111SEC("raw_tp/sys_enter")
112int num_very_big_sum(const void *ctx)
113{
114	__s64 sum = 0, i;
115
116	bpf_for(i, INT_MAX - 2, INT_MAX) sum += i;
117	res_very_big_sum = sum;
118
119	return 0;
120}
121
122const volatile __s64 exp_neg_pos_sum = -3;
123__s64 res_neg_pos_sum;
124
125SEC("raw_tp/sys_enter")
126int num_neg_pos_sum(const void *ctx)
127{
128	__s64 sum = 0, i;
129
130	bpf_for(i, -3, 3) sum += i;
131	res_neg_pos_sum = sum;
132
133	return 0;
134}
135
136const volatile __s64 exp_invalid_range = -EINVAL;
137__s64 res_invalid_range;
138
139SEC("raw_tp/sys_enter")
140int num_invalid_range(const void *ctx)
141{
142	struct bpf_iter_num it;
143
144	res_invalid_range = bpf_iter_num_new(&it, 1, 0);
145	bpf_iter_num_destroy(&it);
146
147	return 0;
148}
149
150const volatile __s64 exp_max_range = 0 + 10;
151__s64 res_max_range;
152
153SEC("raw_tp/sys_enter")
154int num_max_range(const void *ctx)
155{
156	struct bpf_iter_num it;
157
158	res_max_range = 10 + bpf_iter_num_new(&it, 0, BPF_MAX_LOOPS);
159	bpf_iter_num_destroy(&it);
160
161	return 0;
162}
163
164const volatile __s64 exp_e2big_range = -E2BIG;
165__s64 res_e2big_range;
166
167SEC("raw_tp/sys_enter")
168int num_e2big_range(const void *ctx)
169{
170	struct bpf_iter_num it;
171
172	res_e2big_range = bpf_iter_num_new(&it, -1, BPF_MAX_LOOPS);
173	bpf_iter_num_destroy(&it);
174
175	return 0;
176}
177
178const volatile __s64 exp_succ_elem_cnt = 10;
179__s64 res_succ_elem_cnt;
180
181SEC("raw_tp/sys_enter")
182int num_succ_elem_cnt(const void *ctx)
183{
184	struct bpf_iter_num it;
185	int cnt = 0, *v;
186
187	bpf_iter_num_new(&it, 0, 10);
188	while ((v = bpf_iter_num_next(&it))) {
189		cnt++;
190	}
191	bpf_iter_num_destroy(&it);
192
193	res_succ_elem_cnt = cnt;
194
195	return 0;
196}
197
198const volatile __s64 exp_overfetched_elem_cnt = 5;
199__s64 res_overfetched_elem_cnt;
200
201SEC("raw_tp/sys_enter")
202int num_overfetched_elem_cnt(const void *ctx)
203{
204	struct bpf_iter_num it;
205	int cnt = 0, *v, i;
206
207	bpf_iter_num_new(&it, 0, 5);
208	for (i = 0; i < 10; i++) {
209		v = bpf_iter_num_next(&it);
210		if (v)
211			cnt++;
212	}
213	bpf_iter_num_destroy(&it);
214
215	res_overfetched_elem_cnt = cnt;
216
217	return 0;
218}
219
220const volatile __s64 exp_fail_elem_cnt = 20 + 0;
221__s64 res_fail_elem_cnt;
222
223SEC("raw_tp/sys_enter")
224int num_fail_elem_cnt(const void *ctx)
225{
226	struct bpf_iter_num it;
227	int cnt = 0, *v, i;
228
229	bpf_iter_num_new(&it, 100, 10);
230	for (i = 0; i < 10; i++) {
231		v = bpf_iter_num_next(&it);
232		if (v)
233			cnt++;
234	}
235	bpf_iter_num_destroy(&it);
236
237	res_fail_elem_cnt = 20 + cnt;
238
239	return 0;
240}
241
242char _license[] SEC("license") = "GPL";
243