diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2017-08-01 07:55:13 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2018-04-12 04:32:10 +0800 |
commit | d56acb68abdda19d0e5a684cdf8d40c3d7ce277e (patch) | |
tree | 01d40a2d33c083708ba0d289f1ce3a7fa84b5bad | |
parent | ee5d0ef79bee71a47249ed36081738dd34707900 (diff) | |
download | dexon-solidity-d56acb68abdda19d0e5a684cdf8d40c3d7ce277e.tar.gz dexon-solidity-d56acb68abdda19d0e5a684cdf8d40c3d7ce277e.tar.zst dexon-solidity-d56acb68abdda19d0e5a684cdf8d40c3d7ce277e.zip |
Add abi.encode, abi.encodePacked, abi.encodeWithSelector and abi.encodeWithSignature.
-rw-r--r-- | libsolidity/analysis/GlobalContext.cpp | 1 | ||||
-rw-r--r-- | libsolidity/analysis/ViewPureChecker.cpp | 7 | ||||
-rw-r--r-- | libsolidity/ast/Types.cpp | 49 | ||||
-rw-r--r-- | libsolidity/ast/Types.h | 8 | ||||
-rw-r--r-- | libsolidity/codegen/ExpressionCompiler.cpp | 102 | ||||
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 108 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 16 |
7 files changed, 287 insertions, 4 deletions
diff --git a/libsolidity/analysis/GlobalContext.cpp b/libsolidity/analysis/GlobalContext.cpp index 6a858d36..ef1a59fe 100644 --- a/libsolidity/analysis/GlobalContext.cpp +++ b/libsolidity/analysis/GlobalContext.cpp @@ -35,6 +35,7 @@ namespace solidity GlobalContext::GlobalContext(): m_magicVariables(vector<shared_ptr<MagicVariableDeclaration const>>{ + make_shared<MagicVariableDeclaration>("abi", make_shared<MagicType>(MagicType::Kind::ABI)), make_shared<MagicVariableDeclaration>("addmod", make_shared<FunctionType>(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::AddMod, false, StateMutability::Pure)), make_shared<MagicVariableDeclaration>("assert", make_shared<FunctionType>(strings{"bool"}, strings{}, FunctionType::Kind::Assert, false, StateMutability::Pure)), make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::Block)), diff --git a/libsolidity/analysis/ViewPureChecker.cpp b/libsolidity/analysis/ViewPureChecker.cpp index 13c3ab68..d9843012 100644 --- a/libsolidity/analysis/ViewPureChecker.cpp +++ b/libsolidity/analysis/ViewPureChecker.cpp @@ -305,10 +305,15 @@ void ViewPureChecker::endVisit(MemberAccess const& _memberAccess) mutability = StateMutability::View; break; case Type::Category::Magic: + { // we can ignore the kind of magic and only look at the name of the member - if (member != "data" && member != "sig" && member != "blockhash") + set<string> static const pureMembers{ + "encode", "encodePacked", "encodeWithSelector", "encodeWithSignature", "data", "sig", "blockhash" + }; + if (!pureMembers.count(member)) mutability = StateMutability::View; break; + } case Type::Category::Struct: { if (_memberAccess.expression().annotation().type->dataStoredIn(DataLocation::Storage)) diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 21353080..68b12777 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -2375,7 +2375,11 @@ string FunctionType::richIdentifier() const case Kind::ByteArrayPush: id += "bytearraypush"; break; case Kind::ObjectCreation: id += "objectcreation"; break; case Kind::Assert: id += "assert"; break; - case Kind::Require: id += "require";break; + case Kind::Require: id += "require"; break; + case Kind::ABIEncode: id += "abiencode"; break; + case Kind::ABIEncodePacked: id += "abiencodepacked"; break; + case Kind::ABIEncodeWithSelector: id += "abiencodewithselector"; break; + case Kind::ABIEncodeWithSignature: id += "abiencodewithsignature"; break; default: solAssert(false, "Unknown function location."); break; } id += "_" + stateMutabilityToString(m_stateMutability); @@ -2996,6 +3000,8 @@ string MagicType::richIdentifier() const return "t_magic_message"; case Kind::Transaction: return "t_magic_transaction"; + case Kind::ABI: + return "t_magic_abi"; default: solAssert(false, "Unknown kind of magic"); } @@ -3036,6 +3042,45 @@ MemberList::MemberMap MagicType::nativeMembers(ContractDefinition const*) const {"origin", make_shared<IntegerType>(160, IntegerType::Modifier::Address)}, {"gasprice", make_shared<IntegerType>(256)} }); + case Kind::ABI: + return MemberList::MemberMap({ + {"encode", make_shared<FunctionType>( + TypePointers(), + TypePointers{make_shared<ArrayType>(DataLocation::Memory)}, + strings{}, + strings{}, + FunctionType::Kind::ABIEncode, + true, + StateMutability::Pure + )}, + {"encodePacked", make_shared<FunctionType>( + TypePointers(), + TypePointers{make_shared<ArrayType>(DataLocation::Memory)}, + strings{}, + strings{}, + FunctionType::Kind::ABIEncodePacked, + true, + StateMutability::Pure + )}, + {"encodeWithSelector", make_shared<FunctionType>( + TypePointers{make_shared<FixedBytesType>(4)}, + TypePointers{make_shared<ArrayType>(DataLocation::Memory)}, + strings{}, + strings{}, + FunctionType::Kind::ABIEncodeWithSelector, + true, + StateMutability::Pure + )}, + {"encodeWithSignature", make_shared<FunctionType>( + TypePointers{make_shared<ArrayType>(DataLocation::Memory, true)}, + TypePointers{make_shared<ArrayType>(DataLocation::Memory)}, + strings{}, + strings{}, + FunctionType::Kind::ABIEncodeWithSignature, + true, + StateMutability::Pure + )} + }); default: solAssert(false, "Unknown kind of magic."); } @@ -3051,6 +3096,8 @@ string MagicType::toString(bool) const return "msg"; case Kind::Transaction: return "tx"; + case Kind::ABI: + return "abi"; default: solAssert(false, "Unknown kind of magic."); } diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 05f506f1..47556c4d 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -914,6 +914,10 @@ public: ObjectCreation, ///< array creation using new Assert, ///< assert() Require, ///< require() + ABIEncode, + ABIEncodePacked, + ABIEncodeWithSelector, + ABIEncodeWithSignature, GasLeft ///< gasleft() }; @@ -1049,7 +1053,7 @@ public: ASTPointer<ASTString> documentation() const; /// true iff arguments are to be padded to multiples of 32 bytes for external calls - bool padArguments() const { return !(m_kind == Kind::SHA3 || m_kind == Kind::SHA256 || m_kind == Kind::RIPEMD160); } + bool padArguments() const { return !(m_kind == Kind::SHA3 || m_kind == Kind::SHA256 || m_kind == Kind::RIPEMD160 || m_kind == Kind::ABIEncodePacked); } bool takesArbitraryParameters() const { return m_arbitraryParameters; } bool gasSet() const { return m_gasSet; } bool valueSet() const { return m_valueSet; } @@ -1207,7 +1211,7 @@ private: class MagicType: public Type { public: - enum class Kind { Block, Message, Transaction }; + enum class Kind { Block, Message, Transaction, ABI }; virtual Category category() const override { return Category::Magic; } explicit MagicType(Kind _kind): m_kind(_kind) {} diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 57d49ac6..051db536 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -33,6 +33,8 @@ #include <libsolidity/codegen/LValue.h> #include <libevmasm/GasMeter.h> +#include <libdevcore/Whiskers.h> + using namespace std; namespace dev @@ -912,6 +914,106 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) m_context << success; break; } + case FunctionType::Kind::ABIEncode: + case FunctionType::Kind::ABIEncodePacked: + case FunctionType::Kind::ABIEncodeWithSelector: + case FunctionType::Kind::ABIEncodeWithSignature: + { + bool const isPacked = function.kind() == FunctionType::Kind::ABIEncodePacked; + bool const hasSelectorOrSignature = + function.kind() == FunctionType::Kind::ABIEncodeWithSelector || + function.kind() == FunctionType::Kind::ABIEncodeWithSignature; + + TypePointers argumentTypes; + TypePointers targetTypes; + for (unsigned i = 0; i < arguments.size(); ++i) + { + arguments[i]->accept(*this); + // Do not keep the selector as part of the ABI encoded args + if (!hasSelectorOrSignature || i > 0) + argumentTypes.push_back(arguments[i]->annotation().type); + } + utils().fetchFreeMemoryPointer(); + // stack now: [<selector>] <arg1> .. <argN> <free_mem> + + // adjust by 32(+4) bytes to accommodate the length(+selector) + m_context << u256(32 + (hasSelectorOrSignature ? 4 : 0)) << Instruction::ADD; + // stack now: [<selector>] <arg1> .. <argN> <data_encoding_area_start> + + if (isPacked) + { + solAssert(!function.padArguments(), ""); + utils().packedEncode(argumentTypes, TypePointers()); + } + else + { + solAssert(function.padArguments(), ""); + utils().abiEncode(argumentTypes, TypePointers()); + } + utils().fetchFreeMemoryPointer(); + // stack: [<selector>] <data_encoding_area_end> <bytes_memory_ptr> + + // size is end minus start minus length slot + m_context.appendInlineAssembly(R"({ + mstore(mem_ptr, sub(sub(mem_end, mem_ptr), 0x20)) + })", {"mem_end", "mem_ptr"}); + m_context << Instruction::SWAP1; + utils().storeFreeMemoryPointer(); + // stack: [<selector>] <memory ptr> + + if (hasSelectorOrSignature) + { + // stack: <selector> <memory pointer> + solAssert(arguments.size() >= 1, ""); + TypePointer const& selectorType = arguments[0]->annotation().type; + utils().moveIntoStack(selectorType->sizeOnStack()); + TypePointer dataOnStack = selectorType; + // stack: <memory pointer> <selector> + if (function.kind() == FunctionType::Kind::ABIEncodeWithSignature) + { + // hash the signature + if (auto const* stringType = dynamic_cast<StringLiteralType const*>(selectorType.get())) + { + FixedHash<4> hash(dev::keccak256(stringType->value())); + m_context << (u256(FixedHash<4>::Arith(hash)) << (256 - 32)); + dataOnStack = make_shared<FixedBytesType>(4); + } + else + { + utils().fetchFreeMemoryPointer(); + // stack: <memory pointer> <selector> <free mem ptr> + utils().packedEncode(TypePointers{selectorType}, TypePointers()); + utils().toSizeAfterFreeMemoryPointer(); + m_context << Instruction::KECCAK256; + // stack: <memory pointer> <hash> + + dataOnStack = make_shared<FixedBytesType>(32); + } + } + else + { + solAssert(function.kind() == FunctionType::Kind::ABIEncodeWithSelector, ""); + } + + // Cleanup actually does not clean on shrinking the type. + utils().convertType(*dataOnStack, FixedBytesType(4), true); + + // stack: <memory pointer> <selector> + + // load current memory, mask and combine the selector + string mask = formatNumber((u256(-1) >> 32)); + m_context.appendInlineAssembly(R"({ + let data_start := add(mem_ptr, 0x20) + let data := mload(data_start) + let mask := )" + mask + R"( + mstore(data_start, or(and(data, mask), and(selector, not(mask)))) + })", {"mem_ptr", "selector"}); + m_context << Instruction::POP; + } + + // stack now: <memory pointer> + break; + } case FunctionType::Kind::GasLeft: m_context << Instruction::GAS; break; diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 39f4b03e..a5b990ac 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -3557,6 +3557,45 @@ BOOST_AUTO_TEST_CASE(empty_name_return_parameter) ABI_CHECK(callContractFunction("f(uint256)", 9), encodeArgs(9)); } +BOOST_AUTO_TEST_CASE(sha256_empty) +{ + char const* sourceCode = R"( + contract C { + function f() returns (bytes32) { + return sha256(); + } + } + )"; + compileAndRun(sourceCode); + ABI_CHECK(callContractFunction("f()"), fromHex("0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); +} + +BOOST_AUTO_TEST_CASE(ripemd160_empty) +{ + char const* sourceCode = R"( + contract C { + function f() returns (bytes20) { + return ripemd160(); + } + } + )"; + compileAndRun(sourceCode); + ABI_CHECK(callContractFunction("f()"), fromHex("0x9c1185a5c5e9fc54612808977ee8f548b2258d31000000000000000000000000")); +} + +BOOST_AUTO_TEST_CASE(keccak256_empty) +{ + char const* sourceCode = R"( + contract C { + function f() returns (bytes32) { + return keccak256(); + } + } + )"; + compileAndRun(sourceCode); + ABI_CHECK(callContractFunction("f()"), fromHex("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); +} + BOOST_AUTO_TEST_CASE(keccak256_multiple_arguments) { char const* sourceCode = R"( @@ -11052,6 +11091,75 @@ BOOST_AUTO_TEST_CASE(snark) BOOST_CHECK(callContractFunction("verifyTx()") == encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(abi_encode) +{ + char const* sourceCode = R"( + contract C { + function f() returns (bytes) { + return abi.encode(1, 2); + } + function g() returns (bytes) { + return abi.encodePacked(uint256(1), uint256(2)); + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(0x20), u256(0x40), u256(1), u256(2))); + ABI_CHECK(callContractFunction("g()"), encodeArgs(u256(0x20), u256(0x40), u256(1), u256(2))); +} + +BOOST_AUTO_TEST_CASE(abi_encode_complex_call) +{ + char const* sourceCode = R"T( + contract C { + function f(uint8 a, string b, string c) { + require(a == 42); + require(bytes(b).length == 2); + require(bytes(b)[0] == 72); // 'H' + require(bytes(b)[1] == 101); // 'e' + require(keccak256(b) == keccak256(c)); + } + function g(uint8 a, string b) returns (bool) { + bytes request = abi.encode( + bytes4(keccak256(abi.encodePacked("f(uint8,string)"))), + a, + b, + "He" + ); + return this.call(request); + } + } + )T"; + compileAndRun(sourceCode, 0, "C"); + ABI_CHECK(callContractFunction("g(uint8,string)", u256(42), u256(0x40), u256(2), string("He")), encodeArgs(u256(1))); +} + +BOOST_AUTO_TEST_CASE(abi_encodewithselector_complex_call) +{ + char const* sourceCode = R"T( + contract C { + function f(uint8 a, string b, string c) { + require(a == 42); + require(bytes(b).length == 2); + require(bytes(b)[0] == 72); // 'H' + require(bytes(b)[1] == 101); // 'e' + require(keccak256(b) == keccak256(c)); + } + function g(uint8 a, string b) returns (bool) { + bytes request = abi.encodeWithSignature( + "f(uint8,string)", + a, + b, + "He" + ); + return this.call(request); + } + } + )T"; + compileAndRun(sourceCode, 0, "C"); + ABI_CHECK(callContractFunction("g(uint8,string)", u256(42), u256(0x40), u256(2), string("He")), encodeArgs(u256(1))); +} + BOOST_AUTO_TEST_CASE(staticcall_for_view_and_pure) { char const* sourceCode = R"( diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 18a414e0..7678bf4a 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -7120,6 +7120,22 @@ BOOST_AUTO_TEST_CASE(tight_packing_literals) } )"; CHECK_WARNING(text, "The type of \"int_const 1\" was inferred as uint8."); + text = R"( + contract C { + function f() pure public returns (bytes) { + return abi.encode(1); + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); + text = R"( + contract C { + function f() pure public returns (bytes) { + return abi.encodePacked(1); + } + } + )"; + CHECK_WARNING(text, "The type of \"int_const 1\" was inferred as uint8."); } BOOST_AUTO_TEST_CASE(non_external_fallback) |