Compiler EVM generation now takes into account for the new function hash

identifier

- Changed tests to comply with the new function hash identifier
- Changed the function index offset to 4, and made it a constant for
  easy adjustment in the future
This commit is contained in:
Lefteris Karapetsas 2015-01-08 17:18:31 +01:00
parent 68b9427535
commit 184ddca5a1
5 changed files with 23 additions and 18 deletions

View File

@ -100,7 +100,7 @@ void Compiler::appendConstructorCall(FunctionDefinition const& _constructor)
{
m_context << u256(argumentSize);
m_context.appendProgramSize();
m_context << u256(1); // copy it to byte one as expected for ABI calls
m_context << u256(g_functionIdentifierOffset); // copy it to byte four as expected for ABI calls
m_context << eth::Instruction::CODECOPY;
appendCalldataUnpacker(_constructor, true);
}
@ -125,12 +125,12 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 4294967295 public functions for contract."));
// retrieve the function signature hash from the calldata
m_context << u256(1) << u256(0) << u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296)// some constants
m_context << u256(1) << u256(0) << (u256(1) << 224) // some constants
<< eth::dupInstruction(2) << eth::Instruction::CALLDATALOAD
<< eth::Instruction::DIV;
// stack now is: 1 0 <funhash>
for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it)
for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it)
{
callDataUnpackerEntryPoints.push_back(m_context.newTag());
m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it->first)) << eth::Instruction::EQ;
@ -139,7 +139,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
m_context << eth::Instruction::STOP; // function not found
unsigned funid = 0;
for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it, ++funid)
for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it, ++funid)
{
FunctionDefinition const& function = *it->second;
m_context << callDataUnpackerEntryPoints[funid];
@ -154,7 +154,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory)
{
// We do not check the calldata size, everything is zero-padded.
unsigned dataOffset = 1;
unsigned dataOffset = g_functionIdentifierOffset; // the 4 bytes of the function hash signature
//@todo this can be done more efficiently, saving some CALLDATALOAD calls
for (ASTPointer<VariableDeclaration> const& var: _function.getParameters())
{

View File

@ -30,6 +30,9 @@ namespace solidity {
class Type; // forward
/// The size in bytes of the function (hash) identifier
static const unsigned int g_functionIdentifierOffset = 4;
class CompilerUtils
{
public:

View File

@ -335,7 +335,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
case Type::Category::CONTRACT:
{
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
m_context << type.getFunctionIndex(member);
m_context << type.getFunctionIdentifier(member);
break;
}
case Type::Category::MAGIC:
@ -590,7 +590,11 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
{
solAssert(_arguments.size() == _functionType.getParameterTypes().size(), "");
unsigned dataOffset = _options.bare ? 0 : 1; // reserve one byte for the function index
_options.obtainAddress();
if (!_options.bare)
CompilerUtils(m_context).storeInMemory(0, g_functionIdentifierOffset);
unsigned dataOffset = _options.bare ? 0 : g_functionIdentifierOffset; // reserve 4 bytes for the function's hash identifier
for (unsigned i = 0; i < _arguments.size(); ++i)
{
_arguments[i]->accept(*this);
@ -617,12 +621,13 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
_options.obtainValue();
else
m_context << u256(0);
_options.obtainAddress();
if (!_options.bare)
m_context << u256(0) << eth::Instruction::MSTORE8;
m_context << eth::dupInstruction(6); //copy contract address
m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB
<< eth::Instruction::CALL
<< eth::Instruction::POP; // @todo do not ignore failure indicator
<< eth::Instruction::POP // @todo do not ignore failure indicator
<< eth::Instruction::POP; // pop contract address
if (retSize > 0)
{
bool const leftAligned = firstType->getCategory() == Type::Category::STRING;

View File

@ -323,16 +323,13 @@ shared_ptr<FunctionType const> const& ContractType::getConstructorType() const
return m_constructorType;
}
unsigned ContractType::getFunctionIndex(string const& _functionName) const
u256 ContractType::getFunctionIdentifier(string const& _functionName) const
{
unsigned index = 0;
auto interfaceFunctions = m_contract.getInterfaceFunctions();
for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it)
{
if (it->second->getName() == _functionName)
return index;
++index;
}
return FixedHash<4>::Arith(it->first);
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index of non-existing contract function requested."));
}

View File

@ -254,7 +254,7 @@ public:
/// is not used, as this type cannot be the type of a variable or expression.
std::shared_ptr<FunctionType const> const& getConstructorType() const;
unsigned getFunctionIndex(std::string const& _functionName) const;
u256 getFunctionIdentifier(std::string const& _functionName) const;
private:
ContractDefinition const& m_contract;