xref: /linux/tools/testing/selftests/bpf/prog_tests/pro_epilogue.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3 
4 #include <test_progs.h>
5 #include "pro_epilogue.skel.h"
6 #include "epilogue_tailcall.skel.h"
7 #include "pro_epilogue_goto_start.skel.h"
8 #include "epilogue_exit.skel.h"
9 
10 struct st_ops_args {
11 	__u64 a;
12 };
13 
14 static void test_tailcall(void)
15 {
16 	LIBBPF_OPTS(bpf_test_run_opts, topts);
17 	struct epilogue_tailcall *skel;
18 	struct st_ops_args args;
19 	int err, prog_fd;
20 
21 	skel = epilogue_tailcall__open_and_load();
22 	if (!ASSERT_OK_PTR(skel, "epilogue_tailcall__open_and_load"))
23 		return;
24 
25 	topts.ctx_in = &args;
26 	topts.ctx_size_in = sizeof(args);
27 
28 	skel->links.epilogue_tailcall =
29 		bpf_map__attach_struct_ops(skel->maps.epilogue_tailcall);
30 	if (!ASSERT_OK_PTR(skel->links.epilogue_tailcall, "attach_struct_ops"))
31 		goto done;
32 
33 	/* Both test_epilogue_tailcall and test_epilogue_subprog are
34 	 * patched with epilogue. When syscall_epilogue_tailcall()
35 	 * is run, test_epilogue_tailcall() is triggered.
36 	 * It executes a tail call and control is transferred to
37 	 * test_epilogue_subprog(). Only test_epilogue_subprog()
38 	 * does args->a += 1, thus final args.a value of 10001
39 	 * guarantees that only the epilogue of the
40 	 * test_epilogue_subprog is executed.
41 	 */
42 	memset(&args, 0, sizeof(args));
43 	prog_fd = bpf_program__fd(skel->progs.syscall_epilogue_tailcall);
44 	err = bpf_prog_test_run_opts(prog_fd, &topts);
45 	ASSERT_OK(err, "bpf_prog_test_run_opts");
46 	ASSERT_EQ(args.a, 10001, "args.a");
47 	ASSERT_EQ(topts.retval, 10001 * 2, "topts.retval");
48 
49 done:
50 	epilogue_tailcall__destroy(skel);
51 }
52 
53 void test_pro_epilogue(void)
54 {
55 	RUN_TESTS(pro_epilogue);
56 	RUN_TESTS(pro_epilogue_goto_start);
57 	RUN_TESTS(epilogue_exit);
58 	if (test__start_subtest("tailcall"))
59 		test_tailcall();
60 }
61