Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2020 Facebook */
  3#include <test_progs.h>
  4#include "test_global_func1.skel.h"
  5#include "test_global_func2.skel.h"
  6#include "test_global_func3.skel.h"
  7#include "test_global_func4.skel.h"
  8#include "test_global_func5.skel.h"
  9#include "test_global_func6.skel.h"
 10#include "test_global_func7.skel.h"
 11#include "test_global_func8.skel.h"
 12#include "test_global_func9.skel.h"
 13#include "test_global_func10.skel.h"
 14#include "test_global_func11.skel.h"
 15#include "test_global_func12.skel.h"
 16#include "test_global_func13.skel.h"
 17#include "test_global_func14.skel.h"
 18#include "test_global_func15.skel.h"
 19#include "test_global_func16.skel.h"
 20#include "test_global_func17.skel.h"
 21#include "test_global_func_ctx_args.skel.h"
 22
 23#include "bpf/libbpf_internal.h"
 24#include "btf_helpers.h"
 25
 26static void check_ctx_arg_type(const struct btf *btf, const struct btf_param *p)
 
 27{
 28	const struct btf_type *t;
 29	const char *s;
 30
 31	t = btf__type_by_id(btf, p->type);
 32	if (!ASSERT_EQ(btf_kind(t), BTF_KIND_PTR, "ptr_t"))
 33		return;
 34
 35	s = btf_type_raw_dump(btf, t->type);
 36	if (!ASSERT_HAS_SUBSTR(s, "STRUCT 'bpf_perf_event_data' size=0 vlen=0",
 37			       "ctx_struct_t"))
 38		return;
 39}
 40
 41static void subtest_ctx_arg_rewrite(void)
 42{
 43	struct test_global_func_ctx_args *skel = NULL;
 44	struct bpf_prog_info info;
 45	char func_info_buf[1024] __attribute__((aligned(8)));
 46	struct bpf_func_info_min *rec;
 47	struct btf *btf = NULL;
 48	__u32 info_len = sizeof(info);
 49	int err, fd, i;
 50	struct btf *kern_btf = NULL;
 51
 52	kern_btf = btf__load_vmlinux_btf();
 53	if (!ASSERT_OK_PTR(kern_btf, "kern_btf_load"))
 54		return;
 55
 56	/* simple detection of kernel native arg:ctx tag support */
 57	if (btf__find_by_name_kind(kern_btf, "bpf_subprog_arg_info", BTF_KIND_STRUCT) > 0) {
 58		test__skip();
 59		btf__free(kern_btf);
 60		return;
 61	}
 62	btf__free(kern_btf);
 63
 64	skel = test_global_func_ctx_args__open();
 65	if (!ASSERT_OK_PTR(skel, "skel_open"))
 66		return;
 67
 68	bpf_program__set_autoload(skel->progs.arg_tag_ctx_perf, true);
 69
 70	err = test_global_func_ctx_args__load(skel);
 71	if (!ASSERT_OK(err, "skel_load"))
 72		goto out;
 
 
 
 
 
 
 73
 74	memset(&info, 0, sizeof(info));
 75	info.func_info = ptr_to_u64(&func_info_buf);
 76	info.nr_func_info = 3;
 77	info.func_info_rec_size = sizeof(struct bpf_func_info_min);
 78
 79	fd = bpf_program__fd(skel->progs.arg_tag_ctx_perf);
 80	err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
 81	if (!ASSERT_OK(err, "prog_info"))
 82		goto out;
 83
 84	if (!ASSERT_EQ(info.nr_func_info, 3, "nr_func_info"))
 85		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 86
 87	btf = btf__load_from_kernel_by_id(info.btf_id);
 88	if (!ASSERT_OK_PTR(btf, "obj_kern_btf"))
 89		goto out;
 90
 91	rec = (struct bpf_func_info_min *)func_info_buf;
 92	for (i = 0; i < info.nr_func_info; i++, rec = (void *)rec + info.func_info_rec_size) {
 93		const struct btf_type *fn_t, *proto_t;
 94		const char *name;
 95
 96		if (rec->insn_off == 0)
 97			continue; /* main prog, skip */
 98
 99		fn_t = btf__type_by_id(btf, rec->type_id);
100		if (!ASSERT_OK_PTR(fn_t, "fn_type"))
101			goto out;
102		if (!ASSERT_EQ(btf_kind(fn_t), BTF_KIND_FUNC, "fn_type_kind"))
103			goto out;
104		proto_t = btf__type_by_id(btf, fn_t->type);
105		if (!ASSERT_OK_PTR(proto_t, "proto_type"))
106			goto out;
107
108		name = btf__name_by_offset(btf, fn_t->name_off);
109		if (strcmp(name, "subprog_ctx_tag") == 0) {
110			/* int subprog_ctx_tag(void *ctx __arg_ctx) */
111			if (!ASSERT_EQ(btf_vlen(proto_t), 1, "arg_cnt"))
112				goto out;
113
114			/* arg 0 is PTR -> STRUCT bpf_perf_event_data */
115			check_ctx_arg_type(btf, &btf_params(proto_t)[0]);
116		} else if (strcmp(name, "subprog_multi_ctx_tags") == 0) {
117			/* int subprog_multi_ctx_tags(void *ctx1 __arg_ctx,
118			 *			      struct my_struct *mem,
119			 *			      void *ctx2 __arg_ctx)
120			 */
121			if (!ASSERT_EQ(btf_vlen(proto_t), 3, "arg_cnt"))
122				goto out;
123
124			/* arg 0 is PTR -> STRUCT bpf_perf_event_data */
125			check_ctx_arg_type(btf, &btf_params(proto_t)[0]);
126			/* arg 2 is PTR -> STRUCT bpf_perf_event_data */
127			check_ctx_arg_type(btf, &btf_params(proto_t)[2]);
128		} else {
129			ASSERT_FAIL("unexpected subprog %s", name);
130			goto out;
131		}
132	}
133
134out:
135	btf__free(btf);
136	test_global_func_ctx_args__destroy(skel);
137}
138
 
 
 
 
 
139void test_test_global_funcs(void)
140{
141	RUN_TESTS(test_global_func1);
142	RUN_TESTS(test_global_func2);
143	RUN_TESTS(test_global_func3);
144	RUN_TESTS(test_global_func4);
145	RUN_TESTS(test_global_func5);
146	RUN_TESTS(test_global_func6);
147	RUN_TESTS(test_global_func7);
148	RUN_TESTS(test_global_func8);
149	RUN_TESTS(test_global_func9);
150	RUN_TESTS(test_global_func10);
151	RUN_TESTS(test_global_func11);
152	RUN_TESTS(test_global_func12);
153	RUN_TESTS(test_global_func13);
154	RUN_TESTS(test_global_func14);
155	RUN_TESTS(test_global_func15);
156	RUN_TESTS(test_global_func16);
157	RUN_TESTS(test_global_func17);
158	RUN_TESTS(test_global_func_ctx_args);
159
160	if (test__start_subtest("ctx_arg_rewrite"))
161		subtest_ctx_arg_rewrite();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162}
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2020 Facebook */
  3#include <test_progs.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  4
  5const char *err_str;
  6bool found;
  7
  8static int libbpf_debug_print(enum libbpf_print_level level,
  9			      const char *format, va_list args)
 10{
 11	char *log_buf;
 
 12
 13	if (level != LIBBPF_WARN ||
 14	    strcmp(format, "libbpf: \n%s\n")) {
 15		vprintf(format, args);
 16		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 17	}
 
 
 
 
 
 
 
 18
 19	log_buf = va_arg(args, char *);
 20	if (!log_buf)
 21		goto out;
 22	if (err_str && strstr(log_buf, err_str) == 0)
 23		found = true;
 24out:
 25	printf(format, log_buf);
 26	return 0;
 27}
 28
 29extern int extra_prog_load_log_flags;
 
 
 
 
 
 
 
 
 30
 31static int check_load(const char *file)
 32{
 33	struct bpf_object *obj = NULL;
 34	struct bpf_program *prog;
 35	int err;
 36
 37	found = false;
 38
 39	obj = bpf_object__open_file(file, NULL);
 40	err = libbpf_get_error(obj);
 41	if (err)
 42		return err;
 43
 44	prog = bpf_object__next_program(obj, NULL);
 45	if (!prog) {
 46		err = -ENOENT;
 47		goto err_out;
 48	}
 49
 50	bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32);
 51	bpf_program__set_log_level(prog, extra_prog_load_log_flags);
 
 52
 53	err = bpf_object__load(obj);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54
 55err_out:
 56	bpf_object__close(obj);
 57	return err;
 58}
 59
 60struct test_def {
 61	const char *file;
 62	const char *err_str;
 63};
 64
 65void test_test_global_funcs(void)
 66{
 67	struct test_def tests[] = {
 68		{ "test_global_func1.bpf.o", "combined stack size of 4 calls is 544" },
 69		{ "test_global_func2.bpf.o" },
 70		{ "test_global_func3.bpf.o", "the call stack of 8 frames" },
 71		{ "test_global_func4.bpf.o" },
 72		{ "test_global_func5.bpf.o", "expected pointer to ctx, but got PTR" },
 73		{ "test_global_func6.bpf.o", "modified ctx ptr R2" },
 74		{ "test_global_func7.bpf.o", "foo() doesn't return scalar" },
 75		{ "test_global_func8.bpf.o" },
 76		{ "test_global_func9.bpf.o" },
 77		{ "test_global_func10.bpf.o", "invalid indirect read from stack" },
 78		{ "test_global_func11.bpf.o", "Caller passes invalid args into func#1" },
 79		{ "test_global_func12.bpf.o", "invalid mem access 'mem_or_null'" },
 80		{ "test_global_func13.bpf.o", "Caller passes invalid args into func#1" },
 81		{ "test_global_func14.bpf.o", "reference type('FWD S') size cannot be determined" },
 82		{ "test_global_func15.bpf.o", "At program exit the register R0 has value" },
 83		{ "test_global_func16.bpf.o", "invalid indirect read from stack" },
 84		{ "test_global_func17.bpf.o", "Caller passes invalid args into func#1" },
 85	};
 86	libbpf_print_fn_t old_print_fn = NULL;
 87	int err, i, duration = 0;
 88
 89	old_print_fn = libbpf_set_print(libbpf_debug_print);
 90
 91	for (i = 0; i < ARRAY_SIZE(tests); i++) {
 92		const struct test_def *test = &tests[i];
 93
 94		if (!test__start_subtest(test->file))
 95			continue;
 96
 97		err_str = test->err_str;
 98		err = check_load(test->file);
 99		CHECK_FAIL(!!err ^ !!err_str);
100		if (err_str)
101			CHECK(found, "", "expected string '%s'", err_str);
102	}
103	libbpf_set_print(old_print_fn);
104}