bpf: Selftests, verifier case for non null pointer check branch taken
authorJohn Fastabend <john.fastabend@gmail.com>
Thu, 21 May 2020 20:07:46 +0000 (13:07 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 22 May 2020 00:44:25 +0000 (17:44 -0700)
When we have pointer type that is known to be non-null and comparing
against zero we only follow the non-null branch. This adds tests to
cover this case for reference tracking. Also add the other case when
comparison against a non-zero value and ensure we still fail with
unreleased reference.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/159009166599.6313.1593680633787453767.stgit@john-Precision-5820-Tower
tools/testing/selftests/bpf/verifier/ref_tracking.c

index 604b4615173637ce4476ebd0ef6098fce9a03930..056e0273bf1252414a1567cacb50131b1338b625 100644 (file)
        .result = REJECT,
        .errstr = "invalid mem access",
 },
+{
+       "reference tracking: branch tracking valid pointer null comparison",
+       .insns = {
+       BPF_SK_LOOKUP(sk_lookup_tcp),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+       BPF_MOV64_IMM(BPF_REG_3, 1),
+       BPF_JMP_IMM(BPF_JNE, BPF_REG_6, 0, 1),
+       BPF_MOV64_IMM(BPF_REG_3, 0),
+       BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 2),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       .result = ACCEPT,
+},
+{
+       "reference tracking: branch tracking valid pointer value comparison",
+       .insns = {
+       BPF_SK_LOOKUP(sk_lookup_tcp),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+       BPF_MOV64_IMM(BPF_REG_3, 1),
+       BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 4),
+       BPF_MOV64_IMM(BPF_REG_3, 0),
+       BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 1234, 2),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       .errstr = "Unreleased reference",
+       .result = REJECT,
+},