Skip to content

Commit 85eb035

Browse files
SunHao-0Alexei Starovoitov
authored andcommitted
selftests/bpf: Add test for immediate spilled to stack
Add a test to check if the verifier correctly reason about the sign of an immediate spilled to stack by BPF_ST instruction. Signed-off-by: Hao Sun <sunhao.th@gmail.com> Link: https://lore.kernel.org/r/20231101-fix-check-stack-write-v3-2-f05c2b1473d5@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 811c363 commit 85eb035

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,35 @@
6565
.expected_attach_type = BPF_SK_LOOKUP,
6666
.runs = -1,
6767
},
68+
{
69+
"BPF_ST_MEM stack imm sign",
70+
/* Check if verifier correctly reasons about sign of an
71+
* immediate spilled to stack by BPF_ST instruction.
72+
*
73+
* fp[-8] = -44;
74+
* r0 = fp[-8];
75+
* if r0 s< 0 goto ret0;
76+
* r0 = -1;
77+
* exit;
78+
* ret0:
79+
* r0 = 0;
80+
* exit;
81+
*/
82+
.insns = {
83+
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, -44),
84+
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
85+
BPF_JMP_IMM(BPF_JSLT, BPF_REG_0, 0, 2),
86+
BPF_MOV64_IMM(BPF_REG_0, -1),
87+
BPF_EXIT_INSN(),
88+
BPF_MOV64_IMM(BPF_REG_0, 0),
89+
BPF_EXIT_INSN(),
90+
},
91+
/* Use prog type that requires return value in range [0, 1] */
92+
.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
93+
.expected_attach_type = BPF_SK_LOOKUP,
94+
.result = VERBOSE_ACCEPT,
95+
.runs = -1,
96+
.errstr = "0: (7a) *(u64 *)(r10 -8) = -44 ; R10=fp0 fp-8_w=-44\
97+
2: (c5) if r0 s< 0x0 goto pc+2\
98+
R0_w=-44",
99+
},

0 commit comments

Comments
 (0)