diff options
27 files changed, 744 insertions, 380 deletions
diff --git a/docs/common-patterns.rst b/docs/common-patterns.rst index 322be3ef..74f9c078 100644 --- a/docs/common-patterns.rst +++ b/docs/common-patterns.rst @@ -2,6 +2,110 @@ Common Patterns ############### +.. index:: withdrawal + +.. _withdrawal_pattern: + +************************* +Withdrawal from Contracts +************************* + +The recommended method of sending funds after an effect +is using the withdrawal pattern. Although the most intuitive +method of sending Ether, as a result of an effect, is a +direct ``send`` call, this is not recommended as it +introduces a potential security risk. You may read +more about this on the :ref:`security_considerations` page. + +This is an example of the withdrawal pattern in practice in +a contract where the goal is to send the most money to the +contract in order to become the "richest", inspired by +`King of the Ether <https://www.kingoftheether.com/>`_. + +In the following contract, if you are usurped as the richest, +you will recieve the funds of the person who has gone on to +become the new richest. + +:: + + contract WithdrawalContract { + address public richest; + uint public mostSent; + + mapping (address => uint) pendingWithdrawals; + + function WithdrawalContract() { + richest = msg.sender; + mostSent = msg.value; + } + + function becomeRichest() returns (bool) { + if (msg.value > mostSent) { + pendingWithdrawals[richest] += msg.value; + richest = msg.sender; + mostSent = msg.value; + return true; + } + else { + return false; + } + } + + function withdraw() returns (bool) { + uint amount = pendingWithdrawals[msg.sender]; + // Remember to zero the pending refund before + // sending to prevent re-entrancy attacks + pendingWithdrawals[msg.sender] = 0; + if (msg.sender.send(amount)) { + return true; + } + else { + pendingWithdrawals[msg.sender] = amount; + return false; + } + } + } + +This is as opposed to the more intuitive sending pattern. + +:: + + contract SendContract { + address public richest; + uint public mostSent; + + function SendContract() { + richest = msg.sender; + mostSent = msg.value; + } + + function becomeRichest() returns (bool) { + if (msg.value > mostSent) { + // Check if call succeeds to prevent an attacker + // from trapping the previous person's funds in + // this contract through a callstack attack + if (!richest.send(msg.value)) { + throw; + } + richest = msg.sender; + mostSent = msg.value; + return true; + } + else { + return false; + } + } + } + +Notice that, in this example, an attacker could trap the +contract into an unusable state by causing ``richest`` to be +the address of a contract that has a fallback function +which consumes more than the 2300 gas stipend. That way, +whenever ``send`` is called to deliver funds to the +"poisoned" contract, it will cause execution to always fail +because there will not be enough gas to finish the execution +of the fallback function. + .. index:: access;restricting ****************** diff --git a/docs/contracts.rst b/docs/contracts.rst index 3d592ecf..c369bfd9 100644 --- a/docs/contracts.rst +++ b/docs/contracts.rst @@ -314,14 +314,40 @@ inheritable properties of contracts and may be overridden by derived contracts. } } + contract Mutex { + bool locked; + modifier noReentrancy() { + if (locked) throw; + locked = true; + _ + locked = false; + } + + /// This function is protected by a mutex, which means that + /// reentrant calls from within msg.sender.call cannot call f again. + /// The `return 7` statement assigns 7 to the return value but still + /// executes the statement `locked = false` in the modifier. + function f() noReentrancy returns (uint) { + if (!msg.sender.call()) throw; + return 7; + } + } + Multiple modifiers can be applied to a function by specifying them in a -whitespace-separated list and will be evaluated in order. Explicit returns from -a modifier or function body immediately leave the whole function, while control -flow reaching the end of a function or modifier body continues after the "_" in -the preceding modifier. Arbitrary expressions are allowed for modifier -arguments and in this context, all symbols visible from the function are -visible in the modifier. Symbols introduced in the modifier are not visible in -the function (as they might change by overriding). +whitespace-separated list and will be evaluated in order. + +.. warning:: + In an earlier version of Solidity, ``return`` statements in functions + having modifiers behaved differently. + +Explicit returns from a modifier or function body only leave the current +modifier or function body. Return variables are assigned and +control flow continues after the "_" in the preceding modifier. + +Arbitrary expressions are allowed for modifier arguments and in this context, +all symbols visible from the function are visible in the modifier. Symbols +introduced in the modifier are not visible in the function (as they might +change by overriding). .. index:: ! constant @@ -896,7 +922,7 @@ custom types without the overhead of external function calls: As the compiler cannot know where the library will be deployed at, these addresses have to be filled into the final bytecode by a linker -(see :ref:`commandline-compiler`) on how to use the +(see :ref:`commandline-compiler` for how to use the commandline compiler for linking). If the addresses are not given as arguments to the compiler, the compiled hex code will contain placeholders of the form ``__Set______`` (where diff --git a/docs/control-structures.rst b/docs/control-structures.rst index a6daccac..73b0131f 100644 --- a/docs/control-structures.rst +++ b/docs/control-structures.rst @@ -69,6 +69,10 @@ this does not execute a constructor. We could also have used ``function setFeed( only (locally) sets the value and amount of gas sent with the function call and only the parentheses at the end perform the actual call. +Function calls cause exceptions if the called contract does not exist (in the +sense that the account does not contain code) or if the called contract itself +throws an exception or goes out of gas. + .. warning:: Any interaction with another contract imposes a potential danger, especially if the source code of the contract is not known in advance. The current @@ -295,11 +299,14 @@ In the following example, we show how ``throw`` can be used to easily revert an } } -Currently, there are three situations, where exceptions happen automatically in Solidity: +Currently, there are six situations, where exceptions happen automatically in Solidity: -1. If you access an array beyond its length (i.e. ``x[i]`` where ``i >= x.length``) +1. If you access an array beyond its length (i.e. ``x[i]`` where ``i >= x.length``). 2. If a function called via a message call does not finish properly (i.e. it runs out of gas or throws an exception itself). 3. If a non-existent function on a library is called or Ether is sent to a library. +4. If you divide or modulo by zero (e.g. ``5 / 0`` or ``23 % 0``). +5. If you perform an external function call targeting a contract that contains no code. +6. If a contract-creation call using the ``new`` keyword fails. Internally, Solidity performs an "invalid jump" when an exception is thrown and thus causes the EVM to revert all changes made to the state. The reason for this is that there is no safe way to continue execution, because an expected effect did not occur. Because we want to retain the atomicity of transactions, the safest thing to do is to revert all changes and make the whole transaction (or at least call) without effect. diff --git a/docs/index.rst b/docs/index.rst index 4b3ace89..a330172e 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -70,7 +70,7 @@ Solidity Tools * `Solidity REPL <https://github.com/raineorshine/solidity-repl>`_ Try Solidity instantly with a command-line Solidity console. - + * `solgraph <https://github.com/raineorshine/solgraph>`_ Visualize Solidity control flow and highlight potential security vulnerabilities. diff --git a/docs/installing-solidity.rst b/docs/installing-solidity.rst index 47388c25..16a02310 100644 --- a/docs/installing-solidity.rst +++ b/docs/installing-solidity.rst @@ -101,7 +101,7 @@ installed either by adding the Ethereum PPA (Option 1) or by backporting sudo apt-get -y update sudo apt-get -y upgrade sudo apt-get -y install libcryptopp-dev - + ## (Option 2) For those willing to backport libcrypto++: #sudo apt-get -y install ubuntu-dev-tools #sudo pbuilder create diff --git a/docs/layout-of-source-files.rst b/docs/layout-of-source-files.rst index ef6fd656..ae1e0d26 100644 --- a/docs/layout-of-source-files.rst +++ b/docs/layout-of-source-files.rst @@ -101,7 +101,7 @@ and then run the compiler as As a more complex example, suppose you rely on some module that uses a very old version of dapp-bin. That old version of dapp-bin is checked -out at ``/usr/local/dapp-bin_old``, then you can use +out at ``/usr/local/dapp-bin_old``, then you can use .. code-block:: bash diff --git a/docs/security-considerations.rst b/docs/security-considerations.rst index bae6e20b..eff3c5e8 100644 --- a/docs/security-considerations.rst +++ b/docs/security-considerations.rst @@ -1,3 +1,5 @@ +.. _security_considerations: + ####################### Security Considerations ####################### @@ -124,7 +126,7 @@ Sending and Receiving Ether because the operation is just too expensive) - it "runs out of gas" (OOG). If the return value of ``send`` is checked, this might provide a means for the recipient to block progress in the sending contract. Again, the best practice here is to use - a "withdraw" pattern instead of a "send" pattern. + a :ref:`"withdraw" pattern instead of a "send" pattern <withdrawal_pattern>`. Callstack Depth =============== diff --git a/docs/solidity-by-example.rst b/docs/solidity-by-example.rst index 7dd51f00..86d6f72b 100644 --- a/docs/solidity-by-example.rst +++ b/docs/solidity-by-example.rst @@ -191,6 +191,8 @@ contract into a blind auction where it is not possible to see the actual bid until the bidding period ends. +.. _simple_auction: + Simple Open Auction =================== @@ -269,7 +271,7 @@ activate themselves. // highestBidder.send(highestBid) is a security risk // because it can be prevented by the caller by e.g. // raising the call stack to 1023. It is always safer - // to let the recipient withdraw their money themselves. + // to let the recipient withdraw their money themselves. pendingReturns[highestBidder] += highestBid; } highestBidder = msg.sender; diff --git a/docs/types.rst b/docs/types.rst index 31f6b53d..d6445ed9 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -57,6 +57,8 @@ Operators: Division always truncates (it just maps to the DIV opcode of the EVM), but it does not truncate if both operators are :ref:`literals<rational_literals>` (or literal expressions). +Division by zero and modulus with zero throws an exception. + .. index:: address, balance, send, call, callcode, delegatecall .. _address: diff --git a/libevmasm/Assembly.cpp b/libevmasm/Assembly.cpp index 2024b1e9..c7822819 100644 --- a/libevmasm/Assembly.cpp +++ b/libevmasm/Assembly.cpp @@ -123,7 +123,7 @@ ostream& Assembly::streamAsm(ostream& _out, string const& _prefix, StringMap con _out << " " << instructionInfo(i.instruction()).name << "\t" << i.getJumpTypeAsString(); break; case Push: - _out << " PUSH " << hex << i.data(); + _out << " PUSH" << dec << max<unsigned>(1, dev::bytesRequired(i.data())) << " 0x" << hex << i.data(); break; case PushString: _out << " PUSH \"" << m_strings.at((h256)i.data()) << "\""; diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index c4134f4e..35fd0b7d 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -31,21 +31,10 @@ namespace dev namespace solidity { -void ASTJsonConverter::addKeyValue(Json::Value& _obj, string const& _key, string const& _val) -{ - // special handling for booleans - if (_key == "const" || _key == "public" || _key == "local" || - _key == "lvalue" || _key == "local_lvalue" || _key == "prefix") - _obj[_key] = (_val == "1") ? true : false; - else - // else simply add it as a string - _obj[_key] = _val; -} - void ASTJsonConverter::addJsonNode( ASTNode const& _node, string const& _nodeName, - initializer_list<pair<string const, string const>> _list, + initializer_list<pair<string const, Json::Value const>> _attributes, bool _hasChildren = false ) { @@ -54,11 +43,11 @@ void ASTJsonConverter::addJsonNode( node["id"] = reinterpret_cast<Json::UInt64>(&_node); node["src"] = sourceLocationToString(_node.location()); node["name"] = _nodeName; - if (_list.size() != 0) + if (_attributes.size() != 0) { Json::Value attrs; - for (auto& e: _list) - addKeyValue(attrs, e.first, e.second); + for (auto& e: _attributes) + attrs[e.first] = e.second; node["attributes"] = attrs; } @@ -89,11 +78,6 @@ ASTJsonConverter::ASTJsonConverter( map<string, unsigned> _sourceIndices ): m_ast(&_ast), m_sourceIndices(_sourceIndices) { - Json::Value children(Json::arrayValue); - - m_astJson["name"] = "root"; - m_astJson["children"] = children; - m_jsonNodePtrs.push(&m_astJson["children"]); } void ASTJsonConverter::print(ostream& _stream) @@ -108,21 +92,64 @@ Json::Value const& ASTJsonConverter::json() return m_astJson; } +bool ASTJsonConverter::visit(SourceUnit const&) +{ + Json::Value children(Json::arrayValue); + + m_astJson["name"] = "SourceUnit"; + m_astJson["children"] = children; + m_jsonNodePtrs.push(&m_astJson["children"]); + + return true; +} + bool ASTJsonConverter::visit(ImportDirective const& _node) { - addJsonNode(_node, "Import", { make_pair("file", _node.path())}); + addJsonNode(_node, "ImportDirective", { make_pair("file", _node.path())}); return true; } bool ASTJsonConverter::visit(ContractDefinition const& _node) { - addJsonNode(_node, "Contract", { make_pair("name", _node.name()) }, true); + Json::Value linearizedBaseContracts(Json::arrayValue); + for (auto const& baseContract: _node.annotation().linearizedBaseContracts) + linearizedBaseContracts.append(reinterpret_cast<Json::UInt64>(baseContract)); + addJsonNode(_node, "ContractDefinition", { + make_pair("name", _node.name()), + make_pair("isLibrary", _node.isLibrary()), + make_pair("fullyImplemented", _node.annotation().isFullyImplemented), + make_pair("linearizedBaseContracts", linearizedBaseContracts), + }, true); + return true; +} + +bool ASTJsonConverter::visit(InheritanceSpecifier const& _node) +{ + addJsonNode(_node, "InheritanceSpecifier", {}, true); + return true; +} + +bool ASTJsonConverter::visit(UsingForDirective const& _node) +{ + addJsonNode(_node, "UsingForDirective", {}, true); return true; } bool ASTJsonConverter::visit(StructDefinition const& _node) { - addJsonNode(_node, "Struct", { make_pair("name", _node.name()) }, true); + addJsonNode(_node, "StructDefinition", { make_pair("name", _node.name()) }, true); + return true; +} + +bool ASTJsonConverter::visit(EnumDefinition const& _node) +{ + addJsonNode(_node, "EnumDefinition", { make_pair("name", _node.name()) }, true); + return true; +} + +bool ASTJsonConverter::visit(EnumValue const& _node) +{ + addJsonNode(_node, "EnumValue", { make_pair("name", _node.name()) }); return true; } @@ -134,11 +161,11 @@ bool ASTJsonConverter::visit(ParameterList const& _node) bool ASTJsonConverter::visit(FunctionDefinition const& _node) { - addJsonNode(_node, "Function", - { make_pair("name", _node.name()), - make_pair("public", boost::lexical_cast<std::string>(_node.isPublic())), - make_pair("const", boost::lexical_cast<std::string>(_node.isDeclaredConst())) }, - true); + addJsonNode(_node, "FunctionDefinition", { + make_pair("name", _node.name()), + make_pair("public", _node.isPublic()), + make_pair("constant", _node.isDeclaredConst()) + }, true); return true; } @@ -146,16 +173,34 @@ bool ASTJsonConverter::visit(VariableDeclaration const& _node) { addJsonNode(_node, "VariableDeclaration", { make_pair("name", _node.name()), - make_pair("name", _node.name()), + make_pair("type", type(_node)) }, true); return true; } +bool ASTJsonConverter::visit(ModifierDefinition const& _node) +{ + addJsonNode(_node, "ModifierDefinition", { make_pair("name", _node.name()) }, true); + return true; +} + +bool ASTJsonConverter::visit(ModifierInvocation const& _node) +{ + addJsonNode(_node, "ModifierInvocation", {}, true); + return true; +} + bool ASTJsonConverter::visit(TypeName const&) { return true; } +bool ASTJsonConverter::visit(EventDefinition const& _node) +{ + addJsonNode(_node, "EventDefinition", { make_pair("name", _node.name()) }, true); + return true; +} + bool ASTJsonConverter::visit(ElementaryTypeName const& _node) { addJsonNode(_node, "ElementaryTypeName", { make_pair("name", _node.typeName().toString()) }); @@ -176,6 +221,12 @@ bool ASTJsonConverter::visit(Mapping const& _node) return true; } +bool ASTJsonConverter::visit(ArrayTypeName const& _node) +{ + addJsonNode(_node, "ArrayTypeName", {}, true); + return true; +} + bool ASTJsonConverter::visit(InlineAssembly const& _node) { addJsonNode(_node, "InlineAssembly", {}, true); @@ -188,6 +239,12 @@ bool ASTJsonConverter::visit(Block const& _node) return true; } +bool ASTJsonConverter::visit(PlaceholderStatement const& _node) +{ + addJsonNode(_node, "PlaceholderStatement", {}); + return true; +} + bool ASTJsonConverter::visit(IfStatement const& _node) { addJsonNode(_node, "IfStatement", {}, true); @@ -232,7 +289,7 @@ bool ASTJsonConverter::visit(Throw const& _node) bool ASTJsonConverter::visit(VariableDeclarationStatement const& _node) { - addJsonNode(_node, "VariableDefinition", {}, true); + addJsonNode(_node, "VariableDefinitionStatement", {}, true); return true; } @@ -266,7 +323,7 @@ bool ASTJsonConverter::visit(TupleExpression const& _node) bool ASTJsonConverter::visit(UnaryOperation const& _node) { addJsonNode(_node, "UnaryOperation", - { make_pair("prefix", boost::lexical_cast<std::string>(_node.isPrefixOperation())), + { make_pair("prefix", _node.isPrefixOperation()), make_pair("operator", Token::toString(_node.getOperator())), make_pair("type", type(_node)) }, true); @@ -285,7 +342,7 @@ bool ASTJsonConverter::visit(BinaryOperation const& _node) bool ASTJsonConverter::visit(FunctionCall const& _node) { addJsonNode(_node, "FunctionCall", { - make_pair("type_conversion", boost::lexical_cast<std::string>(_node.annotation().isTypeConversion)), + make_pair("type_conversion", _node.annotation().isTypeConversion), make_pair("type", type(_node)) }, true); return true; @@ -299,10 +356,10 @@ bool ASTJsonConverter::visit(NewExpression const& _node) bool ASTJsonConverter::visit(MemberAccess const& _node) { - addJsonNode(_node, "MemberAccess", - { make_pair("member_name", _node.memberName()), - make_pair("type", type(_node)) }, - true); + addJsonNode(_node, "MemberAccess", { + make_pair("member_name", _node.memberName()), + make_pair("type", type(_node)) + }, true); return true; } @@ -321,21 +378,29 @@ bool ASTJsonConverter::visit(Identifier const& _node) bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node) { - addJsonNode(_node, "ElementaryTypenameExpression", - { make_pair("value", _node.typeName().toString()), make_pair("type", type(_node)) }); + addJsonNode(_node, "ElementaryTypenameExpression", { + make_pair("value", _node.typeName().toString()), + make_pair("type", type(_node)) + }); return true; } bool ASTJsonConverter::visit(Literal const& _node) { char const* tokenString = Token::toString(_node.token()); - addJsonNode(_node, "Literal", - { make_pair("string", (tokenString) ? tokenString : "null"), - make_pair("value", _node.value()), - make_pair("type", type(_node)) }); + addJsonNode(_node, "Literal", { + make_pair("string", tokenString ? tokenString : Json::Value()), + make_pair("value", _node.value()), + make_pair("type", type(_node)) + }); return true; } +void ASTJsonConverter::endVisit(SourceUnit const&) +{ + goUp(); +} + void ASTJsonConverter::endVisit(ImportDirective const&) { } @@ -345,11 +410,30 @@ void ASTJsonConverter::endVisit(ContractDefinition const&) goUp(); } +void ASTJsonConverter::endVisit(InheritanceSpecifier const&) +{ + goUp(); +} + +void ASTJsonConverter::endVisit(UsingForDirective const&) +{ + goUp(); +} + void ASTJsonConverter::endVisit(StructDefinition const&) { goUp(); } +void ASTJsonConverter::endVisit(EnumDefinition const&) +{ + goUp(); +} + +void ASTJsonConverter::endVisit(EnumValue const&) +{ +} + void ASTJsonConverter::endVisit(ParameterList const&) { goUp(); @@ -365,6 +449,21 @@ void ASTJsonConverter::endVisit(VariableDeclaration const&) goUp(); } +void ASTJsonConverter::endVisit(ModifierDefinition const&) +{ + goUp(); +} + +void ASTJsonConverter::endVisit(ModifierInvocation const&) +{ + goUp(); +} + +void ASTJsonConverter::endVisit(EventDefinition const&) +{ + goUp(); +} + void ASTJsonConverter::endVisit(TypeName const&) { } @@ -382,6 +481,11 @@ void ASTJsonConverter::endVisit(Mapping const&) goUp(); } +void ASTJsonConverter::endVisit(ArrayTypeName const&) +{ + goUp(); +} + void ASTJsonConverter::endVisit(InlineAssembly const&) { goUp(); @@ -392,6 +496,10 @@ void ASTJsonConverter::endVisit(Block const&) goUp(); } +void ASTJsonConverter::endVisit(PlaceholderStatement const&) +{ +} + void ASTJsonConverter::endVisit(IfStatement const&) { goUp(); diff --git a/libsolidity/ast/ASTJsonConverter.h b/libsolidity/ast/ASTJsonConverter.h index ca4d9c2d..97aa8654 100644 --- a/libsolidity/ast/ASTJsonConverter.h +++ b/libsolidity/ast/ASTJsonConverter.h @@ -51,18 +51,28 @@ public: void print(std::ostream& _stream); Json::Value const& json(); + bool visit(SourceUnit const& _node) override; bool visit(ImportDirective const& _node) override; bool visit(ContractDefinition const& _node) override; + bool visit(InheritanceSpecifier const& _node) override; + bool visit(UsingForDirective const& _node) override; bool visit(StructDefinition const& _node) override; + bool visit(EnumDefinition const& _node) override; + bool visit(EnumValue const& _node) override; bool visit(ParameterList const& _node) override; bool visit(FunctionDefinition const& _node) override; bool visit(VariableDeclaration const& _node) override; + bool visit(ModifierDefinition const& _node) override; + bool visit(ModifierInvocation const& _node) override; + bool visit(EventDefinition const& _node) override; bool visit(TypeName const& _node) override; bool visit(ElementaryTypeName const& _node) override; bool visit(UserDefinedTypeName const& _node) override; bool visit(Mapping const& _node) override; + bool visit(ArrayTypeName const& _node) override; bool visit(InlineAssembly const& _node) override; bool visit(Block const& _node) override; + bool visit(PlaceholderStatement const& _node) override; bool visit(IfStatement const& _node) override; bool visit(WhileStatement const& _node) override; bool visit(ForStatement const& _node) override; @@ -85,18 +95,28 @@ public: bool visit(ElementaryTypeNameExpression const& _node) override; bool visit(Literal const& _node) override; + void endVisit(SourceUnit const&) override; void endVisit(ImportDirective const&) override; void endVisit(ContractDefinition const&) override; + void endVisit(InheritanceSpecifier const&) override; + void endVisit(UsingForDirective const&) override; void endVisit(StructDefinition const&) override; + void endVisit(EnumDefinition const&) override; + void endVisit(EnumValue const&) override; void endVisit(ParameterList const&) override; void endVisit(FunctionDefinition const&) override; void endVisit(VariableDeclaration const&) override; + void endVisit(ModifierDefinition const&) override; + void endVisit(ModifierInvocation const&) override; + void endVisit(EventDefinition const&) override; void endVisit(TypeName const&) override; void endVisit(ElementaryTypeName const&) override; void endVisit(UserDefinedTypeName const&) override; void endVisit(Mapping const&) override; + void endVisit(ArrayTypeName const&) override; void endVisit(InlineAssembly const&) override; void endVisit(Block const&) override; + void endVisit(PlaceholderStatement const&) override; void endVisit(IfStatement const&) override; void endVisit(WhileStatement const&) override; void endVisit(ForStatement const&) override; @@ -121,11 +141,10 @@ public: private: void process(); - void addKeyValue(Json::Value& _obj, std::string const& _key, std::string const& _val); void addJsonNode( ASTNode const& _node, std::string const& _nodeName, - std::initializer_list<std::pair<std::string const, std::string const>> _list, + std::initializer_list<std::pair<std::string const, Json::Value const>> _attributes, bool _hasChildren ); std::string sourceLocationToString(SourceLocation const& _location) const; diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp index bcfd33f2..715852be 100644 --- a/libsolidity/codegen/ContractCompiler.cpp +++ b/libsolidity/codegen/ContractCompiler.cpp @@ -431,16 +431,16 @@ bool ContractCompiler::visit(FunctionDefinition const& _function) if (auto c = m_context.nextConstructor(dynamic_cast<ContractDefinition const&>(*_function.scope()))) appendBaseConstructor(*c); - m_returnTag = m_context.newTag(); + solAssert(m_returnTags.empty(), ""); m_breakTags.clear(); m_continueTags.clear(); m_stackCleanupForReturn = 0; m_currentFunction = &_function; - m_modifierDepth = 0; + m_modifierDepth = -1; appendModifierOrFunctionCode(); - m_context << m_returnTag; + solAssert(m_returnTags.empty(), ""); // Now we need to re-shuffle the stack. For this we keep a record of the stack layout // that shows the target positions of the elements, where "-1" denotes that this element needs @@ -695,7 +695,7 @@ bool ContractCompiler::visit(Return const& _return) } for (unsigned i = 0; i < m_stackCleanupForReturn; ++i) m_context << Instruction::POP; - m_context.appendJumpTo(m_returnTag); + m_context.appendJumpTo(m_returnTags.back()); m_context.adjustStackOffset(m_stackCleanupForReturn); return false; } @@ -755,9 +755,7 @@ bool ContractCompiler::visit(PlaceholderStatement const& _placeholderStatement) { StackHeightChecker checker(m_context); CompilerContext::LocationSetter locationSetter(m_context, _placeholderStatement); - ++m_modifierDepth; appendModifierOrFunctionCode(); - --m_modifierDepth; checker.check(); return true; } @@ -775,10 +773,15 @@ void ContractCompiler::appendMissingFunctions() void ContractCompiler::appendModifierOrFunctionCode() { solAssert(m_currentFunction, ""); + unsigned stackSurplus = 0; + Block const* codeBlock = nullptr; + + m_modifierDepth++; + if (m_modifierDepth >= m_currentFunction->modifiers().size()) { solAssert(m_currentFunction->isImplemented(), ""); - m_currentFunction->body().accept(*this); + codeBlock = &m_currentFunction->body(); } else { @@ -786,37 +789,45 @@ void ContractCompiler::appendModifierOrFunctionCode() // constructor call should be excluded if (dynamic_cast<ContractDefinition const*>(modifierInvocation->name()->annotation().referencedDeclaration)) - { - ++m_modifierDepth; appendModifierOrFunctionCode(); - --m_modifierDepth; - return; - } - - ModifierDefinition const& modifier = m_context.functionModifier(modifierInvocation->name()->name()); - CompilerContext::LocationSetter locationSetter(m_context, modifier); - solAssert(modifier.parameters().size() == modifierInvocation->arguments().size(), ""); - for (unsigned i = 0; i < modifier.parameters().size(); ++i) + else { - m_context.addVariable(*modifier.parameters()[i]); - compileExpression( - *modifierInvocation->arguments()[i], - modifier.parameters()[i]->annotation().type - ); + ModifierDefinition const& modifier = m_context.functionModifier(modifierInvocation->name()->name()); + CompilerContext::LocationSetter locationSetter(m_context, modifier); + solAssert(modifier.parameters().size() == modifierInvocation->arguments().size(), ""); + for (unsigned i = 0; i < modifier.parameters().size(); ++i) + { + m_context.addVariable(*modifier.parameters()[i]); + compileExpression( + *modifierInvocation->arguments()[i], + modifier.parameters()[i]->annotation().type + ); + } + for (VariableDeclaration const* localVariable: modifier.localVariables()) + appendStackVariableInitialisation(*localVariable); + + stackSurplus = + CompilerUtils::sizeOnStack(modifier.parameters()) + + CompilerUtils::sizeOnStack(modifier.localVariables()); + codeBlock = &modifier.body(); + + codeBlock = &modifier.body(); } - for (VariableDeclaration const* localVariable: modifier.localVariables()) - appendStackVariableInitialisation(*localVariable); + } + + if (codeBlock) + { + m_returnTags.push_back(m_context.newTag()); - unsigned const c_stackSurplus = CompilerUtils::sizeOnStack(modifier.parameters()) + - CompilerUtils::sizeOnStack(modifier.localVariables()); - m_stackCleanupForReturn += c_stackSurplus; + codeBlock->accept(*this); - modifier.body().accept(*this); + solAssert(!m_returnTags.empty(), ""); + m_context << m_returnTags.back(); + m_returnTags.pop_back(); - for (unsigned i = 0; i < c_stackSurplus; ++i) - m_context << Instruction::POP; - m_stackCleanupForReturn -= c_stackSurplus; + CompilerUtils(m_context).popStackSlots(stackSurplus); } + m_modifierDepth--; } void ContractCompiler::appendStackVariableInitialisation(VariableDeclaration const& _variable) diff --git a/libsolidity/codegen/ContractCompiler.h b/libsolidity/codegen/ContractCompiler.h index d1517e88..0799a543 100644 --- a/libsolidity/codegen/ContractCompiler.h +++ b/libsolidity/codegen/ContractCompiler.h @@ -40,11 +40,9 @@ class ContractCompiler: private ASTConstVisitor public: explicit ContractCompiler(CompilerContext& _context, bool _optimise): m_optimise(_optimise), - m_context(_context), - m_returnTag(eth::Tag, u256(-1)) + m_context(_context) { m_context = CompilerContext(); - m_returnTag = m_context.newTag(); } void compileContract( @@ -122,7 +120,8 @@ private: CompilerContext& m_context; std::vector<eth::AssemblyItem> m_breakTags; ///< tag to jump to for a "break" statement std::vector<eth::AssemblyItem> m_continueTags; ///< tag to jump to for a "continue" statement - eth::AssemblyItem m_returnTag; ///< tag to jump to for a "return" statement + /// Tag to jump to for a "return" statement, needs to be stacked because of modifiers. + std::vector<eth::AssemblyItem> m_returnTags; unsigned m_modifierDepth = 0; FunctionDefinition const* m_currentFunction = nullptr; unsigned m_stackCleanupForReturn = 0; ///< this number of stack elements need to be removed before jump to m_returnTag diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 1f93cf8c..4a81e27d 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -1324,11 +1324,18 @@ void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Ty m_context << Instruction::MUL; break; case Token::Div: - m_context << (c_isSigned ? Instruction::SDIV : Instruction::DIV); - break; case Token::Mod: - m_context << (c_isSigned ? Instruction::SMOD : Instruction::MOD); + { + // Test for division by zero + m_context << Instruction::DUP2 << Instruction::ISZERO; + m_context.appendConditionalJumpTo(m_context.errorTag()); + + if (_operator == Token::Div) + m_context << (c_isSigned ? Instruction::SDIV : Instruction::DIV); + else + m_context << (c_isSigned ? Instruction::SMOD : Instruction::MOD); break; + } case Token::Exp: m_context << Instruction::EXP; break; @@ -1517,6 +1524,13 @@ void ExpressionCompiler::appendExternalFunctionCall( m_context << u256(0); m_context << dupInstruction(m_context.baseToCurrentStackOffset(contractStackPos)); + // Check the the target contract exists (has code) for non-low-level calls. + if (funKind == FunctionKind::External || funKind == FunctionKind::CallCode || funKind == FunctionKind::DelegateCall) + { + m_context << Instruction::DUP1 << Instruction::EXTCODESIZE << Instruction::ISZERO; + m_context.appendConditionalJumpTo(m_context.errorTag()); + } + if (_functionType.gasSet()) m_context << dupInstruction(m_context.baseToCurrentStackOffset(gasStackPos)); else diff --git a/libsolidity/formal/Why3Translator.cpp b/libsolidity/formal/Why3Translator.cpp index bd0a020d..e16c41ab 100644 --- a/libsolidity/formal/Why3Translator.cpp +++ b/libsolidity/formal/Why3Translator.cpp @@ -466,7 +466,8 @@ bool Why3Translator::visit(BinaryOperation const& _binaryOperation) auto const& constantNumber = dynamic_cast<RationalNumberType const&>(commonType); if (constantNumber.isFractional()) error(_binaryOperation, "Fractional numbers not supported."); - add("(of_int " + toString(commonType.literalValue(nullptr)) + ")"); + else + add("(of_int " + toString(commonType.literalValue(nullptr)) + ")"); return false; } static const map<Token::Value, char const*> optrans({ @@ -488,7 +489,10 @@ bool Why3Translator::visit(BinaryOperation const& _binaryOperation) {Token::GreaterThanOrEqual, " >= "} }); if (!optrans.count(c_op)) + { error(_binaryOperation, "Operator not supported."); + return true; + } add("("); leftExpression.accept(*this); @@ -676,7 +680,8 @@ bool Why3Translator::visit(Literal const& _literal) auto const& constantNumber = dynamic_cast<RationalNumberType const&>(*type); if (constantNumber.isFractional()) error(_literal, "Fractional numbers not supported."); - add("(of_int " + toString(type->literalValue(&_literal)) + ")"); + else + add("(of_int " + toString(type->literalValue(&_literal)) + ")"); break; } default: diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index f7982872..0e5ead2b 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -358,11 +358,6 @@ string const& CompilerStack::interface(string const& _contractName) const return metadata(_contractName, DocumentationType::ABIInterface); } -string const& CompilerStack::solidityInterface(string const& _contractName) const -{ - return metadata(_contractName, DocumentationType::ABISolidityInterface); -} - string const& CompilerStack::metadata(string const& _contractName, DocumentationType _type) const { if (!m_parseSuccessful) @@ -383,9 +378,6 @@ string const& CompilerStack::metadata(string const& _contractName, Documentation case DocumentationType::ABIInterface: doc = ¤tContract.interface; break; - case DocumentationType::ABISolidityInterface: - doc = ¤tContract.solidityInterface; - break; default: BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Illegal documentation type.")); } diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index a4b8447f..b3c4450c 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -63,8 +63,7 @@ enum class DocumentationType: uint8_t { NatspecUser = 1, NatspecDev, - ABIInterface, - ABISolidityInterface + ABIInterface }; /** @@ -167,9 +166,6 @@ public: /// @returns a string representing the contract interface in JSON. /// Prerequisite: Successful call to parse or compile. std::string const& interface(std::string const& _contractName = "") const; - /// @returns a string representing the contract interface in Solidity. - /// Prerequisite: Successful call to parse or compile. - std::string const& solidityInterface(std::string const& _contractName = "") const; /// @returns a string representing the contract's documentation in JSON. /// Prerequisite: Successful call to parse or compile. /// @param type The type of the documentation to get. @@ -219,7 +215,6 @@ private: eth::LinkerObject runtimeObject; eth::LinkerObject cloneObject; mutable std::unique_ptr<std::string const> interface; - mutable std::unique_ptr<std::string const> solidityInterface; mutable std::unique_ptr<std::string const> userDocumentation; mutable std::unique_ptr<std::string const> devDocumentation; mutable std::unique_ptr<std::string const> sourceMapping; diff --git a/libsolidity/interface/InterfaceHandler.cpp b/libsolidity/interface/InterfaceHandler.cpp index e254137f..f5c10356 100644 --- a/libsolidity/interface/InterfaceHandler.cpp +++ b/libsolidity/interface/InterfaceHandler.cpp @@ -21,8 +21,6 @@ string InterfaceHandler::documentation( return devDocumentation(_contractDef); case DocumentationType::ABIInterface: return abiInterface(_contractDef); - case DocumentationType::ABISolidityInterface: - return ABISolidityInterface(_contractDef); } BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation type")); @@ -98,74 +96,6 @@ string InterfaceHandler::abiInterface(ContractDefinition const& _contractDef) return Json::FastWriter().write(abi); } -string InterfaceHandler::ABISolidityInterface(ContractDefinition const& _contractDef) -{ - string ret = (_contractDef.isLibrary() ? "library " : "contract ") + _contractDef.name() + "{"; - - auto populateParameters = [](vector<string> const& _paramNames, vector<string> const& _paramTypes) - { - string ret = "("; - for (size_t i = 0; i < _paramNames.size(); ++i) - ret += _paramTypes[i] + " " + _paramNames[i] + ","; - if (ret.size() != 1) - ret.pop_back(); - return ret + ")"; - }; - // If this is a library, include all its enum and struct types. Should be more intelligent - // in the future and check what is actually used (it might even use types from other libraries - // or contracts or in the global scope). - if (_contractDef.isLibrary()) - { - for (auto const& stru: _contractDef.definedStructs()) - { - ret += "struct " + stru->name() + "{"; - for (ASTPointer<VariableDeclaration> const& _member: stru->members()) - ret += _member->type()->canonicalName(false) + " " + _member->name() + ";"; - ret += "}"; - } - for (auto const& enu: _contractDef.definedEnums()) - { - ret += "enum " + enu->name() + "{"; - for (ASTPointer<EnumValue> const& val: enu->members()) - ret += val->name() + ","; - if (ret.back() == ',') - ret.pop_back(); - ret += "}"; - } - } - if (_contractDef.constructor()) - { - auto externalFunction = FunctionType(*_contractDef.constructor()).interfaceFunctionType(); - solAssert(!!externalFunction, ""); - ret += - "function " + - _contractDef.name() + - populateParameters( - externalFunction->parameterNames(), - externalFunction->parameterTypeNames(_contractDef.isLibrary()) - ) + - ";"; - } - for (auto const& it: _contractDef.interfaceFunctions()) - { - ret += "function " + it.second->declaration().name() + - populateParameters( - it.second->parameterNames(), - it.second->parameterTypeNames(_contractDef.isLibrary()) - ) + (it.second->isConstant() ? "constant " : ""); - if (it.second->returnParameterTypes().size()) - ret += "returns" + populateParameters( - it.second->returnParameterNames(), - it.second->returnParameterTypeNames(_contractDef.isLibrary()) - ); - else if (ret.back() == ' ') - ret.pop_back(); - ret += ";"; - } - - return ret + "}"; -} - string InterfaceHandler::userDocumentation(ContractDefinition const& _contractDef) { Json::Value doc; diff --git a/libsolidity/interface/InterfaceHandler.h b/libsolidity/interface/InterfaceHandler.h index 3e0a1660..54199e4e 100644 --- a/libsolidity/interface/InterfaceHandler.h +++ b/libsolidity/interface/InterfaceHandler.h @@ -73,7 +73,6 @@ public: /// @param _contractDef The contract definition /// @return A string with the json representation of the contract's ABI Interface static std::string abiInterface(ContractDefinition const& _contractDef); - static std::string ABISolidityInterface(ContractDefinition const& _contractDef); /// Get the User documentation of the contract /// @param _contractDef The contract definition /// @return A string with the json representation of the contract's user documentation diff --git a/scripts/travis-emscripten/publish_binary.sh b/scripts/travis-emscripten/publish_binary.sh index ac1fa95f..d202764a 100755 --- a/scripts/travis-emscripten/publish_binary.sh +++ b/scripts/travis-emscripten/publish_binary.sh @@ -60,7 +60,7 @@ fi # This file is assumed to be the product of the build_emscripten.sh script. cp ../soljson.js ./bin/"soljson-$VER-$DATE-$COMMIT.js" -./update-index.sh +node ./update cd bin LATEST=$(ls -r soljson-v* | head -n 1) cp "$LATEST" soljson-latest.js diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index ec87b891..08c08797 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -65,7 +65,6 @@ namespace solidity { static string const g_argAbiStr = "abi"; -static string const g_argSolInterfaceStr = "interface"; static string const g_argSignatureHashes = "hashes"; static string const g_argGas = "gas"; static string const g_argAsmStr = "asm"; @@ -116,7 +115,6 @@ static bool needsHumanTargetedStdout(po::variables_map const& _args) return false; for (string const& arg: { g_argAbiStr, - g_argSolInterfaceStr, g_argSignatureHashes, g_argNatspecUserStr, g_argAstJson, @@ -215,11 +213,6 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co suffix = ".abi"; title = "Contract JSON ABI"; break; - case DocumentationType::ABISolidityInterface: - argName = g_argSolInterfaceStr; - suffix = "_interface.sol"; - title = "Contract Solidity ABI"; - break; case DocumentationType::NatspecUser: argName = g_argNatspecUserStr; suffix = ".docuser"; @@ -310,21 +303,18 @@ void CommandLineInterface::handleFormal() void CommandLineInterface::readInputFilesAndConfigureRemappings() { + vector<string> inputFiles; + bool addStdin = false; if (!m_args.count("input-file")) - { - string s; - while (!cin.eof()) - { - getline(cin, s); - m_sourceCodes[g_stdinFileName].append(s + '\n'); - } - } + addStdin = true; else for (string path: m_args["input-file"].as<vector<string>>()) { auto eq = find(path.begin(), path.end(), '='); if (eq != path.end()) path = string(eq + 1, path.end()); + else if (path == "-") + addStdin = true; else { auto infile = boost::filesystem::path(path); @@ -345,6 +335,15 @@ void CommandLineInterface::readInputFilesAndConfigureRemappings() } m_allowedDirectories.push_back(boost::filesystem::path(path).remove_filename()); } + if (addStdin) + { + string s; + while (!cin.eof()) + { + getline(cin, s); + m_sourceCodes[g_stdinFileName].append(s + '\n'); + } + } } bool CommandLineInterface::parseLibraryOption(string const& _input) @@ -399,9 +398,9 @@ bool CommandLineInterface::parseArguments(int _argc, char** _argv) po::options_description desc( R"(solc, the Solidity commandline compiler. Usage: solc [options] [input_file...] -Compiles the given Solidity input files (or the standard input if none given) and -outputs the components specified in the options at standard output or in files in -the output directory, if specified. +Compiles the given Solidity input files (or the standard input if none given or +"-" is used as a file name) and outputs the components specified in the options +at standard output or in files in the output directory, if specified. Example: solc --bin -o /tmp/solcoutput contract.sol Allowed options)", @@ -455,7 +454,6 @@ Allowed options)", (g_argRuntimeBinaryStr.c_str(), "Binary of the runtime part of the contracts in hex.") (g_argCloneBinaryStr.c_str(), "Binary of the clone contracts in hex.") (g_argAbiStr.c_str(), "ABI specification of the contracts.") - (g_argSolInterfaceStr.c_str(), "Solidity interface of the contracts.") (g_argSignatureHashes.c_str(), "Function signature hashes of the contracts.") (g_argNatspecUserStr.c_str(), "Natspec user documentation of all contracts.") (g_argNatspecDevStr.c_str(), "Natspec developer documentation of all contracts.") @@ -643,8 +641,6 @@ void CommandLineInterface::handleCombinedJSON() for (string const& contractName: contracts) { Json::Value contractData(Json::objectValue); - if (requests.count("interface")) - contractData["interface"] = m_compiler->solidityInterface(contractName); if (requests.count("abi")) contractData["abi"] = m_compiler->interface(contractName); if (requests.count("bin")) @@ -901,7 +897,6 @@ void CommandLineInterface::outputCompilationResults() handleBytecode(contract); handleSignatureHashes(contract); handleMeta(DocumentationType::ABIInterface, contract); - handleMeta(DocumentationType::ABISolidityInterface, contract); handleMeta(DocumentationType::NatspecDev, contract); handleMeta(DocumentationType::NatspecUser, contract); } // end of contracts iteration diff --git a/solc/jsonCompiler.cpp b/solc/jsonCompiler.cpp index 0a1b9a87..896a5922 100644 --- a/solc/jsonCompiler.cpp +++ b/solc/jsonCompiler.cpp @@ -207,7 +207,6 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback for (string const& contractName: compiler.contractNames()) { Json::Value contractData(Json::objectValue); - contractData["solidityInterface"] = compiler.solidityInterface(contractName); contractData["interface"] = compiler.interface(contractName); contractData["bytecode"] = compiler.object(contractName).toHex(); contractData["runtimeBytecode"] = compiler.runtimeObject(contractName).toHex(); diff --git a/test/libsolidity/ASTJSON.cpp b/test/libsolidity/ASTJSON.cpp index 6d914391..ec60b668 100644 --- a/test/libsolidity/ASTJSON.cpp +++ b/test/libsolidity/ASTJSON.cpp @@ -45,7 +45,7 @@ BOOST_AUTO_TEST_CASE(smoke_test) map<string, unsigned> sourceIndices; sourceIndices["a"] = 1; Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); - BOOST_CHECK_EQUAL(astJson["name"], "root"); + BOOST_CHECK_EQUAL(astJson["name"], "SourceUnit"); } BOOST_AUTO_TEST_CASE(source_location) @@ -56,12 +56,145 @@ BOOST_AUTO_TEST_CASE(source_location) map<string, unsigned> sourceIndices; sourceIndices["a"] = 1; Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); - BOOST_CHECK_EQUAL(astJson["name"], "root"); - BOOST_CHECK_EQUAL(astJson["children"][0]["name"], "Contract"); - BOOST_CHECK_EQUAL(astJson["children"][0]["children"][0]["name"], "Function"); + BOOST_CHECK_EQUAL(astJson["name"], "SourceUnit"); + BOOST_CHECK_EQUAL(astJson["children"][0]["name"], "ContractDefinition"); + BOOST_CHECK_EQUAL(astJson["children"][0]["children"][0]["name"], "FunctionDefinition"); BOOST_CHECK_EQUAL(astJson["children"][0]["children"][0]["src"], "13:32:1"); } +BOOST_AUTO_TEST_CASE(inheritance_specifier) +{ + CompilerStack c; + c.addSource("a", "contract C1 {} contract C2 is C1 {}"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + BOOST_CHECK_EQUAL(astJson["children"][1]["attributes"]["name"], "C2"); + BOOST_CHECK_EQUAL(astJson["children"][1]["children"][0]["name"], "InheritanceSpecifier"); + BOOST_CHECK_EQUAL(astJson["children"][1]["children"][0]["src"], "30:2:1"); + BOOST_CHECK_EQUAL(astJson["children"][1]["children"][0]["children"][0]["name"], "UserDefinedTypeName"); + BOOST_CHECK_EQUAL(astJson["children"][1]["children"][0]["children"][0]["attributes"]["name"], "C1"); +} + +BOOST_AUTO_TEST_CASE(using_for_directive) +{ + CompilerStack c; + c.addSource("a", "library L {} contract C { using L for uint; }"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + Json::Value usingFor = astJson["children"][1]["children"][0]; + BOOST_CHECK_EQUAL(usingFor["name"], "UsingForDirective"); + BOOST_CHECK_EQUAL(usingFor["src"], "26:17:1"); + BOOST_CHECK_EQUAL(usingFor["children"][0]["name"], "UserDefinedTypeName"); + BOOST_CHECK_EQUAL(usingFor["children"][0]["attributes"]["name"], "L"); + BOOST_CHECK_EQUAL(usingFor["children"][1]["name"], "ElementaryTypeName"); + BOOST_CHECK_EQUAL(usingFor["children"][1]["attributes"]["name"], "uint"); +} + +BOOST_AUTO_TEST_CASE(enum_definition) +{ + CompilerStack c; + c.addSource("a", "contract C { enum E {} }"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + Json::Value enumDefinition = astJson["children"][0]["children"][0]; + BOOST_CHECK_EQUAL(enumDefinition["name"], "EnumDefinition"); + BOOST_CHECK_EQUAL(enumDefinition["attributes"]["name"], "E"); + BOOST_CHECK_EQUAL(enumDefinition["src"], "13:9:1"); +} + +BOOST_AUTO_TEST_CASE(enum_value) +{ + CompilerStack c; + c.addSource("a", "contract C { enum E { A, B } }"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + Json::Value enumDefinition = astJson["children"][0]["children"][0]; + BOOST_CHECK_EQUAL(enumDefinition["children"][0]["name"], "EnumValue"); + BOOST_CHECK_EQUAL(enumDefinition["children"][0]["attributes"]["name"], "A"); + BOOST_CHECK_EQUAL(enumDefinition["children"][0]["src"], "22:1:1"); + BOOST_CHECK_EQUAL(enumDefinition["children"][1]["name"], "EnumValue"); + BOOST_CHECK_EQUAL(enumDefinition["children"][1]["attributes"]["name"], "B"); + BOOST_CHECK_EQUAL(enumDefinition["children"][1]["src"], "25:1:1"); +} + +BOOST_AUTO_TEST_CASE(modifier_definition) +{ + CompilerStack c; + c.addSource("a", "contract C { modifier M(uint i) { _ } function F() M(1) {} }"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + Json::Value modifier = astJson["children"][0]["children"][0]; + BOOST_CHECK_EQUAL(modifier["name"], "ModifierDefinition"); + BOOST_CHECK_EQUAL(modifier["attributes"]["name"], "M"); + BOOST_CHECK_EQUAL(modifier["src"], "13:24:1"); +} + +BOOST_AUTO_TEST_CASE(modifier_invocation) +{ + CompilerStack c; + c.addSource("a", "contract C { modifier M(uint i) { _ } function F() M(1) {} }"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + Json::Value modifier = astJson["children"][0]["children"][1]["children"][2]; + BOOST_CHECK_EQUAL(modifier["name"], "ModifierInvocation"); + BOOST_CHECK_EQUAL(modifier["src"], "51:4:1"); + BOOST_CHECK_EQUAL(modifier["children"][0]["attributes"]["type"], "modifier (uint256)"); + BOOST_CHECK_EQUAL(modifier["children"][0]["attributes"]["value"], "M"); + BOOST_CHECK_EQUAL(modifier["children"][1]["attributes"]["value"], "1"); +} + +BOOST_AUTO_TEST_CASE(event_definition) +{ + CompilerStack c; + c.addSource("a", "contract C { event E(); }"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + Json::Value event = astJson["children"][0]["children"][0]; + BOOST_CHECK_EQUAL(event["name"], "EventDefinition"); + BOOST_CHECK_EQUAL(event["attributes"]["name"], "E"); + BOOST_CHECK_EQUAL(event["src"], "13:10:1"); +} + +BOOST_AUTO_TEST_CASE(array_type_name) +{ + CompilerStack c; + c.addSource("a", "contract C { uint[] i; }"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + Json::Value array = astJson["children"][0]["children"][0]["children"][0]; + BOOST_CHECK_EQUAL(array["name"], "ArrayTypeName"); + BOOST_CHECK_EQUAL(array["src"], "13:6:1"); +} + +BOOST_AUTO_TEST_CASE(placeholder_statement) +{ + CompilerStack c; + c.addSource("a", "contract C { modifier M { _ } }"); + c.parse(); + map<string, unsigned> sourceIndices; + sourceIndices["a"] = 1; + Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); + Json::Value placeholder = astJson["children"][0]["children"][0]["children"][1]["children"][0]; + BOOST_CHECK_EQUAL(placeholder["name"], "PlaceholderStatement"); + BOOST_CHECK_EQUAL(placeholder["src"], "26:1:1"); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index f998a67c..45f6aec5 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -2401,7 +2401,8 @@ BOOST_AUTO_TEST_CASE(function_modifier_multi_invocation) BOOST_AUTO_TEST_CASE(function_modifier_multi_with_return) { - // Here, the explicit return prevents the second execution + // Note that return sets the return variable and jumps to the end of the current function or + // modifier code block. char const* sourceCode = R"( contract C { modifier repeat(bool twice) { if (twice) _ _ } @@ -2410,7 +2411,7 @@ BOOST_AUTO_TEST_CASE(function_modifier_multi_with_return) )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1)); - BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(1)); + BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(2)); } BOOST_AUTO_TEST_CASE(function_modifier_overriding) @@ -6219,6 +6220,27 @@ BOOST_AUTO_TEST_CASE(addmod_mulmod) BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(0))); } +BOOST_AUTO_TEST_CASE(divisiod_by_zero) +{ + char const* sourceCode = R"( + contract C { + function div(uint a, uint b) returns (uint) { + return a / b; + } + function mod(uint a, uint b) returns (uint) { + return a % b; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("div(uint256,uint256)", 7, 2) == encodeArgs(u256(3))); + // throws + BOOST_CHECK(callContractFunction("div(uint256,uint256)", 7, 0) == encodeArgs()); + BOOST_CHECK(callContractFunction("mod(uint256,uint256)", 7, 2) == encodeArgs(u256(1))); + // throws + BOOST_CHECK(callContractFunction("mod(uint256,uint256)", 7, 0) == encodeArgs()); +} + BOOST_AUTO_TEST_CASE(string_allocation_bug) { char const* sourceCode = R"( @@ -6891,6 +6913,137 @@ BOOST_AUTO_TEST_CASE(create_dynamic_array_with_zero_length) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(7))); } +BOOST_AUTO_TEST_CASE(return_does_not_skip_modifier) +{ + char const* sourceCode = R"( + contract C { + uint public x; + modifier setsx { + _ + x = 9; + } + function f() setsx returns (uint) { + return 2; + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(0))); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(2))); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(9))); +} + +BOOST_AUTO_TEST_CASE(break_in_modifier) +{ + char const* sourceCode = R"( + contract C { + uint public x; + modifier run() { + for (uint i = 0; i < 10; i++) { + _ + break; + } + } + function f() run { + x++; + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(0))); + BOOST_CHECK(callContractFunction("f()") == encodeArgs()); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(1))); +} + +BOOST_AUTO_TEST_CASE(stacked_return_with_modifiers) +{ + char const* sourceCode = R"( + contract C { + uint public x; + modifier run() { + for (uint i = 0; i < 10; i++) { + _ + break; + } + } + function f() run { + x++; + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(0))); + BOOST_CHECK(callContractFunction("f()") == encodeArgs()); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(1))); +} + +BOOST_AUTO_TEST_CASE(mutex) +{ + char const* sourceCode = R"( + contract mutexed { + bool locked; + modifier protected { + if (locked) throw; + locked = true; + _ + locked = false; + } + } + contract Fund is mutexed { + uint shares; + function Fund() { shares = msg.value; } + function withdraw(uint amount) protected returns (uint) { + // NOTE: It is very bad practice to write this function this way. + // Please refer to the documentation of how to do this properly. + if (amount > shares) throw; + if (!msg.sender.call.value(amount)()) throw; + shares -= amount; + return shares; + } + function withdrawUnprotected(uint amount) returns (uint) { + // NOTE: It is very bad practice to write this function this way. + // Please refer to the documentation of how to do this properly. + if (amount > shares) throw; + if (!msg.sender.call.value(amount)()) throw; + shares -= amount; + return shares; + } + } + contract Attacker { + Fund public fund; + uint callDepth; + bool protected; + function setProtected(bool _protected) { protected = _protected; } + function Attacker(Fund _fund) { fund = _fund; } + function attack() returns (uint) { + callDepth = 0; + return attackInternal(); + } + function attackInternal() internal returns (uint) { + if (protected) + return fund.withdraw(10); + else + return fund.withdrawUnprotected(10); + } + function() { + callDepth++; + if (callDepth < 4) + attackInternal(); + } + } + )"; + compileAndRun(sourceCode, 500, "Fund"); + auto fund = m_contractAddress; + BOOST_CHECK_EQUAL(balanceAt(fund), 500); + compileAndRun(sourceCode, 0, "Attacker", encodeArgs(u160(fund))); + BOOST_CHECK(callContractFunction("setProtected(bool)", true) == encodeArgs()); + BOOST_CHECK(callContractFunction("attack()") == encodeArgs()); + BOOST_CHECK_EQUAL(balanceAt(fund), 500); + BOOST_CHECK(callContractFunction("setProtected(bool)", false) == encodeArgs()); + BOOST_CHECK(callContractFunction("attack()") == encodeArgs(u256(460))); + BOOST_CHECK_EQUAL(balanceAt(fund), 460); +} + BOOST_AUTO_TEST_CASE(failing_ecrecover_invalid_input) { // ecrecover should return zero for malformed input @@ -6907,6 +7060,32 @@ BOOST_AUTO_TEST_CASE(failing_ecrecover_invalid_input) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0))); } +BOOST_AUTO_TEST_CASE(calling_nonexisting_contract_throws) +{ + char const* sourceCode = R"( + contract D { function g(); } + contract C { + D d = D(0x1212); + function f() returns (uint) { + d.g(); + return 7; + } + function g() returns (uint) { + d.g.gas(200)(); + return 7; + } + function h() returns (uint) { + d.call(); // this does not throw (low-level) + return 7; + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs()); + BOOST_CHECK(callContractFunction("g()") == encodeArgs()); + BOOST_CHECK(callContractFunction("h()") == encodeArgs(u256(7))); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityExpressionCompiler.cpp b/test/libsolidity/SolidityExpressionCompiler.cpp index 967b2907..e9a05745 100644 --- a/test/libsolidity/SolidityExpressionCompiler.cpp +++ b/test/libsolidity/SolidityExpressionCompiler.cpp @@ -323,7 +323,15 @@ BOOST_AUTO_TEST_CASE(arithmetics) byte(Instruction::OR), byte(Instruction::SUB), byte(Instruction::ADD), + byte(Instruction::DUP2), + byte(Instruction::ISZERO), + byte(Instruction::PUSH1), 0x2, + byte(Instruction::JUMPI), byte(Instruction::MOD), + byte(Instruction::DUP2), + byte(Instruction::ISZERO), + byte(Instruction::PUSH1), 0x2, + byte(Instruction::JUMPI), byte(Instruction::DIV), byte(Instruction::MUL)}); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); diff --git a/test/libsolidity/SolidityInterface.cpp b/test/libsolidity/SolidityInterface.cpp deleted file mode 100644 index 9a1c104d..00000000 --- a/test/libsolidity/SolidityInterface.cpp +++ /dev/null @@ -1,165 +0,0 @@ -/* - This file is part of cpp-ethereum. - - cpp-ethereum 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. - - cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. - */ -/** - * @author Christian <c@ethdev.com> - * @date 2015 - * Unit tests for generating source interfaces for Solidity contracts. - */ - -#include "../TestHelper.h" -#include <libsolidity/interface/CompilerStack.h> -#include <libsolidity/ast/AST.h> - -using namespace std; - -namespace dev -{ -namespace solidity -{ -namespace test -{ - -class SolidityInterfaceChecker -{ -public: - SolidityInterfaceChecker(): m_compilerStack(false) {} - - /// Compiles the given code, generates the interface and parses that again. - ContractDefinition const& checkInterface(string const& _code, string const& _contractName = "") - { - m_code = _code; - ETH_TEST_REQUIRE_NO_THROW(m_compilerStack.parse(_code), "Parsing failed"); - m_interface = m_compilerStack.metadata("", DocumentationType::ABISolidityInterface); - ETH_TEST_REQUIRE_NO_THROW(m_reCompiler.parse(m_interface), "Interface parsing failed"); - return m_reCompiler.contractDefinition(_contractName); - } - - string sourcePart(ASTNode const& _node) const - { - SourceLocation location = _node.location(); - BOOST_REQUIRE(!location.isEmpty()); - return m_interface.substr(location.start, location.end - location.start); - } - -protected: - string m_code; - string m_interface; - CompilerStack m_compilerStack; - CompilerStack m_reCompiler; -}; - -BOOST_FIXTURE_TEST_SUITE(SolidityInterface, SolidityInterfaceChecker) - -BOOST_AUTO_TEST_CASE(empty_contract) -{ - ContractDefinition const& contract = checkInterface("contract test {}"); - BOOST_CHECK_EQUAL(sourcePart(contract), "contract test{}"); -} - -BOOST_AUTO_TEST_CASE(single_function) -{ - ContractDefinition const& contract = checkInterface( - "contract test {\n" - " function f(uint a) returns(uint d) { return a * 7; }\n" - "}\n"); - BOOST_REQUIRE_EQUAL(1, contract.definedFunctions().size()); - BOOST_CHECK_EQUAL(sourcePart(*contract.definedFunctions().front()), - "function f(uint256 a)returns(uint256 d);"); -} - -BOOST_AUTO_TEST_CASE(single_constant_function) -{ - ContractDefinition const& contract = checkInterface( - "contract test { function f(uint a) constant returns(bytes1 x) { 1==2; } }"); - BOOST_REQUIRE_EQUAL(1, contract.definedFunctions().size()); - BOOST_CHECK_EQUAL(sourcePart(*contract.definedFunctions().front()), - "function f(uint256 a)constant returns(bytes1 x);"); -} - -BOOST_AUTO_TEST_CASE(multiple_functions) -{ - char const* sourceCode = "contract test {\n" - " function f(uint a) returns(uint d) { return a * 7; }\n" - " function g(uint b) returns(uint e) { return b * 8; }\n" - "}\n"; - ContractDefinition const& contract = checkInterface(sourceCode); - set<string> expectation({"function f(uint256 a)returns(uint256 d);", - "function g(uint256 b)returns(uint256 e);"}); - BOOST_REQUIRE_EQUAL(2, contract.definedFunctions().size()); - BOOST_CHECK(expectation == set<string>({sourcePart(*contract.definedFunctions().at(0)), - sourcePart(*contract.definedFunctions().at(1))})); -} - -BOOST_AUTO_TEST_CASE(exclude_fallback_function) -{ - char const* sourceCode = "contract test { function() {} }"; - ContractDefinition const& contract = checkInterface(sourceCode); - BOOST_CHECK_EQUAL(sourcePart(contract), "contract test{}"); -} - -BOOST_AUTO_TEST_CASE(events) -{ - char const* sourceCode = "contract test {\n" - " function f(uint a) returns(uint d) { return a * 7; }\n" - " event e1(uint b, address indexed c); \n" - " event e2(); \n" - "}\n"; - ContractDefinition const& contract = checkInterface(sourceCode); - // events should not appear in the Solidity Interface - BOOST_REQUIRE_EQUAL(0, contract.events().size()); -} - -BOOST_AUTO_TEST_CASE(inheritance) -{ - char const* sourceCode = - " contract Base { \n" - " function baseFunction(uint p) returns (uint i) { return p; } \n" - " event baseEvent(bytes32 indexed evtArgBase); \n" - " } \n" - " contract Derived is Base { \n" - " function derivedFunction(bytes32 p) returns (bytes32 i) { return p; } \n" - " event derivedEvent(uint indexed evtArgDerived); \n" - " }"; - ContractDefinition const& contract = checkInterface(sourceCode); - set<string> expectedFunctions({"function baseFunction(uint256 p)returns(uint256 i);", - "function derivedFunction(bytes32 p)returns(bytes32 i);"}); - BOOST_REQUIRE_EQUAL(2, contract.definedFunctions().size()); - BOOST_CHECK(expectedFunctions == set<string>({sourcePart(*contract.definedFunctions().at(0)), - sourcePart(*contract.definedFunctions().at(1))})); -} - -BOOST_AUTO_TEST_CASE(libraries) -{ - char const* sourceCode = R"( - library Lib { - struct Str { uint a; } - enum E { E1, E2 } - function f(uint[] x,Str storage y,E z) external; - } - )"; - ContractDefinition const& contract = checkInterface(sourceCode); - BOOST_CHECK(contract.isLibrary()); - set<string> expectedFunctions({"function f(uint256[] x,Lib.Str storage y,Lib.E z);"}); - BOOST_REQUIRE_EQUAL(1, contract.definedFunctions().size()); - BOOST_CHECK(expectedFunctions == set<string>({sourcePart(*contract.definedFunctions().at(0))})); -} - -BOOST_AUTO_TEST_SUITE_END() - -} -} -} |