Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit fb584c0

Browse files
committed
interpreter: Simplify exception usage
Instead of using complex exception types from Aleth, throw EVMC status code in simple cases.
1 parent 5c76154 commit fb584c0

File tree

3 files changed

+26
-57
lines changed

3 files changed

+26
-57
lines changed

libaleth-interpreter/VM.cpp

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,44 +39,16 @@ evmc_result execute(evmc_vm* _instance, evmc_host_context* _context, evmc_revisi
3939
result.status_code = EVMC_SUCCESS;
4040
result.gas_left = vm->m_io_gas;
4141
}
42+
catch (evmc_status_code statusCode)
43+
{
44+
result.status_code = statusCode;
45+
}
4246
catch (dev::eth::RevertInstruction& ex)
4347
{
4448
result.status_code = EVMC_REVERT;
4549
result.gas_left = vm->m_io_gas;
4650
output = ex.output(); // This moves the output from the exception!
4751
}
48-
catch (dev::eth::InvalidInstruction const&)
49-
{
50-
result.status_code = EVMC_INVALID_INSTRUCTION;
51-
}
52-
catch (dev::eth::BadInstruction const&)
53-
{
54-
result.status_code = EVMC_UNDEFINED_INSTRUCTION;
55-
}
56-
catch (dev::eth::OutOfStack const&)
57-
{
58-
result.status_code = EVMC_STACK_OVERFLOW;
59-
}
60-
catch (dev::eth::StackUnderflow const&)
61-
{
62-
result.status_code = EVMC_STACK_UNDERFLOW;
63-
}
64-
catch (dev::eth::BufferOverrun const&)
65-
{
66-
result.status_code = EVMC_INVALID_MEMORY_ACCESS;
67-
}
68-
catch (dev::eth::OutOfGas const&)
69-
{
70-
result.status_code = EVMC_OUT_OF_GAS;
71-
}
72-
catch (dev::eth::BadJumpDestination const&)
73-
{
74-
result.status_code = EVMC_BAD_JUMP_DESTINATION;
75-
}
76-
catch (dev::eth::DisallowedStateChange const&)
77-
{
78-
result.status_code = EVMC_STATIC_MODE_VIOLATION;
79-
}
8052
catch (dev::eth::VMException const&)
8153
{
8254
result.status_code = EVMC_FAILURE;
@@ -163,10 +135,10 @@ void VM::adjustStack(int _required, int _change)
163135

164136
// adjust stack and check bounds
165137
if (m_stackEnd < m_SP + _required)
166-
throwBadStack(_required, _change);
138+
throwBadStack(_required);
167139
m_SPP -= _change;
168140
if (m_SPP < m_stack)
169-
throwBadStack(_required, _change);
141+
throwBadStack(_required);
170142
}
171143

172144
uint64_t VM::gasForMem(intx::uint512 const& _size)
@@ -1004,7 +976,7 @@ void VM::interpretCases()
1004976
throwBadInstruction();
1005977
intx::uint512 const endOfAccess = intx::uint512(m_SP[1]) + intx::uint512(m_SP[2]);
1006978
if (m_returnData.size() < endOfAccess)
1007-
throwBufferOverrun(endOfAccess);
979+
throwBufferOverrun();
1008980

1009981
m_copyMemSize = toInt63(m_SP[2]);
1010982
updateMem(memNeed(m_SP[0], m_SP[2]));

libaleth-interpreter/VM.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ class VM
119119

120120
const evmc_tx_context& getTxContext();
121121

122-
void throwOutOfGas();
123-
void throwInvalidInstruction();
124-
void throwBadInstruction();
125-
void throwBadJumpDestination();
126-
void throwBadStack(int _removed, int _added);
127-
void throwRevertInstruction(owning_bytes_ref&& _output);
128-
void throwDisallowedStateChange();
129-
void throwBufferOverrun(intx::uint512 const& _enfOfAccess);
122+
static void throwOutOfGas();
123+
static void throwInvalidInstruction();
124+
static void throwBadInstruction();
125+
static void throwBadJumpDestination();
126+
void throwBadStack(int _removed);
127+
static void throwRevertInstruction(owning_bytes_ref&& _output);
128+
static void throwDisallowedStateChange();
129+
static void throwBufferOverrun();
130130

131131
std::vector<uint64_t> m_beginSubs;
132132
std::vector<uint64_t> m_jumpDests;

libaleth-interpreter/VMCalls.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,38 @@ void VM::copyDataToMemory(bytesConstRef _data, intx::uint256*_sp)
2727

2828
void VM::throwOutOfGas()
2929
{
30-
BOOST_THROW_EXCEPTION(OutOfGas());
30+
throw EVMC_OUT_OF_GAS;
3131
}
3232

3333
void VM::throwInvalidInstruction()
3434
{
35-
BOOST_THROW_EXCEPTION(InvalidInstruction());
35+
throw EVMC_INVALID_INSTRUCTION;
3636
}
3737

3838
void VM::throwBadInstruction()
3939
{
40-
BOOST_THROW_EXCEPTION(BadInstruction());
40+
throw EVMC_UNDEFINED_INSTRUCTION;
4141
}
4242

4343
void VM::throwBadJumpDestination()
4444
{
45-
BOOST_THROW_EXCEPTION(BadJumpDestination());
45+
throw EVMC_BAD_JUMP_DESTINATION;
4646
}
4747

4848
void VM::throwDisallowedStateChange()
4949
{
50-
BOOST_THROW_EXCEPTION(DisallowedStateChange());
50+
throw EVMC_STATIC_MODE_VIOLATION;
5151
}
5252

5353
// throwBadStack is called from fetchInstruction() -> adjustStack()
5454
// its the only exception that can happen before ON_OP() log is done for an opcode case in VM.cpp
5555
// so the call to m_onFail is needed here
56-
void VM::throwBadStack(int _required, int _change)
56+
void VM::throwBadStack(int _required)
5757
{
58-
bigint size = m_stackEnd - m_SPP;
59-
if (size < _required)
60-
BOOST_THROW_EXCEPTION(StackUnderflow() << RequirementError((bigint)_required, size));
58+
if (m_stackEnd - m_SPP < _required)
59+
throw EVMC_STACK_UNDERFLOW;
6160
else
62-
BOOST_THROW_EXCEPTION(OutOfStack() << RequirementError((bigint)_change, size));
61+
throw EVMC_STACK_OVERFLOW;
6362
}
6463

6564
void VM::throwRevertInstruction(owning_bytes_ref&& _output)
@@ -69,11 +68,9 @@ void VM::throwRevertInstruction(owning_bytes_ref&& _output)
6968
throw RevertInstruction(std::move(_output));
7069
}
7170

72-
void VM::throwBufferOverrun(intx::uint512 const& _endOfAccess)
71+
void VM::throwBufferOverrun()
7372
{
74-
BOOST_THROW_EXCEPTION(
75-
BufferOverrun() << RequirementError(
76-
bigint(std::string("0x") + intx::hex(_endOfAccess)), bigint(m_returnData.size())));
73+
throw EVMC_INVALID_MEMORY_ACCESS;
7774
}
7875

7976
int64_t VM::verifyJumpDest(intx::uint256 const& _dest, bool _throw)

0 commit comments

Comments
 (0)