1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
#include <unistd.h>
#include "test_sleepable_tracepoints.skel.h"
#include "test_sleepable_tracepoints_fail.skel.h"
static void run_test(struct test_sleepable_tracepoints *skel)
{
char buf[PATH_MAX] = "/";
skel->bss->target_pid = getpid();
skel->bss->prog_triggered = 0;
skel->bss->err = 0;
skel->bss->copied_byte = 0;
syscall(__NR_getcwd, buf, sizeof(buf));
ASSERT_EQ(skel->bss->prog_triggered, 1, "prog_triggered");
ASSERT_EQ(skel->bss->err, 0, "err");
ASSERT_EQ(skel->bss->copied_byte, '/', "copied_byte");
}
static void run_auto_attach_test(struct bpf_program *prog,
struct test_sleepable_tracepoints *skel)
{
struct bpf_link *link;
link = bpf_program__attach(prog);
if (!ASSERT_OK_PTR(link, "prog_attach"))
return;
run_test(skel);
bpf_link__destroy(link);
}
static void test_attach_only(struct bpf_program *prog)
{
struct bpf_link *link;
link = bpf_program__attach(prog);
if (ASSERT_OK_PTR(link, "attach"))
bpf_link__destroy(link);
}
static void test_attach_reject(struct bpf_program *prog)
{
struct bpf_link *link;
link = bpf_program__attach(prog);
if (!ASSERT_ERR_PTR(link, "attach_should_fail"))
bpf_link__destroy(link);
}
static void test_raw_tp_bare(struct test_sleepable_tracepoints *skel)
{
struct bpf_link *link;
link = bpf_program__attach_raw_tracepoint(skel->progs.handle_raw_tp_bare,
"sys_enter");
if (ASSERT_OK_PTR(link, "attach"))
bpf_link__destroy(link);
}
static void test_tp_bare(struct test_sleepable_tracepoints *skel)
{
struct bpf_link *link;
link = bpf_program__attach_tracepoint(skel->progs.handle_tp_bare,
"syscalls", "sys_enter_getcwd");
if (ASSERT_OK_PTR(link, "attach"))
bpf_link__destroy(link);
}
static void test_test_run(struct test_sleepable_tracepoints *skel)
{
__u64 args[2] = {0x1234ULL, 0x5678ULL};
LIBBPF_OPTS(bpf_test_run_opts, topts,
.ctx_in = args,
.ctx_size_in = sizeof(args),
);
int fd, err;
fd = bpf_program__fd(skel->progs.handle_test_run);
err = bpf_prog_test_run_opts(fd, &topts);
ASSERT_OK(err, "test_run");
ASSERT_EQ(topts.retval, args[0] + args[1], "test_run_retval");
}
static void test_test_run_on_cpu_reject(struct test_sleepable_tracepoints *skel)
{
__u64 args[2] = {};
LIBBPF_OPTS(bpf_test_run_opts, topts,
.ctx_in = args,
.ctx_size_in = sizeof(args),
.flags = BPF_F_TEST_RUN_ON_CPU,
);
int fd, err;
fd = bpf_program__fd(skel->progs.handle_test_run);
err = bpf_prog_test_run_opts(fd, &topts);
ASSERT_ERR(err, "test_run_on_cpu_reject");
}
void test_sleepable_tracepoints(void)
{
struct test_sleepable_tracepoints *skel;
skel = test_sleepable_tracepoints__open_and_load();
if (!ASSERT_OK_PTR(skel, "open_and_load"))
return;
if (test__start_subtest("tp_btf"))
run_auto_attach_test(skel->progs.handle_sys_enter_tp_btf, skel);
if (test__start_subtest("raw_tp"))
run_auto_attach_test(skel->progs.handle_sys_enter_raw_tp, skel);
if (test__start_subtest("tracepoint"))
run_auto_attach_test(skel->progs.handle_sys_enter_tp, skel);
if (test__start_subtest("sys_exit"))
run_auto_attach_test(skel->progs.handle_sys_exit_tp, skel);
if (test__start_subtest("tracepoint_alias"))
test_attach_only(skel->progs.handle_sys_enter_tp_alias);
if (test__start_subtest("raw_tracepoint_alias"))
test_attach_only(skel->progs.handle_sys_enter_raw_tp_alias);
if (test__start_subtest("raw_tp_bare"))
test_raw_tp_bare(skel);
if (test__start_subtest("tp_bare"))
test_tp_bare(skel);
if (test__start_subtest("test_run"))
test_test_run(skel);
if (test__start_subtest("test_run_on_cpu_reject"))
test_test_run_on_cpu_reject(skel);
if (test__start_subtest("raw_tp_non_faultable"))
test_attach_reject(skel->progs.handle_raw_tp_non_faultable);
if (test__start_subtest("tp_non_syscall"))
test_attach_reject(skel->progs.handle_tp_non_syscall);
if (test__start_subtest("tp_btf_non_faultable_reject"))
RUN_TESTS(test_sleepable_tracepoints_fail);
test_sleepable_tracepoints__destroy(skel);
}
|