Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VM Trace fixes #372

Merged
merged 5 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private static FlatTrace.Context handleSelfDestruct(
final List<FlatTrace.Builder> flatTraces,
final Deque<FlatTrace.Context> tracesContexts) {
final Bytes32[] stack = traceFrame.getStack().orElseThrow();
final Address refundAddress = toAddress(stack[0]);
final Address refundAddress = toAddress(stack[stack.length - 1]);
final FlatTrace.Builder subTraceBuilder =
FlatTrace.builder()
.type("suicide")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class VmTraceGenerator {
private int currentIndex = 0;
private VmTrace currentTrace;
private TraceFrame currentTraceFrame;
private String currentOperation;
private final TransactionTrace transactionTrace;
private final VmTrace rootVmTrace = new VmTrace();
private final Deque<VmTrace> parentTraces = new ArrayDeque<>();
Expand Down Expand Up @@ -117,10 +118,9 @@ private void completeStep(final VmOperation op, final VmOperationExecutionReport

private void handleDepthIncreased(final VmOperation op, final VmOperationExecutionReport report) {
// check if next frame depth has increased i.e the current operation is a call
if (currentTraceFrame.depthHasIncreased()) {
op.setCost(currentTraceFrame.getGasRemainingPostExecution().toLong() + op.getCost());
final VmTrace newSubTrace = new VmTrace();
parentTraces.addLast(newSubTrace);
if (currentTraceFrame.depthHasIncreased()
|| "STATICCALL".equals(currentOperation)
|| "CALL".equals(currentOperation)) {
findLastFrameInCall(currentTraceFrame, currentIndex)
.ifPresent(
lastFrameInCall -> {
Expand All @@ -138,13 +138,21 @@ private void handleDepthIncreased(final VmOperation op, final VmOperationExecuti
break;
default:
lastFrameInCall
.getMemory()
.map(mem -> mem.length > 0 ? new Mem(mem[0].toHexString(), 0) : null)
.getMaybeUpdatedMemory()
.map(
mem ->
new Mem(mem.getValue().toHexString(), mem.getOffset().intValue()))
.ifPresent(report::setMem);
}
});

op.setSub(newSubTrace);
if (currentTraceFrame.depthHasIncreased()) {
op.setCost(currentTraceFrame.getGasRemainingPostExecution().toLong() + op.getCost());
final VmTrace newSubTrace = new VmTrace();
parentTraces.addLast(newSubTrace);
op.setSub(newSubTrace);
} else {
op.setSub(new VmTrace());
}
}
}

Expand Down Expand Up @@ -173,13 +181,26 @@ private VmOperationExecutionReport generateExecutionReport() {
}

private void generateTracingMemory(final VmOperationExecutionReport report) {
currentTraceFrame
.getMaybeUpdatedMemory()
.map(
updatedMemory ->
new Mem(
updatedMemory.getValue().toHexString(), updatedMemory.getOffset().intValue()))
.ifPresent(report::setMem);
switch (currentOperation) {
case "CALLDATACOPY":
case "CODECOPY":
case "EXTCODECOPY":
case "MLOAD":
case "MSTORE":
case "MSTORE8":
case "RETURNDATACOPY":
currentTraceFrame
.getMaybeUpdatedMemory()
.map(
updatedMemory ->
new Mem(
updatedMemory.getValue().toHexString(),
updatedMemory.getOffset().intValue()))
.ifPresent(report::setMem);
break;
default:
break;
}
}

private void generateTracingPush(final VmOperationExecutionReport report) {
Expand Down Expand Up @@ -214,6 +235,7 @@ private void generateTracingStorage(final VmOperationExecutionReport report) {
*/
private void initStep(final TraceFrame frame) {
this.currentTraceFrame = frame;
this.currentOperation = frame.getOpcode();
currentTrace = parentTraces.getLast();
// set smart contract code
currentTrace.setCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ public void shouldTraceContractCreation() {
frame,
"0000000000000000000000000000000000000000000000000000000000000080",
"0000000000000000000000000000000000000000000000000000000000000040");
assertMemoryContainsExactly(frame);
assertMemoryContainsExactly(
frame,
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000080");
assertStorageContainsExactly(frame);
// Reference implementation actually records the memory after expansion but before the store.
// assertMemoryContainsExactly(frame,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ public void complete(final MessageFrame frame, final MessageFrame childFrame) {

if (outputSizeAsInt > outputData.size()) {
frame.expandMemory(outputOffset.toLong(), outputSizeAsInt);
frame.writeMemory(outputOffset, UInt256.valueOf(outputData.size()), outputData);
frame.writeMemory(outputOffset, UInt256.valueOf(outputData.size()), outputData, true);
} else {
frame.writeMemory(outputOffset, outputSize, outputData);
frame.writeMemory(outputOffset, outputSize, outputData, true);
}

frame.setReturnData(outputData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public void traceExecution(
EnumSet.copyOf(frame.getExceptionalHaltReasons());
final Bytes inputData = frame.getInputData();
final Optional<Bytes32[]> stack = captureStack(frame);
final Optional<Bytes[]> memory = captureMemory(frame);
final Optional<Map<UInt256, UInt256>> storagePreExecution = captureStorage(frame);
final WorldUpdater worldUpdater = frame.getWorldState();
final Optional<Bytes32[]> stackPostExecution;
Expand All @@ -70,6 +69,7 @@ public void traceExecution(
executeOperation.execute();
} finally {
final Bytes outputData = frame.getOutputData();
final Optional<Bytes[]> memory = captureMemory(frame);
stackPostExecution = captureStack(frame);
memoryPostExecution = captureMemory(frame);
if (lastFrame != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void execute(final MessageFrame frame) {
final UInt256 sourceOffset = UInt256.fromBytes(frame.popStackItem());
final UInt256 numBytes = UInt256.fromBytes(frame.popStackItem());

frame.writeMemory(memOffset, sourceOffset, numBytes, returnData);
frame.writeMemory(memOffset, sourceOffset, numBytes, returnData, true);
}

@Override
Expand Down