summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/kfunc_call_test.c
blob: 8e6560c31e781f5d33ca11999daaca79e8fa2b92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

static struct bpf_spin_lock kfunc_call_lock SEC(".data.A");

SEC("tc")
int kfunc_call_test_spin_lock_safe(struct __sk_buff *skb)
{
	bpf_spin_lock(&kfunc_call_lock);
	bpf_testmod_test_mod_kfunc(42);
	bpf_spin_unlock(&kfunc_call_lock);

	return 0;
}

SEC("tc")
int kfunc_call_test5(struct __sk_buff *skb)
{
	struct bpf_sock *sk = skb->sk;
	int ret;
	u32 val32;
	u16 val16;
	u8 val8;

	if (!sk)
		return -1;

	sk = bpf_sk_fullsock(sk);
	if (!sk)
		return -1;

	/*
	 * Test with constant values to verify zero-extension.
	 * ISA-dependent BPF asm:
	 *   With ALU32:    w1 = 0xFF; w2 = 0xFFFF; w3 = 0xFFFFffff
	 *   Without ALU32: r1 = 0xFF; r2 = 0xFFFF; r3 = 0xFFFFffff
	 * Both zero-extend to 64-bit before the kfunc call.
	 */
	ret = bpf_kfunc_call_test5(0xFF, 0xFFFF, 0xFFFFffffULL);
	if (ret)
		return ret;

	val32 = bpf_get_prandom_u32();
	val16 = val32 & 0xFFFF;
	val8 = val32 & 0xFF;
	ret = bpf_kfunc_call_test5(val8, val16, val32);
	if (ret)
		return ret;

	/*
	 * Test multiplication with different operand sizes:
	 *
	 * val8 * 0xFF:
	 *   - Both operands promote to int (32-bit signed)
	 *   - Result: 32-bit multiplication, truncated to u8, then zero-extended
	 *
	 * val16 * 0xFFFF:
	 *   - Both operands promote to int (32-bit signed)
	 *   - Result: 32-bit multiplication, truncated to u16, then zero-extended
	 *
	 * val32 * 0xFFFFffffULL:
	 *   - val32 (u32) promotes to unsigned long long (due to ULL suffix)
	 *   - Result: 64-bit unsigned multiplication, truncated to u32, then zero-extended
	 */
	ret = bpf_kfunc_call_test5(val8 * 0xFF, val16 * 0xFFFF, val32 * 0xFFFFffffULL);
	if (ret)
		return ret;

	return 0;
}

/*
 * Assembly version testing the multiplication edge case explicitly.
 * This ensures consistent testing across different ISA versions.
 */
SEC("tc")
__naked int kfunc_call_test5_asm(void)
{
	asm volatile (
		/* Get a random u32 value */
		"call %[bpf_get_prandom_u32];"
		"r6 = r0;"              /* Save val32 in r6 */

		/* Prepare first argument: val8 * 0xFF */
		"r1 = r6;"
		"r1 &= 0xFF;"           /* val8 = val32 & 0xFF */
		"r7 = 0xFF;"
		"r1 *= r7;"             /* 64-bit mult: r1 = r1 * r7 */

		/* Prepare second argument: val16 * 0xFFFF */
		"r2 = r6;"
		"r2 &= 0xFFFF;"         /* val16 = val32 & 0xFFFF */
		"r7 = 0xFFFF;"
		"r2 *= r7;"             /* 64-bit mult: r2 = r2 * r7 */

		/* Prepare third argument: val32 * 0xFFFFffff */
		"r3 = r6;"              /* val32 */
		"r7 = 0xFFFFffff;"
		"r3 *= r7;"             /* 64-bit mult: r3 = r3 * r7 */

		/* Call kfunc with multiplication results */
		"call bpf_kfunc_call_test5;"

		/* Check return value */
		"if r0 != 0 goto exit_%=;"
		"r0 = 0;"
		"exit_%=: exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

SEC("tc")
int kfunc_call_test4(struct __sk_buff *skb)
{
	struct bpf_sock *sk = skb->sk;
	long tmp;

	if (!sk)
		return -1;

	sk = bpf_sk_fullsock(sk);
	if (!sk)
		return -1;

	tmp = bpf_kfunc_call_test4(-3, -30, -200, -1000);
	return (tmp >> 32) + tmp;
}

SEC("tc")
int kfunc_call_test2(struct __sk_buff *skb)
{
	struct bpf_sock *sk = skb->sk;

	if (!sk)
		return -1;

	sk = bpf_sk_fullsock(sk);
	if (!sk)
		return -1;

	return bpf_kfunc_call_test2((struct sock *)sk, 1, 2);
}

SEC("tc")
int kfunc_call_test1(struct __sk_buff *skb)
{
	struct bpf_sock *sk = skb->sk;
	__u64 a = 1ULL << 32;
	__u32 ret;

	if (!sk)
		return -1;

	sk = bpf_sk_fullsock(sk);
	if (!sk)
		return -1;

	a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4);
	ret = a >> 32;   /* ret should be 2 */
	ret += (__u32)a; /* ret should be 12 */

	return ret;
}

SEC("tc")
int kfunc_call_test_ref_btf_id(struct __sk_buff *skb)
{
	struct prog_test_ref_kfunc *pt;
	unsigned long s = 0;
	int ret = 0;

	pt = bpf_kfunc_call_test_acquire(&s);
	if (pt) {
		if (pt->a != 42 || pt->b != 108)
			ret = -1;
		bpf_kfunc_call_test_release(pt);
	}
	return ret;
}

SEC("tc")
int kfunc_call_test_pass(struct __sk_buff *skb)
{
	struct prog_test_pass1 p1 = {};
	struct prog_test_pass2 p2 = {};
	short a = 0;
	__u64 b = 0;
	long c = 0;
	char d = 0;
	int e = 0;

	bpf_kfunc_call_test_pass_ctx(skb);
	bpf_kfunc_call_test_pass1(&p1);
	bpf_kfunc_call_test_pass2(&p2);

	bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a));
	bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b));
	bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c));
	bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d));
	bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e));
	bpf_kfunc_call_test_mem_len_fail2(&b, -1);

	return 0;
}

struct syscall_test_args {
	__u8 data[16];
	size_t size;
};

SEC("syscall")
int kfunc_syscall_test(struct syscall_test_args *args)
{
	const long size = args->size;

	if (size > sizeof(args->data))
		return -7; /* -E2BIG */

	bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(args->data));
	bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(*args));
	bpf_kfunc_call_test_mem_len_pass1(&args->data, size);

	return 0;
}

SEC("syscall")
int kfunc_syscall_test_null(struct syscall_test_args *args)
{
	/* Must be called with args as a NULL pointer
	 * we do not check for it to have the verifier consider that
	 * the pointer might not be null, and so we can load it.
	 *
	 * So the following can not be added:
	 *
	 * if (args)
	 *      return -22;
	 */

	bpf_kfunc_call_test_mem_len_pass1(args, 0);

	return 0;
}

SEC("tc")
int kfunc_call_test_get_mem(struct __sk_buff *skb)
{
	struct prog_test_ref_kfunc *pt;
	unsigned long s = 0;
	int *p = NULL;
	int ret = 0;

	pt = bpf_kfunc_call_test_acquire(&s);
	if (pt) {
		p = bpf_kfunc_call_test_get_rdwr_mem(pt, 2 * sizeof(int));
		if (p) {
			p[0] = 42;
			ret = p[1]; /* 108 */
		} else {
			ret = -1;
		}

		if (ret >= 0) {
			p = bpf_kfunc_call_test_get_rdonly_mem(pt, 2 * sizeof(int));
			if (p)
				ret = p[0]; /* 42 */
			else
				ret = -1;
		}

		bpf_kfunc_call_test_release(pt);
	}
	return ret;
}

SEC("tc")
int kfunc_call_test_static_unused_arg(struct __sk_buff *skb)
{

	u32 expected = 5, actual;

	actual = bpf_kfunc_call_test_static_unused_arg(expected, 0xdeadbeef);
	return actual != expected ? -1 : 0;
}

struct ctx_val {
	struct bpf_testmod_ctx __kptr *ctx;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, struct ctx_val);
} ctx_map SEC(".maps");

SEC("tc")
int kfunc_call_ctx(struct __sk_buff *skb)
{
	struct bpf_testmod_ctx *ctx;
	int err = 0;

	ctx = bpf_testmod_ctx_create(&err);
	if (!ctx && !err)
		err = -1;
	if (ctx) {
		int key = 0;
		struct ctx_val *ctx_val = bpf_map_lookup_elem(&ctx_map, &key);

		/* Transfer ctx to map to be freed via implicit dtor call
		 * on cleanup.
		 */
		if (ctx_val)
			ctx = bpf_kptr_xchg(&ctx_val->ctx, ctx);
		if (ctx) {
			bpf_testmod_ctx_release(ctx);
			err = -1;
		}
	}
	return err;
}

char _license[] SEC("license") = "GPL";