Browse Source

Give back an amount of gas not used by CALL instructions

[#79942174]
cl-refactor
Paweł Bylica 10 years ago
parent
commit
01ca987093
  1. 1
      evmcc/Compiler.cpp
  2. 7
      evmcc/Ext.cpp
  3. 2
      evmcc/Ext.h
  4. 7
      evmcc/GasMeter.cpp
  5. 3
      evmcc/GasMeter.h

1
evmcc/Compiler.cpp

@ -786,6 +786,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
gasMeter.commitCostBlock(gas);
auto ret = ext.call(gas, receiveAddress, value, inOff, inSize, outOff, outSize);
gasMeter.giveBack(gas);
stack.push(ret);
break;
}

7
evmcc/Ext.cpp

@ -165,7 +165,7 @@ Value* Ext::create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value*
return address;
}
llvm::Value* Ext::call(llvm::Value* _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize)
llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize)
{
m_builder.CreateStore(_gas, m_args[0]);
auto receiveAddress = bswap(_receiveAddress); // to BE
@ -177,6 +177,7 @@ llvm::Value* Ext::call(llvm::Value* _gas, llvm::Value* _receiveAddress, llvm::Va
m_builder.CreateStore(_outSize, m_arg7);
llvm::Value* args[] = {m_args[0], m_arg2, m_arg3, m_arg4, m_arg5, m_arg6, m_arg7, m_args[1]};
m_builder.CreateCall(m_call, args);
_gas = m_builder.CreateLoad(m_args[0]); // Return gas
return m_builder.CreateLoad(m_args[1]);
}
@ -284,10 +285,10 @@ EXPORT void ext_call(i256* _gas, h256* _receiveAddress, i256* _value, i256* _inO
auto value = llvm2eth(*_value);
auto ret = false;
auto gas = llvm2eth(*_gas);
if (ext.balance(ext.myAddress) >= value)
{
ext.subBalance(value);
auto gas = llvm2eth(*_gas);
auto receiveAddress = dev::right160(*_receiveAddress);
auto inOff = static_cast<size_t>(llvm2eth(*_inOff));
auto inSize = static_cast<size_t>(llvm2eth(*_inSize));
@ -299,7 +300,7 @@ EXPORT void ext_call(i256* _gas, h256* _receiveAddress, i256* _value, i256* _inO
auto ret = ext.call(receiveAddress, value, inRef, &gas, outRef, onOp, {}, receiveAddress);
}
// m_gas += gas; // TODO: Handle gas
*_gas = eth2llvm(gas);
_ret->a = ret ? 1 : 0;
}

2
evmcc/Ext.h

@ -36,7 +36,7 @@ public:
void suicide(llvm::Value* _address);
llvm::Value* calldataload(llvm::Value* _index);
llvm::Value* create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize);
llvm::Value* call(llvm::Value* _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize);
llvm::Value* call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize);
llvm::Value* sha3(llvm::Value* _inOff, llvm::Value* _inSize);
llvm::Value* exp(llvm::Value* _left, llvm::Value* _right);

7
evmcc/GasMeter.cpp

@ -108,6 +108,13 @@ void GasMeter::count(Instruction _inst)
commitCostBlock();
}
void GasMeter::giveBack(llvm::Value* _gas)
{
llvm::Value* gasCounter = m_builder.CreateLoad(m_gas, "gas");
gasCounter = m_builder.CreateAdd(gasCounter, _gas);
m_builder.CreateStore(gasCounter, m_gas);
}
void GasMeter::commitCostBlock(llvm::Value* _additionalCost)
{
assert(!_additionalCost || m_checkCall); // _additionalCost => m_checkCall; Must be inside cost-block

3
evmcc/GasMeter.h

@ -23,6 +23,9 @@ public:
/// @param _additionalCost adds additional cost to cost-block before commit
void commitCostBlock(llvm::Value* _additionalCost = nullptr);
/// Give back an amount of gas not used by a call
void giveBack(llvm::Value* _gas);
/// Generate code that checks the cost of additional memory used by program
void checkMemory(llvm::Value* _additionalMemoryInWords, llvm::IRBuilder<>& _builder);

Loading…
Cancel
Save