test_bpf: add more tests for LD_ABS and LD_IND.
authorNicolas Schichan <nschichan@freebox.fr>
Tue, 4 Aug 2015 13:19:11 +0000 (15:19 +0200)
committerDavid S. Miller <davem@davemloft.net>
Fri, 7 Aug 2015 05:02:32 +0000 (22:02 -0700)
This exerces the LD_ABS and LD_IND instructions for various sizes and
alignments. This also checks that X when used as an offset to a
BPF_IND instruction first in a filter is correctly set to 0.

Signed-off-by: Nicolas Schichan <nschichan@freebox.fr>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
lib/test_bpf.c

index 820098004481e82dd08820dd54be7accc2d11335..e6ef56df0dfea35a95f2d8bc8c6028b4bcd61a87 100644 (file)
@@ -4635,6 +4635,302 @@ static struct bpf_test tests[] = {
                { {0x40, 0x25051982} },
                .frag_data = { 0x19, 0x82 },
        },
+       /*
+        * LD_IND / LD_ABS on non fragmented SKBs
+        */
+       {
+               /*
+                * this tests that the JIT/interpreter correctly resets X
+                * before using it in an LD_IND instruction.
+                */
+               "LD_IND byte default X",
+               .u.insns = {
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_B, 0x1),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               { [0x1] = 0x42 },
+               { {0x40, 0x42 } },
+       },
+       {
+               "LD_IND byte positive offset",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_B, 0x1),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               { [0x3c] = 0x25, [0x3d] = 0x05,  [0x3e] = 0x19, [0x3f] = 0x82 },
+               { {0x40, 0x82 } },
+       },
+       {
+               "LD_IND byte negative offset",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x3e),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_B, -0x1),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               { [0x3c] = 0x25, [0x3d] = 0x05,  [0x3e] = 0x19, [0x3f] = 0x82 },
+               { {0x40, 0x05 } },
+       },
+       {
+               "LD_IND halfword positive offset",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_H, 0x2),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+               },
+               { {0x40, 0xdd88 } },
+       },
+       {
+               "LD_IND halfword negative offset",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_H, -0x2),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+               },
+               { {0x40, 0xbb66 } },
+       },
+       {
+               "LD_IND halfword unaligned",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_H, -0x1),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+               },
+               { {0x40, 0x66cc } },
+       },
+       {
+               "LD_IND word positive offset",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_W, 0x4),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0xee99ffaa } },
+       },
+       {
+               "LD_IND word negative offset",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_W, -0x4),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0xaa55bb66 } },
+       },
+       {
+               "LD_IND word unaligned (addr & 3 == 2)",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_W, -0x2),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0xbb66cc77 } },
+       },
+       {
+               "LD_IND word unaligned (addr & 3 == 1)",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_W, -0x3),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0x55bb66cc } },
+       },
+       {
+               "LD_IND word unaligned (addr & 3 == 3)",
+               .u.insns = {
+                       BPF_STMT(BPF_LDX | BPF_IMM, 0x20),
+                       BPF_STMT(BPF_LD | BPF_IND | BPF_W, -0x1),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0x66cc77dd } },
+       },
+       {
+               "LD_ABS byte",
+               .u.insns = {
+                       BPF_STMT(BPF_LD | BPF_ABS | BPF_B, 0x20),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0xcc } },
+       },
+       {
+               "LD_ABS halfword",
+               .u.insns = {
+                       BPF_STMT(BPF_LD | BPF_ABS | BPF_H, 0x22),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0xdd88 } },
+       },
+       {
+               "LD_ABS halfword unaligned",
+               .u.insns = {
+                       BPF_STMT(BPF_LD | BPF_ABS | BPF_H, 0x25),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0x99ff } },
+       },
+       {
+               "LD_ABS word",
+               .u.insns = {
+                       BPF_STMT(BPF_LD | BPF_ABS | BPF_W, 0x1c),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0xaa55bb66 } },
+       },
+       {
+               "LD_ABS word unaligned (addr & 3 == 2)",
+               .u.insns = {
+                       BPF_STMT(BPF_LD | BPF_ABS | BPF_W, 0x22),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0xdd88ee99 } },
+       },
+       {
+               "LD_ABS word unaligned (addr & 3 == 1)",
+               .u.insns = {
+                       BPF_STMT(BPF_LD | BPF_ABS | BPF_W, 0x21),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0x77dd88ee } },
+       },
+       {
+               "LD_ABS word unaligned (addr & 3 == 3)",
+               .u.insns = {
+                       BPF_STMT(BPF_LD | BPF_ABS | BPF_W, 0x23),
+                       BPF_STMT(BPF_RET | BPF_A, 0x0),
+               },
+               CLASSIC,
+               {
+                       [0x1c] = 0xaa, [0x1d] = 0x55,
+                       [0x1e] = 0xbb, [0x1f] = 0x66,
+                       [0x20] = 0xcc, [0x21] = 0x77,
+                       [0x22] = 0xdd, [0x23] = 0x88,
+                       [0x24] = 0xee, [0x25] = 0x99,
+                       [0x26] = 0xff, [0x27] = 0xaa,
+               },
+               { {0x40, 0x88ee99ff } },
+       },
 };
 
 static struct net_device dev;