Skip to content

Commit

Permalink
Replace InvalidOpcode exception with AssemblyImportException and asserts
Browse files Browse the repository at this point in the history
- The only case where it needs to be an exception is assembly import. In other situations we don't catch it so it's effectively an assert.
  • Loading branch information
cameel committed Nov 29, 2024
1 parent 6634b9e commit c127395
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 15 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
2 changes: 0 additions & 2 deletions libevmasm/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ struct OptimizerException: virtual AssemblyException {};
struct StackTooDeepException: virtual OptimizerException {};
struct ItemNotAvailableException: virtual OptimizerException {};

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: 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 c127395

Please sign in to comment.