bpf: add tests to verifier testsuite
authorAlexei Starovoitov <ast@plumgrid.com>
Tue, 30 Sep 2014 01:50:02 +0000 (18:50 -0700)
committerDavid S. Miller <davem@davemloft.net>
Thu, 2 Oct 2014 01:30:33 +0000 (21:30 -0400)
add 4 extra tests to cover jump verification better

Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
samples/bpf/test_verifier.c

index d10992e2740e4acdde150d6fb4fb879466bb8353..f44ef11f65a787a26fe99791b24b2b2606321a0e 100644 (file)
@@ -461,6 +461,136 @@ static struct bpf_test tests[] = {
                .errstr = "R0 invalid mem access",
                .result = REJECT,
        },
+       {
+               "jump test 1",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, -8),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 1, 1),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -16, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 2, 1),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 3, 1),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -16, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 4, 1),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 5, 1),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -32, 5),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+       },
+       {
+               "jump test 2",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 0),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 14),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 1, 2),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -16, 0),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 11),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 2, 2),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -32, 0),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 8),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 3, 2),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -40, 0),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 5),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 4, 2),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -48, 0),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 5, 1),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -56, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+       },
+       {
+               "jump test 3",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -8, 0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 19),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 1, 3),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -16, 0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 15),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 2, 3),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -32, 0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -32),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 11),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 3, 3),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -40, 0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -40),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 7),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 4, 3),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -48, 0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48),
+                       BPF_JMP_IMM(BPF_JA, 0, 0, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 5, 0),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_2, -56, 0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -56),
+                       BPF_LD_MAP_FD(BPF_REG_1, 0),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_unspec),
+                       BPF_EXIT_INSN(),
+               },
+               .fixup = {24},
+               .result = ACCEPT,
+       },
+       {
+               "jump test 4",
+               .insns = {
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 2),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 3),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, BPF_REG_10, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+       },
 };
 
 static int probe_filter_length(struct bpf_insn *fp)