1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
toggle_object_autoload_progs(const struct bpf_object * obj,const char * name_load)4 static void toggle_object_autoload_progs(const struct bpf_object *obj,
5 					 const char *name_load)
6 {
7 	struct bpf_program *prog;
8 
9 	bpf_object__for_each_program(prog, obj) {
10 		const char *name = bpf_program__name(prog);
11 
12 		if (!strcmp(name_load, name))
13 			bpf_program__set_autoload(prog, true);
14 		else
15 			bpf_program__set_autoload(prog, false);
16 	}
17 }
18 
test_reference_tracking(void)19 void test_reference_tracking(void)
20 {
21 	const char *file = "test_sk_lookup_kern.o";
22 	const char *obj_name = "ref_track";
23 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts,
24 		.object_name = obj_name,
25 		.relaxed_maps = true,
26 	);
27 	struct bpf_object *obj_iter, *obj = NULL;
28 	struct bpf_program *prog;
29 	__u32 duration = 0;
30 	int err = 0;
31 
32 	obj_iter = bpf_object__open_file(file, &open_opts);
33 	if (!ASSERT_OK_PTR(obj_iter, "obj_iter_open_file"))
34 		return;
35 
36 	if (CHECK(strcmp(bpf_object__name(obj_iter), obj_name), "obj_name",
37 		  "wrong obj name '%s', expected '%s'\n",
38 		  bpf_object__name(obj_iter), obj_name))
39 		goto cleanup;
40 
41 	bpf_object__for_each_program(prog, obj_iter) {
42 		const char *name;
43 
44 		name = bpf_program__name(prog);
45 		if (!test__start_subtest(name))
46 			continue;
47 
48 		obj = bpf_object__open_file(file, &open_opts);
49 		if (!ASSERT_OK_PTR(obj, "obj_open_file"))
50 			goto cleanup;
51 
52 		toggle_object_autoload_progs(obj, name);
53 		/* Expect verifier failure if test name has 'err' */
54 		if (strncmp(name, "err_", sizeof("err_") - 1) == 0) {
55 			libbpf_print_fn_t old_print_fn;
56 
57 			old_print_fn = libbpf_set_print(NULL);
58 			err = !bpf_object__load(obj);
59 			libbpf_set_print(old_print_fn);
60 		} else {
61 			err = bpf_object__load(obj);
62 		}
63 		ASSERT_OK(err, name);
64 
65 		bpf_object__close(obj);
66 		obj = NULL;
67 	}
68 
69 cleanup:
70 	bpf_object__close(obj);
71 	bpf_object__close(obj_iter);
72 }
73