From dd2f878e594807cb86bdb49bc979c63e2b8d7e81 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 16 Sep 2016 12:56:43 +0200 Subject: [PATCH 1/3] Test case. --- test/libsolidity/SolidityEndToEndTest.cpp | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 7ee5700c7..c8bc7ff91 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -7186,6 +7186,33 @@ BOOST_AUTO_TEST_CASE(no_nonpayable_circumvention_by_modifier) BOOST_CHECK_EQUAL(balanceAt(m_contractAddress), 0); } +BOOST_AUTO_TEST_CASE(mem_resize_is_not_paid_at_call) +{ + // This tests that memory resize for return values is not paid during the call, which would + // make the gas calculation overly complex. We access the end of the output area before + // the call is made. + // Tests that this also survivecs the optimizer. + char const* sourceCode = R"( + contract C { + function f() returns (uint[200]) {} + } + contract D { + function f(C c) returns (uint) { c.f(); return 7; } + } + )"; + + compileAndRun(sourceCode, 0, "C"); + u160 cAddr = m_contractAddress; + compileAndRun(sourceCode, 0, "D"); + BOOST_CHECK(callContractFunction("f(address)", cAddr) == encodeArgs(u256(7))); + + m_optimize = true; + + compileAndRun(sourceCode, 0, "C"); + u160 cAddrOpt = m_contractAddress; + compileAndRun(sourceCode, 0, "D"); + BOOST_CHECK(callContractFunction("f(address)", cAddrOpt) == encodeArgs(u256(7))); +} BOOST_AUTO_TEST_SUITE_END() From 5a45990458e9fc39a124d2b949ff77d1f6f1d8a7 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 16 Sep 2016 12:56:52 +0200 Subject: [PATCH 2/3] Access output memory area so that we do not pay for resize during call. --- libsolidity/codegen/ExpressionCompiler.cpp | 17 +++++++++++++---- test/libsolidity/SolidityEndToEndTest.cpp | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 96ca42966..26acd8a47 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -1476,6 +1476,18 @@ void ExpressionCompiler::appendExternalFunctionCall( utils().storeFreeMemoryPointer(); } + // Touch the end of the output area so that we do not pay for memory resize during the call + // (which we would have to subtract from the gas left) + // We could also just use MLOAD; POP right before the gas calculation, but the optimizer + // would remove that, so we use MSTORE here. + if (!_functionType.gasSet() && retSize > 0) + { + m_context << u256(0); + utils().fetchFreeMemoryPointer(); + // This touches too much, but that way we save some rounding arithmetics + m_context << u256(retSize) << Instruction::ADD << Instruction::MSTORE; + } + // Copy function identifier to memory. utils().fetchFreeMemoryPointer(); if (!_functionType.isBareCall() || manualFunctionId) @@ -1551,10 +1563,7 @@ void ExpressionCompiler::appendExternalFunctionCall( gasNeededByCaller += eth::GasCosts::callValueTransferGas; if (!isCallCode && !isDelegateCall && !existenceChecked) gasNeededByCaller += eth::GasCosts::callNewAccountGas; // we never know - m_context << - gasNeededByCaller << - Instruction::GAS << - Instruction::SUB; + m_context << gasNeededByCaller << Instruction::GAS << Instruction::SUB; } if (isDelegateCall) m_context << Instruction::DELEGATECALL; diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index c8bc7ff91..0ce2851b5 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -7191,7 +7191,7 @@ BOOST_AUTO_TEST_CASE(mem_resize_is_not_paid_at_call) // This tests that memory resize for return values is not paid during the call, which would // make the gas calculation overly complex. We access the end of the output area before // the call is made. - // Tests that this also survivecs the optimizer. + // Tests that this also survives the optimizer. char const* sourceCode = R"( contract C { function f() returns (uint[200]) {} From a4f6e5b16c7b9a9ba94c63b6042c20a988219e8e Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 16 Sep 2016 17:03:47 +0200 Subject: [PATCH 3/3] Changelog entry. --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index d61ac7c83..609bb9f8b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,8 @@ ### 0.4.2 * Type Checker: Fixed a crash about invalid array types. + * Code Generator: Fixed a call gas bug that became visible after + version 0.4.0 for calls where the output is larger than the input. ### 0.4.1 (2016-09-09)