mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Split out disassemble to remove numeric from instruction includes.
This commit is contained in:
parent
2bcb027533
commit
b0dcd7b915
@ -26,6 +26,7 @@
|
||||
#include <libevmasm/Exceptions.h>
|
||||
#include <liblangutil/SourceLocation.h>
|
||||
#include <libsolutil/Common.h>
|
||||
#include <libsolutil/Numeric.h>
|
||||
#include <libsolutil/Assertions.h>
|
||||
#include <optional>
|
||||
#include <iostream>
|
||||
|
@ -11,6 +11,8 @@ set(sources
|
||||
ConstantOptimiser.h
|
||||
ControlFlowGraph.cpp
|
||||
ControlFlowGraph.h
|
||||
Disassemble.cpp
|
||||
Disassemble.h
|
||||
Exceptions.h
|
||||
ExpressionClasses.cpp
|
||||
ExpressionClasses.h
|
||||
|
@ -26,6 +26,7 @@
|
||||
|
||||
#include <liblangutil/EVMVersion.h>
|
||||
|
||||
#include <libsolutil/Numeric.h>
|
||||
#include <libsolutil/Assertions.h>
|
||||
|
||||
#include <vector>
|
||||
|
76
libevmasm/Disassemble.cpp
Normal file
76
libevmasm/Disassemble.cpp
Normal 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
38
libevmasm/Disassemble.h
Normal 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 = " ");
|
||||
|
||||
}
|
@ -22,10 +22,6 @@
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
|
||||
#include <libsolutil/Common.h>
|
||||
#include <libsolutil/CommonIO.h>
|
||||
#include <functional>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
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 } }
|
||||
};
|
||||
|
||||
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)
|
||||
{
|
||||
try
|
||||
@ -380,7 +329,7 @@ InstructionInfo solidity::evmasm::instructionInfo(Instruction _inst)
|
||||
}
|
||||
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});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,8 +25,6 @@
|
||||
#include <libevmasm/Exceptions.h>
|
||||
#include <libsolutil/Common.h>
|
||||
#include <libsolutil/Assertions.h>
|
||||
#include <libsolutil/Numeric.h>
|
||||
#include <functional>
|
||||
|
||||
namespace solidity::evmasm
|
||||
{
|
||||
@ -217,25 +215,25 @@ inline bool isLogInstruction(Instruction _inst)
|
||||
/// @returns the number of PUSH 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
|
||||
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
|
||||
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
|
||||
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
|
||||
@ -266,7 +264,7 @@ inline Instruction logInstruction(unsigned _number)
|
||||
return Instruction(unsigned(Instruction::LOG0) + _number);
|
||||
}
|
||||
|
||||
enum class Tier : unsigned
|
||||
enum class Tier
|
||||
{
|
||||
Zero = 0, // 0, Zero
|
||||
Base, // 2, Quick
|
||||
@ -301,10 +299,4 @@ bool isValidInstruction(Instruction _inst);
|
||||
/// Convert from string mnemonic to Instruction type.
|
||||
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 = " ");
|
||||
|
||||
}
|
||||
|
@ -26,6 +26,9 @@
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace solidity::evmasm
|
||||
{
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
||||
#include <libyul/Exceptions.h>
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
#include <libevmasm/Disassemble.h>
|
||||
|
||||
#include <libsmtutil/Exceptions.h>
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <liblangutil/EVMVersion.h>
|
||||
#include <libsolutil/Numeric.h>
|
||||
#include <libevmasm/Instruction.h>
|
||||
|
||||
namespace solidity::yul
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include <libyul/YulString.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
#include <libsolutil/Numeric.h>
|
||||
|
||||
#include <liblangutil/EVMVersion.h>
|
||||
#include <liblangutil/SourceLocation.h>
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include <libyul/AssemblyStack.h>
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
#include <libevmasm/Disassemble.h>
|
||||
#include <libevmasm/GasMeter.h>
|
||||
|
||||
#include <liblangutil/Exceptions.h>
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <libsolutil/Exceptions.h>
|
||||
#include <liblangutil/EVMVersion.h>
|
||||
#include <liblangutil/Exceptions.h>
|
||||
#include <libsolutil/Numeric.h>
|
||||
|
||||
#include <test/evmc/evmc.h>
|
||||
|
||||
@ -47,7 +48,7 @@ static constexpr auto heraFilename = "libhera.so";
|
||||
static constexpr auto heraDownloadLink = "https://github.com/ewasm/hera/releases/download/v0.5.0/hera-0.5.0-linux-x86_64.tar.gz";
|
||||
#endif
|
||||
|
||||
struct ConfigException : public util::Exception {};
|
||||
struct ConfigException: public util::Exception {};
|
||||
|
||||
struct CommonOptions
|
||||
{
|
||||
|
@ -23,6 +23,7 @@
|
||||
|
||||
#include <libevmasm/Assembly.h>
|
||||
#include <libsolutil/JSON.h>
|
||||
#include <libevmasm/Disassemble.h>
|
||||
#include <libyul/Exceptions.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include <test/libsolidity/SolidityExecutionFramework.h>
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
#include <libevmasm/Disassemble.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include <libyul/AssemblyStack.h>
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
#include <libevmasm/Disassemble.h>
|
||||
|
||||
#include <liblangutil/DebugInfoSelection.h>
|
||||
#include <liblangutil/SourceReferenceFormatter.h>
|
||||
|
Loading…
Reference in New Issue
Block a user