Skip to content

Commit

Permalink
Merge pull request #15596 from ethereum/remove-superfluous-assembly-e…
Browse files Browse the repository at this point in the history
…xceptions

Replace `InvalidDeposit` and `InvalidOpcode` exceptions with asserts
  • Loading branch information
cameel authored Nov 29, 2024
2 parents 311e8db + c127395 commit d2a7eb3
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 20 deletions.
6 changes: 3 additions & 3 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ AssemblyItem Assembly::createAssemblyItemFromJSON(Json const& _json, std::vector
result = item;
}
else
solThrow(InvalidOpcode, "Invalid opcode: " + name);
solThrow(AssemblyImportException, "Invalid opcode (" + name + ")");
}
result.setLocation(location);
result.m_modifierDepth = modifierDepth;
Expand Down Expand Up @@ -1236,7 +1236,7 @@ LinkerObject const& Assembly::assembleLegacy() const
ret.bytecode += assembleTag(item, ret.bytecode.size(), true);
break;
default:
assertThrow(false, InvalidOpcode, "Unexpected opcode while assembling.");
solAssert(false, "Unexpected opcode while assembling.");
}
}

Expand Down Expand Up @@ -1469,7 +1469,7 @@ LinkerObject const& Assembly::assembleEOF() const
break;
}
default:
solThrow(InvalidOpcode, "Unexpected opcode while assembling.");
solAssert(false, "Unexpected opcode while assembling.");
}
}

Expand Down
4 changes: 2 additions & 2 deletions libevmasm/Assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ class Assembly
void appendToAuxiliaryData(bytes const& _data) { m_auxiliaryData += _data; }

int deposit() const { return m_deposit; }
void adjustDeposit(int _adjustment) { m_deposit += _adjustment; assertThrow(m_deposit >= 0, InvalidDeposit, ""); }
void setDeposit(int _deposit) { m_deposit = _deposit; assertThrow(m_deposit >= 0, InvalidDeposit, ""); }
void adjustDeposit(int _adjustment) { m_deposit += _adjustment; solAssert(m_deposit >= 0); }
void setDeposit(int _deposit) { m_deposit = _deposit; solAssert(m_deposit >= 0); }
std::string const& name() const { return m_name; }

/// Changes the source location used for each appended item.
Expand Down
3 changes: 0 additions & 3 deletions libevmasm/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,4 @@ struct OptimizerException: virtual AssemblyException {};
struct StackTooDeepException: virtual OptimizerException {};
struct ItemNotAvailableException: virtual OptimizerException {};

DEV_SIMPLE_EXCEPTION(InvalidDeposit);
DEV_SIMPLE_EXCEPTION(InvalidOpcode);

}
9 changes: 5 additions & 4 deletions libevmasm/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <libsolutil/Common.h>
#include <libsolutil/Assertions.h>
#include <liblangutil/EVMVersion.h>
#include <liblangutil/Exceptions.h>

namespace solidity::evmasm
{
Expand Down Expand Up @@ -267,28 +268,28 @@ inline unsigned getLogNumber(Instruction _inst)
/// @returns the PUSH<_number> instruction
inline Instruction pushInstruction(unsigned _number)
{
assertThrow(_number <= 32, InvalidOpcode, std::string("Invalid PUSH instruction requested (") + std::to_string(_number) + ").");
solAssert(_number <= 32);
return Instruction(unsigned(Instruction::PUSH0) + _number);
}

/// @returns the DUP<_number> instruction
inline Instruction dupInstruction(unsigned _number)
{
assertThrow(1 <= _number && _number <= 16, InvalidOpcode, std::string("Invalid DUP instruction requested (") + std::to_string(_number) + ").");
solAssert(1 <= _number && _number <= 16);
return Instruction(unsigned(Instruction::DUP1) + _number - 1);
}

/// @returns the SWAP<_number> instruction
inline Instruction swapInstruction(unsigned _number)
{
assertThrow(1 <= _number && _number <= 16, InvalidOpcode, std::string("Invalid SWAP instruction requested (") + std::to_string(_number) + ").");
solAssert(1 <= _number && _number <= 16);
return Instruction(unsigned(Instruction::SWAP1) + _number - 1);
}

/// @returns the LOG<_number> instruction
inline Instruction logInstruction(unsigned _number)
{
assertThrow(_number <= 4, InvalidOpcode, std::string("Invalid LOG instruction requested (") + std::to_string(_number) + ").");
solAssert(_number <= 4);
return Instruction(unsigned(Instruction::LOG0) + _number);
}

Expand Down
4 changes: 2 additions & 2 deletions libevmasm/KnownState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ KnownState::StoreOperation KnownState::feedItem(AssemblyItem const& _item, bool
}
else if (_item.type() != Operation)
{
assertThrow(_item.deposit() == 1, InvalidDeposit, "");
solAssert(_item.deposit() == 1);
if (_item.pushedValue())
// only available after assembly stage, should not be used for optimisation
setStackElement(++m_stackHeight, m_expressionClasses->find(*_item.pushedValue()));
Expand Down Expand Up @@ -194,7 +194,7 @@ KnownState::StoreOperation KnownState::feedItem(AssemblyItem const& _item, bool
resetStorage();
if (invMem || invStor)
m_sequenceNumber += 2; // Increment by two because it can read and write
assertThrow(info.ret <= 1, InvalidDeposit, "");
solAssert(info.ret <= 1);
if (info.ret == 1)
setStackElement(
m_stackHeight + static_cast<int>(_item.deposit()),
Expand Down
4 changes: 0 additions & 4 deletions libsolidity/interface/StandardCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,10 +1220,6 @@ Json StandardCompiler::importEVMAssembly(StandardCompiler::InputsAndSettings _in
{
return formatFatalError(Error::Type::Exception, "Assembly import error: " + std::string(e.what()));
}
catch (evmasm::InvalidOpcode const& e)
{
return formatFatalError(Error::Type::Exception, "Assembly import error: " + std::string(e.what()));
}
catch (...)
{
return formatError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"errors": [
{
"component": "general",
"formattedMessage": "Assembly import error: InvalidOpcode",
"message": "Assembly import error: InvalidOpcode",
"formattedMessage": "Assembly import error: Invalid opcode (INVALID_OPCODE)",
"message": "Assembly import error: Invalid opcode (INVALID_OPCODE)",
"severity": "error",
"type": "Exception"
}
Expand Down

0 comments on commit d2a7eb3

Please sign in to comment.