|
3 | 3 | // SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | #include <evmone/eof.hpp> |
| 6 | +#include <evmone/instruction_traits.hpp> |
6 | 7 | #include <gtest/gtest.h> |
7 | 8 | #include <test/utils/utils.hpp> |
8 | 9 |
|
@@ -88,3 +89,51 @@ TEST(eof_validation, EOF1_multiple_data_sections) |
88 | 89 | EXPECT_EQ(validate_eof(from_hex("EF0001 010001 020001 020001 00 FE DA DA")), |
89 | 90 | EOFValidationErrror::multiple_data_sections); |
90 | 91 | } |
| 92 | + |
| 93 | +TEST(eof_validation, EOF1_undefined_opcodes) |
| 94 | +{ |
| 95 | + auto code = from_hex("EF0001 010001 00 00"); |
| 96 | + |
| 97 | + const auto& gas_table = evmone::instr::gas_costs[EVMC_SHANGHAI]; |
| 98 | + |
| 99 | + for (uint16_t opcode = 0; opcode <= 0xff; ++opcode) |
| 100 | + { |
| 101 | + // PUSH* require immediate argument to be valid, checked in a separate test |
| 102 | + if (opcode >= OP_PUSH1 && opcode <= OP_PUSH32) |
| 103 | + continue; |
| 104 | + |
| 105 | + code.back() = static_cast<uint8_t>(opcode); |
| 106 | + |
| 107 | + const auto expected = (gas_table[opcode] == evmone::instr::undefined ? |
| 108 | + EOFValidationErrror::undefined_instruction : |
| 109 | + EOFValidationErrror::success); |
| 110 | + EXPECT_EQ(validate_eof(code), expected) << hex(code); |
| 111 | + } |
| 112 | + |
| 113 | + EXPECT_EQ(validate_eof(from_hex("EF0001 010001 00 FE")), EOFValidationErrror::success); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(eof_validation, EOF1_truncated_push) |
| 117 | +{ |
| 118 | + auto eof_header = from_hex("EF0001 010001 00"); |
| 119 | + auto& code_size_byte = eof_header[5]; |
| 120 | + for (uint8_t opcode = OP_PUSH1; opcode <= OP_PUSH32; ++opcode) |
| 121 | + { |
| 122 | + const auto required_bytes = static_cast<size_t>(opcode - OP_PUSH1 + 1); |
| 123 | + for (size_t i = 0; i < required_bytes; ++i) |
| 124 | + { |
| 125 | + bytes code{opcode + bytes(i, 0)}; |
| 126 | + code_size_byte = static_cast<uint8_t>(code.size()); |
| 127 | + const auto container = eof_header + code; |
| 128 | + |
| 129 | + EXPECT_EQ(validate_eof(container), EOFValidationErrror::truncated_immediate) |
| 130 | + << hex(container); |
| 131 | + } |
| 132 | + |
| 133 | + bytes code{opcode + bytes(required_bytes, 0)}; |
| 134 | + code_size_byte = static_cast<uint8_t>(code.size()); |
| 135 | + const auto container = eof_header + code; |
| 136 | + |
| 137 | + EXPECT_EQ(validate_eof(container), EOFValidationErrror::success) << hex(container); |
| 138 | + } |
| 139 | +} |
0 commit comments