-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy patheof.hpp
195 lines (166 loc) · 6.54 KB
/
eof.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2021 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <evmc/bytes.hpp>
#include <evmc/evmc.h>
#include <evmc/utils.h>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <string>
#include <variant>
#include <vector>
namespace evmone
{
using evmc::bytes;
using evmc::bytes_view;
struct EOFCodeType
{
uint8_t inputs; ///< Number of code inputs.
uint8_t outputs; ///< Number of code outputs.
uint16_t max_stack_height; ///< Maximum stack height reached in the code.
EOFCodeType(uint8_t inputs_, uint8_t outputs_, uint16_t max_stack_height_)
: inputs{inputs_}, outputs{outputs_}, max_stack_height{max_stack_height_}
{}
};
struct EOF1Header
{
/// The EOF version, 0 means legacy code.
uint8_t version = 0;
/// Size of every code section.
std::vector<uint16_t> code_sizes;
/// Offset of every code section from the beginning of the EOF container.
std::vector<uint16_t> code_offsets;
/// Size of the data section.
/// In case of deploy container it is the minimal data size of the container that will be
/// deployed, taking into account part of data appended at deploy-time (static_aux_data).
/// In this case the size of data section present in current container can be less than
/// @data_size.
uint16_t data_size = 0;
/// Offset of data container section start.
uint16_t data_offset = 0;
/// Size of every container section.
std::vector<uint16_t> container_sizes;
/// Offset of every container section start;
std::vector<uint16_t> container_offsets;
std::vector<EOFCodeType> types;
/// A helper to extract reference to a specific code section.
[[nodiscard]] bytes_view get_code(bytes_view container, size_t code_idx) const noexcept
{
assert(code_idx < code_offsets.size());
return container.substr(code_offsets[code_idx], code_sizes[code_idx]);
}
/// A helper to extract reference to the data section.
[[nodiscard]] bytes_view get_data(bytes_view container) const noexcept
{
return container.substr(data_offset);
}
/// A helper to check whether the container has data section body size equal to declare size.
[[nodiscard]] bool has_full_data(size_t container_size) const noexcept
{
// Containers with truncated data section cannot be initcontainers.
const auto truncated_data = static_cast<size_t>(data_offset + data_size) > container_size;
return !truncated_data;
}
/// A helper to extract reference to a specific container section.
[[nodiscard]] bytes_view get_container(
bytes_view container, size_t container_idx) const noexcept
{
assert(container_idx < container_offsets.size());
return container.substr(container_offsets[container_idx], container_sizes[container_idx]);
}
/// Offset of the data section size value in the header.
[[nodiscard]] size_t data_size_position() const noexcept;
};
/// Checks if code starts with EOF FORMAT + MAGIC, doesn't validate the format.
[[nodiscard]] EVMC_EXPORT bool is_eof_container(bytes_view code) noexcept;
/// Reads the section sizes assuming that container has valid format.
/// (must be true for all EOF contracts on-chain)
[[nodiscard]] EVMC_EXPORT EOF1Header read_valid_eof1_header(bytes_view container);
/// Modifies container by appending aux_data to data section and updating data section size
/// in the header.
bool append_data_section(bytes& container, bytes_view aux_data);
enum class EOFValidationError
{
success,
invalid_prefix,
eof_version_unknown,
incomplete_section_size,
incomplete_section_number,
header_terminator_missing,
type_section_missing,
code_section_missing,
data_section_missing,
zero_section_size,
section_headers_not_terminated,
invalid_section_bodies_size,
unreachable_code_sections,
undefined_instruction,
truncated_instruction,
invalid_rjump_destination,
too_many_code_sections,
invalid_type_section_size,
invalid_first_section_type,
invalid_max_stack_height,
no_terminating_instruction,
stack_height_mismatch,
stack_higher_than_outputs_required,
max_stack_height_above_limit,
inputs_outputs_num_above_limit,
unreachable_instructions,
stack_underflow,
stack_overflow,
invalid_code_section_index,
invalid_dataloadn_index,
jumpf_destination_incompatible_outputs,
invalid_non_returning_flag,
callf_to_non_returning_function,
too_many_container_sections,
invalid_container_section_index,
eofcreate_with_truncated_container,
toplevel_container_truncated,
ambiguous_container_kind,
incompatible_container_kind,
impossible,
};
enum class ContainerKind : uint8_t
{
/// Container that uses RETURNCONTRACT. Can be used by EOFCREATE/TXCREATE/Creation transaction.
initcode,
/// Container that uses STOP/RETURN. Can be returned by RETURNCONTRACT.
runtime,
/// Container that uses only REVERT/INVALID or does not terminate execution.
/// Can be used in any context.
initcode_runtime,
};
/// Determines the EOF version of the container by inspecting container's EOF prefix.
/// If the prefix is missing or invalid, 0 is returned meaning legacy code.
[[nodiscard]] uint8_t get_eof_version(bytes_view container) noexcept;
/// Validates the header and returns its representation if successful.
[[nodiscard]] EVMC_EXPORT std::variant<EOF1Header, EOFValidationError> validate_header(
evmc_revision rev, bytes_view container) noexcept;
/// Validates whether given container is a valid EOF according to the rules of given revision.
[[nodiscard]] EVMC_EXPORT EOFValidationError validate_eof(
evmc_revision rev, ContainerKind kind, bytes_view container) noexcept;
/// Returns the error message corresponding to an error code.
[[nodiscard]] EVMC_EXPORT std::string_view get_error_message(EOFValidationError err) noexcept;
/// Output operator for EOFValidationError.
EVMC_EXPORT std::ostream& operator<<(std::ostream& os, EOFValidationError err) noexcept;
/// Loads big endian int16_t from data. Unsafe.
/// TODO: Move it to intx
inline int16_t read_int16_be(auto it) noexcept
{
const uint8_t h = *it++;
const uint8_t l = *it;
return static_cast<int16_t>((h << 8) | l);
}
/// Loads big endian uint16_t from data. Unsafe.
/// TODO: Move it to intx
inline uint16_t read_uint16_be(auto it) noexcept
{
const uint8_t h = *it++;
const uint8_t l = *it;
return static_cast<uint16_t>((h << 8) | l);
}
} // namespace evmone