From b84367b6056d5f559557e6414d60c1c6f3b31b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Bojarski?= <54240434+letypequividelespoubelles@users.noreply.github.com> Date: Wed, 27 Nov 2024 18:32:49 +0100 Subject: [PATCH] 1568 some last mmio issues second part (#1571) * fix(mmuCall): extcodecopy of account under deployment Signed-off-by: F Bojarski * fix: need to snapshot memory even if memorySpan empty Signed-off-by: F Bojarski --------- Signed-off-by: F Bojarski --- .../module/hub/defer/DeferRegistry.java | 4 ++ .../module/hub/section/call/CallSection.java | 50 +++++++++---------- .../ModexpSubsection.java | 8 +-- .../PrecompileSubsection.java | 4 +- .../zktracer/runtime/callstack/CallFrame.java | 1 - .../linea/zktracer/types/MemoryRange.java | 10 ++-- 6 files changed, 41 insertions(+), 36 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/defer/DeferRegistry.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/defer/DeferRegistry.java index 6d6c586614..8c4ec1b84b 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/defer/DeferRegistry.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/defer/DeferRegistry.java @@ -224,4 +224,8 @@ public void resolveUponContextReEntry(Hub hub, CallFrame callFrame) { public void unscheduleForContextReEntry(ContextReEntryDefer defer, CallFrame callFrame) { contextReEntryDefers.get(callFrame).remove(defer); } + + public void unscheduleForPostTransaction(PostTransactionDefer defer) { + postTransactionDefers.remove(defer); + } } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/CallSection.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/CallSection.java index 944c7c126d..6141266e15 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/CallSection.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/CallSection.java @@ -105,10 +105,6 @@ public class CallSection extends TraceSection // row i+0 private final CallScenarioFragment scenarioFragment = new CallScenarioFragment(); - public boolean isAbortingScenario() { - return scenarioFragment.getScenario().isAbortingScenario(); - } - // last row @Setter private ContextFragment finalContextFragment; @@ -211,7 +207,7 @@ public CallSection(Hub hub, MessageFrame frame) { value = opCode.callHasValueArgument() - ? Wei.of(currentFrame.frame().getStackItem(2).toUnsignedBigInteger()) + ? Wei.of(frame.getStackItem(2).toUnsignedBigInteger()) : Wei.ZERO; final CallOobCall oobCall = new CallOobCall(); @@ -224,13 +220,13 @@ public CallSection(Hub hub, MessageFrame frame) { hub.defers().scheduleForPostTransaction(this); // The CALL is now unexceptional and un-aborted - refineUndefinedScenario(hub); - CallScenarioFragment.CallScenario scenario = scenarioFragment.getScenario(); + refineUndefinedScenario(hub, frame); + final CallScenarioFragment.CallScenario scenario = scenarioFragment.getScenario(); switch (scenario) { - case CALL_ABORT_WONT_REVERT -> abortingCall(hub); case CALL_EOA_UNDEFINED -> eoaProcessing(hub); - case CALL_PRC_UNDEFINED -> prcProcessing(hub); case CALL_SMC_UNDEFINED -> smcProcessing(hub, frame); + case CALL_PRC_UNDEFINED -> prcProcessing(hub); + case CALL_ABORT_WONT_REVERT -> abortingCall(hub); default -> throw new RuntimeException("Illegal CALL scenario"); } } @@ -313,7 +309,7 @@ private void abortingCall(Hub hub) { * * @param hub */ - private void refineUndefinedScenario(Hub hub) { + private void refineUndefinedScenario(Hub hub, MessageFrame frame) { final boolean aborts = hub.pch().abortingConditions().any(); if (aborts) { @@ -321,13 +317,10 @@ private void refineUndefinedScenario(Hub hub) { return; } - final WorldUpdater world = hub.currentFrame().frame().getWorldUpdater(); + final WorldUpdater world = frame.getWorldUpdater(); if (isPrecompile(calleeAddress)) { precompileAddress = Optional.of(calleeAddress); scenarioFragment.setScenario(CALL_PRC_UNDEFINED); - - precompileSubsection = - ADDRESS_TO_PRECOMPILE.get(preOpcodeCalleeSnapshot.address()).apply(hub, this); } else { Optional.ofNullable(world.get(calleeAddress)) .ifPresentOrElse( @@ -361,8 +354,17 @@ private void smcProcessing(Hub hub, MessageFrame frame) { } private void prcProcessing(Hub hub) { + precompileSubsection = + ADDRESS_TO_PRECOMPILE.get(preOpcodeCalleeSnapshot.address()).apply(hub, this); hub.defers().scheduleForContextEntry(this); hub.defers().scheduleForContextReEntry(this, hub.currentFrame()); + // In case of arguments too large for MODEXP, transaction will be popped anyway, and resolving + // some defers will create NPE + if (precompileSubsection instanceof ModexpSubsection + && ((ModexpSubsection) precompileSubsection).transactionWillBePopped) { + hub.defers().unscheduleForContextReEntry(this, hub.currentFrame()); + hub.defers().unscheduleForPostTransaction(this); + } } @Override @@ -375,7 +377,7 @@ public void resolvePostExecution( @Override public void resolveUponContextEntry(Hub hub) { - CallScenarioFragment.CallScenario scenario = scenarioFragment.getScenario(); + final CallScenarioFragment.CallScenario scenario = scenarioFragment.getScenario(); checkState(scenario == CALL_SMC_UNDEFINED | scenario == CALL_PRC_UNDEFINED); postOpcodeCallerSnapshot = preOpcodeCallerSnapshot.deepCopy(); @@ -462,33 +464,29 @@ public void resolveAtContextReEntry(Hub hub, CallFrame frame) { } case CALL_SMC_UNDEFINED -> { - // CALL_SMC_SUCCESS_XXX case if (successBit) { scenarioFragment.setScenario(CALL_SMC_SUCCESS_WONT_REVERT); return; } - - AccountSnapshot beforeFailureCallerSnapshot = + final AccountSnapshot beforeFailureCallerSnapshot = postOpcodeCallerSnapshot.deepCopy().setDeploymentInfo(hub); - AccountSnapshot afterFailureCallerSnapshot = + final AccountSnapshot afterFailureCallerSnapshot = preOpcodeCallerSnapshot.deepCopy().setDeploymentInfo(hub); - AccountSnapshot beforeFailureCalleeSnapshot = + final AccountSnapshot beforeFailureCalleeSnapshot = postOpcodeCalleeSnapshot.deepCopy().setDeploymentInfo(hub); - AccountSnapshot afterFailureCalleeSnapshot = + final AccountSnapshot afterFailureCalleeSnapshot = preOpcodeCalleeSnapshot.deepCopy().setDeploymentInfo(hub).turnOnWarmth(); // CALL_SMC_FAILURE_XXX case scenarioFragment.setScenario(CALL_SMC_FAILURE_WONT_REVERT); - if (isNonzeroValueSelfCall()) { childContextExitCallerSnapshot.decrementBalanceBy(value); reEntryCalleeSnapshot.decrementBalanceBy(value); } - - int childId = hub.currentFrame().childFrameIds().getLast(); - CallFrame childFrame = hub.callStack().getById(childId); - int childContextRevertStamp = childFrame.revertStamp(); + final int childId = hub.currentFrame().childFrameIds().getLast(); + final CallFrame childFrame = hub.callStack().getById(childId); + final int childContextRevertStamp = childFrame.revertStamp(); final AccountFragment postReEntryCallerAccountFragment = hub.factories() diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/precompileSubsection/ModexpSubsection.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/precompileSubsection/ModexpSubsection.java index 6f1b74d697..c7b16a1e59 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/precompileSubsection/ModexpSubsection.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/precompileSubsection/ModexpSubsection.java @@ -53,6 +53,7 @@ public class ModexpSubsection extends PrecompileSubsection { public final ModexpMetadata modexpMetaData; private ModexpPricingOobCall sixthOobCall; private ImcFragment seventhImcFragment; + public boolean transactionWillBePopped = false; public ModexpSubsection(final Hub hub, final CallSection callSection) { super(hub, callSection); @@ -62,19 +63,20 @@ public ModexpSubsection(final Hub hub, final CallSection callSection) { .bbs() .toUnsignedBigInteger() .compareTo(BigInteger.valueOf(MODEXP_COMPONENT_BYTE_SIZE)) - >= 0 + > 0 || modexpMetaData .mbs() .toUnsignedBigInteger() .compareTo(BigInteger.valueOf(MODEXP_COMPONENT_BYTE_SIZE)) - >= 0 + > 0 || modexpMetaData .ebs() .toUnsignedBigInteger() .compareTo(BigInteger.valueOf(MODEXP_COMPONENT_BYTE_SIZE)) - >= 0) { + > 0) { hub.modexpEffectiveCall().addPrecompileLimit(Integer.MAX_VALUE); hub.defers().unscheduleForContextReEntry(this, hub.currentFrame()); + transactionWillBePopped = true; return; } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/precompileSubsection/PrecompileSubsection.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/precompileSubsection/PrecompileSubsection.java index 9cb6438f67..ca535aa440 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/precompileSubsection/PrecompileSubsection.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/call/precompileSubsection/PrecompileSubsection.java @@ -209,7 +209,9 @@ public long returnDataSize() { } public Bytes rawCallerMemory() { - return callSection.getCallDataRange().getRawData(); + return getCallDataRange().isEmpty() + ? getReturnAtRange().getRawData() + : getCallDataRange().getRawData(); } public Bytes extractCallData() { diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/runtime/callstack/CallFrame.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/runtime/callstack/CallFrame.java index 15bf11f170..9c8ef9c014 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/runtime/callstack/CallFrame.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/runtime/callstack/CallFrame.java @@ -71,7 +71,6 @@ public boolean isMessageCall() { @Getter private int byteCodeDeploymentNumber; private EWord eCodeAddress = null; // memoization @Getter private Bytecode code = Bytecode.EMPTY; - @Getter private int codeFragmentIndex = -1; // caller related information @Getter private Address callerAddress = Address.ZERO; diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/types/MemoryRange.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/types/MemoryRange.java index 3aff1f5b68..b9e37502b7 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/types/MemoryRange.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/types/MemoryRange.java @@ -73,21 +73,21 @@ public MemoryRange(long contextNumber) { public MemoryRange(final long contextNumber, final Range range, final Bytes rawData) { this.contextNumber = contextNumber; this.range = range; - this.rawData = rawData; + this.rawData = range.isEmpty() ? Bytes.EMPTY : rawData; } public MemoryRange( final long contextNumber, final long offset, final long size, final Bytes rawData) { this.contextNumber = contextNumber; this.range = Range.fromOffsetAndSize(offset, size); - this.rawData = rawData; + this.rawData = isEmpty() ? Bytes.EMPTY : rawData; } public MemoryRange(final long contextNumber, final Range range, final MessageFrame frame) { this.contextNumber = contextNumber; this.range = range; this.rawData = - (range.isEmpty()) ? Bytes.EMPTY : frame.shadowReadMemory(0, frame.memoryByteSize()); + range.isEmpty() ? Bytes.EMPTY : frame.shadowReadMemory(0, frame.memoryByteSize()); } public long offset() { @@ -103,7 +103,7 @@ public long contextNumber() { } public Bytes extract() { - return range.isEmpty() + return isEmpty() ? Bytes.EMPTY : rightPaddedSlice(rawData, safeLongToInt(range.offset()), safeLongToInt(range.size())); } @@ -113,6 +113,6 @@ public boolean isEmpty() { } public MemoryRange snapshot() { - return new MemoryRange(this.contextNumber, this.range.snapshot(), this.rawData); + return new MemoryRange(contextNumber, range.snapshot(), rawData.copy()); } }