Add JUMPDEST

This commit is contained in:
nicksavers 2014-10-16 03:49:08 +02:00
parent 07245866bb
commit 7b914794e9
4 changed files with 70 additions and 13 deletions

View File

@ -54,7 +54,13 @@ public class Program {
this.result.setRepository(invokeData.getRepository()); this.result.setRepository(invokeData.getRepository());
} }
} }
public byte getOp(int pc) {
if(ops.length == 0)
return 0;
return ops[pc];
}
public byte getCurrentOp() { public byte getCurrentOp() {
if(ops.length == 0) if(ops.length == 0)
return 0; return 0;
@ -93,9 +99,12 @@ public class Program {
} }
public void setPC(DataWord pc) { public void setPC(DataWord pc) {
this.setPC(pc.intValue());
}
this.pc = pc.intValue(); public void setPC(int pc) {
this.pc = pc;
if (this.pc == ops.length) if (this.pc == ops.length)
stop(); stop();
@ -105,10 +114,6 @@ public class Program {
} }
} }
public void setPC(int pc) {
this.pc = pc;
}
public boolean isStopped() { public boolean isStopped() {
return stopped; return stopped;
} }

View File

@ -800,23 +800,25 @@ public class VM {
case JUMP:{ case JUMP:{
program.stackRequire(1); program.stackRequire(1);
DataWord pos = program.stackPop(); DataWord pos = program.stackPop();
// if (!pos.equals(DataWord.ZERO) && OpCode.code(program.getCurrentOp()) != OpCode.JUMPDEST) int nextPC = pos.intValue();
// if (nextPC != 0 && program.getOp(nextPC-1) != OpCode.JUMPDEST.asInt())
// throw new BadJumpDestinationException(); // throw new BadJumpDestinationException();
if (logger.isInfoEnabled()) if (logger.isInfoEnabled())
hint = "~> " + pos.value(); hint = "~> " + pos.value();
program.setPC(pos); program.setPC(nextPC);
} break; } break;
case JUMPI:{ case JUMPI:{
program.stackRequire(2); program.stackRequire(2);
DataWord pos = program.stackPop(); DataWord pos = program.stackPop();
DataWord cond = program.stackPop(); DataWord cond = program.stackPop();
// if (!pos.isZero() && OpCode.code(program.getCurrentOp()) != OpCode.JUMPDEST) int nextPC = pos.intValue();
// if (nextPC != 0 && program.getOp(nextPC-1) != OpCode.JUMPDEST.asInt())
// throw new BadJumpDestinationException(); // throw new BadJumpDestinationException();
if (!cond.isZero()) { if (!cond.isZero()) {
program.setPC(pos); program.setPC(nextPC);
} else { } else {
program.step(); program.step();
} }

View File

@ -12,8 +12,9 @@ import java.nio.file.Files;
public class JSONReader { public class JSONReader {
public static String loadJSON(String filename) { public static String loadJSON(String filename) {
String json = getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/" + filename); return getFromLocal(filename);
return json == "" ? json = getFromLocal(filename) : json; // String json = getFromUrl("https://raw.githubusercontent.com/ethereum/tests/master/vmtests/" + filename);
// return json == "" ? json = getFromLocal(filename) : json;
} }
public static String getFromLocal(String filename) { public static String getFromLocal(String filename) {

View File

@ -1776,6 +1776,55 @@ public class VMTest {
assertTrue(program.isStopped()); assertTrue(program.isStopped());
} }
} }
@Test // JUMPDEST OP for JUMP
public void testJUMPDEST_1() {
VM vm = new VM();
ProgramInvokeMockImpl invoke = new ProgramInvokeMockImpl();
Program program = new Program(Hex.decode("602360085860015d600257"), invoke);
String s_expected_key = "0000000000000000000000000000000000000000000000000000000000000002";
String s_expected_val = "0000000000000000000000000000000000000000000000000000000000000023";
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
DataWord key = new DataWord(Hex.decode(s_expected_key));
DataWord val = program.result.getRepository().getStorageValue(invoke.getOwnerAddress().getNoLeadZeroesData(), key);
assertTrue(program.isStopped());
program.getResult().getRepository().close();
assertEquals(s_expected_val, Hex.toHexString(val.getData()).toUpperCase());
}
@Test // JUMPDEST OP for JUMPI
public void testJUMPDEST_2() {
VM vm = new VM();
ProgramInvokeMockImpl invoke = new ProgramInvokeMockImpl();
Program program = new Program(Hex.decode("60236001600a5960015d600257"), invoke);
String s_expected_key = "0000000000000000000000000000000000000000000000000000000000000002";
String s_expected_val = "0000000000000000000000000000000000000000000000000000000000000023";
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
vm.step(program);
DataWord key = new DataWord(Hex.decode(s_expected_key));
DataWord val = program.result.getRepository().getStorageValue(invoke.getOwnerAddress().getNoLeadZeroesData(), key);
assertTrue(program.isStopped());
program.getResult().getRepository().close();
assertEquals(s_expected_val, Hex.toHexString(val.getData()).toUpperCase());
}
@Test // ADD OP mal @Test // ADD OP mal
public void testADD_1() { public void testADD_1() {