Merge remote-tracking branch 'origin/develop' into breaking

This commit is contained in:
chriseth
2022-03-07 16:34:55 +01:00
259 changed files with 2810 additions and 1360 deletions
-20
View File
@@ -397,26 +397,6 @@ AssemblyItem Assembly::newImmutableAssignment(string const& _identifier)
return AssemblyItem{AssignImmutable, h};
}
Assembly& Assembly::optimise(bool _enable, EVMVersion _evmVersion, bool _isCreation, size_t _runs)
{
OptimiserSettings settings;
settings.isCreation = _isCreation;
settings.runInliner = true;
settings.runJumpdestRemover = true;
settings.runPeephole = true;
if (_enable)
{
settings.runDeduplicate = true;
settings.runCSE = true;
settings.runConstantOptimiser = true;
}
settings.evmVersion = _evmVersion;
settings.expectedExecutionsPerDeployment = _runs;
optimise(settings);
return *this;
}
Assembly& Assembly::optimise(OptimiserSettings const& _settings)
{
optimiseInternal(_settings, {});
-7
View File
@@ -134,13 +134,6 @@ public:
/// is optimised according to the settings in @a _settings.
Assembly& optimise(OptimiserSettings const& _settings);
/// Modify (if @a _enable is set) and return the current assembly such that creation and
/// execution gas usage is optimised. @a _isCreation should be true for the top-level assembly.
/// @a _runs specifes an estimate on how often each opcode in this assembly will be executed,
/// i.e. use a small value to optimise for size and a large value to optimise for runtime.
/// If @a _enable is not set, will perform some simple peephole optimizations.
Assembly& optimise(bool _enable, langutil::EVMVersion _evmVersion, bool _isCreation, size_t _runs);
/// Create a text representation of the assembly.
std::string assemblyString(
langutil::DebugInfoSelection const& _debugInfoSelection = langutil::DebugInfoSelection::Default(),
+1
View File
@@ -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>
+2
View File
@@ -11,6 +11,8 @@ set(sources
ConstantOptimiser.h
ControlFlowGraph.cpp
ControlFlowGraph.h
Disassemble.cpp
Disassemble.h
Exceptions.h
ExpressionClasses.cpp
ExpressionClasses.h
+1
View File
@@ -26,6 +26,7 @@
#include <liblangutil/EVMVersion.h>
#include <libsolutil/Numeric.h>
#include <libsolutil/Assertions.h>
#include <vector>
+76
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
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 = " ");
}
+1 -52
View File
@@ -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});
}
}
+5 -13
View File
@@ -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 = " ");
}
+125 -6
View File
@@ -29,6 +29,125 @@ using namespace std;
using namespace solidity;
using namespace solidity::evmasm;
vector<SemanticInformation::Operation> SemanticInformation::readWriteOperations(Instruction _instruction)
{
switch (_instruction)
{
case Instruction::SSTORE:
case Instruction::SLOAD:
{
assertThrow(memory(_instruction) == Effect::None, OptimizerException, "");
assertThrow(storage(_instruction) != Effect::None, OptimizerException, "");
Operation op;
op.effect = storage(_instruction);
op.location = Location::Storage;
op.startParameter = 0;
// We know that exactly one slot is affected.
op.lengthConstant = 1;
return {op};
}
case Instruction::MSTORE:
case Instruction::MSTORE8:
case Instruction::MLOAD:
{
assertThrow(memory(_instruction) != Effect::None, OptimizerException, "");
assertThrow(storage(_instruction) == Effect::None, OptimizerException, "");
Operation op;
op.effect = memory(_instruction);
op.location = Location::Memory;
op.startParameter = 0;
if (_instruction == Instruction::MSTORE || _instruction == Instruction::MLOAD)
op.lengthConstant = 32;
else if (_instruction == Instruction::MSTORE8)
op.lengthConstant = 1;
return {op};
}
case Instruction::REVERT:
case Instruction::RETURN:
case Instruction::KECCAK256:
case Instruction::LOG0:
case Instruction::LOG1:
case Instruction::LOG2:
case Instruction::LOG3:
case Instruction::LOG4:
{
assertThrow(storage(_instruction) == Effect::None, OptimizerException, "");
assertThrow(memory(_instruction) == Effect::Read, OptimizerException, "");
Operation op;
op.effect = memory(_instruction);
op.location = Location::Memory;
op.startParameter = 0;
op.lengthParameter = 1;
return {op};
}
case Instruction::EXTCODECOPY:
{
assertThrow(memory(_instruction) == Effect::Write, OptimizerException, "");
assertThrow(storage(_instruction) == Effect::None, OptimizerException, "");
Operation op;
op.effect = memory(_instruction);
op.location = Location::Memory;
op.startParameter = 1;
op.lengthParameter = 3;
return {op};
}
case Instruction::CODECOPY:
case Instruction::CALLDATACOPY:
case Instruction::RETURNDATACOPY:
{
assertThrow(memory(_instruction) == Effect::Write, OptimizerException, "");
assertThrow(storage(_instruction) == Effect::None, OptimizerException, "");
Operation op;
op.effect = memory(_instruction);
op.location = Location::Memory;
op.startParameter = 0;
op.lengthParameter = 2;
return {op};
}
case Instruction::STATICCALL:
case Instruction::CALL:
case Instruction::CALLCODE:
case Instruction::DELEGATECALL:
{
size_t paramCount = static_cast<size_t>(instructionInfo(_instruction).args);
vector<Operation> operations{
Operation{Location::Memory, Effect::Read, paramCount - 4, paramCount - 3, {}},
Operation{Location::Storage, Effect::Read, {}, {}, {}}
};
if (_instruction != Instruction::STATICCALL)
operations.emplace_back(Operation{Location::Storage, Effect::Write, {}, {}, {}});
operations.emplace_back(Operation{
Location::Memory,
Effect::Write,
paramCount - 2,
paramCount - 1,
{}
});
return operations;
}
case Instruction::CREATE:
case Instruction::CREATE2:
return vector<Operation>{
Operation{
Location::Memory,
Effect::Read,
1,
2,
{}
},
Operation{Location::Storage, Effect::Read, {}, {}, {}},
Operation{Location::Storage, Effect::Write, {}, {}, {}}
};
case Instruction::MSIZE:
// This is just to satisfy the assert below.
return vector<Operation>{};
default:
assertThrow(storage(_instruction) == None && memory(_instruction) == None, AssemblyException, "");
}
return {};
}
bool SemanticInformation::breaksCSEAnalysisBlock(AssemblyItem const& _item, bool _msizeImportant)
{
switch (_item.type())
@@ -49,7 +168,7 @@ bool SemanticInformation::breaksCSEAnalysisBlock(AssemblyItem const& _item, bool
case PushLibraryAddress:
case PushImmutable:
return false;
case Operation:
case evmasm::Operation:
{
if (isSwapInstruction(_item) || isDupInstruction(_item))
return false;
@@ -79,7 +198,7 @@ bool SemanticInformation::breaksCSEAnalysisBlock(AssemblyItem const& _item, bool
bool SemanticInformation::isCommutativeOperation(AssemblyItem const& _item)
{
if (_item.type() != Operation)
if (_item.type() != evmasm::Operation)
return false;
switch (_item.instruction())
{
@@ -97,14 +216,14 @@ bool SemanticInformation::isCommutativeOperation(AssemblyItem const& _item)
bool SemanticInformation::isDupInstruction(AssemblyItem const& _item)
{
if (_item.type() != Operation)
if (_item.type() != evmasm::Operation)
return false;
return evmasm::isDupInstruction(_item.instruction());
}
bool SemanticInformation::isSwapInstruction(AssemblyItem const& _item)
{
if (_item.type() != Operation)
if (_item.type() != evmasm::Operation)
return false;
return evmasm::isSwapInstruction(_item.instruction());
}
@@ -116,7 +235,7 @@ bool SemanticInformation::isJumpInstruction(AssemblyItem const& _item)
bool SemanticInformation::altersControlFlow(AssemblyItem const& _item)
{
if (_item.type() != Operation)
if (_item.type() != evmasm::Operation)
return false;
switch (_item.instruction())
{
@@ -166,7 +285,7 @@ bool SemanticInformation::isDeterministic(AssemblyItem const& _item)
{
assertThrow(_item.type() != VerbatimBytecode, AssemblyException, "");
if (_item.type() != Operation)
if (_item.type() != evmasm::Operation)
return true;
switch (_item.instruction())
+29
View File
@@ -26,6 +26,9 @@
#include <libevmasm/Instruction.h>
#include <optional>
#include <vector>
namespace solidity::evmasm
{
@@ -45,6 +48,32 @@ struct SemanticInformation
Write
};
enum class Location { Storage, Memory };
/**
* Represents a read or write operation from or to one of the data locations.
*/
struct Operation
{
Location location;
Effect effect;
/// Start of affected area as an index into the parameters.
/// Unknown if not provided.
std::optional<size_t> startParameter;
/// Length of the affected area as an index into the parameters (if this is an opcode).
/// Unknown if neither this nor lengthConstant is provided.
std::optional<size_t> lengthParameter;
/// Length as a constant.
/// Unknown if neither this nor lengthArgument is provided.
std::optional<size_t> lengthConstant;
};
/// @returns the sequence of read write operations performed by the instruction.
/// Order matters.
/// For external calls, there is just one unknown read and one unknown write operation,
/// event though there might be multiple.
static std::vector<Operation> readWriteOperations(Instruction _instruction);
/// @returns true if the given items starts a new block for common subexpression analysis.
/// @param _msizeImportant if false, consider an operation non-breaking if its only side-effect is that it modifies msize.
static bool breaksCSEAnalysisBlock(AssemblyItem const& _item, bool _msizeImportant);