From 7454d99b60d5ac15e0bbc1a3c913f55ed8c89f6f Mon Sep 17 00:00:00 2001 From: nicksavers Date: Sun, 24 Aug 2014 11:38:27 +0200 Subject: [PATCH 1/3] Guard CALL against high initial gas price and overflow --- .../src/main/java/org/ethereum/vm/VM.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java index 52a2d901..6a5e075e 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -79,7 +79,6 @@ public class VM { Stack stack = program.getStack(); String hint = ""; - long gasBefore = program.getGas().longValue(); int stepBefore = program.getPC(); // Log debugging line for VM @@ -141,6 +140,9 @@ public class VM { break; case CALL: program.spendGas(GasCost.CALL, op.name()); + if(stack.get(stack.size()-1).value().compareTo(MAX_GAS) == 1) { + throw program.new OutOfGasException(); // protect against overflow (needs refactoring) + } 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()); newMemSize = x.max(y); @@ -326,7 +328,7 @@ public class VM { DataWord word2 = program.stackPop(); if (logger.isInfoEnabled()) - hint = word1.value() + " < " + word2.value(); + hint = word1.value() + " > " + word2.value(); if (word1.value().compareTo(word2.value()) == 1) { word1.and(DataWord.ZERO); @@ -761,7 +763,8 @@ public class VM { DataWord inSize = program.stackPop(); if (logger.isInfoEnabled()) - logger.info(logString, program.getPC(), op.name(), + logger.info(logString, program.getPC(), + String.format("%-12s", op.name()), program.getGas().value(), program.invokeData.getCallDeep(), hint); @@ -781,7 +784,8 @@ public class VM { DataWord outDataSize = program.stackPop(); if (logger.isInfoEnabled()) - logger.info(logString, program.getPC(), op.name(), + logger.info(logString, program.getPC(), + String.format("%-12s", op.name()), program.getGas().value(), program.invokeData.getCallDeep(), hint); @@ -817,7 +821,7 @@ public class VM { if (logger.isInfoEnabled() && !op.equals(CALL) && !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.fullTrace(); From 1f3935a3ae56f4b2edc573297363b4ccc18be0af Mon Sep 17 00:00:00 2001 From: nicksavers Date: Sun, 24 Aug 2014 12:13:04 +0200 Subject: [PATCH 2/3] Compare CALL gas value against gas remaining --- ethereumj-core/src/main/java/org/ethereum/vm/VM.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java index 6a5e075e..a8c65597 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -140,8 +140,9 @@ public class VM { break; case CALL: program.spendGas(GasCost.CALL, op.name()); - if(stack.get(stack.size()-1).value().compareTo(MAX_GAS) == 1) { - throw program.new OutOfGasException(); // protect against overflow (needs refactoring) + 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 y = stack.get(stack.size()-4).value().add(stack.get(stack.size()-5).value()); From abfc40f293069aa6103db5685f89945722a6d30b Mon Sep 17 00:00:00 2001 From: nicksavers Date: Sun, 24 Aug 2014 12:36:00 +0200 Subject: [PATCH 3/3] Add placeholder for Unit test of too high CALL gas --- .../src/test/java/org/ethereum/vm/VMComplexTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ethereumj-core/src/test/java/org/ethereum/vm/VMComplexTest.java b/ethereumj-core/src/test/java/org/ethereum/vm/VMComplexTest.java index cc5780a7..7c0f02ed 100644 --- a/ethereumj-core/src/test/java/org/ethereum/vm/VMComplexTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/vm/VMComplexTest.java @@ -4,6 +4,7 @@ import org.ethereum.core.AccountState; import org.ethereum.crypto.HashUtil; import org.ethereum.db.Repository; import org.junit.FixMethodOrder; +import org.junit.Ignore; import org.junit.Test; import org.junit.runners.MethodSorters; 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 repository.close(); } + + @Test // CALL contract with too much gas + @Ignore + public void test5() { + // TODO: CALL contract with gas > gasRemaining && gas > Long.MAX_VALUE + } + }