Merge pull request #88 from nicksavers/vmfix

Guard CALL against high initial gas price and overflow
This commit is contained in:
Nick Savers 2014-08-24 12:40:49 +02:00
commit 446f613cf6
2 changed files with 18 additions and 5 deletions

View File

@ -79,7 +79,6 @@ public class VM {
Stack<DataWord> stack = program.getStack(); Stack<DataWord> stack = program.getStack();
String hint = ""; String hint = "";
long gasBefore = program.getGas().longValue();
int stepBefore = program.getPC(); int stepBefore = program.getPC();
// Log debugging line for VM // Log debugging line for VM
@ -141,6 +140,10 @@ public class VM {
break; break;
case CALL: case CALL:
program.spendGas(GasCost.CALL, op.name()); program.spendGas(GasCost.CALL, op.name());
BigInteger callGas = stack.get(stack.size()-1).value();
if(callGas.compareTo(program.getGas().value()) == 1) {
throw program.new OutOfGasException();
}
BigInteger x = stack.get(stack.size()-6).value().add(stack.get(stack.size()-7).value()); BigInteger x = stack.get(stack.size()-6).value().add(stack.get(stack.size()-7).value());
BigInteger y = stack.get(stack.size()-4).value().add(stack.get(stack.size()-5).value()); BigInteger y = stack.get(stack.size()-4).value().add(stack.get(stack.size()-5).value());
newMemSize = x.max(y); newMemSize = x.max(y);
@ -326,7 +329,7 @@ public class VM {
DataWord word2 = program.stackPop(); DataWord word2 = program.stackPop();
if (logger.isInfoEnabled()) if (logger.isInfoEnabled())
hint = word1.value() + " < " + word2.value(); hint = word1.value() + " > " + word2.value();
if (word1.value().compareTo(word2.value()) == 1) { if (word1.value().compareTo(word2.value()) == 1) {
word1.and(DataWord.ZERO); word1.and(DataWord.ZERO);
@ -761,7 +764,8 @@ public class VM {
DataWord inSize = program.stackPop(); DataWord inSize = program.stackPop();
if (logger.isInfoEnabled()) if (logger.isInfoEnabled())
logger.info(logString, program.getPC(), op.name(), logger.info(logString, program.getPC(),
String.format("%-12s", op.name()),
program.getGas().value(), program.getGas().value(),
program.invokeData.getCallDeep(), hint); program.invokeData.getCallDeep(), hint);
@ -781,7 +785,8 @@ public class VM {
DataWord outDataSize = program.stackPop(); DataWord outDataSize = program.stackPop();
if (logger.isInfoEnabled()) if (logger.isInfoEnabled())
logger.info(logString, program.getPC(), op.name(), logger.info(logString, program.getPC(),
String.format("%-12s", op.name()),
program.getGas().value(), program.getGas().value(),
program.invokeData.getCallDeep(), hint); program.invokeData.getCallDeep(), hint);
@ -817,7 +822,7 @@ public class VM {
if (logger.isInfoEnabled() && !op.equals(CALL) if (logger.isInfoEnabled() && !op.equals(CALL)
&& !op.equals(CREATE)) && !op.equals(CREATE))
logger.info(logString, stepBefore, String.format("%-12s", op.name()), gasBefore, logger.info(logString, stepBefore, String.format("%-12s", op.name()), program.getGas().longValue(),
program.invokeData.getCallDeep(), hint); program.invokeData.getCallDeep(), hint);
// program.fullTrace(); // program.fullTrace();

View File

@ -4,6 +4,7 @@ import org.ethereum.core.AccountState;
import org.ethereum.crypto.HashUtil; import org.ethereum.crypto.HashUtil;
import org.ethereum.db.Repository; import org.ethereum.db.Repository;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
@ -346,4 +347,11 @@ public class VMComplexTest {
// TODO: check that the value pushed after exec is the new address // TODO: check that the value pushed after exec is the new address
repository.close(); repository.close();
} }
@Test // CALL contract with too much gas
@Ignore
public void test5() {
// TODO: CALL contract with gas > gasRemaining && gas > Long.MAX_VALUE
}
} }