1 // SPDX-License-Identifier: GPL-2.0
2 #include <regex.h>
3 #include <test_progs.h>
4 #include <network_helpers.h>
5 
6 #include "test_spin_lock.skel.h"
7 #include "test_spin_lock_fail.skel.h"
8 
9 static char log_buf[1024 * 1024];
10 
11 static struct {
12 	const char *prog_name;
13 	const char *err_msg;
14 } spin_lock_fail_tests[] = {
15 	{ "lock_id_kptr_preserve",
16 	  "5: (bf) r1 = r0                       ; R0_w=ptr_foo(id=2,ref_obj_id=2,off=0,imm=0) "
17 	  "R1_w=ptr_foo(id=2,ref_obj_id=2,off=0,imm=0) refs=2\n6: (85) call bpf_this_cpu_ptr#154\n"
18 	  "R1 type=ptr_ expected=percpu_ptr_" },
19 	{ "lock_id_global_zero",
20 	  "; R1_w=map_value(off=0,ks=4,vs=4,imm=0)\n2: (85) call bpf_this_cpu_ptr#154\n"
21 	  "R1 type=map_value expected=percpu_ptr_" },
22 	{ "lock_id_mapval_preserve",
23 	  "[0-9]\\+: (bf) r1 = r0                       ;"
24 	  " R0_w=map_value(id=1,off=0,ks=4,vs=8,imm=0)"
25 	  " R1_w=map_value(id=1,off=0,ks=4,vs=8,imm=0)\n"
26 	  "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n"
27 	  "R1 type=map_value expected=percpu_ptr_" },
28 	{ "lock_id_innermapval_preserve",
29 	  "[0-9]\\+: (bf) r1 = r0                      ;"
30 	  " R0=map_value(id=2,off=0,ks=4,vs=8,imm=0)"
31 	  " R1_w=map_value(id=2,off=0,ks=4,vs=8,imm=0)\n"
32 	  "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n"
33 	  "R1 type=map_value expected=percpu_ptr_" },
34 	{ "lock_id_mismatch_kptr_kptr", "bpf_spin_unlock of different lock" },
35 	{ "lock_id_mismatch_kptr_global", "bpf_spin_unlock of different lock" },
36 	{ "lock_id_mismatch_kptr_mapval", "bpf_spin_unlock of different lock" },
37 	{ "lock_id_mismatch_kptr_innermapval", "bpf_spin_unlock of different lock" },
38 	{ "lock_id_mismatch_global_global", "bpf_spin_unlock of different lock" },
39 	{ "lock_id_mismatch_global_kptr", "bpf_spin_unlock of different lock" },
40 	{ "lock_id_mismatch_global_mapval", "bpf_spin_unlock of different lock" },
41 	{ "lock_id_mismatch_global_innermapval", "bpf_spin_unlock of different lock" },
42 	{ "lock_id_mismatch_mapval_mapval", "bpf_spin_unlock of different lock" },
43 	{ "lock_id_mismatch_mapval_kptr", "bpf_spin_unlock of different lock" },
44 	{ "lock_id_mismatch_mapval_global", "bpf_spin_unlock of different lock" },
45 	{ "lock_id_mismatch_mapval_innermapval", "bpf_spin_unlock of different lock" },
46 	{ "lock_id_mismatch_innermapval_innermapval1", "bpf_spin_unlock of different lock" },
47 	{ "lock_id_mismatch_innermapval_innermapval2", "bpf_spin_unlock of different lock" },
48 	{ "lock_id_mismatch_innermapval_kptr", "bpf_spin_unlock of different lock" },
49 	{ "lock_id_mismatch_innermapval_global", "bpf_spin_unlock of different lock" },
50 	{ "lock_id_mismatch_innermapval_mapval", "bpf_spin_unlock of different lock" },
51 };
52 
match_regex(const char * pattern,const char * string)53 static int match_regex(const char *pattern, const char *string)
54 {
55 	int err, rc;
56 	regex_t re;
57 
58 	err = regcomp(&re, pattern, REG_NOSUB);
59 	if (err) {
60 		char errbuf[512];
61 
62 		regerror(err, &re, errbuf, sizeof(errbuf));
63 		PRINT_FAIL("Can't compile regex: %s\n", errbuf);
64 		return -1;
65 	}
66 	rc = regexec(&re, string, 0, NULL, 0);
67 	regfree(&re);
68 	return rc == 0 ? 1 : 0;
69 }
70 
test_spin_lock_fail_prog(const char * prog_name,const char * err_msg)71 static void test_spin_lock_fail_prog(const char *prog_name, const char *err_msg)
72 {
73 	LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf,
74 						.kernel_log_size = sizeof(log_buf),
75 						.kernel_log_level = 1);
76 	struct test_spin_lock_fail *skel;
77 	struct bpf_program *prog;
78 	int ret;
79 
80 	skel = test_spin_lock_fail__open_opts(&opts);
81 	if (!ASSERT_OK_PTR(skel, "test_spin_lock_fail__open_opts"))
82 		return;
83 
84 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
85 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
86 		goto end;
87 
88 	bpf_program__set_autoload(prog, true);
89 
90 	ret = test_spin_lock_fail__load(skel);
91 	if (!ASSERT_ERR(ret, "test_spin_lock_fail__load must fail"))
92 		goto end;
93 
94 	/* Skip check if JIT does not support kfuncs */
95 	if (strstr(log_buf, "JIT does not support calling kernel function")) {
96 		test__skip();
97 		goto end;
98 	}
99 
100 	ret = match_regex(err_msg, log_buf);
101 	if (!ASSERT_GE(ret, 0, "match_regex"))
102 		goto end;
103 
104 	if (!ASSERT_TRUE(ret, "no match for expected error message")) {
105 		fprintf(stderr, "Expected: %s\n", err_msg);
106 		fprintf(stderr, "Verifier: %s\n", log_buf);
107 	}
108 
109 end:
110 	test_spin_lock_fail__destroy(skel);
111 }
112 
spin_lock_thread(void * arg)113 static void *spin_lock_thread(void *arg)
114 {
115 	int err, prog_fd = *(u32 *) arg;
116 	LIBBPF_OPTS(bpf_test_run_opts, topts,
117 		.data_in = &pkt_v4,
118 		.data_size_in = sizeof(pkt_v4),
119 		.repeat = 10000,
120 	);
121 
122 	err = bpf_prog_test_run_opts(prog_fd, &topts);
123 	ASSERT_OK(err, "test_run");
124 	ASSERT_OK(topts.retval, "test_run retval");
125 	pthread_exit(arg);
126 }
127 
test_spin_lock_success(void)128 void test_spin_lock_success(void)
129 {
130 	struct test_spin_lock *skel;
131 	pthread_t thread_id[4];
132 	int prog_fd, i;
133 	void *ret;
134 
135 	skel = test_spin_lock__open_and_load();
136 	if (!ASSERT_OK_PTR(skel, "test_spin_lock__open_and_load"))
137 		return;
138 	prog_fd = bpf_program__fd(skel->progs.bpf_spin_lock_test);
139 	for (i = 0; i < 4; i++) {
140 		int err;
141 
142 		err = pthread_create(&thread_id[i], NULL, &spin_lock_thread, &prog_fd);
143 		if (!ASSERT_OK(err, "pthread_create"))
144 			goto end;
145 	}
146 
147 	for (i = 0; i < 4; i++) {
148 		if (!ASSERT_OK(pthread_join(thread_id[i], &ret), "pthread_join"))
149 			goto end;
150 		if (!ASSERT_EQ(ret, &prog_fd, "ret == prog_fd"))
151 			goto end;
152 	}
153 end:
154 	test_spin_lock__destroy(skel);
155 }
156 
test_spin_lock(void)157 void test_spin_lock(void)
158 {
159 	int i;
160 
161 	test_spin_lock_success();
162 
163 	for (i = 0; i < ARRAY_SIZE(spin_lock_fail_tests); i++) {
164 		if (!test__start_subtest(spin_lock_fail_tests[i].prog_name))
165 			continue;
166 		test_spin_lock_fail_prog(spin_lock_fail_tests[i].prog_name,
167 					 spin_lock_fail_tests[i].err_msg);
168 	}
169 }
170