From 6667c6736483b2ff9855b9b9a9147a9703a188d2 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 5 Jun 2015 12:25:10 +0200 Subject: [PATCH 1/3] Fallback takes constant amount of gas, and send to gas with send. --- Compiler.cpp | 16 +++++++++++++++- ExpressionCompiler.cpp | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Compiler.cpp b/Compiler.cpp index 6425367dd..5398ed711 100644 --- a/Compiler.cpp +++ b/Compiler.cpp @@ -174,6 +174,16 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) map, FunctionTypePointer> interfaceFunctions = _contract.getInterfaceFunctions(); map, const eth::AssemblyItem> callDataUnpackerEntryPoints; + FunctionDefinition const* fallback = _contract.getFallbackFunction(); + eth::AssemblyItem notFound = m_context.newTag(); + // shortcut messages without data if we have many functions in order to be able to receive + // ether with constant gas + if (interfaceFunctions.size() > 5 || fallback) + { + m_context << eth::Instruction::CALLDATASIZE << eth::Instruction::ISZERO; + m_context.appendConditionalJumpTo(notFound); + } + // retrieve the function signature hash from the calldata if (!interfaceFunctions.empty()) CompilerUtils(m_context).loadFromMemory(0, IntegerType(CompilerUtils::dataStartOffset * 8), true); @@ -185,7 +195,10 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it.first)) << eth::Instruction::EQ; m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.at(it.first)); } - if (FunctionDefinition const* fallback = _contract.getFallbackFunction()) + m_context.appendJumpTo(notFound); + + m_context << notFound; + if (fallback) { eth::AssemblyItem returnTag = m_context.pushNewTag(); fallback->accept(*this); @@ -194,6 +207,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) } else m_context << eth::Instruction::STOP; // function not found + for (auto const& it: interfaceFunctions) { FunctionTypePointer const& functionType = it.second; diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index 62df9205f..f91839edf 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -521,6 +521,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) break; case Location::Send: _functionCall.getExpression().accept(*this); + m_context << u256(0); // do not send gas (there still is the stipend) arguments.front()->accept(*this); appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true); @@ -532,7 +533,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) strings(), Location::Bare, false, - false, + true, true ), {} From 5a9d01b815de437a287ac749b528a3dab1f98d9a Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 5 Jun 2015 14:34:10 +0200 Subject: [PATCH 2/3] Bare functions return success condition. --- ExpressionCompiler.cpp | 35 ++++++++++++++++++++++++----------- Types.cpp | 6 +++--- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index f91839edf..11de73082 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -1058,10 +1058,15 @@ void ExpressionCompiler::appendExternalFunctionCall( unsigned gasStackPos = m_context.currentToBaseStackOffset(gasValueSize); unsigned valueStackPos = m_context.currentToBaseStackOffset(1); + bool returnSuccessCondition = + _functionType.getLocation() == FunctionType::Location::Bare || + _functionType.getLocation() == FunctionType::Location::BareCallCode; //@todo only return the first return value for now Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr : _functionType.getReturnParameterTypes().front().get(); unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0; + if (returnSuccessCondition) + retSize = 0; // return value actually is success condition m_context << u256(retSize) << u256(0); if (_functionType.isBareCall()) @@ -1112,19 +1117,27 @@ void ExpressionCompiler::appendExternalFunctionCall( else m_context << eth::Instruction::CALL; - //Propagate error condition (if CALL pushes 0 on stack). - m_context << eth::Instruction::ISZERO; - m_context.appendConditionalJumpTo(m_context.errorTag()); + unsigned remainsSize = 1 + // contract address + _functionType.valueSet() + + _functionType.gasSet() + + !_functionType.isBareCall(); - if (_functionType.valueSet()) - m_context << eth::Instruction::POP; - if (_functionType.gasSet()) - m_context << eth::Instruction::POP; - if (!_functionType.isBareCall()) - m_context << eth::Instruction::POP; - m_context << eth::Instruction::POP; // pop contract address + if (returnSuccessCondition) + m_context << eth::swapInstruction(remainsSize); + else + { + //Propagate error condition (if CALL pushes 0 on stack). + m_context << eth::Instruction::ISZERO; + m_context.appendConditionalJumpTo(m_context.errorTag()); + } - if (_functionType.getLocation() == FunctionType::Location::RIPEMD160) + CompilerUtils(m_context).popStackSlots(remainsSize); + + if (returnSuccessCondition) + { + // already there + } + else if (_functionType.getLocation() == FunctionType::Location::RIPEMD160) { // fix: built-in contract returns right-aligned data CompilerUtils(m_context).loadFromMemory(0, IntegerType(160), false, true); diff --git a/Types.cpp b/Types.cpp index 03e8e4ee0..1316bbc37 100644 --- a/Types.cpp +++ b/Types.cpp @@ -317,9 +317,9 @@ TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointe const MemberList IntegerType::AddressMemberList({ {"balance", make_shared(256)}, - {"call", make_shared(strings(), strings(), FunctionType::Location::Bare, true)}, - {"callcode", make_shared(strings(), strings(), FunctionType::Location::BareCallCode, true)}, - {"send", make_shared(strings{"uint"}, strings{}, FunctionType::Location::Send)} + {"call", make_shared(strings(), strings{"bool"}, FunctionType::Location::Bare, true)}, + {"callcode", make_shared(strings(), strings{"bool"}, FunctionType::Location::BareCallCode, true)}, + {"send", make_shared(strings{"uint"}, strings{"bool"}, FunctionType::Location::Send)} }); IntegerConstantType::IntegerConstantType(Literal const& _literal) From 651d755e562dc8b5bc12e42b0b808ff1046f5d43 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 5 Jun 2015 17:38:06 +0200 Subject: [PATCH 3/3] Style. --- ExpressionCompiler.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index 11de73082..bac967d84 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -1117,10 +1117,11 @@ void ExpressionCompiler::appendExternalFunctionCall( else m_context << eth::Instruction::CALL; - unsigned remainsSize = 1 + // contract address - _functionType.valueSet() + - _functionType.gasSet() + - !_functionType.isBareCall(); + unsigned remainsSize = + 1 + // contract address + _functionType.valueSet() + + _functionType.gasSet() + + !_functionType.isBareCall(); if (returnSuccessCondition) m_context << eth::swapInstruction(remainsSize);