Skip to content

Commit

Permalink
Tests for RJUMP/RJUMPI instruction validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Mar 22, 2022
1 parent 587ed4a commit c28ff2a
Showing 1 changed file with 103 additions and 1 deletion.
104 changes: 103 additions & 1 deletion test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,108 @@ TEST(eof_validation, EOF1_terminating_instructions)
opcode == OP_INVALID || opcode == OP_SELFDESTRUCT) ?
EOFValidationErrror::success :
EOFValidationErrror::missing_terminating_instruction);
EXPECT_EQ(validate_eof(container), expected) << hex(code);
EXPECT_EQ(validate_eof(container, EVMC_CANCUN), expected) << hex(code);
}
}

TEST(eof_validation, EOF1_valid_rjump)
{
// offset = 0
EXPECT_EQ(validate_eof(from_hex("EF0001 010004 00 5C000000"), EVMC_CANCUN),
EOFValidationErrror::success);

// offset = 3
EXPECT_EQ(validate_eof(from_hex("EF0001 010007 00 5C000300000000"), EVMC_CANCUN),
EOFValidationErrror::success);

// offset = -4
EXPECT_EQ(validate_eof(from_hex("EF0001 010005 00 005CFFFC00"), EVMC_CANCUN),
EOFValidationErrror::success);
}

TEST(eof_validation, EOF1_valid_rjumpi)
{
// offset = 0
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005D000000"), EVMC_CANCUN),
EOFValidationErrror::success);

// offset = 3
EXPECT_EQ(validate_eof(from_hex("EF0001 010009 00 60005D000300000000"), EVMC_CANCUN),
EOFValidationErrror::success);

// offset = -5
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005DFFFB00"), EVMC_CANCUN),
EOFValidationErrror::success);
}

TEST(eof_validation, EOF1_rjump_truncated)
{
EXPECT_EQ(validate_eof(from_hex("EF0001 010001 00 5C"), EVMC_CANCUN),
EOFValidationErrror::missing_terminating_instruction);

EXPECT_EQ(validate_eof(from_hex("EF0001 010002 00 5C00"), EVMC_CANCUN),
EOFValidationErrror::missing_terminating_instruction);
}

TEST(eof_validation, EOF1_rjumpi_truncated)
{
EXPECT_EQ(validate_eof(from_hex("EF0001 010003 00 60005D"), EVMC_CANCUN),
EOFValidationErrror::missing_terminating_instruction);

EXPECT_EQ(validate_eof(from_hex("EF0001 010004 00 60005D00"), EVMC_CANCUN),
EOFValidationErrror::missing_terminating_instruction);
}

TEST(eof_validation, EOF1_rjump_invalid_destination)
{
// Into header (offset = -5)
EXPECT_EQ(validate_eof(from_hex("EF0001 010004 00 5CFFFB00"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To before code begin (offset = -13)
EXPECT_EQ(validate_eof(from_hex("EF0001 010004 00 5CFFF300"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To after code end (offset = 2)
EXPECT_EQ(validate_eof(from_hex("EF0001 010004 00 5C000200"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To code end (offset = 1)
EXPECT_EQ(validate_eof(from_hex("EF0001 010004 00 5C000100"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To the same RJUMP immediate (offset = -1)
EXPECT_EQ(validate_eof(from_hex("EF0001 010004 00 5CFFFF00"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To PUSH immediate (offset = -4)
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005CFFFC00"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);
}

TEST(eof_validation, EOF1_rjumpi_invalid_destination)
{
// Into header (offset = -7)
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005DFFF900"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To before code begin (offset = -15)
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005DFFF100"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To after code end (offset = 2)
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005D000200"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To code end (offset = 1)
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005D000100"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To the same RJUMPI immediate (offset = -1)
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005DFFFF00"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);

// To PUSH immediate (offset = -4)
EXPECT_EQ(validate_eof(from_hex("EF0001 010006 00 60005DFFFC00"), EVMC_CANCUN),
EOFValidationErrror::invalid_rjump_destination);
}

0 comments on commit c28ff2a

Please sign in to comment.