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