Skip to content

Commit ea99184

Browse files
Yonghong SongKernel Patches Daemon
authored andcommitted
selftests/bpf: Add tracing prog private stack tests
Some private stack tests are added including: - prog with stack size greater than BPF_PSTACK_MIN_SUBTREE_SIZE. - prog with stack size less than BPF_PSTACK_MIN_SUBTREE_SIZE. - prog with one subprog having MAX_BPF_STACK stack size and another subprog having non-zero stack size. - prog with callback function. - prog with exception in main prog or subprog. Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
1 parent 51663f0 commit ea99184

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "verifier_or_jmp32_k.skel.h"
6060
#include "verifier_precision.skel.h"
6161
#include "verifier_prevent_map_lookup.skel.h"
62+
#include "verifier_private_stack.skel.h"
6263
#include "verifier_raw_stack.skel.h"
6364
#include "verifier_raw_tp_writable.skel.h"
6465
#include "verifier_reg_equal.skel.h"
@@ -185,6 +186,7 @@ void test_verifier_bpf_fastcall(void) { RUN(verifier_bpf_fastcall); }
185186
void test_verifier_or_jmp32_k(void) { RUN(verifier_or_jmp32_k); }
186187
void test_verifier_precision(void) { RUN(verifier_precision); }
187188
void test_verifier_prevent_map_lookup(void) { RUN(verifier_prevent_map_lookup); }
189+
void test_verifier_private_stack(void) { RUN(verifier_private_stack); }
188190
void test_verifier_raw_stack(void) { RUN(verifier_raw_stack); }
189191
void test_verifier_raw_tp_writable(void) { RUN(verifier_raw_tp_writable); }
190192
void test_verifier_reg_equal(void) { RUN(verifier_reg_equal); }
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
6+
#include "bpf_experimental.h"
7+
8+
/* From include/linux/filter.h */
9+
#define MAX_BPF_STACK 512
10+
11+
#if defined(__TARGET_ARCH_x86)
12+
13+
SEC("kprobe")
14+
__description("Private stack, single prog")
15+
__success
16+
__arch_x86_64
17+
__jited(" movabsq $0x{{.*}}, %r9")
18+
__jited(" addq %gs:0x{{.*}}, %r9")
19+
__jited(" movl $0x2a, %edi")
20+
__jited(" movq %rdi, -0x100(%r9)")
21+
__naked void private_stack_single_prog(void)
22+
{
23+
asm volatile (
24+
"r1 = 42;"
25+
"*(u64 *)(r10 - 256) = r1;"
26+
"r0 = 0;"
27+
"exit;"
28+
:
29+
:
30+
: __clobber_all);
31+
}
32+
33+
__used
34+
__naked static void cumulative_stack_depth_subprog(void)
35+
{
36+
asm volatile (
37+
"r1 = 41;"
38+
"*(u64 *)(r10 - 32) = r1;"
39+
"call %[bpf_get_smp_processor_id];"
40+
"exit;"
41+
:: __imm(bpf_get_smp_processor_id)
42+
: __clobber_all);
43+
}
44+
45+
SEC("kprobe")
46+
__description("Private stack, subtree > MAX_BPF_STACK")
47+
__success
48+
__arch_x86_64
49+
/* private stack fp for the main prog */
50+
__jited(" movabsq $0x{{.*}}, %r9")
51+
__jited(" addq %gs:0x{{.*}}, %r9")
52+
__jited(" movl $0x2a, %edi")
53+
__jited(" movq %rdi, -0x200(%r9)")
54+
__jited(" pushq %r9")
55+
__jited(" callq 0x{{.*}}")
56+
__jited(" popq %r9")
57+
__jited(" xorl %eax, %eax")
58+
__naked void private_stack_nested_1(void)
59+
{
60+
asm volatile (
61+
"r1 = 42;"
62+
"*(u64 *)(r10 - %[max_bpf_stack]) = r1;"
63+
"call cumulative_stack_depth_subprog;"
64+
"r0 = 0;"
65+
"exit;"
66+
:
67+
: __imm_const(max_bpf_stack, MAX_BPF_STACK)
68+
: __clobber_all);
69+
}
70+
71+
SEC("kprobe")
72+
__description("Private stack, subtree > MAX_BPF_STACK")
73+
__success
74+
__arch_x86_64
75+
/* private stack fp for the subprog */
76+
__jited(" addq $0x20, %r9")
77+
__naked void private_stack_nested_2(void)
78+
{
79+
asm volatile (
80+
"r1 = 42;"
81+
"*(u64 *)(r10 - %[max_bpf_stack]) = r1;"
82+
"call cumulative_stack_depth_subprog;"
83+
"r0 = 0;"
84+
"exit;"
85+
:
86+
: __imm_const(max_bpf_stack, MAX_BPF_STACK)
87+
: __clobber_all);
88+
}
89+
90+
SEC("raw_tp")
91+
__description("No private stack, nested")
92+
__success
93+
__arch_x86_64
94+
__jited(" subq $0x8, %rsp")
95+
__naked void no_private_stack_nested(void)
96+
{
97+
asm volatile (
98+
"r1 = 42;"
99+
"*(u64 *)(r10 - 8) = r1;"
100+
"call cumulative_stack_depth_subprog;"
101+
"r0 = 0;"
102+
"exit;"
103+
:
104+
:
105+
: __clobber_all);
106+
}
107+
108+
__naked __noinline __used
109+
static unsigned long loop_callback(void)
110+
{
111+
asm volatile (
112+
"call %[bpf_get_prandom_u32];"
113+
"r1 = 42;"
114+
"*(u64 *)(r10 - 512) = r1;"
115+
"call cumulative_stack_depth_subprog;"
116+
"r0 = 0;"
117+
"exit;"
118+
:
119+
: __imm(bpf_get_prandom_u32)
120+
: __clobber_common);
121+
}
122+
123+
SEC("raw_tp")
124+
__description("Private stack, callback")
125+
__success
126+
__arch_x86_64
127+
/* for func loop_callback */
128+
__jited("func #1")
129+
__jited(" endbr64")
130+
__jited(" nopl (%rax,%rax)")
131+
__jited(" nopl (%rax)")
132+
__jited(" pushq %rbp")
133+
__jited(" movq %rsp, %rbp")
134+
__jited(" endbr64")
135+
__jited(" movabsq $0x{{.*}}, %r9")
136+
__jited(" addq %gs:0x{{.*}}, %r9")
137+
__jited(" pushq %r9")
138+
__jited(" callq")
139+
__jited(" popq %r9")
140+
__jited(" movl $0x2a, %edi")
141+
__jited(" movq %rdi, -0x200(%r9)")
142+
__jited(" pushq %r9")
143+
__jited(" callq")
144+
__jited(" popq %r9")
145+
__naked void private_stack_callback(void)
146+
{
147+
asm volatile (
148+
"r1 = 1;"
149+
"r2 = %[loop_callback];"
150+
"r3 = 0;"
151+
"r4 = 0;"
152+
"call %[bpf_loop];"
153+
"r0 = 0;"
154+
"exit;"
155+
:
156+
: __imm_ptr(loop_callback),
157+
__imm(bpf_loop)
158+
: __clobber_common);
159+
}
160+
161+
SEC("fentry/bpf_fentry_test9")
162+
__description("Private stack, exception in main prog")
163+
__success __retval(0)
164+
__arch_x86_64
165+
__jited(" pushq %r9")
166+
__jited(" callq")
167+
__jited(" popq %r9")
168+
int private_stack_exception_main_prog(void)
169+
{
170+
asm volatile (
171+
"r1 = 42;"
172+
"*(u64 *)(r10 - 512) = r1;"
173+
::: __clobber_common);
174+
175+
bpf_throw(0);
176+
return 0;
177+
}
178+
179+
__used static int subprog_exception(void)
180+
{
181+
bpf_throw(0);
182+
return 0;
183+
}
184+
185+
SEC("fentry/bpf_fentry_test9")
186+
__description("Private stack, exception in subprog")
187+
__success __retval(0)
188+
__arch_x86_64
189+
__jited(" movq %rdi, -0x200(%r9)")
190+
__jited(" pushq %r9")
191+
__jited(" callq")
192+
__jited(" popq %r9")
193+
int private_stack_exception_sub_prog(void)
194+
{
195+
asm volatile (
196+
"r1 = 42;"
197+
"*(u64 *)(r10 - 512) = r1;"
198+
"call subprog_exception;"
199+
::: __clobber_common);
200+
201+
return 0;
202+
}
203+
204+
#else
205+
206+
SEC("kprobe")
207+
__description("private stack is not supported, use a dummy test")
208+
__success
209+
int dummy_test(void)
210+
{
211+
return 0;
212+
}
213+
214+
#endif
215+
216+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)