diff options
-rw-r--r-- | Changelog.md | 1 | ||||
-rw-r--r-- | libdevcore/JSON.h | 4 | ||||
-rw-r--r-- | libsolidity/analysis/StaticAnalyzer.cpp | 78 | ||||
-rw-r--r-- | libsolidity/analysis/StaticAnalyzer.h | 72 | ||||
-rw-r--r-- | libsolidity/ast/ASTJsonConverter.cpp | 4 | ||||
-rw-r--r-- | libsolidity/ast/Types.h | 2 | ||||
-rw-r--r-- | libsolidity/interface/CompilerStack.cpp | 10 | ||||
-rwxr-xr-x | scripts/install_deps.sh | 6 | ||||
-rw-r--r-- | solc/CommandLineInterface.cpp | 1 | ||||
-rw-r--r-- | test/ExecutionFramework.cpp | 7 | ||||
-rw-r--r-- | test/liblll/EndToEndTest.cpp | 229 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 90 |
12 files changed, 490 insertions, 14 deletions
diff --git a/Changelog.md b/Changelog.md index c76d10e1..1eb90c22 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ ### 0.4.7 (unreleased) Features: + * Type checker: Warn when ``msg.value`` is used in non-payable function. * Code generator: Inject the Swarm hash of a metadata file into the bytecode. * Optimizer: Some dead code elimination. diff --git a/libdevcore/JSON.h b/libdevcore/JSON.h index 0d6e0d2e..9f7d9a03 100644 --- a/libdevcore/JSON.h +++ b/libdevcore/JSON.h @@ -27,13 +27,13 @@ namespace dev { -/// Serialise the JSON object (@a _input) with identation +/// Serialise the JSON object (@a _input) with indentation inline std::string jsonPrettyPrint(Json::Value const& _input) { return Json::StyledWriter().write(_input); } -/// Serialise theJ SON object (@a _input) without identation +/// Serialise the JSON object (@a _input) without indentation inline std::string jsonCompactPrint(Json::Value const& _input) { Json::FastWriter writer; diff --git a/libsolidity/analysis/StaticAnalyzer.cpp b/libsolidity/analysis/StaticAnalyzer.cpp new file mode 100644 index 00000000..c39f874e --- /dev/null +++ b/libsolidity/analysis/StaticAnalyzer.cpp @@ -0,0 +1,78 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see <http://www.gnu.org/licenses/>. +*/ +/** + * @author Federico Bond <federicobond@gmail.com> + * @date 2016 + * Static analyzer and checker. + */ + +#include <libsolidity/analysis/StaticAnalyzer.h> +#include <memory> +#include <libsolidity/ast/AST.h> + +using namespace std; +using namespace dev; +using namespace dev::solidity; + + +bool StaticAnalyzer::analyze(SourceUnit const& _sourceUnit) +{ + _sourceUnit.accept(*this); + return Error::containsOnlyWarnings(m_errors); +} + +bool StaticAnalyzer::visit(ContractDefinition const& _contract) +{ + m_library = _contract.isLibrary(); + return true; +} + +void StaticAnalyzer::endVisit(ContractDefinition const&) +{ + m_library = false; +} + +bool StaticAnalyzer::visit(FunctionDefinition const& _function) +{ + m_nonPayablePublic = _function.isPublic() && !_function.isPayable(); + return true; +} + +void StaticAnalyzer::endVisit(FunctionDefinition const&) +{ + m_nonPayablePublic = false; +} + +bool StaticAnalyzer::visit(MemberAccess const& _memberAccess) +{ + if (m_nonPayablePublic && !m_library) + if (MagicType const* type = dynamic_cast<MagicType const*>(_memberAccess.expression().annotation().type.get())) + if (type->kind() == MagicType::Kind::Message && _memberAccess.memberName() == "value") + warning(_memberAccess.location(), "\"msg.value\" used in non-payable function. Do you want to add the \"payable\" modifier to this function?"); + + return true; +} + +void StaticAnalyzer::warning(SourceLocation const& _location, string const& _description) +{ + auto err = make_shared<Error>(Error::Type::Warning); + *err << + errinfo_sourceLocation(_location) << + errinfo_comment(_description); + + m_errors.push_back(err); +} diff --git a/libsolidity/analysis/StaticAnalyzer.h b/libsolidity/analysis/StaticAnalyzer.h new file mode 100644 index 00000000..b6cf783e --- /dev/null +++ b/libsolidity/analysis/StaticAnalyzer.h @@ -0,0 +1,72 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see <http://www.gnu.org/licenses/>. +*/ +/** + * @author Federico Bond <federicobond@gmail.com> + * @date 2016 + * Static analyzer and checker. + */ + +#pragma once + +#include <libsolidity/analysis/TypeChecker.h> +#include <libsolidity/ast/Types.h> +#include <libsolidity/ast/ASTAnnotations.h> +#include <libsolidity/ast/ASTForward.h> +#include <libsolidity/ast/ASTVisitor.h> + +namespace dev +{ +namespace solidity +{ + + +/** + * The module that performs static analysis on the AST. + */ +class StaticAnalyzer: private ASTConstVisitor +{ +public: + /// @param _errors the reference to the list of errors and warnings to add them found during static analysis. + explicit StaticAnalyzer(ErrorList& _errors): m_errors(_errors) {} + + /// Performs static analysis on the given source unit and all of its sub-nodes. + /// @returns true iff all checks passed. Note even if all checks passed, errors() can still contain warnings + bool analyze(SourceUnit const& _sourceUnit); + +private: + /// Adds a new warning to the list of errors. + void warning(SourceLocation const& _location, std::string const& _description); + + virtual bool visit(ContractDefinition const& _contract) override; + virtual void endVisit(ContractDefinition const& _contract) override; + + virtual bool visit(FunctionDefinition const& _function) override; + virtual void endVisit(FunctionDefinition const& _function) override; + + virtual bool visit(MemberAccess const& _memberAccess) override; + + ErrorList& m_errors; + + /// Flag that indicates whether the current contract definition is a library. + bool m_library = false; + + /// Flag that indicates whether a public function does not contain the "payable" modifier. + bool m_nonPayablePublic = false; +}; + +} +} diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index f6b06be6..493707b9 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -318,7 +318,7 @@ bool ASTJsonConverter::visit(Throw const& _node) bool ASTJsonConverter::visit(VariableDeclarationStatement const& _node) { - addJsonNode(_node, "VariableDefinitionStatement", {}, true); + addJsonNode(_node, "VariableDeclarationStatement", {}, true); return true; } @@ -407,7 +407,7 @@ bool ASTJsonConverter::visit(Identifier const& _node) bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node) { - addJsonNode(_node, "ElementaryTypenameExpression", { + addJsonNode(_node, "ElementaryTypeNameExpression", { make_pair("value", _node.typeName().toString()), make_pair("type", type(_node)) }); diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 72640a1c..26e2b8f2 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -1117,6 +1117,8 @@ public: virtual std::string toString(bool _short) const override; + Kind kind() const { return m_kind; } + private: Kind m_kind; }; diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index c243ccb8..0b8cecc7 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -32,6 +32,7 @@ #include <libsolidity/analysis/NameAndTypeResolver.h> #include <libsolidity/analysis/TypeChecker.h> #include <libsolidity/analysis/DocStringAnalyser.h> +#include <libsolidity/analysis/StaticAnalyzer.h> #include <libsolidity/analysis/SyntaxChecker.h> #include <libsolidity/codegen/Compiler.h> #include <libsolidity/interface/InterfaceHandler.h> @@ -202,6 +203,15 @@ bool CompilerStack::parse() m_contracts[contract->name()].contract = contract; } + + if (noErrors) + { + StaticAnalyzer staticAnalyzer(m_errors); + for (Source const* source: m_sourceOrder) + if (!staticAnalyzer.analyze(*source->ast)) + noErrors = false; + } + m_parseSuccessful = noErrors; return m_parseSuccessful; } diff --git a/scripts/install_deps.sh b/scripts/install_deps.sh index c9f82769..255176ab 100755 --- a/scripts/install_deps.sh +++ b/scripts/install_deps.sh @@ -83,12 +83,6 @@ case $(uname -s) in ;; 10.12) echo "Installing solidity dependencies on macOS 10.12 Sierra." - echo "" - echo "NOTE - You are in unknown territory with this preview OS." - echo "Even Homebrew doesn't have official support yet, and there are" - echo "known issues (see https://github.com/ethereum/webthree-umbrella/issues/614)." - echo "If you would like to partner with us to work through these issues, that" - echo "would be fantastic. Please just comment on that issue. Thanks!" ;; *) echo "Unsupported macOS version." diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 01f5462b..e322455b 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -370,7 +370,6 @@ void CommandLineInterface::handleFormal() void CommandLineInterface::readInputFilesAndConfigureRemappings() { - vector<string> inputFiles; bool addStdin = false; if (!m_args.count(g_argInputFile)) addStdin = true; diff --git a/test/ExecutionFramework.cpp b/test/ExecutionFramework.cpp index 9e3ecac3..ddcd9cb6 100644 --- a/test/ExecutionFramework.cpp +++ b/test/ExecutionFramework.cpp @@ -62,7 +62,7 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 cout << "CALL " << m_sender.hex() << " -> " << m_contractAddress.hex() << ":" << endl; if (_value > 0) cout << " value: " << _value << endl; - cout << " in: " << toHex(_data) << endl; + cout << " in: " << toHex(_data) << endl; } RPCSession::TransactionData d; d.data = "0x" + toHex(_data); @@ -91,7 +91,10 @@ void ExecutionFramework::sendMessage(bytes const& _data, bool _isCreation, u256 } if (m_showMessages) - cout << " out: " << toHex(m_output) << endl; + { + cout << " out: " << toHex(m_output) << endl; + cout << " tx hash: " << txHash << endl; + } m_gasUsed = u256(receipt.gasUsed); m_logs.clear(); diff --git a/test/liblll/EndToEndTest.cpp b/test/liblll/EndToEndTest.cpp index b5e32e94..77c1f740 100644 --- a/test/liblll/EndToEndTest.cpp +++ b/test/liblll/EndToEndTest.cpp @@ -43,6 +43,235 @@ BOOST_AUTO_TEST_CASE(smoke_test) BOOST_CHECK(callFallback() == encodeArgs(string("test", 4))); } +BOOST_AUTO_TEST_CASE(bare_panic) +{ + char const* sourceCode = "(panic)"; + compileAndRunWithoutCheck(sourceCode); + BOOST_REQUIRE(m_output.empty()); +} + +BOOST_AUTO_TEST_CASE(exp_operator_const) +{ + char const* sourceCode = R"( + (returnlll + (return (exp 2 3))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == toBigEndian(u256(8))); +} + +BOOST_AUTO_TEST_CASE(exp_operator_const_signed) +{ + char const* sourceCode = R"( + (returnlll + (return (exp (- 0 2) 3))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == toBigEndian(u256(-8))); +} + +BOOST_AUTO_TEST_CASE(exp_operator_on_range) +{ + char const* sourceCode = R"( + (returnlll + (seq + (when (= (div (calldataload 0x00) (exp 2 224)) 0xb3de648b) + (return (exp 2 (calldataload 0x04)))) + (jump 0x02))) + )"; + compileAndRun(sourceCode); + testContractAgainstCppOnRange("f(uint256)", [](u256 const& a) -> u256 { return u256(1 << a.convert_to<int>()); }, 0, 16); +} + +BOOST_AUTO_TEST_CASE(constructor_argument_internal_numeric) +{ + char const* sourceCode = R"( + (seq + (sstore 0x00 65535) + (returnlll + (return @@0x00))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs(u256(65535))); +} + +BOOST_AUTO_TEST_CASE(constructor_argument_internal_string) +{ + char const* sourceCode = R"( + (seq + (sstore 0x00 "test") + (returnlll + (return @@0x00))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs("test")); +} + +BOOST_AUTO_TEST_CASE(constructor_arguments_external) +{ + char const* sourceCode = R"( + (seq + (codecopy 0x00 (bytecodesize) 64) + (sstore 0x00 @0x00) + (sstore 0x01 @0x20) + (returnlll + (seq + (when (= (div (calldataload 0x00) (exp 2 224)) 0xf2c9ecd8) + (return @@0x00)) + (when (= (div (calldataload 0x00) (exp 2 224)) 0x89ea642f) + (return @@0x01))))) + )"; + compileAndRun(sourceCode, 0, "", encodeArgs(u256(65535), "test")); + BOOST_CHECK(callContractFunction("getNumber()") == encodeArgs(u256(65535))); + BOOST_CHECK(callContractFunction("getString()") == encodeArgs("test")); +} + +BOOST_AUTO_TEST_CASE(fallback_and_invalid_function) +{ + char const* sourceCode = R"( + (returnlll + (seq + (when (= (div (calldataload 0x00) (exp 2 224)) 0xab5ed150) + (return "one")) + (when (= (div (calldataload 0x00) (exp 2 224)) 0xee784123) + (return "two")) + (return "three"))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("getOne()") == encodeArgs("one")); + BOOST_CHECK(callContractFunction("getTwo()") == encodeArgs("two")); + BOOST_CHECK(callContractFunction("invalidFunction()") == encodeArgs("three")); + BOOST_CHECK(callFallback() == encodeArgs("three")); +} + +BOOST_AUTO_TEST_CASE(lit_string) +{ + char const* sourceCode = R"( + (returnlll + (seq + (lit 0x00 "abcdef") + (return 0x00 0x20))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs(string("abcdef"))); +} + +BOOST_AUTO_TEST_CASE(arithmetic) +{ + char const* sourceCode = R"( + (returnlll + (seq + (mstore8 0x00 (+ 160 22)) + (mstore8 0x01 (- 223 41)) + (mstore8 0x02 (* 33 2)) + (mstore8 0x03 (/ 10 2)) + (mstore8 0x04 (% 67 2)) + (mstore8 0x05 (& 15 8)) + (mstore8 0x06 (| 18 8)) + (mstore8 0x07 (^ 26 6)) + (return 0x00 0x20))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs( + fromHex("b6b6420501081a1c000000000000000000000000000000000000000000000000"))); +} + +BOOST_AUTO_TEST_CASE(binary) +{ + char const* sourceCode = R"( + (returnlll + (seq + (mstore8 0x00 (< 53 87)) + (mstore8 0x01 (< 73 42)) + (mstore8 0x02 (<= 37 94)) + (mstore8 0x03 (<= 37 37)) + (mstore8 0x04 (<= 183 34)) + (mstore8 0x05 (S< (- 0 53) 87)) + (mstore8 0x06 (S< 73 (- 0 42))) + (mstore8 0x07 (S<= (- 0 37) 94)) + (mstore8 0x08 (S<= (- 0 37) (- 0 37))) + (mstore8 0x09 (S<= 183 (- 0 34))) + (mstore8 0x0a (> 73 42)) + (mstore8 0x0b (> 53 87)) + (mstore8 0x0c (>= 94 37)) + (mstore8 0x0d (>= 94 94)) + (mstore8 0x0e (>= 34 183)) + (mstore8 0x0f (S> 73 (- 0 42))) + (mstore8 0x10 (S> (- 0 53) 87)) + (mstore8 0x11 (S>= 94 (- 0 37))) + (mstore8 0x12 (S>= (- 0 94) (- 0 94))) + (mstore8 0x13 (S>= (- 0 34) 183)) + (mstore8 0x14 (= 53 53)) + (mstore8 0x15 (= 73 42)) + (mstore8 0x16 (!= 37 94)) + (mstore8 0x17 (!= 37 37)) + (return 0x00 0x20))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs( + fromHex("0100010100010001010001000101000100010100010001000000000000000000"))); +} + +BOOST_AUTO_TEST_CASE(unary) +{ + char const* sourceCode = R"( + (returnlll + (seq + (mstore8 0x00 (! (< 53 87))) + (mstore8 0x01 (! (>= 42 73))) + (mstore8 0x02 (~ 0x7f)) + (mstore8 0x03 (~ 0xaa)) + (return 0x00 0x20))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs( + fromHex("0001805500000000000000000000000000000000000000000000000000000000"))); +} + +BOOST_AUTO_TEST_CASE(assembly_mload_mstore) +{ + char const* sourceCode = R"( + (returnlll + (asm + 0x07 0x00 mstore + "abcdef" 0x20 mstore + 0x00 mload 0x40 mstore + 0x20 mload 0x60 mstore + 0x40 0x40 return)) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs(u256(7), string("abcdef"))); +} + +BOOST_AUTO_TEST_CASE(assembly_sload_sstore) +{ + char const* sourceCode = R"( + (returnlll + (asm + 0x07 0x00 sstore + "abcdef" 0x01 sstore + 0x00 sload 0x00 mstore + 0x01 sload 0x20 mstore + 0x40 0x00 return)) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs(u256(7), string("abcdef"))); +} + +BOOST_AUTO_TEST_CASE(assembly_codecopy) +{ + char const* sourceCode = R"( + (returnlll + (seq + (lit 0x00 "abcdef") + (asm + 0x06 0x16 0x20 codecopy + 0x20 0x20 return))) + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callFallback() == encodeArgs(string("abcdef"))); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 11c38114..a4e601f7 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -26,6 +26,7 @@ #include <libsolidity/parsing/Scanner.h> #include <libsolidity/parsing/Parser.h> #include <libsolidity/analysis/NameAndTypeResolver.h> +#include <libsolidity/analysis/StaticAnalyzer.h> #include <libsolidity/analysis/SyntaxChecker.h> #include <libsolidity/interface/Exceptions.h> #include <libsolidity/analysis/GlobalContext.h> @@ -89,8 +90,12 @@ parseAnalyseAndReturnError(string const& _source, bool _reportWarnings = false, TypeChecker typeChecker(errors); bool success = typeChecker.checkTypeRequirements(*contract); BOOST_CHECK(success || !errors.empty()); - } + if (success) + { + StaticAnalyzer staticAnalyzer(errors); + staticAnalyzer.analyze(*sourceUnit); + } if (errors.size() > 1 && !_allowMultipleErrors) BOOST_FAIL("Multiple errors found"); for (auto const& currentError: errors) @@ -189,6 +194,14 @@ CHECK_ERROR_OR_WARNING(text, Warning, substring, true, false) // [checkSuccess(text)] asserts that the compilation down to typechecking succeeds. #define CHECK_SUCCESS(text) do { BOOST_CHECK(success((text))); } while(0) +#define CHECK_SUCCESS_NO_WARNINGS(text) \ +do \ +{ \ + auto sourceAndError = parseAnalyseAndReturnError((text), true); \ + BOOST_CHECK(sourceAndError.second == nullptr); \ +} \ +while(0) + BOOST_AUTO_TEST_SUITE(SolidityNameAndTypeResolution) @@ -4777,6 +4790,81 @@ BOOST_AUTO_TEST_CASE(invalid_mobile_type) CHECK_ERROR(text, TypeError, ""); } +BOOST_AUTO_TEST_CASE(warns_msg_value_in_non_payable_public_function) +{ + char const* text = R"( + contract C { + function f() { + msg.value; + } + } + )"; + CHECK_WARNING(text, "\"msg.value\" used in non-payable function. Do you want to add the \"payable\" modifier to this function?"); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_msg_value_in_payable_function) +{ + char const* text = R"( + contract C { + function f() payable { + msg.value; + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_msg_value_in_internal_function) +{ + char const* text = R"( + contract C { + function f() internal { + msg.value; + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_msg_value_in_library) +{ + char const* text = R"( + library C { + function f() { + msg.value; + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_non_magic_msg_value) +{ + char const* text = R"( + contract C { + struct msg { + uint256 value; + } + + function f() { + msg.value; + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + +BOOST_AUTO_TEST_CASE(does_not_warn_msg_value_in_modifier_following_non_payable_public_function) +{ + char const* text = R"( + contract c { + function f() { } + modifier m() { msg.value; _; } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); +} + BOOST_AUTO_TEST_SUITE_END() } |