diff options
Diffstat (limited to 'contrib/netbsd-tests/lib/libbpfjit/t_bpfjit.c')
-rw-r--r-- | contrib/netbsd-tests/lib/libbpfjit/t_bpfjit.c | 818 |
1 files changed, 798 insertions, 20 deletions
diff --git a/contrib/netbsd-tests/lib/libbpfjit/t_bpfjit.c b/contrib/netbsd-tests/lib/libbpfjit/t_bpfjit.c index da67c1b3..4d81dd6 100644 --- a/contrib/netbsd-tests/lib/libbpfjit/t_bpfjit.c +++ b/contrib/netbsd-tests/lib/libbpfjit/t_bpfjit.c @@ -1,7 +1,7 @@ -/* $NetBSD: t_bpfjit.c,v 1.6 2014/07/08 21:07:52 alnsn Exp $ */ +/* $NetBSD: t_bpfjit.c,v 1.14 2015/02/14 22:40:18 alnsn Exp $ */ /*- - * Copyright (c) 2011-2012, 2014 Alexander Nasonov. + * Copyright (c) 2011-2012, 2014-2015 Alexander Nasonov. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: t_bpfjit.c,v 1.6 2014/07/08 21:07:52 alnsn Exp $"); +__RCSID("$NetBSD: t_bpfjit.c,v 1.14 2015/02/14 22:40:18 alnsn Exp $"); #include <atf-c.h> #include <stdint.h> @@ -67,9 +67,75 @@ ATF_TC_BODY(libbpfjit_empty, tc) { struct bpf_insn dummy; + ATF_CHECK(!bpf_validate(&dummy, 0)); ATF_CHECK(bpfjit_generate_code(NULL, &dummy, 0) == NULL); } +ATF_TC(libbpfjit_ret_k); +ATF_TC_HEAD(libbpfjit_ret_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of a trivial bpf program"); +} + +ATF_TC_BODY(libbpfjit_ret_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_RET+BPF_K, 17) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 17); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_bad_ret_k); +ATF_TC_HEAD(libbpfjit_bad_ret_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test that JIT compilation of a program with bad BPF_RET fails"); +} + +ATF_TC_BODY(libbpfjit_bad_ret_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_RET+BPF_K+0x8000, 13) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + /* + * The point of this test is checking a bad instruction of + * a valid class and with a valid BPF_RVAL data. + */ + const uint16_t rcode = insns[0].code; + ATF_CHECK(BPF_CLASS(rcode) == BPF_RET && + (BPF_RVAL(rcode) == BPF_K || BPF_RVAL(rcode) == BPF_A)); + + ATF_CHECK(!bpf_validate(insns, insn_count)); + + /* Current implementation generates code. */ + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 13); + + bpfjit_free_code(code); +} + ATF_TC(libbpfjit_alu_add_k); ATF_TC_HEAD(libbpfjit_alu_add_k, tc) { @@ -399,6 +465,245 @@ ATF_TC_BODY(libbpfjit_alu_div80000000_k, tc) bpfjit_free_code(code); } +ATF_TC(libbpfjit_alu_mod0_k); +ATF_TC_HEAD(libbpfjit_alu_mod0_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=0"); +} + +ATF_TC_BODY(libbpfjit_alu_mod0_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + //ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 0); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod1_k); +ATF_TC_HEAD(libbpfjit_alu_mod1_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=1"); +} + +ATF_TC_BODY(libbpfjit_alu_mod1_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, 7), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 0); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod2_k); +ATF_TC_HEAD(libbpfjit_alu_mod2_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=2"); +} + +ATF_TC_BODY(libbpfjit_alu_mod2_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, 7), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 2), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 1); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod4_k); +ATF_TC_HEAD(libbpfjit_alu_mod4_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=4"); +} + +ATF_TC_BODY(libbpfjit_alu_mod4_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 4), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 3); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod10_k); +ATF_TC_HEAD(libbpfjit_alu_mod10_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=10"); +} + +ATF_TC_BODY(libbpfjit_alu_mod10_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 10), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 9); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod10000_k); +ATF_TC_HEAD(libbpfjit_alu_mod10000_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=10000"); +} + +ATF_TC_BODY(libbpfjit_alu_mod10000_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 10000), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 3849); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod7609801_k); +ATF_TC_HEAD(libbpfjit_alu_mod7609801_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_mod+BPF_K with k=7609801"); +} + +ATF_TC_BODY(libbpfjit_alu_mod7609801_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, UINT32_C(7609801)), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(3039531)); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod80000000_k); +ATF_TC_HEAD(libbpfjit_alu_mod80000000_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=0x80000000"); +} + +ATF_TC_BODY(libbpfjit_alu_mod80000000_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, UINT32_C(0x80000000)), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x7fffffde)); + + bpfjit_free_code(code); +} + ATF_TC(libbpfjit_alu_and_k); ATF_TC_HEAD(libbpfjit_alu_and_k, tc) { @@ -459,6 +764,36 @@ ATF_TC_BODY(libbpfjit_alu_or_k, tc) bpfjit_free_code(code); } +ATF_TC(libbpfjit_alu_xor_k); +ATF_TC_HEAD(libbpfjit_alu_xor_k, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_XOR+BPF_K"); +} + +ATF_TC_BODY(libbpfjit_alu_xor_k, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, 0xdead0f0f), + BPF_STMT(BPF_ALU+BPF_XOR+BPF_K, 0x0000b1e0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef); + + bpfjit_free_code(code); +} + ATF_TC(libbpfjit_alu_lsh_k); ATF_TC_HEAD(libbpfjit_alu_lsh_k, tc) { @@ -962,7 +1297,7 @@ ATF_TC_HEAD(libbpfjit_alu_div80000000_x, tc) ATF_TC_BODY(libbpfjit_alu_div80000000_x, tc) { static struct bpf_insn insns[] = { - BPF_STMT(BPF_LD+BPF_IMM, UINT32_MAX - 33), + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)), BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0x80000000)), BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0), BPF_STMT(BPF_RET+BPF_A, 0) @@ -983,6 +1318,253 @@ ATF_TC_BODY(libbpfjit_alu_div80000000_x, tc) bpfjit_free_code(code); } +ATF_TC(libbpfjit_alu_mod0_x); +ATF_TC_HEAD(libbpfjit_alu_mod0_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=0"); +} + +ATF_TC_BODY(libbpfjit_alu_mod0_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 0); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod1_x); +ATF_TC_HEAD(libbpfjit_alu_mod1_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=1"); +} + +ATF_TC_BODY(libbpfjit_alu_mod1_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, 7), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 0); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod2_x); +ATF_TC_HEAD(libbpfjit_alu_mod2_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=2"); +} + +ATF_TC_BODY(libbpfjit_alu_mod2_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, 7), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 1); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod4_x); +ATF_TC_HEAD(libbpfjit_alu_mod4_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=4"); +} + +ATF_TC_BODY(libbpfjit_alu_mod4_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 3); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod10_x); +ATF_TC_HEAD(libbpfjit_alu_mod10_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=10"); +} + +ATF_TC_BODY(libbpfjit_alu_mod10_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 9); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod10000_x); +ATF_TC_HEAD(libbpfjit_alu_mod10000_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=10000"); +} + +ATF_TC_BODY(libbpfjit_alu_mod10000_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10000), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 3849); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod7609801_x); +ATF_TC_HEAD(libbpfjit_alu_mod7609801_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=7609801"); +} + +ATF_TC_BODY(libbpfjit_alu_mod7609801_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(7609801)), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(3039531)); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_alu_mod80000000_x); +ATF_TC_HEAD(libbpfjit_alu_mod80000000_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=0x80000000"); +} + +ATF_TC_BODY(libbpfjit_alu_mod80000000_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0x80000000)), + BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x7fffffde)); + + bpfjit_free_code(code); +} + ATF_TC(libbpfjit_alu_and_x); ATF_TC_HEAD(libbpfjit_alu_and_x, tc) { @@ -1045,6 +1627,37 @@ ATF_TC_BODY(libbpfjit_alu_or_x, tc) bpfjit_free_code(code); } +ATF_TC(libbpfjit_alu_xor_x); +ATF_TC_HEAD(libbpfjit_alu_xor_x, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test JIT compilation of BPF_ALU+BPF_XOR+BPF_X"); +} + +ATF_TC_BODY(libbpfjit_alu_xor_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_IMM, 0xdead0f0f), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0x0000b1e0), + BPF_STMT(BPF_ALU+BPF_XOR+BPF_X, 0), + BPF_STMT(BPF_RET+BPF_A, 0) + }; + + bpfjit_func_t code; + uint8_t pkt[1]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef); + + bpfjit_free_code(code); +} + ATF_TC(libbpfjit_alu_lsh_x); ATF_TC_HEAD(libbpfjit_alu_lsh_x, tc) { @@ -1305,6 +1918,54 @@ ATF_TC_BODY(libbpfjit_jmp_ja, tc) bpfjit_free_code(code); } +ATF_TC(libbpfjit_jmp_ja_invalid); +ATF_TC_HEAD(libbpfjit_jmp_ja_invalid, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test BPF_JMP+BPF_JA to invalid destination"); +} + +ATF_TC_BODY(libbpfjit_jmp_ja_invalid, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_JMP+BPF_JA, 4), + BPF_STMT(BPF_RET+BPF_K, 0), + BPF_STMT(BPF_RET+BPF_K, 1), + BPF_STMT(BPF_RET+BPF_K, 2), + BPF_STMT(BPF_RET+BPF_K, 3), + }; + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(!bpf_validate(insns, insn_count)); + ATF_CHECK(bpfjit_generate_code(NULL, insns, insn_count) == NULL); +} + +ATF_TC(libbpfjit_jmp_ja_overflow); +ATF_TC_HEAD(libbpfjit_jmp_ja_overflow, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test BPF_JMP+BPF_JA with negative offset"); +} + +ATF_TC_BODY(libbpfjit_jmp_ja_overflow, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_JMP+BPF_JA, 1), + BPF_STMT(BPF_RET+BPF_K, 777), + BPF_STMT(BPF_JMP+BPF_JA, UINT32_MAX - 1), // -2 + BPF_STMT(BPF_RET+BPF_K, 0) + }; + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + /* Jumps with negative offsets work in userspace ... */ + ATF_CHECK(bpf_validate(insns, insn_count)); + + /* .. but not for bpfjit. */ + ATF_CHECK(bpfjit_generate_code(NULL, insns, insn_count) == NULL); +} + ATF_TC(libbpfjit_jmp_jgt_k); ATF_TC_HEAD(libbpfjit_jmp_jgt_k, tc) { @@ -1693,26 +2354,26 @@ ATF_TC_BODY(libbpfjit_jmp_jeq_x, tc) BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0), BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 8), BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1), - BPF_STMT(BPF_RET+BPF_K, 0), + BPF_STMT(BPF_RET+BPF_K, 1), BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3), BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 2, 0), BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 9), BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 1), - BPF_STMT(BPF_RET+BPF_K, 1), - BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5), - BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 1), BPF_STMT(BPF_RET+BPF_K, 2), + BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5), + BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1), + BPF_STMT(BPF_RET+BPF_K, 3), BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7), BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 2, 3), - BPF_STMT(BPF_RET+BPF_K, 3), BPF_STMT(BPF_RET+BPF_K, 4), BPF_STMT(BPF_RET+BPF_K, 5), + BPF_STMT(BPF_RET+BPF_K, 6), BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 6), BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 3, 1), - BPF_STMT(BPF_RET+BPF_K, 6), - BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 1, 0, 0), BPF_STMT(BPF_RET+BPF_K, 7), - BPF_STMT(BPF_RET+BPF_K, 8) + BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0), + BPF_STMT(BPF_RET+BPF_K, 8), + BPF_STMT(BPF_RET+BPF_K, 9) }; bpfjit_func_t code; @@ -1725,14 +2386,14 @@ ATF_TC_BODY(libbpfjit_jmp_jeq_x, tc) code = bpfjit_generate_code(NULL, insns, insn_count); ATF_REQUIRE(code != NULL); - ATF_CHECK(jitcall(code, pkt, 1, 1) == 7); - ATF_CHECK(jitcall(code, pkt, 2, 2) == 7); - ATF_CHECK(jitcall(code, pkt, 3, 3) == 1); - ATF_CHECK(jitcall(code, pkt, 4, 4) == 7); - ATF_CHECK(jitcall(code, pkt, 5, 5) == 7); - ATF_CHECK(jitcall(code, pkt, 6, 6) == 8); - ATF_CHECK(jitcall(code, pkt, 7, 7) == 5); - ATF_CHECK(jitcall(code, pkt, 8, 8) == 0); + ATF_CHECK(jitcall(code, pkt, 1, 1) == 8); + ATF_CHECK(jitcall(code, pkt, 2, 2) == 8); + ATF_CHECK(jitcall(code, pkt, 3, 3) == 2); + ATF_CHECK(jitcall(code, pkt, 4, 4) == 8); + ATF_CHECK(jitcall(code, pkt, 5, 5) == 3); + ATF_CHECK(jitcall(code, pkt, 6, 6) == 9); + ATF_CHECK(jitcall(code, pkt, 7, 7) == 6); + ATF_CHECK(jitcall(code, pkt, 8, 8) == 1); bpfjit_free_code(code); } @@ -1794,6 +2455,98 @@ ATF_TC_BODY(libbpfjit_jmp_jset_x, tc) bpfjit_free_code(code); } +ATF_TC(libbpfjit_jmp_jeq_x_noinit_ax); +ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_ax, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test JIT compilation " + "of BPF_JMP+BPF_EQ+BPF_X with uninitialised A and X"); +} + +ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_ax, tc) +{ + static struct bpf_insn insns[] = { + BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1), + BPF_STMT(BPF_RET+BPF_K, 10), + BPF_STMT(BPF_RET+BPF_K, 11) + }; + + bpfjit_func_t code; + uint8_t pkt[8]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 10); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_jmp_jeq_x_noinit_a); +ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_a, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test JIT compilation " + "of BPF_JMP+BPF_EQ+BPF_X with uninitialised A"); +} + +ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_a, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0), /* X > 0 */ + BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1), + BPF_STMT(BPF_RET+BPF_K, 10), + BPF_STMT(BPF_RET+BPF_K, 11) + }; + + bpfjit_func_t code; + uint8_t pkt[8]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 11); + + bpfjit_free_code(code); +} + +ATF_TC(libbpfjit_jmp_jeq_x_noinit_x); +ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_x, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test JIT compilation " + "of BPF_JMP+BPF_EQ+BPF_X with uninitialised X"); +} + +ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_x, tc) +{ + static struct bpf_insn insns[] = { + BPF_STMT(BPF_LD+BPF_LEN, 0), /* A > 0 */ + BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1), + BPF_STMT(BPF_RET+BPF_K, 10), + BPF_STMT(BPF_RET+BPF_K, 11) + }; + + bpfjit_func_t code; + uint8_t pkt[8]; /* the program doesn't read any data */ + + size_t insn_count = sizeof(insns) / sizeof(insns[0]); + + ATF_CHECK(bpf_validate(insns, insn_count)); + + code = bpfjit_generate_code(NULL, insns, insn_count); + ATF_REQUIRE(code != NULL); + + ATF_CHECK(jitcall(code, pkt, 1, 1) == 11); + + bpfjit_free_code(code); +} + ATF_TC(libbpfjit_jmp_modulo_x); ATF_TC_HEAD(libbpfjit_jmp_modulo_x, tc) { @@ -3883,6 +4636,8 @@ ATF_TP_ADD_TCS(tp) * to ../../net/bpfjit/t_bpfjit.c */ ATF_TP_ADD_TC(tp, libbpfjit_empty); + ATF_TP_ADD_TC(tp, libbpfjit_ret_k); + ATF_TP_ADD_TC(tp, libbpfjit_bad_ret_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_add_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_sub_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_mul_k); @@ -3894,8 +4649,17 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, libbpfjit_alu_div10000_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_div7609801_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_div80000000_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod0_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod1_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod2_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod4_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod10_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod10000_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod7609801_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod80000000_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_and_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_or_k); + ATF_TP_ADD_TC(tp, libbpfjit_alu_xor_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_lsh_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_lsh0_k); ATF_TP_ADD_TC(tp, libbpfjit_alu_rsh_k); @@ -3912,8 +4676,17 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, libbpfjit_alu_div10000_x); ATF_TP_ADD_TC(tp, libbpfjit_alu_div7609801_x); ATF_TP_ADD_TC(tp, libbpfjit_alu_div80000000_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod0_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod1_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod2_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod4_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod10_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod10000_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod7609801_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_mod80000000_x); ATF_TP_ADD_TC(tp, libbpfjit_alu_and_x); ATF_TP_ADD_TC(tp, libbpfjit_alu_or_x); + ATF_TP_ADD_TC(tp, libbpfjit_alu_xor_x); ATF_TP_ADD_TC(tp, libbpfjit_alu_lsh_x); ATF_TP_ADD_TC(tp, libbpfjit_alu_lsh0_x); ATF_TP_ADD_TC(tp, libbpfjit_alu_rsh_x); @@ -3921,6 +4694,8 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, libbpfjit_alu_modulo_x); ATF_TP_ADD_TC(tp, libbpfjit_alu_neg); ATF_TP_ADD_TC(tp, libbpfjit_jmp_ja); + ATF_TP_ADD_TC(tp, libbpfjit_jmp_ja_invalid); + ATF_TP_ADD_TC(tp, libbpfjit_jmp_ja_overflow); ATF_TP_ADD_TC(tp, libbpfjit_jmp_jgt_k); ATF_TP_ADD_TC(tp, libbpfjit_jmp_jge_k); ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_k); @@ -3930,6 +4705,9 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, libbpfjit_jmp_jge_x); ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_x); ATF_TP_ADD_TC(tp, libbpfjit_jmp_jset_x); + ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_x_noinit_ax); + ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_x_noinit_a); + ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_x_noinit_x); ATF_TP_ADD_TC(tp, libbpfjit_jmp_modulo_x); ATF_TP_ADD_TC(tp, libbpfjit_ld_abs); ATF_TP_ADD_TC(tp, libbpfjit_ld_abs_k_overflow); |