1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
6 #include <test_progs.h>
9 #include "task_kfunc_failure.skel.h"
10 #include "task_kfunc_success.skel.h"
12 static struct task_kfunc_success *open_load_task_kfunc_skel(void)
14 struct task_kfunc_success *skel;
17 skel = task_kfunc_success__open();
18 if (!ASSERT_OK_PTR(skel, "skel_open"))
21 skel->bss->pid = getpid();
23 err = task_kfunc_success__load(skel);
24 if (!ASSERT_OK(err, "skel_load"))
30 task_kfunc_success__destroy(skel);
34 static void run_success_test(const char *prog_name)
36 struct task_kfunc_success *skel;
39 struct bpf_program *prog;
40 struct bpf_link *link = NULL;
42 skel = open_load_task_kfunc_skel();
43 if (!ASSERT_OK_PTR(skel, "open_load_skel"))
46 if (!ASSERT_OK(skel->bss->err, "pre_spawn_err"))
49 prog = bpf_object__find_program_by_name(skel->obj, prog_name);
50 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
53 link = bpf_program__attach(prog);
54 if (!ASSERT_OK_PTR(link, "attached_link"))
58 if (!ASSERT_GT(child_pid, -1, "child_pid"))
62 waitpid(child_pid, &status, 0);
64 ASSERT_OK(skel->bss->err, "post_wait_err");
67 bpf_link__destroy(link);
68 task_kfunc_success__destroy(skel);
71 static int run_vpid_test(void *prog_name)
73 struct task_kfunc_success *skel;
74 struct bpf_program *prog;
80 skel = open_load_task_kfunc_skel();
89 prog = bpf_object__find_program_by_name(skel->obj, prog_name);
95 prog_fd = bpf_program__fd(prog);
101 if (bpf_prog_test_run_opts(prog_fd, NULL)) {
107 err = 7 + skel->bss->err;
109 task_kfunc_success__destroy(skel);
113 static void run_vpid_success_test(const char *prog_name)
115 const int stack_size = 1024 * 1024;
116 int child_pid, wstatus;
119 stack = (char *)malloc(stack_size);
120 if (!ASSERT_OK_PTR(stack, "clone_stack"))
123 child_pid = clone(run_vpid_test, stack + stack_size,
124 CLONE_NEWPID | SIGCHLD, (void *)prog_name);
125 if (!ASSERT_GT(child_pid, -1, "child_pid"))
128 if (!ASSERT_GT(waitpid(child_pid, &wstatus, 0), -1, "waitpid"))
131 if (WEXITSTATUS(wstatus) > 7)
132 ASSERT_OK(WEXITSTATUS(wstatus) - 7, "vpid_test_failure");
134 ASSERT_OK(WEXITSTATUS(wstatus), "run_vpid_test_err");
139 static const char * const success_tests[] = {
140 "test_task_acquire_release_argument",
141 "test_task_acquire_release_current",
142 "test_task_acquire_leave_in_map",
143 "test_task_xchg_release",
144 "test_task_map_acquire_release",
145 "test_task_current_acquire_release",
146 "test_task_from_pid_arg",
147 "test_task_from_pid_current",
148 "test_task_from_pid_invalid",
149 "task_kfunc_acquire_trusted_walked",
150 "test_task_kfunc_flavor_relo",
151 "test_task_kfunc_flavor_relo_not_found",
154 static const char * const vpid_success_tests[] = {
155 "test_task_from_vpid_current",
156 "test_task_from_vpid_invalid",
159 void test_task_kfunc(void)
163 for (i = 0; i < ARRAY_SIZE(success_tests); i++) {
164 if (!test__start_subtest(success_tests[i]))
167 run_success_test(success_tests[i]);
170 for (i = 0; i < ARRAY_SIZE(vpid_success_tests); i++) {
171 if (!test__start_subtest(vpid_success_tests[i]))
174 run_vpid_success_test(vpid_success_tests[i]);
177 RUN_TESTS(task_kfunc_failure);