1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
4 #include <linux/btf_ids.h>
5 #include <linux/error-injection.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/percpu-defs.h>
9 #include <linux/sysfs.h>
10 #include <linux/tracepoint.h>
11 #include "bpf_testmod.h"
12 #include "bpf_testmod_kfunc.h"
14 #define CREATE_TRACE_POINTS
15 #include "bpf_testmod-events.h"
17 typedef int (*func_proto_typedef)(long);
18 typedef int (*func_proto_typedef_nested1)(func_proto_typedef);
19 typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);
21 DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
22 long bpf_testmod_test_struct_arg_result;
24 struct bpf_testmod_struct_arg_1 {
27 struct bpf_testmod_struct_arg_2 {
32 struct bpf_testmod_struct_arg_3 {
37 struct bpf_testmod_struct_arg_4 {
43 __diag_ignore_all("-Wmissing-prototypes",
44 "Global functions as their definitions will be in bpf_testmod.ko BTF");
47 bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a, int b, int c) {
48 bpf_testmod_test_struct_arg_result = a.a + a.b + b + c;
49 return bpf_testmod_test_struct_arg_result;
53 bpf_testmod_test_struct_arg_2(int a, struct bpf_testmod_struct_arg_2 b, int c) {
54 bpf_testmod_test_struct_arg_result = a + b.a + b.b + c;
55 return bpf_testmod_test_struct_arg_result;
59 bpf_testmod_test_struct_arg_3(int a, int b, struct bpf_testmod_struct_arg_2 c) {
60 bpf_testmod_test_struct_arg_result = a + b + c.a + c.b;
61 return bpf_testmod_test_struct_arg_result;
65 bpf_testmod_test_struct_arg_4(struct bpf_testmod_struct_arg_1 a, int b,
66 int c, int d, struct bpf_testmod_struct_arg_2 e) {
67 bpf_testmod_test_struct_arg_result = a.a + b + c + d + e.a + e.b;
68 return bpf_testmod_test_struct_arg_result;
72 bpf_testmod_test_struct_arg_5(void) {
73 bpf_testmod_test_struct_arg_result = 1;
74 return bpf_testmod_test_struct_arg_result;
78 bpf_testmod_test_struct_arg_6(struct bpf_testmod_struct_arg_3 *a) {
79 bpf_testmod_test_struct_arg_result = a->b[0];
80 return bpf_testmod_test_struct_arg_result;
84 bpf_testmod_test_struct_arg_7(u64 a, void *b, short c, int d, void *e,
85 struct bpf_testmod_struct_arg_4 f)
87 bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
89 return bpf_testmod_test_struct_arg_result;
93 bpf_testmod_test_struct_arg_8(u64 a, void *b, short c, int d, void *e,
94 struct bpf_testmod_struct_arg_4 f, int g)
96 bpf_testmod_test_struct_arg_result = a + (long)b + c + d +
97 (long)e + f.a + f.b + g;
98 return bpf_testmod_test_struct_arg_result;
102 bpf_testmod_test_mod_kfunc(int i)
104 *(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
107 __bpf_kfunc int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt)
120 __bpf_kfunc s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq* it)
130 __bpf_kfunc void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it)
135 struct bpf_testmod_btf_type_tag_1 {
139 struct bpf_testmod_btf_type_tag_2 {
140 struct bpf_testmod_btf_type_tag_1 __user *p;
143 struct bpf_testmod_btf_type_tag_3 {
144 struct bpf_testmod_btf_type_tag_1 __percpu *p;
148 bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
149 BTF_TYPE_EMIT(func_proto_typedef);
150 BTF_TYPE_EMIT(func_proto_typedef_nested1);
151 BTF_TYPE_EMIT(func_proto_typedef_nested2);
156 bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
161 bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu *arg) {
166 bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
170 noinline int bpf_testmod_loop_test(int n)
172 /* Make sum volatile, so smart compilers, such as clang, will not
173 * optimize the code by removing the loop.
175 volatile int sum = 0;
178 /* the primary goal of this test is to test LBR. Create a lot of
179 * branches in the function, so we can catch it easily.
181 for (i = 0; i < n; i++)
186 __weak noinline struct file *bpf_testmod_return_ptr(int arg)
188 static struct file f = {};
191 case 1: return (void *)EINVAL; /* user addr */
192 case 2: return (void *)0xcafe4a11; /* user addr */
193 case 3: return (void *)-EINVAL; /* canonical, but invalid */
194 case 4: return (void *)(1ull << 60); /* non-canonical and invalid */
195 case 5: return (void *)~(1ull << 30); /* trigger extable */
196 case 6: return &f; /* valid addr */
197 case 7: return (void *)((long)&f | 1); /* kernel tricks */
198 default: return NULL;
202 noinline int bpf_testmod_fentry_test1(int a)
207 noinline int bpf_testmod_fentry_test2(int a, u64 b)
212 noinline int bpf_testmod_fentry_test3(char a, int b, u64 c)
217 noinline int bpf_testmod_fentry_test7(u64 a, void *b, short c, int d,
218 void *e, char f, int g)
220 return a + (long)b + c + d + (long)e + f + g;
223 noinline int bpf_testmod_fentry_test11(u64 a, void *b, short c, int d,
224 void *e, char f, int g,
225 unsigned int h, long i, __u64 j,
228 return a + (long)b + c + d + (long)e + f + g + h + i + j + k;
231 int bpf_testmod_fentry_ok;
234 bpf_testmod_test_read(struct file *file, struct kobject *kobj,
235 struct bin_attribute *bin_attr,
236 char *buf, loff_t off, size_t len)
238 struct bpf_testmod_test_read_ctx ctx = {
243 struct bpf_testmod_struct_arg_1 struct_arg1 = {10};
244 struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
245 struct bpf_testmod_struct_arg_3 *struct_arg3;
246 struct bpf_testmod_struct_arg_4 struct_arg4 = {21, 22};
249 while (bpf_testmod_return_ptr(i))
252 (void)bpf_testmod_test_struct_arg_1(struct_arg2, 1, 4);
253 (void)bpf_testmod_test_struct_arg_2(1, struct_arg2, 4);
254 (void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
255 (void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
256 (void)bpf_testmod_test_struct_arg_5();
257 (void)bpf_testmod_test_struct_arg_7(16, (void *)17, 18, 19,
258 (void *)20, struct_arg4);
259 (void)bpf_testmod_test_struct_arg_8(16, (void *)17, 18, 19,
260 (void *)20, struct_arg4, 23);
263 struct_arg3 = kmalloc((sizeof(struct bpf_testmod_struct_arg_3) +
264 sizeof(int)), GFP_KERNEL);
265 if (struct_arg3 != NULL) {
266 struct_arg3->b[0] = 1;
267 (void)bpf_testmod_test_struct_arg_6(struct_arg3);
271 /* This is always true. Use the check to make sure the compiler
272 * doesn't remove bpf_testmod_loop_test.
274 if (bpf_testmod_loop_test(101) > 100)
275 trace_bpf_testmod_test_read(current, &ctx);
277 /* Magic number to enable writable tp */
279 struct bpf_testmod_test_writable_ctx writable = {
282 trace_bpf_testmod_test_writable_bare(&writable);
283 if (writable.early_ret)
284 return snprintf(buf, len, "%d\n", writable.val);
287 if (bpf_testmod_fentry_test1(1) != 2 ||
288 bpf_testmod_fentry_test2(2, 3) != 5 ||
289 bpf_testmod_fentry_test3(4, 5, 6) != 15 ||
290 bpf_testmod_fentry_test7(16, (void *)17, 18, 19, (void *)20,
292 bpf_testmod_fentry_test11(16, (void *)17, 18, 19, (void *)20,
293 21, 22, 23, 24, 25, 26) != 231)
296 bpf_testmod_fentry_ok = 1;
298 return -EIO; /* always fail */
300 EXPORT_SYMBOL(bpf_testmod_test_read);
301 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
304 bpf_testmod_test_write(struct file *file, struct kobject *kobj,
305 struct bin_attribute *bin_attr,
306 char *buf, loff_t off, size_t len)
308 struct bpf_testmod_test_write_ctx ctx = {
314 trace_bpf_testmod_test_write_bare(current, &ctx);
316 return -EIO; /* always fail */
318 EXPORT_SYMBOL(bpf_testmod_test_write);
319 ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
321 noinline int bpf_fentry_shadow_test(int a)
325 EXPORT_SYMBOL_GPL(bpf_fentry_shadow_test);
329 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
330 .attr = { .name = "bpf_testmod", .mode = 0666, },
331 .read = bpf_testmod_test_read,
332 .write = bpf_testmod_test_write,
335 BTF_SET8_START(bpf_testmod_common_kfunc_ids)
336 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
337 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
338 BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
339 BTF_SET8_END(bpf_testmod_common_kfunc_ids)
341 static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {
342 .owner = THIS_MODULE,
343 .set = &bpf_testmod_common_kfunc_ids,
346 __bpf_kfunc u64 bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d)
348 return a + b + c + d;
351 __bpf_kfunc int bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b)
356 __bpf_kfunc struct sock *bpf_kfunc_call_test3(struct sock *sk)
361 __bpf_kfunc long noinline bpf_kfunc_call_test4(signed char a, short b, int c, long d)
363 /* Provoke the compiler to assume that the caller has sign-extended a,
364 * b and c on platforms where this is required (e.g. s390x).
366 return (long)a + (long)b + (long)c + d;
369 static struct prog_test_ref_kfunc prog_test_struct = {
372 .next = &prog_test_struct,
373 .cnt = REFCOUNT_INIT(1),
376 __bpf_kfunc struct prog_test_ref_kfunc *
377 bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr)
379 refcount_inc(&prog_test_struct.cnt);
380 return &prog_test_struct;
383 __bpf_kfunc void bpf_kfunc_call_test_offset(struct prog_test_ref_kfunc *p)
388 __bpf_kfunc struct prog_test_member *
389 bpf_kfunc_call_memb_acquire(void)
395 __bpf_kfunc void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p)
400 static int *__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc *p, const int size)
402 if (size > 2 * sizeof(int))
408 __bpf_kfunc int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p,
409 const int rdwr_buf_size)
411 return __bpf_kfunc_call_test_get_mem(p, rdwr_buf_size);
414 __bpf_kfunc int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p,
415 const int rdonly_buf_size)
417 return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
420 /* the next 2 ones can't be really used for testing expect to ensure
421 * that the verifier rejects the call.
422 * Acquire functions must return struct pointers, so these ones are
425 __bpf_kfunc int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p,
426 const int rdonly_buf_size)
428 return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
431 __bpf_kfunc void bpf_kfunc_call_int_mem_release(int *p)
435 __bpf_kfunc void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb)
439 __bpf_kfunc void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p)
443 __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)
447 __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p)
451 __bpf_kfunc void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p)
455 __bpf_kfunc void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p)
459 __bpf_kfunc void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz)
463 __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len)
467 __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len)
471 __bpf_kfunc void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
473 /* p != NULL, but p->cnt could be 0 */
476 __bpf_kfunc void bpf_kfunc_call_test_destructive(void)
480 __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused)
485 BTF_SET8_START(bpf_testmod_check_kfunc_ids)
486 BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
487 BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
488 BTF_ID_FLAGS(func, bpf_kfunc_call_test2)
489 BTF_ID_FLAGS(func, bpf_kfunc_call_test3)
490 BTF_ID_FLAGS(func, bpf_kfunc_call_test4)
491 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
492 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
493 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
494 BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL)
495 BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL)
496 BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE)
497 BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdwr_mem, KF_RET_NULL)
498 BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdonly_mem, KF_RET_NULL)
499 BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_rdonly_mem, KF_ACQUIRE | KF_RET_NULL)
500 BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE)
501 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx)
502 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1)
503 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2)
504 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1)
505 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)
506 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3)
507 BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS | KF_RCU)
508 BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
509 BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
510 BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset)
511 BTF_SET8_END(bpf_testmod_check_kfunc_ids)
513 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
514 .owner = THIS_MODULE,
515 .set = &bpf_testmod_check_kfunc_ids,
518 extern int bpf_fentry_test1(int a);
520 static int bpf_testmod_init(void)
524 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_testmod_common_kfunc_set);
525 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
526 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_testmod_kfunc_set);
527 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_testmod_kfunc_set);
530 if (bpf_fentry_test1(0) < 0)
532 return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
535 static void bpf_testmod_exit(void)
537 return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
540 module_init(bpf_testmod_init);
541 module_exit(bpf_testmod_exit);
543 MODULE_AUTHOR("Andrii Nakryiko");
544 MODULE_DESCRIPTION("BPF selftests module");
545 MODULE_LICENSE("Dual BSD/GPL");