Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/lib/libbpfjit Test Array Bounds Check Elimination (ABC...
details: https://anonhg.NetBSD.org/src/rev/82294e1232d7
branches: trunk
changeset: 329407:82294e1232d7
user: alnsn <alnsn%NetBSD.org@localhost>
date: Fri May 23 11:47:59 2014 +0000
description:
Test Array Bounds Check Elimination (ABC) and test bpf programs from bpf(4).
diffstat:
tests/lib/libbpfjit/t_bpfjit.c | 545 ++++++++++++++++++++++++++++++++++++++++-
1 files changed, 542 insertions(+), 3 deletions(-)
diffs (truncated from 573 to 300 lines):
diff -r af463869fb47 -r 82294e1232d7 tests/lib/libbpfjit/t_bpfjit.c
--- a/tests/lib/libbpfjit/t_bpfjit.c Fri May 23 11:04:03 2014 +0000
+++ b/tests/lib/libbpfjit/t_bpfjit.c Fri May 23 11:47:59 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_bpfjit.c,v 1.2 2013/11/15 00:12:45 rmind Exp $ */
+/* $NetBSD: t_bpfjit.c,v 1.3 2014/05/23 11:47:59 alnsn Exp $ */
/*-
* Copyright (c) 2011-2012 Alexander Nasonov.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: t_bpfjit.c,v 1.2 2013/11/15 00:12:45 rmind Exp $");
+__RCSID("$NetBSD: t_bpfjit.c,v 1.3 2014/05/23 11:47:59 alnsn Exp $");
#include <atf-c.h>
#include <stdint.h>
@@ -3290,6 +3290,540 @@
bpfjit_free_code(code);
}
+ATF_TC(bpfjit_abc_ja);
+ATF_TC_HEAD(bpfjit_abc_ja, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Test ABC optimization with a single BPF_JMP+BPF_JA");
+}
+
+ATF_TC_BODY(bpfjit_abc_ja, tc)
+{
+ static struct bpf_insn insns[] = {
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3), /* min. length 4 */
+ BPF_STMT(BPF_JMP+BPF_JA, 2),
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, UINT32_MAX - 1),
+ BPF_STMT(BPF_RET+BPF_K, 0),
+ BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 2), /* min. length 6 */
+ BPF_STMT(BPF_RET+BPF_A, 0),
+ BPF_STMT(BPF_RET+BPF_K, 1),
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 6),
+ BPF_STMT(BPF_RET+BPF_K, 2),
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
+ BPF_STMT(BPF_RET+BPF_K, 3),
+ };
+
+ bpfjit_func_t code;
+ uint8_t pkt[6] = {0, 0, /* UINT32_MAX: */ 255, 255, 255, 255};
+
+ size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+ ATF_CHECK(bpf_validate(insns, insn_count));
+
+ code = bpfjit_generate_code(bc, insns, insn_count);
+ ATF_REQUIRE(code != NULL);
+
+ ATF_CHECK(code(pkt, 0, 0) == 0);
+ ATF_CHECK(code(pkt, 1, 1) == 0);
+ ATF_CHECK(code(pkt, 2, 2) == 0);
+ ATF_CHECK(code(pkt, 3, 3) == 0);
+ ATF_CHECK(code(pkt, 4, 4) == 0);
+ ATF_CHECK(code(pkt, 5, 5) == 0);
+ ATF_CHECK(code(pkt, 6, 6) == UINT32_MAX);
+
+ bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_abc_ja_over);
+ATF_TC_HEAD(bpfjit_abc_ja_over, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Test ABC optimization when BPF_JMP+BPF_JA jumps over all loads");
+}
+
+ATF_TC_BODY(bpfjit_abc_ja_over, tc)
+{
+ static struct bpf_insn insns[] = {
+ BPF_STMT(BPF_JMP+BPF_JA, 2),
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3),
+ BPF_STMT(BPF_RET+BPF_K, 0),
+ BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 4),
+ BPF_STMT(BPF_RET+BPF_K, 1),
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 5),
+ BPF_STMT(BPF_RET+BPF_K, 2),
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 6),
+ BPF_STMT(BPF_RET+BPF_K, 3),
+ };
+
+ 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(bc, insns, insn_count);
+ ATF_REQUIRE(code != NULL);
+
+ ATF_CHECK(code(pkt, 1, 1) == UINT32_MAX);
+
+ bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_abc_ld_chain);
+ATF_TC_HEAD(bpfjit_abc_ld_chain, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Test ABC optimization of a chain of BPF_LD instructions "
+ "with exits leading to a single BPF_RET");
+}
+
+ATF_TC_BODY(bpfjit_abc_ld_chain, tc)
+{
+ static struct bpf_insn insns[] = {
+ BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3), /* min. length 4 */
+ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 8, 0, 4),
+ BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 4), /* min. length 6 */
+ BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 7, 0, 2),
+ BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 6), /* min. length 10 */
+ BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 6, 0, 1),
+ BPF_STMT(BPF_RET+BPF_K, 123456789),
+ BPF_STMT(BPF_RET+BPF_K, 987654321),
+ };
+
+ bpfjit_func_t code;
+ uint8_t pkt[10] = {};
+
+ size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+ ATF_CHECK(bpf_validate(insns, insn_count));
+
+ code = bpfjit_generate_code(bc, insns, insn_count);
+ ATF_REQUIRE(code != NULL);
+
+ /* Packet is too short. */
+ ATF_CHECK(code(pkt, 1, 1) == 0);
+ ATF_CHECK(code(pkt, 2, 2) == 0);
+ ATF_CHECK(code(pkt, 3, 3) == 0);
+
+ /* !(pkt[3] == 8) => return 123456789 */
+ ATF_CHECK(code(pkt, 4, 4) == 123456789);
+ ATF_CHECK(code(pkt, 5, 5) == 123456789);
+ ATF_CHECK(code(pkt, 6, 6) == 123456789);
+ ATF_CHECK(code(pkt, 7, 7) == 123456789);
+ ATF_CHECK(code(pkt, 8, 8) == 123456789);
+ ATF_CHECK(code(pkt, 9, 9) == 123456789);
+
+ /* !(pkt[4:2] >= 7) => too short or return 123456789 */
+ pkt[3] = 8;
+ ATF_CHECK(code(pkt, 1, 1) == 0);
+ ATF_CHECK(code(pkt, 2, 2) == 0);
+ ATF_CHECK(code(pkt, 3, 3) == 0);
+ ATF_CHECK(code(pkt, 4, 4) == 0);
+ ATF_CHECK(code(pkt, 5, 5) == 0);
+ ATF_CHECK(code(pkt, 6, 6) == 123456789);
+ ATF_CHECK(code(pkt, 9, 9) == 123456789);
+
+ /* !(pkt[6:4] > 6) => too short or return 987654321 */
+ pkt[4] = pkt[5] = 1;
+ ATF_CHECK(code(pkt, 1, 1) == 0);
+ ATF_CHECK(code(pkt, 2, 2) == 0);
+ ATF_CHECK(code(pkt, 3, 3) == 0);
+ ATF_CHECK(code(pkt, 4, 4) == 0);
+ ATF_CHECK(code(pkt, 5, 5) == 0);
+ ATF_CHECK(code(pkt, 6, 6) == 0);
+ ATF_CHECK(code(pkt, 7, 7) == 0);
+ ATF_CHECK(code(pkt, 8, 8) == 0);
+ ATF_CHECK(code(pkt, 9, 9) == 0);
+ ATF_CHECK(code(pkt, 10, 10) == 987654321);
+
+ /* (pkt[6:4] > 6) => too short or return 123456789 */
+ pkt[6] = pkt[7] = pkt[8] = pkt[9] = 1;
+ ATF_CHECK(code(pkt, 1, 1) == 0);
+ ATF_CHECK(code(pkt, 2, 2) == 0);
+ ATF_CHECK(code(pkt, 3, 3) == 0);
+ ATF_CHECK(code(pkt, 4, 4) == 0);
+ ATF_CHECK(code(pkt, 5, 5) == 0);
+ ATF_CHECK(code(pkt, 6, 6) == 0);
+ ATF_CHECK(code(pkt, 7, 7) == 0);
+ ATF_CHECK(code(pkt, 8, 8) == 0);
+ ATF_CHECK(code(pkt, 9, 9) == 0);
+ ATF_CHECK(code(pkt, 10, 10) == 123456789);
+
+ bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_examples_1);
+ATF_TC_HEAD(bpfjit_examples_1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Test the first example from bpf(4) - "
+ "accept Reverse ARP requests");
+}
+
+ATF_TC_BODY(bpfjit_examples_1, tc)
+{
+ /*
+ * The following filter is taken from the Reverse ARP
+ * Daemon. It accepts only Reverse ARP requests.
+ */
+ struct bpf_insn insns[] = {
+ BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
+ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8035, 0, 3),
+ BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
+ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 3, 0, 1),
+ BPF_STMT(BPF_RET+BPF_K, 42),
+ BPF_STMT(BPF_RET+BPF_K, 0),
+ };
+
+ bpfjit_func_t code;
+ uint8_t pkt[22] = {};
+
+ size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+ ATF_CHECK(bpf_validate(insns, insn_count));
+
+ code = bpfjit_generate_code(bc, insns, insn_count);
+ ATF_REQUIRE(code != NULL);
+
+ /* Packet is too short. */
+ ATF_CHECK(code(pkt, 1, 1) == 0);
+ ATF_CHECK(code(pkt, 2, 2) == 0);
+ ATF_CHECK(code(pkt, 3, 3) == 0);
+ ATF_CHECK(code(pkt, 4, 4) == 0);
+ ATF_CHECK(code(pkt, 5, 5) == 0);
+ ATF_CHECK(code(pkt, 6, 6) == 0);
+ ATF_CHECK(code(pkt, 7, 7) == 0);
+ ATF_CHECK(code(pkt, 8, 8) == 0);
+ ATF_CHECK(code(pkt, 9, 9) == 0);
+ ATF_CHECK(code(pkt, 10, 10) == 0);
+ ATF_CHECK(code(pkt, 11, 11) == 0);
+ ATF_CHECK(code(pkt, 12, 12) == 0);
+ ATF_CHECK(code(pkt, 13, 13) == 0);
+ ATF_CHECK(code(pkt, 14, 14) == 0);
+ ATF_CHECK(code(pkt, 15, 15) == 0);
+ ATF_CHECK(code(pkt, 16, 16) == 0);
+ ATF_CHECK(code(pkt, 17, 17) == 0);
+ ATF_CHECK(code(pkt, 18, 18) == 0);
+ ATF_CHECK(code(pkt, 19, 19) == 0);
+ ATF_CHECK(code(pkt, 20, 20) == 0);
+ ATF_CHECK(code(pkt, 21, 21) == 0);
+
+ /* The packet doesn't match. */
+ ATF_CHECK(code(pkt, 22, 22) == 0);
+
+ /* Still no match after setting the protocol field. */
+ pkt[12] = 0x80; pkt[13] = 0x35;
+ ATF_CHECK(code(pkt, 22, 22) == 0);
+
+ /* Set RARP message type. */
+ pkt[21] = 3;
+ ATF_CHECK(code(pkt, 22, 22) == 42);
+
+ /* Packet is too short. */
+ ATF_CHECK(code(pkt, 1, 1) == 0);
+ ATF_CHECK(code(pkt, 2, 2) == 0);
+ ATF_CHECK(code(pkt, 3, 3) == 0);
+ ATF_CHECK(code(pkt, 4, 4) == 0);
+ ATF_CHECK(code(pkt, 5, 5) == 0);
+ ATF_CHECK(code(pkt, 6, 6) == 0);
+ ATF_CHECK(code(pkt, 7, 7) == 0);
+ ATF_CHECK(code(pkt, 8, 8) == 0);
+ ATF_CHECK(code(pkt, 9, 9) == 0);
+ ATF_CHECK(code(pkt, 10, 10) == 0);
+ ATF_CHECK(code(pkt, 11, 11) == 0);
+ ATF_CHECK(code(pkt, 12, 12) == 0);
+ ATF_CHECK(code(pkt, 13, 13) == 0);
+ ATF_CHECK(code(pkt, 14, 14) == 0);
+ ATF_CHECK(code(pkt, 15, 15) == 0);
+ ATF_CHECK(code(pkt, 16, 16) == 0);
+ ATF_CHECK(code(pkt, 17, 17) == 0);
+ ATF_CHECK(code(pkt, 18, 18) == 0);
+ ATF_CHECK(code(pkt, 19, 19) == 0);
+ ATF_CHECK(code(pkt, 20, 20) == 0);
+ ATF_CHECK(code(pkt, 21, 21) == 0);
+
+ /* Change RARP message type. */
+ pkt[20] = 3;
+ ATF_CHECK(code(pkt, 22, 22) == 0);
+
+ bpfjit_free_code(code);
+}
+
+ATF_TC(bpfjit_examples_2);
+ATF_TC_HEAD(bpfjit_examples_2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Test the second example from bpf(4) - "
+ "accept IP packets between two specified hosts");
+}
+
+ATF_TC_BODY(bpfjit_examples_2, tc)
+{
+ /*
+ * This filter accepts only IP packets between host 128.3.112.15
+ * and 128.3.112.35.
+ */
+ static struct bpf_insn insns[] = {
+ BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
+ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x0800, 0, 8),
Home |
Main Index |
Thread Index |
Old Index