Browse Source

ADDMOD, MULMOD, unsigned->bigint VM fixes.

cl-refactor
Gav Wood 10 years ago
parent
commit
eff34d1ab0
  1. 6
      eth/main.cpp
  2. 2
      libethereum/Executive.cpp
  3. 2
      libevm/ExtVMFace.h
  4. 43
      libevm/VM.h
  5. 6
      libevmface/Instruction.cpp
  6. 6
      libevmface/Instruction.h

6
eth/main.cpp

@ -598,7 +598,7 @@ int main(int argc, char** argv)
e.setup(&r);
if (format == "pretty")
e.go([&](uint64_t steps, Instruction instr, unsigned newMemSize, bigint gasCost, void* vvm, void const* vextVM)
e.go([&](uint64_t steps, Instruction instr, bigint newMemSize, bigint gasCost, void* vvm, void const* vextVM)
{
eth::VM* vm = (VM*)vvm;
eth::ExtVM const* ext = (ExtVM const*)vextVM;
@ -612,14 +612,14 @@ int main(int argc, char** argv)
f << dec << ext->level << " | " << ext->myAddress << " | #" << steps << " | " << hex << setw(4) << setfill('0') << vm->curPC() << " : " << c_instructionInfo.at(instr).name << " | " << dec << vm->gas() << " | -" << dec << gasCost << " | " << newMemSize << "x32";
});
else if (format == "standard")
e.go([&](uint64_t, Instruction instr, unsigned, bigint, void* vvm, void const* vextVM)
e.go([&](uint64_t, Instruction instr, bigint, bigint, void* vvm, void const* vextVM)
{
eth::VM* vm = (VM*)vvm;
eth::ExtVM const* ext = (ExtVM const*)vextVM;
f << ext->myAddress << " " << hex << toHex(eth::toCompactBigEndian(vm->curPC(), 1)) << " " << hex << toHex(eth::toCompactBigEndian((int)(byte)instr, 1)) << " " << hex << toHex(eth::toCompactBigEndian((uint64_t)vm->gas(), 1)) << endl;
});
else if (format == "standard+")
e.go([&](uint64_t, Instruction instr, unsigned, bigint, void* vvm, void const* vextVM)
e.go([&](uint64_t, Instruction instr, bigint, bigint, void* vvm, void const* vextVM)
{
eth::VM* vm = (VM*)vvm;
eth::ExtVM const* ext = (ExtVM const*)vextVM;

2
libethereum/Executive.cpp

@ -144,7 +144,7 @@ bool Executive::create(Address _sender, u256 _endowment, u256 _gasPrice, u256 _g
OnOpFunc Executive::simpleTrace()
{
return [](uint64_t steps, Instruction inst, unsigned newMemSize, bigint gasCost, void* voidVM, void const* voidExt)
return [](uint64_t steps, Instruction inst, bigint newMemSize, bigint gasCost, void* voidVM, void const* voidExt)
{
ExtVM const& ext = *(ExtVM const*)voidExt;
VM& vm = *(VM*)voidVM;

2
libevm/ExtVMFace.h

@ -83,6 +83,6 @@ public:
std::set<Address> suicides; ///< Any accounts that have suicided.
};
typedef std::function<void(uint64_t /*steps*/, Instruction /*instr*/, unsigned /*newMemSize*/, bigint /*gasCost*/, void/*VM*/*, void/*ExtVM*/ const*)> OnOpFunc;
typedef std::function<void(uint64_t /*steps*/, Instruction /*instr*/, bigint /*newMemSize*/, bigint /*gasCost*/, void/*VM*/*, void/*ExtVM*/ const*)> OnOpFunc;
}

43
libevm/VM.h

@ -98,7 +98,7 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
// FEES...
bigint runGas = c_stepGas;
unsigned newTempSize = (unsigned)m_temp.size();
bigint newTempSize = m_temp.size();
switch (inst)
{
case Instruction::STOP:
@ -126,32 +126,32 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
// These all operate on memory and therefore potentially expand it:
case Instruction::MSTORE:
require(2);
newTempSize = (unsigned)m_stack.back() + 32;
newTempSize = m_stack.back() + 32;
break;
case Instruction::MSTORE8:
require(2);
newTempSize = (unsigned)m_stack.back() + 1;
newTempSize = m_stack.back() + 1;
break;
case Instruction::MLOAD:
require(1);
newTempSize = (unsigned)m_stack.back() + 32;
newTempSize = m_stack.back() + 32;
break;
case Instruction::RETURN:
require(2);
newTempSize = (unsigned)m_stack.back() + (unsigned)m_stack[m_stack.size() - 2];
newTempSize = m_stack.back() + m_stack[m_stack.size() - 2];
break;
case Instruction::SHA3:
require(2);
runGas = c_sha3Gas;
newTempSize = (unsigned)m_stack.back() + (unsigned)m_stack[m_stack.size() - 2];
newTempSize = m_stack.back() + m_stack[m_stack.size() - 2];
break;
case Instruction::CALLDATACOPY:
require(3);
newTempSize = (unsigned)m_stack.back() + (unsigned)m_stack[m_stack.size() - 3];
newTempSize = m_stack.back() + m_stack[m_stack.size() - 3];
break;
case Instruction::CODECOPY:
require(3);
newTempSize = (unsigned)m_stack.back() + (unsigned)m_stack[m_stack.size() - 3];
newTempSize = m_stack.back() + m_stack[m_stack.size() - 3];
break;
case Instruction::BALANCE:
@ -160,15 +160,15 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
case Instruction::CALL:
require(7);
runGas = c_callGas + (unsigned)m_stack[m_stack.size() - 1];
newTempSize = std::max((unsigned)m_stack[m_stack.size() - 6] + (unsigned)m_stack[m_stack.size() - 7], (unsigned)m_stack[m_stack.size() - 4] + (unsigned)m_stack[m_stack.size() - 5]);
runGas = c_callGas + m_stack[m_stack.size() - 1];
newTempSize = std::max(m_stack[m_stack.size() - 6] + m_stack[m_stack.size() - 7], m_stack[m_stack.size() - 4] + m_stack[m_stack.size() - 5]);
break;
case Instruction::CREATE:
{
require(3);
unsigned inOff = (unsigned)m_stack[m_stack.size() - 2];
unsigned inSize = (unsigned)m_stack[m_stack.size() - 3];
auto inOff = m_stack[m_stack.size() - 2];
auto inSize = m_stack[m_stack.size() - 3];
newTempSize = inOff + inSize;
runGas = c_createGas;
break;
@ -183,7 +183,7 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
runGas += c_memoryGas * (newTempSize - m_temp.size()) / 32;
if (_onOp)
_onOp(osteps - _steps - 1, inst, newTempSize > m_temp.size() ? (newTempSize - m_temp.size()) / 32 : 0, runGas, this, &_ext);
_onOp(osteps - _steps - 1, inst, newTempSize > m_temp.size() ? (newTempSize - m_temp.size()) / 32 : bigint(0), runGas, this, &_ext);
if (m_gas < runGas)
{
@ -195,7 +195,7 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
m_gas = (u256)((bigint)m_gas - runGas);
if (newTempSize > m_temp.size())
m_temp.resize(newTempSize);
m_temp.resize((size_t)newTempSize);
// EXECUTE...
switch (inst)
@ -299,6 +299,18 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
m_stack[m_stack.size() - 2] = m_stack.back() < 32 ? (m_stack[m_stack.size() - 2] >> (uint)(8 * (31 - m_stack.back()))) & 0xff : 0;
m_stack.pop_back();
break;
case Instruction::ADDMOD:
require(3);
m_stack[m_stack.size() - 3] = u256((bigint(m_stack.back()) + bigint(m_stack[m_stack.size() - 2])) % m_stack[m_stack.size() - 3]);
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::MULMOD:
require(3);
m_stack[m_stack.size() - 3] = u256((bigint(m_stack.back()) * bigint(m_stack[m_stack.size() - 2])) % m_stack[m_stack.size() - 3]);
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::SHA3:
{
require(2);
@ -610,9 +622,6 @@ template <class Ext> eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _
}
case Instruction::STOP:
return bytesConstRef();
case Instruction::NOP1:
case Instruction::NOP2:
break;
default:
throw BadInstruction();
}

6
libevmface/Instruction.cpp

@ -47,6 +47,8 @@ const std::map<std::string, Instruction> eth::c_instructions =
{ "OR", Instruction::OR },
{ "XOR", Instruction::XOR },
{ "BYTE", Instruction::BYTE },
{ "ADDMOD", Instruction::ADDMOD },
{ "MULMOD", Instruction::MULMOD },
{ "SHA3", Instruction::SHA3 },
{ "ADDRESS", Instruction::ADDRESS },
{ "BALANCE", Instruction::BALANCE },
@ -66,8 +68,6 @@ const std::map<std::string, Instruction> eth::c_instructions =
{ "DIFFICULTY", Instruction::DIFFICULTY },
{ "GASLIMIT", Instruction::GASLIMIT },
{ "POP", Instruction::POP },
{ "NOP1", Instruction::NOP1 },
{ "NOP2", Instruction::NOP2 },
{ "MLOAD", Instruction::MLOAD },
{ "MSTORE", Instruction::MSTORE },
{ "MSTORE8", Instruction::MSTORE8 },
@ -170,6 +170,8 @@ const std::map<Instruction, InstructionInfo> eth::c_instructionInfo =
{ Instruction::OR, { "OR", 0, 2, 1 } },
{ Instruction::XOR, { "XOR", 0, 2, 1 } },
{ Instruction::BYTE, { "BYTE", 0, 2, 1 } },
{ Instruction::ADDMOD, { "ADDMOD", 0, 3, 1 } },
{ Instruction::MULMOD, { "MULMOD", 0, 3, 1 } },
{ Instruction::SHA3, { "SHA3", 0, 2, 1 } },
{ Instruction::ADDRESS, { "ADDRESS", 0, 0, 1 } },
{ Instruction::BALANCE, { "BALANCE", 0, 1, 1 } },

6
libevmface/Instruction.h

@ -56,6 +56,8 @@ enum class Instruction: uint8_t
OR,
XOR,
BYTE,
ADDMOD,
MULMOD,
SHA3 = 0x20,
@ -79,9 +81,7 @@ enum class Instruction: uint8_t
GASLIMIT,
POP = 0x50,
NOP1,
NOP2,
MLOAD,
MLOAD = 0x53,
MSTORE,
MSTORE8,
SLOAD,

Loading…
Cancel
Save