Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add pruning test case for bpf_spin_lock

Test that when reg->id is not same for the same register of type
PTR_TO_MAP_VALUE between current and old explored state, we currently
return false from regsafe and continue exploring.

Without the fix in prior commit, the test case fails.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20221209135733.28851-7-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Kumar Kartikeya Dwivedi and committed by
Alexei Starovoitov
2026f206 4ea2bb15

+39
+39
tools/testing/selftests/bpf/verifier/spin_lock.c
··· 331 331 .errstr = "inside bpf_spin_lock", 332 332 .prog_type = BPF_PROG_TYPE_SCHED_CLS, 333 333 }, 334 + { 335 + "spin_lock: regsafe compare reg->id for map value", 336 + .insns = { 337 + BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), 338 + BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_6, offsetof(struct __sk_buff, mark)), 339 + BPF_LD_MAP_FD(BPF_REG_1, 0), 340 + BPF_MOV64_REG(BPF_REG_9, BPF_REG_1), 341 + BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0), 342 + BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 343 + BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 344 + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 345 + BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), 346 + BPF_EXIT_INSN(), 347 + BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), 348 + BPF_MOV64_REG(BPF_REG_1, BPF_REG_9), 349 + BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 350 + BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 351 + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 352 + BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), 353 + BPF_EXIT_INSN(), 354 + BPF_MOV64_REG(BPF_REG_8, BPF_REG_0), 355 + BPF_MOV64_REG(BPF_REG_1, BPF_REG_7), 356 + BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 4), 357 + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_spin_lock), 358 + BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 1), 359 + BPF_JMP_IMM(BPF_JA, 0, 0, 1), 360 + BPF_MOV64_REG(BPF_REG_7, BPF_REG_8), 361 + BPF_MOV64_REG(BPF_REG_1, BPF_REG_7), 362 + BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 4), 363 + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_spin_unlock), 364 + BPF_MOV64_IMM(BPF_REG_0, 0), 365 + BPF_EXIT_INSN(), 366 + }, 367 + .fixup_map_spin_lock = { 2 }, 368 + .result = REJECT, 369 + .errstr = "bpf_spin_unlock of different lock", 370 + .prog_type = BPF_PROG_TYPE_SCHED_CLS, 371 + .flags = BPF_F_TEST_STATE_FREQ, 372 + },