Merge pull request #12729 from ethereum/splitOutDisassemble

Split out disassemble to remove numeric from instruction includes.
This commit is contained in:
chriseth 2022-03-02 16:18:17 +01:00 committed by GitHub
commit d118f21417
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 136 additions and 67 deletions

View File

@ -26,6 +26,7 @@
#include <libevmasm/Exceptions.h> #include <libevmasm/Exceptions.h>
#include <liblangutil/SourceLocation.h> #include <liblangutil/SourceLocation.h>
#include <libsolutil/Common.h> #include <libsolutil/Common.h>
#include <libsolutil/Numeric.h>
#include <libsolutil/Assertions.h> #include <libsolutil/Assertions.h>
#include <optional> #include <optional>
#include <iostream> #include <iostream>

View File

@ -11,6 +11,8 @@ set(sources
ConstantOptimiser.h ConstantOptimiser.h
ControlFlowGraph.cpp ControlFlowGraph.cpp
ControlFlowGraph.h ControlFlowGraph.h
Disassemble.cpp
Disassemble.h
Exceptions.h Exceptions.h
ExpressionClasses.cpp ExpressionClasses.cpp
ExpressionClasses.h ExpressionClasses.h

View File

@ -26,6 +26,7 @@
#include <liblangutil/EVMVersion.h> #include <liblangutil/EVMVersion.h>
#include <libsolutil/Numeric.h>
#include <libsolutil/Assertions.h> #include <libsolutil/Assertions.h>
#include <vector> #include <vector>

76
libevmasm/Disassemble.cpp Normal file
View File

@ -0,0 +1,76 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libevmasm/Disassemble.h>
#include <libsolutil/Common.h>
#include <libsolutil/CommonIO.h>
#include <functional>
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::evmasm;
void solidity::evmasm::eachInstruction(
bytes const& _mem,
function<void(Instruction,u256 const&)> const& _onInstruction
)
{
for (auto it = _mem.begin(); it < _mem.end(); ++it)
{
Instruction const instr{*it};
int additional = 0;
if (isValidInstruction(instr))
additional = instructionInfo(instr).additional;
u256 data{};
// fill the data with the additional data bytes from the instruction stream
while (additional > 0 && std::next(it) < _mem.end())
{
data <<= 8;
data |= *++it;
--additional;
}
// pad the remaining number of additional octets with zeros
data <<= 8 * additional;
_onInstruction(instr, data);
}
}
string solidity::evmasm::disassemble(bytes const& _mem, string const& _delimiter)
{
stringstream ret;
eachInstruction(_mem, [&](Instruction _instr, u256 const& _data) {
if (!isValidInstruction(_instr))
ret << "0x" << std::uppercase << std::hex << static_cast<int>(_instr) << _delimiter;
else
{
InstructionInfo info = instructionInfo(_instr);
ret << info.name;
if (info.additional)
ret << " 0x" << std::uppercase << std::hex << _data;
ret << _delimiter;
}
});
return ret.str();
}

38
libevmasm/Disassemble.h Normal file
View File

@ -0,0 +1,38 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolutil/Common.h>
#include <libsolutil/Numeric.h>
#include <libevmasm/Instruction.h>
#include <functional>
#include <string>
namespace solidity::evmasm
{
/// Iterate through EVM code and call a function on each instruction.
void eachInstruction(bytes const& _mem, std::function<void(Instruction, u256 const&)> const& _onInstruction);
/// Convert from EVM code to simple EVM assembly language.
std::string disassemble(bytes const& _mem, std::string const& _delimiter = " ");
}

View File

@ -22,10 +22,6 @@
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libsolutil/Common.h>
#include <libsolutil/CommonIO.h>
#include <functional>
using namespace std; using namespace std;
using namespace solidity; using namespace solidity;
using namespace solidity::util; using namespace solidity::util;
@ -325,53 +321,6 @@ static std::map<Instruction, InstructionInfo> const c_instructionInfo =
{ Instruction::SELFDESTRUCT, { "SELFDESTRUCT", 0, 1, 0, true, Tier::Special } } { Instruction::SELFDESTRUCT, { "SELFDESTRUCT", 0, 1, 0, true, Tier::Special } }
}; };
void solidity::evmasm::eachInstruction(
bytes const& _mem,
function<void(Instruction,u256 const&)> const& _onInstruction
)
{
for (auto it = _mem.begin(); it < _mem.end(); ++it)
{
auto instr = Instruction(*it);
int additional = 0;
if (isValidInstruction(instr))
additional = instructionInfo(instr).additional;
u256 data;
// fill the data with the additional data bytes from the instruction stream
while (additional > 0 && std::next(it) < _mem.end())
{
data <<= 8;
data |= *++it;
--additional;
}
// pad the remaining number of additional octets with zeros
data <<= 8 * additional;
_onInstruction(instr, data);
}
}
string solidity::evmasm::disassemble(bytes const& _mem, string const& _delimiter)
{
stringstream ret;
eachInstruction(_mem, [&](Instruction _instr, u256 const& _data) {
if (!isValidInstruction(_instr))
ret << "0x" << std::uppercase << std::hex << static_cast<int>(_instr) << _delimiter;
else
{
InstructionInfo info = instructionInfo(_instr);
ret << info.name;
if (info.additional)
ret << " 0x" << std::uppercase << std::hex << _data;
ret << _delimiter;
}
});
return ret.str();
}
InstructionInfo solidity::evmasm::instructionInfo(Instruction _inst) InstructionInfo solidity::evmasm::instructionInfo(Instruction _inst)
{ {
try try
@ -380,7 +329,7 @@ InstructionInfo solidity::evmasm::instructionInfo(Instruction _inst)
} }
catch (...) catch (...)
{ {
return InstructionInfo({"<INVALID_INSTRUCTION: " + toString((unsigned)_inst) + ">", 0, 0, 0, false, Tier::Invalid}); return InstructionInfo({"<INVALID_INSTRUCTION: " + to_string(static_cast<unsigned>(_inst)) + ">", 0, 0, 0, false, Tier::Invalid});
} }
} }

View File

@ -25,8 +25,6 @@
#include <libevmasm/Exceptions.h> #include <libevmasm/Exceptions.h>
#include <libsolutil/Common.h> #include <libsolutil/Common.h>
#include <libsolutil/Assertions.h> #include <libsolutil/Assertions.h>
#include <libsolutil/Numeric.h>
#include <functional>
namespace solidity::evmasm namespace solidity::evmasm
{ {
@ -217,25 +215,25 @@ inline bool isLogInstruction(Instruction _inst)
/// @returns the number of PUSH Instruction _inst /// @returns the number of PUSH Instruction _inst
inline unsigned getPushNumber(Instruction _inst) inline unsigned getPushNumber(Instruction _inst)
{ {
return (uint8_t)_inst - unsigned(Instruction::PUSH1) + 1; return static_cast<uint8_t>(_inst) - unsigned(Instruction::PUSH1) + 1;
} }
/// @returns the number of DUP Instruction _inst /// @returns the number of DUP Instruction _inst
inline unsigned getDupNumber(Instruction _inst) inline unsigned getDupNumber(Instruction _inst)
{ {
return (uint8_t)_inst - unsigned(Instruction::DUP1) + 1; return static_cast<uint8_t>(_inst) - unsigned(Instruction::DUP1) + 1;
} }
/// @returns the number of SWAP Instruction _inst /// @returns the number of SWAP Instruction _inst
inline unsigned getSwapNumber(Instruction _inst) inline unsigned getSwapNumber(Instruction _inst)
{ {
return (uint8_t)_inst - unsigned(Instruction::SWAP1) + 1; return static_cast<uint8_t>(_inst) - unsigned(Instruction::SWAP1) + 1;
} }
/// @returns the number of LOG Instruction _inst /// @returns the number of LOG Instruction _inst
inline unsigned getLogNumber(Instruction _inst) inline unsigned getLogNumber(Instruction _inst)
{ {
return (uint8_t)_inst - unsigned(Instruction::LOG0); return static_cast<uint8_t>(_inst) - unsigned(Instruction::LOG0);
} }
/// @returns the PUSH<_number> instruction /// @returns the PUSH<_number> instruction
@ -266,7 +264,7 @@ inline Instruction logInstruction(unsigned _number)
return Instruction(unsigned(Instruction::LOG0) + _number); return Instruction(unsigned(Instruction::LOG0) + _number);
} }
enum class Tier : unsigned enum class Tier
{ {
Zero = 0, // 0, Zero Zero = 0, // 0, Zero
Base, // 2, Quick Base, // 2, Quick
@ -301,10 +299,4 @@ bool isValidInstruction(Instruction _inst);
/// Convert from string mnemonic to Instruction type. /// Convert from string mnemonic to Instruction type.
extern const std::map<std::string, Instruction> c_instructions; extern const std::map<std::string, Instruction> c_instructions;
/// Iterate through EVM code and call a function on each instruction.
void eachInstruction(bytes const& _mem, std::function<void(Instruction, u256 const&)> const& _onInstruction);
/// Convert from EVM code to simple EVM assembly language.
std::string disassemble(bytes const& _mem, std::string const& _delimiter = " ");
} }

View File

@ -26,6 +26,9 @@
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <optional>
#include <vector>
namespace solidity::evmasm namespace solidity::evmasm
{ {

View File

@ -29,7 +29,7 @@
#include <libyul/Exceptions.h> #include <libyul/Exceptions.h>
#include <libyul/optimiser/Suite.h> #include <libyul/optimiser/Suite.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Disassemble.h>
#include <libsmtutil/Exceptions.h> #include <libsmtutil/Exceptions.h>

View File

@ -23,6 +23,7 @@
#include <libyul/optimiser/ASTWalker.h> #include <libyul/optimiser/ASTWalker.h>
#include <liblangutil/EVMVersion.h> #include <liblangutil/EVMVersion.h>
#include <libsolutil/Numeric.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
namespace solidity::yul namespace solidity::yul

View File

@ -27,6 +27,7 @@
#include <libyul/YulString.h> #include <libyul/YulString.h>
#include <libsolutil/CommonData.h> #include <libsolutil/CommonData.h>
#include <libsolutil/Numeric.h>
#include <liblangutil/EVMVersion.h> #include <liblangutil/EVMVersion.h>
#include <liblangutil/SourceLocation.h> #include <liblangutil/SourceLocation.h>

View File

@ -44,6 +44,7 @@
#include <libyul/AssemblyStack.h> #include <libyul/AssemblyStack.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libevmasm/Disassemble.h>
#include <libevmasm/GasMeter.h> #include <libevmasm/GasMeter.h>
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>

View File

@ -21,6 +21,7 @@
#include <libsolutil/Exceptions.h> #include <libsolutil/Exceptions.h>
#include <liblangutil/EVMVersion.h> #include <liblangutil/EVMVersion.h>
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
#include <libsolutil/Numeric.h>
#include <test/evmc/evmc.h> #include <test/evmc/evmc.h>

View File

@ -23,6 +23,7 @@
#include <libevmasm/Assembly.h> #include <libevmasm/Assembly.h>
#include <libsolutil/JSON.h> #include <libsolutil/JSON.h>
#include <libevmasm/Disassemble.h>
#include <libyul/Exceptions.h> #include <libyul/Exceptions.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>

View File

@ -25,6 +25,7 @@
#include <test/libsolidity/SolidityExecutionFramework.h> #include <test/libsolidity/SolidityExecutionFramework.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libevmasm/Disassemble.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>

View File

@ -25,6 +25,7 @@
#include <libyul/AssemblyStack.h> #include <libyul/AssemblyStack.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libevmasm/Disassemble.h>
#include <liblangutil/DebugInfoSelection.h> #include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/SourceReferenceFormatter.h> #include <liblangutil/SourceReferenceFormatter.h>