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

Commit

Permalink
interpreter: Inline throws
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Nov 3, 2019
1 parent 6848f24 commit 5b0accd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 84 deletions.
70 changes: 36 additions & 34 deletions libaleth-interpreter/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ uint64_t VM::gasForMem(intx::uint512 const& _size)
void VM::updateIOGas()
{
if (m_io_gas < m_runGas)
throwOutOfGas();
throw EVMC_OUT_OF_GAS;
m_io_gas -= m_runGas;
}

Expand All @@ -158,7 +158,7 @@ void VM::updateGas()
m_runGas += toInt63(gasForMem(m_newMemSize) - gasForMem(m_mem.size()));
m_runGas += (VMSchedule::copyGas * ((m_copyMemSize + 31) / 32));
if (m_io_gas < m_runGas)
throwOutOfGas();
throw EVMC_OUT_OF_GAS;
}

void VM::updateMem(uint64_t _newMem)
Expand Down Expand Up @@ -239,9 +239,9 @@ void VM::interpretCases()
{
ON_OP();
if (m_rev < EVMC_CONSTANTINOPLE)
throwBadInstruction();
throw EVMC_BAD_JUMP_DESTINATION;
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

m_bounce = &VM::caseCreate;
}
Expand All @@ -251,7 +251,7 @@ void VM::interpretCases()
{
ON_OP();
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

m_bounce = &VM::caseCreate;
}
Expand All @@ -264,11 +264,11 @@ void VM::interpretCases()
{
ON_OP();
if (m_OP == Instruction::DELEGATECALL && m_rev < EVMC_HOMESTEAD)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;
if (m_OP == Instruction::STATICCALL && m_rev < EVMC_BYZANTIUM)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;
if (m_OP == Instruction::CALL && m_message->flags & EVMC_STATIC && m_SP[2] != 0)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;
m_bounce = &VM::caseCall;
}
BREAK
Expand All @@ -291,22 +291,24 @@ void VM::interpretCases()
{
// Pre-byzantium
if (m_rev < EVMC_BYZANTIUM)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;

ON_OP();
m_copyMemSize = 0;
updateMem(memNeed(m_SP[0], m_SP[1]));
updateIOGas();

throwRevertInstruction(static_cast<uint64_t>(m_SP[0]), static_cast<uint64_t>(m_SP[1]));
m_output = owning_bytes_ref{
std::move(m_mem), static_cast<uint64_t>(m_SP[0]), static_cast<uint64_t>(m_SP[1])};
throw EVMC_REVERT;
}
BREAK;

CASE(SELFDESTRUCT)
{
ON_OP();
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

auto const destination = intx::be::trunc<evmc::address>(m_SP[0]);

Expand Down Expand Up @@ -393,7 +395,7 @@ void VM::interpretCases()
{
ON_OP();
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

logGasMem();
updateIOGas();
Expand All @@ -410,7 +412,7 @@ void VM::interpretCases()
{
ON_OP();
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

logGasMem();
updateIOGas();
Expand All @@ -430,7 +432,7 @@ void VM::interpretCases()
{
ON_OP();
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

logGasMem();
updateIOGas();
Expand All @@ -451,7 +453,7 @@ void VM::interpretCases()
{
ON_OP();
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

logGasMem();
updateIOGas();
Expand All @@ -472,7 +474,7 @@ void VM::interpretCases()
{
ON_OP();
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

logGasMem();
updateIOGas();
Expand Down Expand Up @@ -681,7 +683,7 @@ void VM::interpretCases()
{
// Pre-constantinople
if (m_rev < EVMC_CONSTANTINOPLE)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;

ON_OP();
updateIOGas();
Expand All @@ -697,7 +699,7 @@ void VM::interpretCases()
{
// Pre-constantinople
if (m_rev < EVMC_CONSTANTINOPLE)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;

ON_OP();
updateIOGas();
Expand All @@ -713,7 +715,7 @@ void VM::interpretCases()
{
// Pre-constantinople
if (m_rev < EVMC_CONSTANTINOPLE)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;

ON_OP();
updateIOGas();
Expand Down Expand Up @@ -781,7 +783,7 @@ void VM::interpretCases()
CASE(JUMPTO) CASE(JUMPIF) CASE(JUMPV) CASE(JUMPSUB) CASE(JUMPSUBV) CASE(RETURNSUB)
CASE(BEGINSUB) CASE(BEGINDATA) CASE(GETLOCAL) CASE(PUTLOCAL)
{
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;
}
CONTINUE

Expand Down Expand Up @@ -817,7 +819,7 @@ void VM::interpretCases()
CASE(XPUT)
CASE(XGET)
CASE(XSWIZZLE)
CASE(XSHUFFLE) { throwBadInstruction(); }
CASE(XSHUFFLE) { throw EVMC_UNDEFINED_INSTRUCTION; }
CONTINUE

CASE(ADDRESS)
Expand Down Expand Up @@ -905,7 +907,7 @@ void VM::interpretCases()
CASE(RETURNDATASIZE)
{
if (m_rev < EVMC_BYZANTIUM)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;

ON_OP();
updateIOGas();
Expand Down Expand Up @@ -950,10 +952,10 @@ void VM::interpretCases()
{
ON_OP();
if (m_rev < EVMC_BYZANTIUM)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;
intx::uint512 const endOfAccess = intx::uint512(m_SP[1]) + intx::uint512(m_SP[2]);
if (m_returnData.size() < endOfAccess)
throwBufferOverrun();
throw EVMC_INVALID_MEMORY_ACCESS;

m_copyMemSize = toInt63(m_SP[2]);
updateMem(memNeed(m_SP[0], m_SP[2]));
Expand All @@ -967,7 +969,7 @@ void VM::interpretCases()
{
ON_OP();
if (m_rev < EVMC_CONSTANTINOPLE)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;

updateIOGas();

Expand Down Expand Up @@ -1091,7 +1093,7 @@ void VM::interpretCases()
ON_OP();

if (m_rev < EVMC_ISTANBUL)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;

updateIOGas();

Expand All @@ -1104,7 +1106,7 @@ void VM::interpretCases()
ON_OP();

if (m_rev < EVMC_ISTANBUL)
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;

updateIOGas();

Expand Down Expand Up @@ -1138,7 +1140,7 @@ void VM::interpretCases()
m_SPP[0] = m_pool[off];
TRACE_VAL(2, "Retrieved pooled const", m_SPP[0]);
#else
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;
#endif
}
CONTINUE
Expand Down Expand Up @@ -1225,7 +1227,7 @@ void VM::interpretCases()

m_PC = uint64_t(m_SP[0]);
#else
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;
#endif
}
CONTINUE
Expand All @@ -1241,7 +1243,7 @@ void VM::interpretCases()
else
++m_PC;
#else
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;
#endif
}
CONTINUE
Expand Down Expand Up @@ -1304,10 +1306,10 @@ void VM::interpretCases()
{
ON_OP();
if (m_message->flags & EVMC_STATIC)
throwDisallowedStateChange();
throw EVMC_STATIC_MODE_VIOLATION;

if (m_rev >= EVMC_ISTANBUL && m_io_gas <= VMSchedule::callStipend)
throwOutOfGas();
throw EVMC_OUT_OF_GAS;

auto const key = intx::be::store<evmc_uint256be>(m_SP[0]);
auto const value = intx::be::store<evmc_uint256be>(m_SP[1]);
Expand Down Expand Up @@ -1373,9 +1375,9 @@ void VM::interpretCases()
CASE(INVALID) DEFAULT
{
if (m_OP == Instruction::INVALID)
throwInvalidInstruction();
throw EVMC_INVALID_INSTRUCTION;
else
throwBadInstruction();
throw EVMC_UNDEFINED_INSTRUCTION;
}
}
WHILE_CASES
Expand Down
12 changes: 2 additions & 10 deletions libaleth-interpreter/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,6 @@ class VM

const evmc_tx_context& getTxContext();

static void throwOutOfGas();
static void throwInvalidInstruction();
static void throwBadInstruction();
static void throwBadJumpDestination();
void throwRevertInstruction(uint64_t _offset, uint64_t _size);
static void throwDisallowedStateChange();
static void throwBufferOverrun();

std::vector<uint64_t> m_beginSubs;
std::vector<uint64_t> m_jumpDests;
int64_t verifyJumpDest(intx::uint256 const& _dest, bool _throw = true);
Expand All @@ -146,7 +138,7 @@ class VM
{
// check for overflow
if (v > 0x7FFFFFFFFFFFFFFF)
throwOutOfGas();
throw EVMC_OUT_OF_GAS;
uint64_t w = uint64_t(v);
return w;
}
Expand All @@ -155,7 +147,7 @@ class VM
{
// check for overflow
if (v > 0x7FFF)
throwOutOfGas();
throw EVMC_OUT_OF_GAS;
uint64_t w = uint64_t(v);
return w;
}
Expand Down
41 changes: 1 addition & 40 deletions libaleth-interpreter/VMCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,6 @@ void VM::copyDataToMemory(bytesConstRef _data, intx::uint256*_sp)
std::memset(m_mem.data() + offset + sizeToBeCopied, 0, size - sizeToBeCopied);
}


// consolidate exception throws to avoid spraying boost code all over interpreter

void VM::throwOutOfGas()
{
throw EVMC_OUT_OF_GAS;
}

void VM::throwInvalidInstruction()
{
throw EVMC_INVALID_INSTRUCTION;
}

void VM::throwBadInstruction()
{
throw EVMC_UNDEFINED_INSTRUCTION;
}

void VM::throwBadJumpDestination()
{
throw EVMC_BAD_JUMP_DESTINATION;
}

void VM::throwDisallowedStateChange()
{
throw EVMC_STATIC_MODE_VIOLATION;
}

void VM::throwRevertInstruction(uint64_t _offset, uint64_t _size)
{
m_output = owning_bytes_ref{std::move(m_mem), _offset, _size};
throw EVMC_REVERT;
}

void VM::throwBufferOverrun()
{
throw EVMC_INVALID_MEMORY_ACCESS;
}

int64_t VM::verifyJumpDest(intx::uint256 const& _dest, bool _throw)
{
// check for overflow
Expand All @@ -73,7 +34,7 @@ int64_t VM::verifyJumpDest(intx::uint256 const& _dest, bool _throw)
return pc;
}
if (_throw)
throwBadJumpDestination();
throw EVMC_BAD_JUMP_DESTINATION;
return -1;
}

Expand Down

0 comments on commit 5b0accd

Please sign in to comment.