Skip to content

Commit

Permalink
More marshaling tests
Browse files Browse the repository at this point in the history
* Add negative tests for bad offset for instructions that vary by
  offset value
* Add marshaling tests for unconditional byteswap
* Add marshaling tests for signed division and modulo
* Add marshaling tests for sign extension

Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler authored and elazarg committed Jan 31, 2024
1 parent f3964ef commit a90ab20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/asm_unmarshal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,21 @@ struct Unmarshaller {
switch (inst.offset) {
case 0: return Bin::Op::UDIV;
case 1: return Bin::Op::SDIV;
default: throw InvalidInstruction{pc, "invalid ALU op 0x30"};
default: throw InvalidInstruction(pc, make_opcode_message("invalid offset for", inst.opcode));
}
case INST_ALU_OP_MOD:
switch (inst.offset) {
case 0: return Bin::Op::UMOD;
case 1: return Bin::Op::SMOD;
default: throw InvalidInstruction{pc, "invalid ALU op 0x90"};
default: throw InvalidInstruction(pc, make_opcode_message("invalid offset for", inst.opcode));
}
case INST_ALU_OP_MOV:
switch (inst.offset) {
case 0: return Bin::Op::MOV;
case 8: return Bin::Op::MOVSX8;
case 16: return Bin::Op::MOVSX16;
case 32: return Bin::Op::MOVSX32;
default: throw InvalidInstruction{pc, "invalid ALU op 0xb0"};
default: throw InvalidInstruction(pc, make_opcode_message("invalid offset for", inst.opcode));
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/test/test_marshal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ static const auto ws = {1, 2, 4, 8};
TEST_CASE("disasm_marshal", "[disasm][marshal]") {
SECTION("Bin") {
auto ops = {Bin::Op::MOV, Bin::Op::ADD, Bin::Op::SUB, Bin::Op::MUL, Bin::Op::UDIV, Bin::Op::UMOD,
Bin::Op::OR, Bin::Op::AND, Bin::Op::LSH, Bin::Op::RSH, Bin::Op::ARSH, Bin::Op::XOR};
Bin::Op::OR, Bin::Op::AND, Bin::Op::LSH, Bin::Op::RSH, Bin::Op::ARSH, Bin::Op::XOR,
Bin::Op::SDIV, Bin::Op::SMOD, Bin::Op::MOVSX8, Bin::Op::MOVSX16, Bin::Op::MOVSX32};
SECTION("Reg src") {
for (auto op : ops) {
compare_marshal_unmarshal(Bin{.op = op, .dst = Reg{1}, .v = Reg{2}, .is64 = true});
Expand Down Expand Up @@ -90,6 +91,9 @@ TEST_CASE("disasm_marshal", "[disasm][marshal]") {
Un::Op::LE32,
Un::Op::LE64,
Un::Op::NEG,
Un::Op::SWAP16,
Un::Op::SWAP32,
Un::Op::SWAP64
};
for (auto op : ops)
compare_marshal_unmarshal(Un{.op = op, .dst = Reg{1}});
Expand Down Expand Up @@ -287,6 +291,16 @@ TEST_CASE("fail unmarshal off0 opcodes", "[disasm][marshal]") {
}
}

TEST_CASE("fail unmarshal offset opcodes", "[disasm][marshal]") {
// The following opcodes are defined for multiple other offset values, but not offset = 2 for example.
uint8_t off2_opcodes[] = {0x34, 0x37, 0x3c, 0x3f, 0x94, 0x97, 0x9c, 0x9f, 0xb4, 0xb7, 0xbc, 0xbf};
for (int i = 0; i < sizeof(off2_opcodes); i++) {
std::ostringstream oss;
oss << "0: invalid offset for op 0x" << std::hex << (int)off2_opcodes[i] << std::endl;
check_unmarshal_fail(ebpf_inst{.opcode = off2_opcodes[i], .offset = 2}, oss.str().c_str());
}
}

TEST_CASE("fail unmarshal misc", "[disasm][marshal]") {
check_unmarshal_fail(ebpf_inst{.opcode = /* 0x06 */ INST_CLS_JMP32}, "0: jump out of bounds\n");
check_unmarshal_fail(ebpf_inst{.opcode = /* 0x16 */ 0x10 | INST_CLS_JMP32}, "0: jump out of bounds\n");
Expand Down

0 comments on commit a90ab20

Please sign in to comment.