Skip to content

Commit 36ce8db

Browse files
image-dragonKernel Patches Daemon
authored andcommitted
selftests/bpf: add testcase for probe read fault
Add testcase for probe read fault to stream.c. Signed-off-by: Menglong Dong <[email protected]>
1 parent cee5f31 commit 36ce8db

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

tools/testing/selftests/bpf/prog_tests/stream.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ static void test_address(struct bpf_program *prog, unsigned long *fault_addr_p)
7272
ASSERT_OK(ret, "ret");
7373
ASSERT_OK(opts.retval, "retval");
7474

75-
sprintf(fault_addr, "0x%lx", *fault_addr_p);
75+
if (fault_addr_p)
76+
sprintf(fault_addr, "0x%lx", *fault_addr_p);
7677

7778
ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDERR, buf, sizeof(buf), &ropts);
7879
ASSERT_GT(ret, 0, "stream read");
@@ -106,3 +107,22 @@ void test_stream_arena_fault_address(void)
106107

107108
stream__destroy(skel);
108109
}
110+
111+
void test_stream_probe_read_fault(void)
112+
{
113+
struct stream *skel;
114+
115+
#if !defined(__x86_64__)
116+
printf("%s:SKIP: probe fault reporting not supported\n", __func__);
117+
test__skip();
118+
return;
119+
#endif
120+
121+
skel = stream__open_and_load();
122+
if (!ASSERT_OK_PTR(skel, "stream__open_and_load"))
123+
return;
124+
125+
test_address(skel->progs.stream_probe_read_fault, NULL);
126+
127+
stream__destroy(skel);
128+
}

tools/testing/selftests/bpf/progs/stream.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "bpf_experimental.h"
88
#include "bpf_arena_common.h"
99

10+
#define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
11+
1012
struct arr_elem {
1113
struct bpf_res_spin_lock lock;
1214
};
@@ -234,4 +236,23 @@ int stream_arena_callback_fault(void *ctx)
234236
return 0;
235237
}
236238

239+
SEC("syscall")
240+
__arch_x86_64
241+
__success __retval(0)
242+
__stderr("ERROR: Probe READ access faule, insn=0x[0-9a-fA-F]+")
243+
__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}")
244+
__stderr("Call trace:\n"
245+
"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n"
246+
"|[ \t]+[^\n]+\n)*}}")
247+
int stream_probe_read_fault(void *ctx)
248+
{
249+
struct sk_buff *skb = bpf_core_cast((void *)0xFFFFFFFF00000000,
250+
struct sk_buff);
251+
252+
/* do the memory read */
253+
READ_ONCE(skb->network_header);
254+
255+
return 0;
256+
}
257+
237258
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)