diff --git a/AST.cpp b/AST.cpp index 79ac314cd..d171006a8 100644 --- a/AST.cpp +++ b/AST.cpp @@ -27,6 +27,8 @@ #include #include +#include + using namespace std; namespace dev @@ -50,18 +52,17 @@ void ContractDefinition::checkTypeRequirements() function->checkTypeRequirements(); } -vector ContractDefinition::getInterfaceFunctions() const +map, FunctionDefinition const*> ContractDefinition::getInterfaceFunctions() const { - vector exportedFunctions; + map, FunctionDefinition const*> exportedFunctions; for (ASTPointer const& f: m_definedFunctions) if (f->isPublic() && f->getName() != getName()) - exportedFunctions.push_back(f.get()); - auto compareNames = [](FunctionDefinition const* _a, FunctionDefinition const* _b) - { - return _a->getName().compare(_b->getName()) < 0; - }; + { + FixedHash<4> hash(dev::sha3(f->getCanonicalSignature())); + auto res = exportedFunctions.insert(std::make_pair(hash,f.get())); + solAssert(res.second, "Hash collision at Function Definition Hash calculation"); + } - sort(exportedFunctions.begin(), exportedFunctions.end(), compareNames); return exportedFunctions; } diff --git a/AST.h b/AST.h index 8493d4323..95121d4cb 100644 --- a/AST.h +++ b/AST.h @@ -183,8 +183,9 @@ public: /// Can contain a nullptr in which case indicates absence of documentation ASTPointer const& getDocumentation() const { return m_documentation; } - /// Returns the functions that make up the calling interface in the intended order. - std::vector getInterfaceFunctions() const; + /// @returns a map of canonical function signatures to FunctionDefinitions + /// as intended for use by the ABI. + std::map, FunctionDefinition const*> getInterfaceFunctions() const; /// Returns the constructor or nullptr if no constructor was specified FunctionDefinition const* getConstructor() const; diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a0b62bdd..9c0b50775 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ endif() target_link_libraries(${EXECUTABLE} ${JSONCPP_LIBRARIES}) target_link_libraries(${EXECUTABLE} evmcore) target_link_libraries(${EXECUTABLE} devcore) +target_link_libraries(${EXECUTABLE} devcrypto) install( TARGETS ${EXECUTABLE} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) diff --git a/Compiler.cpp b/Compiler.cpp index 394ae5f84..4e5b7f558 100644 --- a/Compiler.cpp +++ b/Compiler.cpp @@ -100,7 +100,7 @@ void Compiler::appendConstructorCall(FunctionDefinition const& _constructor) { m_context << u256(argumentSize); m_context.appendProgramSize(); - m_context << u256(1); // copy it to byte one as expected for ABI calls + m_context << u256(CompilerUtils::dataStartOffset); // copy it to byte four as expected for ABI calls m_context << eth::Instruction::CODECOPY; appendCalldataUnpacker(_constructor, true); } @@ -118,35 +118,27 @@ set Compiler::getFunctionsNeededByConstructor(Functio void Compiler::appendFunctionSelector(ContractDefinition const& _contract) { - vector interfaceFunctions = _contract.getInterfaceFunctions(); - vector callDataUnpackerEntryPoints; + map, FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions(); + map, const eth::AssemblyItem> callDataUnpackerEntryPoints; - if (interfaceFunctions.size() > 255) - BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 255 public functions for contract.")); + // retrieve the function signature hash from the calldata + m_context << u256(1) << u256(0); + CompilerUtils(m_context).loadFromMemory(0, 4, false, true); - // retrieve the first byte of the call data, which determines the called function - // @todo This code had a jump table in a previous version which was more efficient but also - // error prone (due to the optimizer and variable length tag addresses) - m_context << u256(1) << u256(0) // some constants - << eth::dupInstruction(1) << eth::Instruction::CALLDATALOAD - << eth::dupInstruction(2) << eth::Instruction::BYTE - << eth::dupInstruction(2); - - // stack here: 1 0 0, stack top will be counted up until it matches funid - for (unsigned funid = 0; funid < interfaceFunctions.size(); ++funid) + // stack now is: 1 0 + // for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) + for (auto const& it: interfaceFunctions) { - callDataUnpackerEntryPoints.push_back(m_context.newTag()); - m_context << eth::dupInstruction(2) << eth::dupInstruction(2) << eth::Instruction::EQ; - m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.back()); - if (funid < interfaceFunctions.size() - 1) - m_context << eth::dupInstruction(4) << eth::Instruction::ADD; + callDataUnpackerEntryPoints.insert(std::make_pair(it.first, m_context.newTag())); + m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it.first)) << eth::Instruction::EQ; + m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.at(it.first)); } m_context << eth::Instruction::STOP; // function not found - for (unsigned funid = 0; funid < interfaceFunctions.size(); ++funid) + for (auto const& it: interfaceFunctions) { - FunctionDefinition const& function = *interfaceFunctions[funid]; - m_context << callDataUnpackerEntryPoints[funid]; + FunctionDefinition const& function = *it.second; + m_context << callDataUnpackerEntryPoints.at(it.first); eth::AssemblyItem returnTag = m_context.pushNewTag(); appendCalldataUnpacker(function); m_context.appendJumpTo(m_context.getFunctionEntryLabel(function)); @@ -158,7 +150,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory) { // We do not check the calldata size, everything is zero-padded. - unsigned dataOffset = 1; + unsigned dataOffset = CompilerUtils::dataStartOffset; // the 4 bytes of the function hash signature //@todo this can be done more efficiently, saving some CALLDATALOAD calls for (ASTPointer const& var: _function.getParameters()) { diff --git a/CompilerUtils.cpp b/CompilerUtils.cpp index 680e9190b..a5254b421 100644 --- a/CompilerUtils.cpp +++ b/CompilerUtils.cpp @@ -31,6 +31,8 @@ namespace dev namespace solidity { +const unsigned int CompilerUtils::dataStartOffset = 4; + void CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, bool _fromCalldata) { if (_bytes == 0) diff --git a/CompilerUtils.h b/CompilerUtils.h index 928f0e2d3..6bd8d3155 100644 --- a/CompilerUtils.h +++ b/CompilerUtils.h @@ -58,10 +58,14 @@ public: static unsigned getSizeOnStack(std::vector const& _variables); static unsigned getSizeOnStack(std::vector> const& _variableTypes); + /// Bytes we need to the start of call data. + /// - The size in bytes of the function (hash) identifier. + static const unsigned int dataStartOffset; private: CompilerContext& m_context; }; + template unsigned CompilerUtils::getSizeOnStack(std::vector const& _variables) { diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index 8e8f492e2..cf91a7630 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -401,7 +401,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess) case Type::Category::CONTRACT: { ContractType const& type = dynamic_cast(*_memberAccess.getExpression().getType()); - m_context << type.getFunctionIndex(member); + m_context << type.getFunctionIdentifier(member); break; } case Type::Category::MAGIC: @@ -663,7 +663,11 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio { solAssert(_arguments.size() == _functionType.getParameterTypes().size(), ""); - unsigned dataOffset = _options.bare ? 0 : 1; // reserve one byte for the function index + _options.obtainAddress(); + if (!_options.bare) + CompilerUtils(m_context).storeInMemory(0, CompilerUtils::dataStartOffset); + + unsigned dataOffset = _options.bare ? 0 : CompilerUtils::dataStartOffset; // reserve 4 bytes for the function's hash identifier for (unsigned i = 0; i < _arguments.size(); ++i) { _arguments[i]->accept(*this); @@ -690,12 +694,13 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio _options.obtainValue(); else m_context << u256(0); - _options.obtainAddress(); - if (!_options.bare) - m_context << u256(0) << eth::Instruction::MSTORE8; + m_context << eth::dupInstruction(6); //copy contract address + m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB << eth::Instruction::CALL - << eth::Instruction::POP; // @todo do not ignore failure indicator + << eth::Instruction::POP // @todo do not ignore failure indicator + << eth::Instruction::POP; // pop contract address + if (retSize > 0) { bool const leftAligned = firstType->getCategory() == Type::Category::STRING; diff --git a/InterfaceHandler.cpp b/InterfaceHandler.cpp index 06b9824c9..4ce6e989a 100644 --- a/InterfaceHandler.cpp +++ b/InterfaceHandler.cpp @@ -39,7 +39,7 @@ std::unique_ptr InterfaceHandler::getABIInterface(ContractDefinitio { Json::Value methods(Json::arrayValue); - for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) + for (auto const& it: _contractDef.getInterfaceFunctions()) { Json::Value method; Json::Value inputs(Json::arrayValue); @@ -58,10 +58,10 @@ std::unique_ptr InterfaceHandler::getABIInterface(ContractDefinitio return params; }; - method["name"] = f->getName(); - method["constant"] = f->isDeclaredConst(); - method["inputs"] = populateParameters(f->getParameters()); - method["outputs"] = populateParameters(f->getReturnParameters()); + method["name"] = it.second->getName(); + method["constant"] = it.second->isDeclaredConst(); + method["inputs"] = populateParameters(it.second->getParameters()); + method["outputs"] = populateParameters(it.second->getReturnParameters()); methods.append(method); } return std::unique_ptr(new std::string(m_writer.write(methods))); @@ -70,8 +70,9 @@ std::unique_ptr InterfaceHandler::getABIInterface(ContractDefinitio unique_ptr InterfaceHandler::getABISolidityInterface(ContractDefinition const& _contractDef) { string ret = "contract " + _contractDef.getName() + "{"; - for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) + for (auto const& it: _contractDef.getInterfaceFunctions()) { + FunctionDefinition const* f = it.second; auto populateParameters = [](vector> const& _vars) { string r = ""; @@ -94,10 +95,10 @@ std::unique_ptr InterfaceHandler::getUserDocumentation(ContractDefi Json::Value doc; Json::Value methods(Json::objectValue); - for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) + for (auto const& it: _contractDef.getInterfaceFunctions()) { Json::Value user; - auto strPtr = f->getDocumentation(); + auto strPtr = it.second->getDocumentation(); if (strPtr) { resetUser(); @@ -105,7 +106,7 @@ std::unique_ptr InterfaceHandler::getUserDocumentation(ContractDefi if (!m_notice.empty()) {// since @notice is the only user tag if missing function should not appear user["notice"] = Json::Value(m_notice); - methods[f->getName()] = user; + methods[it.second->getName()] = user; } } } @@ -135,10 +136,10 @@ std::unique_ptr InterfaceHandler::getDevDocumentation(ContractDefin doc["title"] = m_title; } - for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) + for (auto const& it: _contractDef.getInterfaceFunctions()) { Json::Value method; - auto strPtr = f->getDocumentation(); + auto strPtr = it.second->getDocumentation(); if (strPtr) { resetDev(); @@ -161,7 +162,7 @@ std::unique_ptr InterfaceHandler::getDevDocumentation(ContractDefin method["return"] = m_return; if (!method.empty()) // add the function, only if we have any documentation to add - methods[f->getName()] = method; + methods[it.second->getName()] = method; } } doc["methods"] = methods; diff --git a/Types.cpp b/Types.cpp index dcfc704a4..da042edb4 100644 --- a/Types.cpp +++ b/Types.cpp @@ -460,8 +460,8 @@ MemberList const& ContractType::getMembers() const if (!m_members) { map> members; - for (FunctionDefinition const* function: m_contract.getInterfaceFunctions()) - members[function->getName()] = make_shared(*function, false); + for (auto const& it: m_contract.getInterfaceFunctions()) + members[it.second->getName()] = make_shared(*it.second, false); m_members.reset(new MemberList(members)); } return *m_members; @@ -480,15 +480,13 @@ shared_ptr const& ContractType::getConstructorType() const return m_constructorType; } -unsigned ContractType::getFunctionIndex(string const& _functionName) const +u256 ContractType::getFunctionIdentifier(string const& _functionName) const { - unsigned index = 0; - for (FunctionDefinition const* function: m_contract.getInterfaceFunctions()) - { - if (function->getName() == _functionName) - return index; - ++index; - } + auto interfaceFunctions = m_contract.getInterfaceFunctions(); + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) + if (it->second->getName() == _functionName) + return FixedHash<4>::Arith(it->first); + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index of non-existing contract function requested.")); } diff --git a/Types.h b/Types.h index 828c53b9c..8c800fa0d 100644 --- a/Types.h +++ b/Types.h @@ -291,7 +291,7 @@ public: /// is not used, as this type cannot be the type of a variable or expression. std::shared_ptr const& getConstructorType() const; - unsigned getFunctionIndex(std::string const& _functionName) const; + u256 getFunctionIdentifier(std::string const& _functionName) const; private: ContractDefinition const& m_contract;