-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstructions_calls.cpp
164 lines (129 loc) · 5.51 KB
/
instructions_calls.cpp
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
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2019-2020 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#include "instructions.hpp"
template <evmc_call_kind Kind, bool Static>
evmc_status_code call(ExecutionState& state) noexcept
{
const auto gas = state.stack.pop();
const auto dst = intx::be::trunc<evmc::address>(state.stack.pop());
const auto value = (Static || Kind == EVMC_DELEGATECALL) ? 0 : state.stack.pop();
const auto has_value = value != 0;
const auto input_offset = state.stack.pop();
const auto input_size = state.stack.pop();
const auto output_offset = state.stack.pop();
const auto output_size = state.stack.pop();
state.stack.push(0); // Assume failure.
if (state.rev >= EVMC_BERLIN && state.host.access_account(dst) == EVMC_ACCESS_COLD)
{
if ((state.gas_left -= instr::additional_cold_account_access_cost) < 0)
return EVMC_OUT_OF_GAS;
}
if (!check_memory(state, input_offset, input_size))
return EVMC_OUT_OF_GAS;
if (!check_memory(state, output_offset, output_size))
return EVMC_OUT_OF_GAS;
auto msg = evmc_message{};
msg.kind = Kind;
msg.flags = Static ? uint32_t{EVMC_STATIC} : state.msg->flags;
msg.depth = state.msg->depth + 1;
msg.destination = dst;
msg.sender = (Kind == EVMC_DELEGATECALL) ? state.msg->sender : state.msg->destination;
msg.value =
(Kind == EVMC_DELEGATECALL) ? state.msg->value : intx::be::store<evmc::uint256be>(value);
if (size_t(input_size) > 0)
{
// msg.input_data = &state.memory[size_t(input_offset)];
memcpy(msg.input_data, &state.memory[size_t(input_offset)], size_t(input_size));
msg.input_size = size_t(input_size);
}
auto cost = has_value ? 9000 : 0;
if (Kind == EVMC_CALL)
{
if (has_value && state.msg->flags & EVMC_STATIC)
return EVMC_STATIC_MODE_VIOLATION;
if ((has_value || state.rev < EVMC_SPURIOUS_DRAGON) && !state.host.account_exists(dst))
cost += 25000;
}
if ((state.gas_left -= cost) < 0)
return EVMC_OUT_OF_GAS;
msg.gas = std::numeric_limits<int64_t>::max();
if (gas < msg.gas)
msg.gas = static_cast<int64_t>(gas);
if (state.rev >= EVMC_TANGERINE_WHISTLE) // TODO: Always true for STATICCALL.
msg.gas = std::min(msg.gas, state.gas_left - state.gas_left / 64);
else if (msg.gas > state.gas_left)
return EVMC_OUT_OF_GAS;
if (has_value)
{
msg.gas += 2300; // Add stipend.
state.gas_left += 2300;
}
state.return_data.clear();
if (state.msg->depth >= 1024)
return EVMC_SUCCESS;
if (has_value &&
intx::be::load<uint256>(state.host.get_balance(state.msg->destination)) < value)
return EVMC_SUCCESS;
const auto result = state.host.call(msg);
state.return_data.assign(result.output_data, result.output_size);
state.stack.top() = result.status_code == EVMC_SUCCESS;
const auto copy_size = std::min(size_t(output_size), result.output_size);
if (copy_size > 0)
std::memcpy(&state.memory[size_t(output_offset)], result.output_data, copy_size);
const auto gas_used = msg.gas - result.gas_left;
state.gas_left -= gas_used;
return EVMC_SUCCESS;
}
template evmc_status_code call<EVMC_CALL>(ExecutionState& state) noexcept;
template evmc_status_code call<EVMC_CALL, true>(ExecutionState& state) noexcept;
template evmc_status_code call<EVMC_DELEGATECALL>(ExecutionState& state) noexcept;
template evmc_status_code call<EVMC_CALLCODE>(ExecutionState& state) noexcept;
template <evmc_call_kind Kind>
evmc_status_code create(ExecutionState& state) noexcept
{
if (state.msg->flags & EVMC_STATIC)
return EVMC_STATIC_MODE_VIOLATION;
const auto endowment = state.stack.pop();
const auto init_code_offset = state.stack.pop();
const auto init_code_size = state.stack.pop();
if (!check_memory(state, init_code_offset, init_code_size))
return EVMC_OUT_OF_GAS;
auto salt = uint256{};
if (Kind == EVMC_CREATE2)
{
salt = state.stack.pop();
auto salt_cost = num_words(static_cast<size_t>(init_code_size)) * 6;
if ((state.gas_left -= salt_cost) < 0)
return EVMC_OUT_OF_GAS;
}
state.stack.push(0);
state.return_data.clear();
if (state.msg->depth >= 1024)
return EVMC_SUCCESS;
if (endowment != 0 &&
intx::be::load<uint256>(state.host.get_balance(state.msg->destination)) < endowment)
return EVMC_SUCCESS;
auto msg = evmc_message{};
msg.gas = state.gas_left;
if (state.rev >= EVMC_TANGERINE_WHISTLE)
msg.gas = msg.gas - msg.gas / 64;
msg.kind = Kind;
if (size_t(init_code_size) > 0)
{
memcpy(msg.input_data, &state.memory[size_t(init_code_offset)], size_t(init_code_size));
msg.input_size = size_t(init_code_size);
}
msg.sender = state.msg->destination;
msg.depth = state.msg->depth + 1;
msg.create2_salt = intx::be::store<evmc::bytes32>(salt);
msg.value = intx::be::store<evmc::uint256be>(endowment);
const auto result = state.host.call(msg);
state.gas_left -= msg.gas - result.gas_left;
state.return_data.assign(result.output_data, result.output_size);
if (result.status_code == EVMC_SUCCESS)
state.stack.top() = intx::be::load<uint256>(result.create_address);
return EVMC_SUCCESS;
}
template evmc_status_code create<EVMC_CREATE>(ExecutionState& state) noexcept;
template evmc_status_code create<EVMC_CREATE2>(ExecutionState& state) noexcept;