1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3 
4 #include <test_progs.h>
5 #include "verifier_global_subprogs.skel.h"
6 #include "freplace_dead_global_func.skel.h"
7 
test_global_func_dead_code(void)8 void test_global_func_dead_code(void)
9 {
10 	struct verifier_global_subprogs *tgt_skel = NULL;
11 	struct freplace_dead_global_func *skel = NULL;
12 	char log_buf[4096];
13 	int err, tgt_fd;
14 
15 	/* first, try to load target with good global subprog */
16 	tgt_skel = verifier_global_subprogs__open();
17 	if (!ASSERT_OK_PTR(tgt_skel, "tgt_skel_good_open"))
18 		return;
19 
20 	bpf_program__set_autoload(tgt_skel->progs.chained_global_func_calls_success, true);
21 
22 	err = verifier_global_subprogs__load(tgt_skel);
23 	if (!ASSERT_OK(err, "tgt_skel_good_load"))
24 		goto out;
25 
26 	tgt_fd = bpf_program__fd(tgt_skel->progs.chained_global_func_calls_success);
27 
28 	/* Attach to good non-eliminated subprog */
29 	skel = freplace_dead_global_func__open();
30 	if (!ASSERT_OK_PTR(skel, "skel_good_open"))
31 		goto out;
32 
33 	err = bpf_program__set_attach_target(skel->progs.freplace_prog, tgt_fd, "global_good");
34 	ASSERT_OK(err, "attach_target_good");
35 
36 	err = freplace_dead_global_func__load(skel);
37 	if (!ASSERT_OK(err, "skel_good_load"))
38 		goto out;
39 
40 	freplace_dead_global_func__destroy(skel);
41 
42 	/* Try attaching to dead code-eliminated subprog */
43 	skel = freplace_dead_global_func__open();
44 	if (!ASSERT_OK_PTR(skel, "skel_dead_open"))
45 		goto out;
46 
47 	bpf_program__set_log_buf(skel->progs.freplace_prog, log_buf, sizeof(log_buf));
48 	err = bpf_program__set_attach_target(skel->progs.freplace_prog, tgt_fd, "global_dead");
49 	ASSERT_OK(err, "attach_target_dead");
50 
51 	err = freplace_dead_global_func__load(skel);
52 	if (!ASSERT_ERR(err, "skel_dead_load"))
53 		goto out;
54 
55 	ASSERT_HAS_SUBSTR(log_buf, "Subprog global_dead doesn't exist", "dead_subprog_missing_msg");
56 
57 out:
58 	verifier_global_subprogs__destroy(tgt_skel);
59 	freplace_dead_global_func__destroy(skel);
60 }
61