Lines Matching +full:self +full:- +full:test

1 // SPDX-License-Identifier: GPL-2.0-only
90 /* If specified, test engine looks for this sequence of
92 * test rewrites applied by verifier. Use values
94 * fields if content does not matter. The test case fails if
97 * The sequence could be split into sub-sequences by adding
98 * SKIP_INSNS instruction at the end of each sub-sequence. In
99 * such case sub-sequences are searched for one after another.
102 /* If specified, test engine applies same pattern matching
104 * matched test case is marked as failed.
132 * Can be a tab-separated sequence of expected strings. An empty string
147 void (*fill_helper)(struct bpf_test *self);
188 static void bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self) in bpf_fill_ld_abs_vlan_push_pop() argument
190 /* test: {skb->data[0], vlan_push} x 51 + {skb->data[0], vlan_pop} x 51 */ in bpf_fill_ld_abs_vlan_push_pop()
193 unsigned int len = (1 << 15) - PUSH_CNT * 2 * 5 * 6; in bpf_fill_ld_abs_vlan_push_pop()
194 struct bpf_insn *insn = self->fill_insns; in bpf_fill_ld_abs_vlan_push_pop()
202 insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3); in bpf_fill_ld_abs_vlan_push_pop()
209 insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3); in bpf_fill_ld_abs_vlan_push_pop()
215 insn[i] = BPF_JMP32_IMM(BPF_JNE, BPF_REG_0, 0x34, len - i - 3); in bpf_fill_ld_abs_vlan_push_pop()
220 insn[i] = BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, len - i - 3); in bpf_fill_ld_abs_vlan_push_pop()
226 for (; i < len - 3; i++) in bpf_fill_ld_abs_vlan_push_pop()
228 insn[len - 3] = BPF_JMP_A(1); in bpf_fill_ld_abs_vlan_push_pop()
230 insn[len - 2] = BPF_MOV32_IMM(BPF_REG_0, 0); in bpf_fill_ld_abs_vlan_push_pop()
231 insn[len - 1] = BPF_EXIT_INSN(); in bpf_fill_ld_abs_vlan_push_pop()
232 self->prog_len = len; in bpf_fill_ld_abs_vlan_push_pop()
235 static void bpf_fill_jump_around_ld_abs(struct bpf_test *self) in bpf_fill_jump_around_ld_abs() argument
237 struct bpf_insn *insn = self->fill_insns; in bpf_fill_jump_around_ld_abs()
249 insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 10, len - i - 2); in bpf_fill_jump_around_ld_abs()
251 while (i < len - 1) in bpf_fill_jump_around_ld_abs()
254 self->prog_len = i + 1; in bpf_fill_jump_around_ld_abs()
257 static void bpf_fill_rand_ld_dw(struct bpf_test *self) in bpf_fill_rand_ld_dw() argument
259 struct bpf_insn *insn = self->fill_insns; in bpf_fill_rand_ld_dw()
264 while (i < self->retval) { in bpf_fill_rand_ld_dw()
277 self->prog_len = i + 1; in bpf_fill_rand_ld_dw()
279 self->retval = (uint32_t)res; in bpf_fill_rand_ld_dw()
284 /* test the sequence of 8k jumps */
285 static void bpf_fill_scale1(struct bpf_test *self) in bpf_fill_scale1() argument
287 struct bpf_insn *insn = self->fill_insns; in bpf_fill_scale1()
291 /* test to check that the long sequence of jumps is acceptable */ in bpf_fill_scale1()
298 -8 * (k % 64 + 1)); in bpf_fill_scale1()
303 while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4) in bpf_fill_scale1()
306 self->prog_len = i + 1; in bpf_fill_scale1()
307 self->retval = 42; in bpf_fill_scale1()
310 /* test the sequence of 8k jumps in inner most function (function depth 8)*/
311 static void bpf_fill_scale2(struct bpf_test *self) in bpf_fill_scale2() argument
313 struct bpf_insn *insn = self->fill_insns; in bpf_fill_scale2()
322 /* test to check that the long sequence of jumps is acceptable */ in bpf_fill_scale2()
330 -8 * (k % (64 - 4 * FUNC_NEST) + 1)); in bpf_fill_scale2()
332 while (i < MAX_TEST_INSNS - MAX_JMP_SEQ * 4) in bpf_fill_scale2()
335 self->prog_len = i + 1; in bpf_fill_scale2()
336 self->retval = 42; in bpf_fill_scale2()
339 static void bpf_fill_scale(struct bpf_test *self) in bpf_fill_scale() argument
341 switch (self->retval) { in bpf_fill_scale()
343 return bpf_fill_scale1(self); in bpf_fill_scale()
345 return bpf_fill_scale2(self); in bpf_fill_scale()
347 self->prog_len = 0; in bpf_fill_scale()
360 insn[i + hlen] = BPF_JMP_A(hlen - i); in bpf_fill_torturous_jumps_insn_1()
362 insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 1); in bpf_fill_torturous_jumps_insn_1()
363 insn[len - 1] = BPF_EXIT_INSN(); in bpf_fill_torturous_jumps_insn_1()
380 insn[i + j] = BPF_JMP_A(16 - j - 1); in bpf_fill_torturous_jumps_insn_2()
384 insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 2); in bpf_fill_torturous_jumps_insn_2()
385 insn[len - 1] = BPF_EXIT_INSN(); in bpf_fill_torturous_jumps_insn_2()
390 static void bpf_fill_torturous_jumps(struct bpf_test *self) in bpf_fill_torturous_jumps() argument
392 struct bpf_insn *insn = self->fill_insns; in bpf_fill_torturous_jumps()
395 switch (self->retval) { in bpf_fill_torturous_jumps()
397 self->prog_len = bpf_fill_torturous_jumps_insn_1(insn); in bpf_fill_torturous_jumps()
400 self->prog_len = bpf_fill_torturous_jumps_insn_2(insn); in bpf_fill_torturous_jumps()
406 insn[i++] = BPF_ST_MEM(BPF_B, BPF_REG_10, -32, 0); in bpf_fill_torturous_jumps()
416 self->prog_len = i; in bpf_fill_torturous_jumps()
419 self->prog_len = 0; in bpf_fill_torturous_jumps()
424 static void bpf_fill_big_prog_with_loop_1(struct bpf_test *self) in bpf_fill_big_prog_with_loop_1() argument
426 struct bpf_insn *insn = self->fill_insns; in bpf_fill_big_prog_with_loop_1()
427 /* This test was added to catch a specific use after free in bpf_fill_big_prog_with_loop_1()
434 const int len = getpagesize() - 25; in bpf_fill_big_prog_with_loop_1()
449 while (i < len - 3) in bpf_fill_big_prog_with_loop_1()
457 insn[callback_load_idx].imm = callback_idx - callback_load_idx - 1; in bpf_fill_big_prog_with_loop_1()
458 self->func_info[1].insn_off = callback_idx; in bpf_fill_big_prog_with_loop_1()
459 self->prog_len = i; in bpf_fill_big_prog_with_loop_1()
467 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8), \
468 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -16), \
469 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -24), \
470 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -32), \
471 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -40), \
472 BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -48), \
475 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48), \
497 * positive u32, and zero-extend it into 64-bit.
507 * negative u32, and sign-extend it into 64-bit.
527 for (len = MAX_INSNS - 1; len > 0; --len) in probe_filter_length()
554 return -1; in __create_map()
611 return -1; in create_prog_array()
613 return -1; in create_prog_array()
628 mfd = -1; in create_prog_array()
645 return -1; in create_map_in_map()
655 return -1; in create_map_in_map()
675 return -1; in create_cgroup_storage()
772 btf_fd = bpf_btf_load(raw_btf, ptr - raw_btf, &opts); in load_btf_spec()
778 return btf_fd < 0 ? -1 : btf_fd; in load_btf_spec()
787 static int load_btf_for_test(struct bpf_test *test) in load_btf_for_test() argument
792 test->btf_types[types_num] != BTF_END_RAW) in load_btf_for_test()
795 int types_len = types_num * sizeof(test->btf_types[0]); in load_btf_for_test()
797 return load_btf_spec(test->btf_types, types_len, in load_btf_for_test()
798 test->btf_strings, sizeof(test->btf_strings)); in load_btf_for_test()
811 return -1; in create_map_spin_lock()
830 return -1; in create_sk_storage_map()
849 return -1; in create_map_timer()
868 return -1; in create_map_kptr()
974 while (fixup_kfunc_btf_id->kfunc) { in fixup_prog_kfuncs()
981 fixup_kfunc_btf_id->kfunc, in fixup_prog_kfuncs()
991 fixup_kfunc_btf_id->kfunc, in fixup_prog_kfuncs()
999 prog[fixup_kfunc_btf_id->insn_idx].off = 1; in fixup_prog_kfuncs()
1004 prog[fixup_kfunc_btf_id->insn_idx].imm = btf_id; in fixup_prog_kfuncs()
1009 static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type, in do_test_fixup() argument
1012 int *fixup_map_hash_8b = test->fixup_map_hash_8b; in do_test_fixup()
1013 int *fixup_map_hash_48b = test->fixup_map_hash_48b; in do_test_fixup()
1014 int *fixup_map_hash_16b = test->fixup_map_hash_16b; in do_test_fixup()
1015 int *fixup_map_array_48b = test->fixup_map_array_48b; in do_test_fixup()
1016 int *fixup_map_sockmap = test->fixup_map_sockmap; in do_test_fixup()
1017 int *fixup_map_sockhash = test->fixup_map_sockhash; in do_test_fixup()
1018 int *fixup_map_xskmap = test->fixup_map_xskmap; in do_test_fixup()
1019 int *fixup_map_stacktrace = test->fixup_map_stacktrace; in do_test_fixup()
1020 int *fixup_prog1 = test->fixup_prog1; in do_test_fixup()
1021 int *fixup_prog2 = test->fixup_prog2; in do_test_fixup()
1022 int *fixup_map_in_map = test->fixup_map_in_map; in do_test_fixup()
1023 int *fixup_cgroup_storage = test->fixup_cgroup_storage; in do_test_fixup()
1024 int *fixup_percpu_cgroup_storage = test->fixup_percpu_cgroup_storage; in do_test_fixup()
1025 int *fixup_map_spin_lock = test->fixup_map_spin_lock; in do_test_fixup()
1026 int *fixup_map_array_ro = test->fixup_map_array_ro; in do_test_fixup()
1027 int *fixup_map_array_wo = test->fixup_map_array_wo; in do_test_fixup()
1028 int *fixup_map_array_small = test->fixup_map_array_small; in do_test_fixup()
1029 int *fixup_sk_storage_map = test->fixup_sk_storage_map; in do_test_fixup()
1030 int *fixup_map_event_output = test->fixup_map_event_output; in do_test_fixup()
1031 int *fixup_map_reuseport_array = test->fixup_map_reuseport_array; in do_test_fixup()
1032 int *fixup_map_ringbuf = test->fixup_map_ringbuf; in do_test_fixup()
1033 int *fixup_map_timer = test->fixup_map_timer; in do_test_fixup()
1034 int *fixup_map_kptr = test->fixup_map_kptr; in do_test_fixup()
1036 if (test->fill_helper) { in do_test_fixup()
1037 test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn)); in do_test_fixup()
1038 test->fill_helper(test); in do_test_fixup()
1041 /* Allocating HTs with 1 elem is fine here, since we only test in do_test_fixup()
1235 fixup_prog_kfuncs(prog, fd_array, test->fixup_kfunc_btf_id); in do_test_fixup()
1308 /* Returns true if every part of exp (tab-separated) appears in log, in order.
1325 len = p - exp; in cmp_str_seq()
1416 if (masked->imm == INSN_IMM_MASK) in compare_masked_insn()
1418 if (masked->off == INSN_OFF_MASK) in compare_masked_insn()
1430 return -1; in find_insn_subseq()
1432 for (i = 0; i < seq_len - subseq_len + 1; ++i) { in find_insn_subseq()
1445 return -1; in find_insn_subseq()
1456 return -1; in find_skip_insn_marker()
1459 /* Return true if all sub-sequences in `subseqs` could be found in
1460 * `seq` one after another. Sub-sequences are separated by a single
1477 seq_len -= subseq_idx + cur_subseq_len; in find_all_insn_subseqs()
1479 subseqs_len -= cur_subseq_len + 1; in find_all_insn_subseqs()
1500 i, insn->code, insn->dst_reg, in print_insn()
1501 insn->src_reg, insn->off, insn->imm); in print_insn()
1505 static bool check_xlated_program(struct bpf_test *test, int fd_prog) in check_xlated_program() argument
1510 bool check_expected = !is_null_insn(test->expected_insns); in check_xlated_program()
1511 bool check_unexpected = !is_null_insn(test->unexpected_insns); in check_xlated_program()
1524 !find_all_insn_subseqs(buf, test->expected_insns, in check_xlated_program()
1532 print_insn(test->expected_insns, MAX_EXPECTED_INSNS); in check_xlated_program()
1537 find_all_insn_subseqs(buf, test->unexpected_insns, in check_xlated_program()
1544 printf("Un-expected subsequence:\n"); in check_xlated_program()
1545 print_insn(test->unexpected_insns, MAX_UNEXPECTED_INSNS); in check_xlated_program()
1554 static void do_test_single(struct bpf_test *test, bool unpriv, in do_test_single() argument
1558 int prog_len, prog_type = test->prog_type; in do_test_single()
1559 struct bpf_insn *prog = test->insns; in do_test_single()
1564 int fd_array[2] = { -1, -1 }; in do_test_single()
1570 fd_prog = -1; in do_test_single()
1572 map_fds[i] = -1; in do_test_single()
1573 btf_fd = -1; in do_test_single()
1578 do_test_fixup(test, prog_type, prog, map_fds, &fd_array[1]); in do_test_single()
1579 if (test->fill_insns) { in do_test_single()
1580 prog = test->fill_insns; in do_test_single()
1581 prog_len = test->prog_len; in do_test_single()
1586 * features, skip this test. in do_test_single()
1592 if (test->flags & F_LOAD_WITH_STRICT_ALIGNMENT) in do_test_single()
1594 if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) in do_test_single()
1596 if (test->flags & ~3) in do_test_single()
1597 pflags |= test->flags; in do_test_single()
1599 expected_ret = unpriv && test->result_unpriv != UNDEF ? in do_test_single()
1600 test->result_unpriv : test->result; in do_test_single()
1601 expected_err = unpriv && test->errstr_unpriv ? in do_test_single()
1602 test->errstr_unpriv : test->errstr; in do_test_single()
1604 opts.expected_attach_type = test->expected_attach_type; in do_test_single()
1612 if (fd_array[1] != -1) in do_test_single()
1616 prog_type == BPF_PROG_TYPE_LSM) && test->kfunc) { in do_test_single()
1619 attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc, in do_test_single()
1623 test->kfunc); in do_test_single()
1631 if (test->btf_types[0] != 0) { in do_test_single()
1632 btf_fd = load_btf_for_test(test); in do_test_single()
1638 if (test->func_info_cnt != 0) { in do_test_single()
1639 opts.func_info = test->func_info; in do_test_single()
1640 opts.func_info_cnt = test->func_info_cnt; in do_test_single()
1641 opts.func_info_rec_size = sizeof(test->func_info[0]); in do_test_single()
1675 (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)) in do_test_single()
1693 if (!unpriv && test->insn_processed) { in do_test_single()
1699 if (test->insn_processed != insn_processed) { in do_test_single()
1701 insn_processed, test->insn_processed); in do_test_single()
1709 if (!check_xlated_program(test, fd_prog)) in do_test_single()
1714 if (!alignment_prevented_execution && fd_prog >= 0 && test->runs >= 0) { in do_test_single()
1718 if (!test->runs) in do_test_single()
1719 test->runs = 1; in do_test_single()
1721 for (i = 0; i < test->runs; i++) { in do_test_single()
1722 if (unpriv && test->retvals[i].retval_unpriv) in do_test_single()
1723 expected_val = test->retvals[i].retval_unpriv; in do_test_single()
1725 expected_val = test->retvals[i].retval; in do_test_single()
1728 test->retvals[i].data, in do_test_single()
1729 sizeof(test->retvals[i].data)); in do_test_single()
1731 printf("(run %d/%d) ", i + 1, test->runs); in do_test_single()
1752 if (test->fill_insns) in do_test_single()
1753 free(test->fill_insns); in do_test_single()
1770 /* The test checks for finer cap as CAP_NET_ADMIN, in is_admin()
1782 static bool test_as_unpriv(struct bpf_test *test) in test_as_unpriv() argument
1794 if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) in test_as_unpriv()
1797 return !test->prog_type || in test_as_unpriv()
1798 test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER || in test_as_unpriv()
1799 test->prog_type == BPF_PROG_TYPE_CGROUP_SKB; in test_as_unpriv()
1813 struct bpf_test *test = &tests[i]; in do_test() local
1815 /* Program types that are not supported by non-root we in do_test()
1818 if (test_as_unpriv(test) && unpriv_disabled) { in do_test()
1819 printf("#%d/u %s SKIP\n", i, test->descr); in do_test()
1821 } else if (test_as_unpriv(test)) { in do_test()
1824 printf("#%d/u %s ", i, test->descr); in do_test()
1825 do_test_single(test, true, &passes, &errors); in do_test()
1831 printf("#%d/p %s SKIP\n", i, test->descr); in do_test()
1834 printf("#%d/p %s ", i, test->descr); in do_test()
1835 do_test_single(test, false, &passes, &errors); in do_test()
1853 if (argc > 1 && strcmp(argv[1], "-v") == 0) { in main()
1857 argc--; in main()
1859 if (argc > 1 && strcmp(argv[1], "-vv") == 0) { in main()
1863 argc--; in main()