Skip to content

Commit 536dfcb

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
selftests/bpf: Add a selftest with not-8-byte aligned BPF_ST
Add a selftest with a 4 bytes BPF_ST of 0 where the store is not 8-byte aligned. The goal is to ensure that STACK_ZERO is properly marked in stack slots and the STACK_ZERO value can propagate properly during the load. Acked-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/r/20240110051355.2737232-1-yonghong.song@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 17e25d8 commit 536dfcb

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,47 @@ __naked void partial_stack_load_preserves_zeros(void)
581581
: __clobber_common);
582582
}
583583

584+
SEC("raw_tp")
585+
__log_level(2)
586+
__success
587+
/* fp-4 is STACK_ZERO */
588+
__msg("2: (62) *(u32 *)(r10 -4) = 0 ; R10=fp0 fp-8=0000????")
589+
__msg("4: (71) r2 = *(u8 *)(r10 -1) ; R2_w=0 R10=fp0 fp-8=0000????")
590+
__msg("5: (0f) r1 += r2")
591+
__msg("mark_precise: frame0: last_idx 5 first_idx 0 subseq_idx -1")
592+
__msg("mark_precise: frame0: regs=r2 stack= before 4: (71) r2 = *(u8 *)(r10 -1)")
593+
__naked void partial_stack_load_preserves_partial_zeros(void)
594+
{
595+
asm volatile (
596+
/* fp-4 is value zero */
597+
".8byte %[fp4_st_zero];" /* LLVM-18+: *(u32 *)(r10 -4) = 0; */
598+
599+
/* load single U8 from non-aligned stack zero slot */
600+
"r1 = %[single_byte_buf];"
601+
"r2 = *(u8 *)(r10 -1);"
602+
"r1 += r2;"
603+
"*(u8 *)(r1 + 0) = r2;" /* this should be fine */
604+
605+
/* load single U16 from non-aligned stack zero slot */
606+
"r1 = %[single_byte_buf];"
607+
"r2 = *(u16 *)(r10 -2);"
608+
"r1 += r2;"
609+
"*(u8 *)(r1 + 0) = r2;" /* this should be fine */
610+
611+
/* load single U32 from non-aligned stack zero slot */
612+
"r1 = %[single_byte_buf];"
613+
"r2 = *(u32 *)(r10 -4);"
614+
"r1 += r2;"
615+
"*(u8 *)(r1 + 0) = r2;" /* this should be fine */
616+
617+
"r0 = 0;"
618+
"exit;"
619+
:
620+
: __imm_ptr(single_byte_buf),
621+
__imm_insn(fp4_st_zero, BPF_ST_MEM(BPF_W, BPF_REG_FP, -4, 0))
622+
: __clobber_common);
623+
}
624+
584625
char two_byte_buf[2] SEC(".data.two_byte_buf");
585626

586627
SEC("raw_tp")

0 commit comments

Comments
 (0)