Skip to content

Commit ea74b35

Browse files
committed
feat(vm): clear residual logs on exception escape paths under Osaka
Mirror the in-try failure path: failed txs must not leak event logs into TransactionInfo/Bloom. Gated on Osaka to preserve replay.
1 parent 0af1527 commit ea74b35

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎actuator/src/main/java/org/tron/core/actuator/VMActuator.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,19 @@ public void execute(Object object) throws ContractExeException {
267267
result = program.getResult();
268268
result.setException(e);
269269
result.rejectInternalTransactions();
270+
if (VMConfig.allowTvmOsaka()) {
271+
result.getLogInfoList().clear();
272+
}
270273
result.setRuntimeError(result.getException().getMessage());
271274
logger.info("JVMStackOverFlowException: {}", result.getException().getMessage());
272275
} catch (OutOfTimeException e) {
273276
program.spendAllEnergy();
274277
result = program.getResult();
275278
result.setException(e);
276279
result.rejectInternalTransactions();
280+
if (VMConfig.allowTvmOsaka()) {
281+
result.getLogInfoList().clear();
282+
}
277283
result.setRuntimeError(result.getException().getMessage());
278284
logger.info("timeout: {}", result.getException().getMessage());
279285
} catch (Throwable e) {
@@ -282,6 +288,9 @@ public void execute(Object object) throws ContractExeException {
282288
}
283289
result = program.getResult();
284290
result.rejectInternalTransactions();
291+
if (VMConfig.allowTvmOsaka()) {
292+
result.getLogInfoList().clear();
293+
}
285294
if (Objects.isNull(result.getException())) {
286295
logger.error(e.getMessage(), e);
287296
result.setException(new RuntimeException("Unknown Throwable"));
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.tron.common.runtime;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
5+
import java.lang.reflect.Field;
6+
import java.util.Collections;
7+
import org.junit.Assert;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
import org.mockito.MockedStatic;
11+
import org.mockito.Mockito;
12+
import org.tron.common.runtime.vm.LogInfo;
13+
import org.tron.core.actuator.VMActuator;
14+
import org.tron.core.db.TransactionContext;
15+
import org.tron.core.vm.OperationRegistry;
16+
import org.tron.core.vm.VM;
17+
import org.tron.core.vm.config.VMConfig;
18+
import org.tron.core.vm.program.Program;
19+
20+
public class VMActuatorMockTest {
21+
22+
@BeforeClass
23+
public static void init() {
24+
// warm up the registry so VM.play(..., OperationRegistry.getTable()) arg eval is safe
25+
OperationRegistry.init();
26+
}
27+
28+
private void runCatchPathTest(Throwable thrownByVm, boolean osakaOn, int expectedLogSize)
29+
throws Exception {
30+
boolean prevOsaka = VMConfig.allowTvmOsaka();
31+
VMConfig.initAllowTvmOsaka(osakaOn ? 1 : 0);
32+
try (MockedStatic<VM> vmMock = Mockito.mockStatic(VM.class)) {
33+
Program program = Mockito.mock(Program.class);
34+
ProgramResult result = new ProgramResult();
35+
result.addLogInfo(new LogInfo(new byte[20], Collections.emptyList(), new byte[0]));
36+
Mockito.when(program.getResult()).thenReturn(result);
37+
38+
vmMock.when(() -> VM.play(any(), any())).thenThrow(thrownByVm);
39+
40+
VMActuator actuator = new VMActuator(false);
41+
Field f = VMActuator.class.getDeclaredField("program");
42+
f.setAccessible(true);
43+
f.set(actuator, program);
44+
45+
TransactionContext context = Mockito.mock(TransactionContext.class);
46+
Mockito.when(context.getProgramResult()).thenReturn(new ProgramResult());
47+
48+
actuator.execute(context);
49+
50+
Assert.assertEquals(expectedLogSize, result.getLogInfoList().size());
51+
} finally {
52+
VMConfig.initAllowTvmOsaka(prevOsaka ? 1 : 0);
53+
}
54+
}
55+
56+
@Test
57+
public void osakaClearsLogOnOutOfTime() throws Exception {
58+
runCatchPathTest(new Program.OutOfTimeException("timeout"), true, 0);
59+
}
60+
61+
@Test
62+
public void osakaClearsLogOnJvmStackOverflow() throws Exception {
63+
runCatchPathTest(new Program.JVMStackOverFlowException(), true, 0);
64+
}
65+
66+
@Test
67+
public void osakaClearsLogOnThrowable() throws Exception {
68+
runCatchPathTest(new RuntimeException("boom"), true, 0);
69+
}
70+
71+
@Test
72+
public void preOsakaKeepsLogOnOutOfTime() throws Exception {
73+
runCatchPathTest(new Program.OutOfTimeException("timeout"), false, 1);
74+
}
75+
76+
@Test
77+
public void preOsakaKeepsLogOnJvmStackOverflow() throws Exception {
78+
runCatchPathTest(new Program.JVMStackOverFlowException(), false, 1);
79+
}
80+
81+
@Test
82+
public void preOsakaKeepsLogOnThrowable() throws Exception {
83+
runCatchPathTest(new RuntimeException("boom"), false, 1);
84+
}
85+
}

0 commit comments

Comments
 (0)