1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
3 #include <test_progs.h>
4 static int libbpf_debug_print(enum libbpf_print_level level,
5 			      const char *format, va_list args)
6 {
7 	if (level != LIBBPF_DEBUG) {
8 		vprintf(format, args);
9 		return 0;
10 	}
11 
12 	if (!strstr(format, "verifier log"))
13 		return 0;
14 	vprintf("%s", args);
15 	return 0;
16 }
17 
18 extern int extra_prog_load_log_flags;
19 
20 static int check_load(const char *file, enum bpf_prog_type type)
21 {
22 	struct bpf_prog_load_attr attr;
23 	struct bpf_object *obj = NULL;
24 	int err, prog_fd;
25 
26 	memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
27 	attr.file = file;
28 	attr.prog_type = type;
29 	attr.log_level = 4 | extra_prog_load_log_flags;
30 	attr.prog_flags = BPF_F_TEST_RND_HI32;
31 	err = bpf_prog_load_xattr(&attr, &obj, &prog_fd);
32 	bpf_object__close(obj);
33 	return err;
34 }
35 
36 struct scale_test_def {
37 	const char *file;
38 	enum bpf_prog_type attach_type;
39 	bool fails;
40 };
41 
42 void test_bpf_verif_scale(void)
43 {
44 	struct scale_test_def tests[] = {
45 		{ "loop3.o", BPF_PROG_TYPE_RAW_TRACEPOINT, true /* fails */ },
46 
47 		{ "test_verif_scale1.o", BPF_PROG_TYPE_SCHED_CLS },
48 		{ "test_verif_scale2.o", BPF_PROG_TYPE_SCHED_CLS },
49 		{ "test_verif_scale3.o", BPF_PROG_TYPE_SCHED_CLS },
50 
51 		{ "pyperf_global.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
52 		{ "pyperf_subprogs.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
53 
54 		/* full unroll by llvm */
55 		{ "pyperf50.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
56 		{ "pyperf100.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
57 		{ "pyperf180.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
58 
59 		/* partial unroll. llvm will unroll loop ~150 times.
60 		 * C loop count -> 600.
61 		 * Asm loop count -> 4.
62 		 * 16k insns in loop body.
63 		 * Total of 5 such loops. Total program size ~82k insns.
64 		 */
65 		{ "pyperf600.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
66 
67 		/* no unroll at all.
68 		 * C loop count -> 600.
69 		 * ASM loop count -> 600.
70 		 * ~110 insns in loop body.
71 		 * Total of 5 such loops. Total program size ~1500 insns.
72 		 */
73 		{ "pyperf600_nounroll.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
74 
75 		{ "loop1.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
76 		{ "loop2.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
77 		{ "loop4.o", BPF_PROG_TYPE_SCHED_CLS },
78 		{ "loop5.o", BPF_PROG_TYPE_SCHED_CLS },
79 		{ "loop6.o", BPF_PROG_TYPE_KPROBE },
80 
81 		/* partial unroll. 19k insn in a loop.
82 		 * Total program size 20.8k insn.
83 		 * ~350k processed_insns
84 		 */
85 		{ "strobemeta.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
86 
87 		/* no unroll, tiny loops */
88 		{ "strobemeta_nounroll1.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
89 		{ "strobemeta_nounroll2.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
90 
91 		/* non-inlined subprogs */
92 		{ "strobemeta_subprogs.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
93 
94 		{ "test_sysctl_loop1.o", BPF_PROG_TYPE_CGROUP_SYSCTL },
95 		{ "test_sysctl_loop2.o", BPF_PROG_TYPE_CGROUP_SYSCTL },
96 
97 		{ "test_xdp_loop.o", BPF_PROG_TYPE_XDP },
98 		{ "test_seg6_loop.o", BPF_PROG_TYPE_LWT_SEG6LOCAL },
99 	};
100 	libbpf_print_fn_t old_print_fn = NULL;
101 	int err, i;
102 
103 	if (env.verifier_stats) {
104 		test__force_log();
105 		old_print_fn = libbpf_set_print(libbpf_debug_print);
106 	}
107 
108 	for (i = 0; i < ARRAY_SIZE(tests); i++) {
109 		const struct scale_test_def *test = &tests[i];
110 
111 		if (!test__start_subtest(test->file))
112 			continue;
113 
114 		err = check_load(test->file, test->attach_type);
115 		CHECK_FAIL(err && !test->fails);
116 	}
117 
118 	if (env.verifier_stats)
119 		libbpf_set_print(old_print_fn);
120 }
121