Skip to content

Commit

Permalink
Add tests for sign extension extensions (#182)
Browse files Browse the repository at this point in the history
As defined in https://www.ietf.org/archive/id/draft-ietf-bpf-isa-00.html#section-3.1

Fixes #152

Signed-off-by: Dave Thaler <dthaler1968@gmail.com>
  • Loading branch information
dthaler committed Jan 16, 2024
1 parent 787d8f2 commit a309ef6
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/bpf_assembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef class _bpf_assembler
{"smod", 0x9},
{"xor", 0xa},
{"mov", 0xb},
{"movsx", 0xb},
{"arsh", 0xc},
{"le", 0xd},
{"be", 0xd},
Expand Down Expand Up @@ -260,10 +261,17 @@ typedef class _bpf_assembler
if (mnemonic.ends_with("32")) {
inst.opcode |= EBPF_CLS_ALU;
alu_op = mnemonic.substr(0, mnemonic.size() - 2);
} else if (mnemonic.ends_with("64")) {
inst.opcode |= EBPF_CLS_ALU64;
alu_op = mnemonic.substr(0, mnemonic.size() - 2);
} else {
inst.opcode |= EBPF_CLS_ALU64;
alu_op = mnemonic;
}
if (alu_op.starts_with("movsx")) {
inst.offset = _decode_offset(alu_op.substr(5));
alu_op = "movsx";
}
auto iter = _bpf_encode_alu_ops.find(alu_op);
// It is not possible to reach here with no match.
inst.opcode |= iter->second << 4;
Expand Down Expand Up @@ -478,6 +486,9 @@ typedef class _bpf_assembler
{"lsh", {&_bpf_assembler::_encode_alu, 2}}, {"lsh32", {&_bpf_assembler::_encode_alu, 2}},
{"mod", {&_bpf_assembler::_encode_alu, 2}}, {"mod32", {&_bpf_assembler::_encode_alu, 2}},
{"mov", {&_bpf_assembler::_encode_alu, 2}}, {"mov32", {&_bpf_assembler::_encode_alu, 2}},
{"movsx864", {&_bpf_assembler::_encode_alu, 2}}, {"movsx832", {&_bpf_assembler::_encode_alu, 2}},
{"movsx1664", {&_bpf_assembler::_encode_alu, 2}}, {"movsx1632", {&_bpf_assembler::_encode_alu, 2}},
{"movsx3264", {&_bpf_assembler::_encode_alu, 2}},
{"mul", {&_bpf_assembler::_encode_alu, 2}}, {"mul32", {&_bpf_assembler::_encode_alu, 2}},
{"neg", {&_bpf_assembler::_encode_alu, 1}}, {"neg32", {&_bpf_assembler::_encode_alu, 1}},
{"or", {&_bpf_assembler::_encode_alu, 2}}, {"or32", {&_bpf_assembler::_encode_alu, 2}},
Expand Down
18 changes: 14 additions & 4 deletions src/opcode_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,24 @@ static const std::set<bpf_conformance_instruction_t, InstCmp> instructions_from_
{bpf_conformance_test_cpu_version_t::v2, 0xad},
{bpf_conformance_test_cpu_version_t::v3, 0xae},
{bpf_conformance_test_cpu_version_t::v1, 0xaf},
{bpf_conformance_test_cpu_version_t::v1, 0xb4},
{bpf_conformance_test_cpu_version_t::v1, 0xb4, 0x00, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v4, 0xb4, 0x00, 0x00, 0x08},
{bpf_conformance_test_cpu_version_t::v4, 0xb4, 0x00, 0x00, 0x10},
{bpf_conformance_test_cpu_version_t::v2, 0xb5},
{bpf_conformance_test_cpu_version_t::v3, 0xb6},
{bpf_conformance_test_cpu_version_t::v1, 0xb7},
{bpf_conformance_test_cpu_version_t::v1, 0xbc},
{bpf_conformance_test_cpu_version_t::v1, 0xb7, 0x00, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v4, 0xb7, 0x00, 0x00, 0x08},
{bpf_conformance_test_cpu_version_t::v4, 0xb7, 0x00, 0x00, 0x10},
{bpf_conformance_test_cpu_version_t::v4, 0xb7, 0x00, 0x00, 0x20},
{bpf_conformance_test_cpu_version_t::v1, 0xbc, 0x00, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v4, 0xbc, 0x00, 0x00, 0x08},
{bpf_conformance_test_cpu_version_t::v4, 0xbc, 0x00, 0x00, 0x10},
{bpf_conformance_test_cpu_version_t::v2, 0xbd},
{bpf_conformance_test_cpu_version_t::v3, 0xbe},
{bpf_conformance_test_cpu_version_t::v1, 0xbf},
{bpf_conformance_test_cpu_version_t::v1, 0xbf, 0x00, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v4, 0xbf, 0x00, 0x00, 0x08},
{bpf_conformance_test_cpu_version_t::v4, 0xbf, 0x00, 0x00, 0x10},
{bpf_conformance_test_cpu_version_t::v4, 0xbf, 0x00, 0x00, 0x20},
{bpf_conformance_test_cpu_version_t::v3, 0xc3, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v3, 0xc3, 0x00, 0x01},
{bpf_conformance_test_cpu_version_t::v3, 0xc3, 0x00, 0x40},
Expand Down
7 changes: 7 additions & 0 deletions tests/movsx1632-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
movsx1632 %r0, 0x89abcdef
exit
-- result
0xffffcdef
8 changes: 8 additions & 0 deletions tests/movsx1632-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
lddw %r1, 0x0123456789abcdef
movsx1632 %r0, %r1
exit
-- result
0xffffcdef
7 changes: 7 additions & 0 deletions tests/movsx1664-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
movsx1664 %r0, 0x89abcdef
exit
-- result
0xffffffffffffcdef
8 changes: 8 additions & 0 deletions tests/movsx1664-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
lddw %r1, 0x0123456789abcdef
movsx1664 %r0, %r1
exit
-- result
0xffffffffffffcdef
7 changes: 7 additions & 0 deletions tests/movsx3264-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
movsx3264 %r0, 0x89abcdef
exit
-- result
0xffffffff89abcdef
8 changes: 8 additions & 0 deletions tests/movsx3264-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
lddw %r1, 0x0123456789abcdef
movsx3264 %r0, %r1
exit
-- result
0xffffffff89abcdef
7 changes: 7 additions & 0 deletions tests/movsx832-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
movsx832 %r0, 0x89abcdef
exit
-- result
0xffffffef
8 changes: 8 additions & 0 deletions tests/movsx832-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
lddw %r1, 0x0123456789abcdef
movsx832 %r0, %r1
exit
-- result
0xffffffef
7 changes: 7 additions & 0 deletions tests/movsx864-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
movsx864 %r0, 0x89abcdef
exit
-- result
0xffffffffffffffef
8 changes: 8 additions & 0 deletions tests/movsx864-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
lddw %r1, 0x0123456789abcdef
movsx864 %r0, %r1
exit
-- result
0xffffffffffffffef

0 comments on commit a309ef6

Please sign in to comment.