Skip to content

EVM Storage integration #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions libevmasm/GasMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ GasMeter::GasConsumption GasMeter::estimateMax(AssemblyItem const& _item, bool _
gas = GasConsumption::infinite();
break;
}
case Instruction::IMPORTLOCAL:
gas = runGas(Instruction::IMPORTLOCAL);
break;
case Instruction::DROP:
gas = runGas(Instruction::DROP);
break;
case Instruction::INIT:
gas = runGas(Instruction::INIT);
break;
case Instruction::RETRIEVE:
gas = runGas(Instruction::RETRIEVE);
break;
case Instruction::CALL:
case Instruction::CALLCODE:
case Instruction::DELEGATECALL:
Expand Down Expand Up @@ -201,6 +213,9 @@ GasMeter::GasConsumption GasMeter::estimateMax(AssemblyItem const& _item, bool _
case Instruction::SELFBALANCE:
gas = runGas(Instruction::SELFBALANCE);
break;
case Instruction::CALLACTOR:
gas = runGas(Instruction::CALLACTOR);
break;
default:
gas = runGas(_item.instruction());
break;
Expand Down
19 changes: 15 additions & 4 deletions libevmasm/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@ std::map<std::string, Instruction> const solidity::evmasm::c_instructions =
{ "CREATE2", Instruction::CREATE2 },
{ "REVERT", Instruction::REVERT },
{ "INVALID", Instruction::INVALID },
{ "SELFDESTRUCT", Instruction::SELFDESTRUCT }
{ "SELFDESTRUCT", Instruction::SELFDESTRUCT },
{ "CALLACTOR", Instruction::CALLACTOR },
{ "IMPORTLOCAL", Instruction::IMPORTLOCAL },
{ "DROP", Instruction::DROP },
{ "RETRIEVE", Instruction::RETRIEVE },
{ "INIT", Instruction::INIT }

};

static std::map<Instruction, InstructionInfo> const c_instructionInfo =
Expand Down Expand Up @@ -319,9 +325,14 @@ static std::map<Instruction, InstructionInfo> const c_instructionInfo =
{ Instruction::DELEGATECALL, { "DELEGATECALL", 0, 6, 1, true, Tier::Special } },
{ Instruction::STATICCALL, { "STATICCALL", 0, 6, 1, true, Tier::Special } },
{ Instruction::CREATE2, { "CREATE2", 0, 4, 1, true, Tier::Special } },
{ Instruction::REVERT, { "REVERT", 0, 2, 0, true, Tier::Zero } },
{ Instruction::REVERT, { "REVERT", 0, 2, 0, true, Tier::Zero } },
{ Instruction::INVALID, { "INVALID", 0, 0, 0, true, Tier::Zero } },
{ Instruction::SELFDESTRUCT, { "SELFDESTRUCT", 0, 1, 0, true, Tier::Special } }
{ Instruction::SELFDESTRUCT, { "SELFDESTRUCT", 0, 1, 0, true, Tier::Special } },
{ Instruction::CALLACTOR, { "CALLACTOR", 0, 5, 1, true, Tier::Base } },
{ Instruction::IMPORTLOCAL, { "IMPORTLOCAL", 0, 3, 0, true, Tier::Base } },
{ Instruction::DROP, { "DROP", 0, 1, 0, true, Tier::Base } },
{ Instruction::RETRIEVE, { "RETRIEVE", 0, 11, 0, true, Tier::Base } },
{ Instruction::INIT, { "INIT", 0, 12, 0, true, Tier::Base } }
};

void solidity::evmasm::eachInstruction(
Expand Down Expand Up @@ -385,4 +396,4 @@ InstructionInfo solidity::evmasm::instructionInfo(Instruction _inst)
bool solidity::evmasm::isValidInstruction(Instruction _inst)
{
return !!c_instructionInfo.count(_inst);
}
}
9 changes: 7 additions & 2 deletions libevmasm/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ enum class Instruction: uint8_t
EIP615_RETURNSUB, ///< return to subroutine jumped from -- not part of Instructions.cpp
EIP615_PUTLOCAL, ///< pop top of stack to local variable -- not part of Instructions.cpp
EIP615_GETLOCAL, ///< push local variable to top of stack -- not part of Instructions.cpp

IMPORTLOCAL = 0xc0, ///< import data to local storage
DROP, ///< drop id from local storage
RETRIEVE, ///< get data to local storage
INIT, ///< init deal and get CID of it

CREATE = 0xf0, ///< create a new account with associated code
CALL, ///< message-call into an account
Expand All @@ -193,7 +198,7 @@ enum class Instruction: uint8_t
DELEGATECALL, ///< like CALLCODE but keeps caller's value and sender
CREATE2 = 0xf5, ///< create new account with associated code at address `sha3(0xff + sender + salt + init code) % 2**160`
STATICCALL = 0xfa, ///< like CALL but disallow state modifications

CALLACTOR, ///< call contract actor
REVERT = 0xfd, ///< halt execution, revert state and return output data
INVALID = 0xfe, ///< invalid instruction for expressing runtime errors (e.g., division-by-zero)
SELFDESTRUCT = 0xff ///< halt execution and register account for later deletion
Expand Down Expand Up @@ -316,4 +321,4 @@ void eachInstruction(bytes const& _mem, std::function<void(Instruction,u256 cons
/// Convert from EVM code to simple EVM assembly language.
std::string disassemble(bytes const& _mem);

}
}
7 changes: 6 additions & 1 deletion libevmasm/SemanticInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ SemanticInformation::Effect SemanticInformation::memory(Instruction _instruction
case Instruction::CALLCODE:
case Instruction::DELEGATECALL:
case Instruction::STATICCALL:
case Instruction::CALLACTOR:
case Instruction::INIT:
case Instruction::IMPORTLOCAL:
return SemanticInformation::Write;

case Instruction::CREATE:
Expand All @@ -277,6 +280,8 @@ SemanticInformation::Effect SemanticInformation::memory(Instruction _instruction
case Instruction::LOG2:
case Instruction::LOG3:
case Instruction::LOG4:
case Instruction::DROP:
case Instruction::RETRIEVE:
return SemanticInformation::Read;

default:
Expand Down Expand Up @@ -409,4 +414,4 @@ bool SemanticInformation::invalidInViewFunctions(Instruction _instruction)
break;
}
return false;
}
}
11 changes: 10 additions & 1 deletion libsolidity/analysis/GlobalContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ int magicVariableToID(std::string const& _name)
else if (_name == "tx") return -26;
else if (_name == "type") return -27;
else if (_name == "this") return -28;
else if (_name == "callactor") return -29;
else if (_name == "init") return -30;
else if (_name == "drop") return -31;
else if (_name == "importData") return -32;
else if (_name == "retrieve") return -33;
else
solAssert(false, "Unknown magic variable: \"" + _name + "\".");
}
Expand All @@ -70,7 +75,6 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
static auto const magicVarDecl = [](string const& _name, Type const* _type) {
return make_shared<MagicVariableDeclaration>(magicVariableToID(_name), _name, _type);
};

return {
magicVarDecl("abi", TypeProvider::magic(MagicType::Kind::ABI)),
magicVarDecl("addmod", TypeProvider::function(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::AddMod, false, StateMutability::Pure)),
Expand All @@ -92,6 +96,11 @@ inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariable
magicVarDecl("sha256", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::SHA256, false, StateMutability::Pure)),
magicVarDecl("sha3", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::KECCAK256, false, StateMutability::Pure)),
magicVarDecl("suicide", TypeProvider::function(strings{"address payable"}, strings{}, FunctionType::Kind::Selfdestruct)),
magicVarDecl("callactor", TypeProvider::function(strings{"address payable", "uint256", "bytes memory"}, strings{"bytes memory"}, FunctionType::Kind::CallActor, false, StateMutability::Payable)),
magicVarDecl("init", TypeProvider::function(strings{"string memory", "string memory", "string memory", "uint256", "uint256", "int256", "bool", "bool","address payable"}, strings{}, FunctionType::Kind::Init, false, StateMutability::Payable)),
magicVarDecl("drop", TypeProvider::function(strings{"uint256"}, strings{}, FunctionType::Kind::Drop, false, StateMutability::Pure)),
magicVarDecl("importData", TypeProvider::function(strings{"string memory", "bool"}, strings{}, FunctionType::Kind::ImportData, false, StateMutability::Pure)),
magicVarDecl("retrieve", TypeProvider::function(strings{"string memory", "string memory", "string memory", "string memory", "address payable", "uint256", "bool"}, strings{}, FunctionType::Kind::Retrieve, false, StateMutability::Payable)),
magicVarDecl("tx", TypeProvider::magic(MagicType::Kind::Transaction)),
// Accepts a MagicType that can be any contract type or an Integer type and returns a
// MagicType. The TypeChecker handles the correctness of the input and output types.
Expand Down
11 changes: 11 additions & 0 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2869,6 +2869,11 @@ string FunctionType::richIdentifier() const
case Kind::ObjectCreation: id += "objectcreation"; break;
case Kind::Assert: id += "assert"; break;
case Kind::Require: id += "require"; break;
case Kind::CallActor: id += "callactor"; break;
case Kind::Init: id += "init"; break;
case Kind::Drop: id += "drop"; break;
case Kind::ImportData: id += "importData"; break;
case Kind::Retrieve: id += "retrieve"; break;
case Kind::ABIEncode: id += "abiencode"; break;
case Kind::ABIEncodePacked: id += "abiencodepacked"; break;
case Kind::ABIEncodeWithSelector: id += "abiencodewithselector"; break;
Expand Down Expand Up @@ -3131,6 +3136,11 @@ MemberList::MemberMap FunctionType::nativeMembers(ASTNode const* _scope) const
{
switch (m_kind)
{
case Kind::ImportData:
case Kind::Init:
case Kind::Drop:
case Kind::Retrieve:
return MemberList::MemberMap();
case Kind::Declaration:
if (declaration().isPartOfExternalInterface())
return {{"selector", TypeProvider::fixedBytes(4)}};
Expand All @@ -3156,6 +3166,7 @@ MemberList::MemberMap FunctionType::nativeMembers(ASTNode const* _scope) const
case Kind::Creation:
case Kind::BareCall:
case Kind::BareCallCode:
case Kind::CallActor:
case Kind::BareDelegateCall:
case Kind::BareStaticCall:
{
Expand Down
5 changes: 5 additions & 0 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,11 @@ class FunctionType: public Type
ObjectCreation, ///< array creation using new
Assert, ///< assert()
Require, ///< require()
CallActor, ///< CALLACTOR
ImportData, ///< ImportData
Init, ///< Init
Drop, ///< Drop
Retrieve, ///< Retrieve
ABIEncode,
ABIEncodePacked,
ABIEncodeWithSelector,
Expand Down
112 changes: 110 additions & 2 deletions libsolidity/codegen/ExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
{
auto functionCallKind = *_functionCall.annotation().kind;

CompilerContext::LocationSetter locationSetter(m_context, _functionCall);
if (functionCallKind == FunctionCallKind::TypeConversion)
{
Expand Down Expand Up @@ -564,7 +564,47 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
TypePointers parameterTypes = functionType->parameterTypes();

vector<ASTPointer<Expression const>> const& arguments = _functionCall.sortedArguments();

// lambda-function to convert "bytes memory" or "string memory" types
auto appendArrayMemory = [this, &arguments](size_t arg, bool first = false) {
arguments[arg]->accept(*this);
TypePointers target = {TypeProvider::array(DataLocation::Memory, true)};
if (!first)
{
// stack: [prev_size prev_ptr value]
m_context << Instruction::DUP3 << Instruction::DUP3;

// stack: [prev_size prev_ptr value prev_size prev_ptr]
m_context << Instruction::ADD;

// stack: [prev_size prev_ptr value new_ptr_start]
m_context << Instruction::DUP1 << Instruction::DUP1;

// stack: [prev_size prev_ptr value new_ptr_start new_ptr_start new_ptr_start]

m_context << Instruction::SWAP3 << Instruction::SWAP1;

// stack: [prev_size prev_ptr new_ptr_start new_ptr_start value new_ptr_start]
utils().packedEncode(
{arguments[arg]->annotation().type},
target
);

// stack: [prev_size prev_ptr new_ptr_start new_ptr_start new_ptr_end]
m_context << Instruction::SUB;

// stack: [prev_size prev_ptr new_ptr_start new_size]
m_context << Instruction::SWAP1;
// stack: [prev_size prev_ptr new_size new_ptr_start]
}
else{
utils().fetchFreeMemoryPointer();
utils().packedEncode(
{arguments[arg]->annotation().type},
target
);
utils().toSizeAfterFreeMemoryPointer();
}
};
if (functionCallKind == FunctionCallKind::StructConstructorCall)
{
TypeType const& type = dynamic_cast<TypeType const&>(*_functionCall.expression().annotation().type);
Expand Down Expand Up @@ -914,6 +954,74 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
m_context << Instruction::BLOCKHASH;
break;
}
case FunctionType::Kind::CallActor:
{
acceptAndConvert(*arguments[0], *function.parameterTypes()[0], true);
acceptAndConvert(*arguments[1], *function.parameterTypes()[1], true);
appendArrayMemory(2, true);
m_context << Instruction::CALLACTOR;
utils().returnDataToArray();
break;
}
case FunctionType::Kind::Init:
{
// Params:
//
// 0) string memory
// 1) string memory
// 2) string memory
// 3) uint256
// 4) uint256
// 5) int256
// 6) bool
// 7) bool
// 8) address payable
appendArrayMemory(0, true);
appendArrayMemory(1);
appendArrayMemory(2);
acceptAndConvert(*arguments[3], *function.parameterTypes()[3], true);
acceptAndConvert(*arguments[4], *function.parameterTypes()[4], true);
acceptAndConvert(*arguments[5], *function.parameterTypes()[5], true);
acceptAndConvert(*arguments[6], *function.parameterTypes()[6], true);
acceptAndConvert(*arguments[7], *function.parameterTypes()[7], true);
acceptAndConvert(*arguments[8], *function.parameterTypes()[8], true);
m_context << Instruction::INIT;
break;
}
case FunctionType::Kind::ImportData:
{
appendArrayMemory(0, true);
acceptAndConvert(*arguments[1], *function.parameterTypes()[1], true);
m_context << Instruction::IMPORTLOCAL;
break;
}
case FunctionType::Kind::Drop:
{
acceptAndConvert(*arguments[0], *function.parameterTypes()[0], true);
m_context << Instruction::DROP;
break;
}
case FunctionType::Kind::Retrieve:
{
// Params:
//
// 0) string memory
// 1) string memory
// 2) string memory
// 3) string memory
// 4) address payable
// 5) uint256
// 6) bool
appendArrayMemory(0, true);
appendArrayMemory(1);
appendArrayMemory(2);
appendArrayMemory(3);
acceptAndConvert(*arguments[4], *function.parameterTypes()[4], true);
acceptAndConvert(*arguments[5], *function.parameterTypes()[5], true);
acceptAndConvert(*arguments[6], *function.parameterTypes()[6], true);
m_context << Instruction::RETRIEVE;
break;
}
case FunctionType::Kind::AddMod:
case FunctionType::Kind::MulMod:
{
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/codegen/ir/IRGeneratorForStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
else
define(_memberAccess) << formatNumber(integerType->max()) << "\n";
}
else if (set<string>{"encode", "encodePacked", "encodeWithSelector", "encodeWithSignature", "decode"}.count(member))
else if (set<string>{"encode", "encodePacked", "encodeWithSelector", "encodeWithSignature", "decode", "callactor", "init", "drop", "importData", "retrieve"}.count(member))
{
// no-op
}
Expand Down
1 change: 1 addition & 0 deletions libsolutil/Algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <functional>
#include <set>
#include <list>

namespace solidity::util
{
Expand Down
21 changes: 21 additions & 0 deletions test/tools/yulInterpreter/EVMInstructionInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ u256 EVMInstructionInterpreter::eval(
accessMemory(arg[0], arg[1]);
logTrace(_instruction, arg);
return 0;
// --------------- storage ---------------
case Instruction::RETRIEVE:
accessMemory(arg[0], arg[1]);
accessMemory(arg[2], arg[3]);
accessMemory(arg[6], arg[7]);
accessMemory(arg[8], arg[9]);
return 0;
case Instruction::INIT:
accessMemory(arg[0], arg[1]);
accessMemory(arg[3], arg[4]);
accessMemory(arg[9], arg[10]);
return 0;
case Instruction::IMPORTLOCAL:
accessMemory(arg[0], arg[1]);
return 0;
case Instruction::DROP:
return 0;
// --------------- calls ---------------
case Instruction::CREATE:
accessMemory(arg[1], arg[2]);
Expand All @@ -322,6 +339,10 @@ u256 EVMInstructionInterpreter::eval(
accessMemory(arg[4], arg[5]);
logTrace(_instruction, arg);
return 0;
case Instruction::CALLACTOR:
accessMemory(arg[2], arg[3]);
logTrace(_instruction, arg);
return 0;
case Instruction::RETURN:
{
bytes data;
Expand Down