82
82
import org .graalvm .wasm .WasmFunctionInstance ;
83
83
import org .graalvm .wasm .WasmInstance ;
84
84
import org .graalvm .wasm .WasmLanguage ;
85
- import org .graalvm .wasm .WasmStore ;
86
85
import org .graalvm .wasm .memory .WasmMemory ;
87
86
import org .graalvm .wasm .memory .WasmMemoryLibrary ;
88
87
import org .graalvm .wasm .test .options .WasmTestOptions ;
@@ -210,7 +209,6 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
210
209
final WasmContext wasmContext = WasmContext .get (null );
211
210
final Value mainFunction = findMain (moduleInstances );
212
211
final List <WasmInstance > instanceList = moduleInstances .stream ().map (i -> toWasmInstance (i )).toList ();
213
- final var contextStore = instanceList .get (0 ).store ();
214
212
215
213
resetStatus (System .out , phaseIcon , phaseLabel );
216
214
@@ -244,7 +242,7 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
244
242
if (!wasmContext .environment ().getContext ().isClosed ()) {
245
243
// Save context state, and check that it's consistent with the previous one.
246
244
if (iterationNeedsStateCheck (i )) {
247
- final ContextState contextState = saveContext (contextStore );
245
+ final ContextState contextState = saveContext (wasmContext , instanceList );
248
246
if (firstIterationContextState == null ) {
249
247
firstIterationContextState = contextState ;
250
248
} else {
@@ -255,11 +253,13 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
255
253
// Reset context state.
256
254
final boolean reinitMemory = requiresZeroMemory || iterationNeedsStateCheck (i + 1 );
257
255
if (reinitMemory ) {
258
- for (int j = 0 ; j < contextStore .memories ().count (); ++j ) {
259
- WasmMemoryLibrary .getUncached ().reset (contextStore .memories ().memory (j ));
260
- }
261
- for (int j = 0 ; j < contextStore .tables ().tableCount (); ++j ) {
262
- contextStore .tables ().table (j ).reset ();
256
+ for (WasmInstance instance : instanceList ) {
257
+ for (int j = 0 ; j < instance .store ().memories ().count (); ++j ) {
258
+ WasmMemoryLibrary .getUncached ().reset (instance .store ().memories ().memory (j ));
259
+ }
260
+ for (int j = 0 ; j < instance .store ().tables ().tableCount (); ++j ) {
261
+ instance .store ().tables ().table (j ).reset ();
262
+ }
263
263
}
264
264
}
265
265
@@ -606,13 +606,19 @@ protected String suiteName() {
606
606
return getClass ().getSimpleName ();
607
607
}
608
608
609
- private static ContextState saveContext ( WasmStore store ) {
610
- final MemoryRegistry memories = store .memories ().duplicate ();
611
- final GlobalRegistry globals = store .globals ().duplicate ();
612
- return new ContextState (memories , globals , store . fdManager (). size () );
609
+ private static InstanceState saveInstanceState ( WasmInstance instance ) {
610
+ final MemoryRegistry memories = instance . store () .memories ().duplicate ();
611
+ final GlobalRegistry globals = instance .globals ().duplicate ();
612
+ return new InstanceState (memories , globals );
613
613
}
614
614
615
- private static void assertContextEqual (ContextState expectedState , ContextState actualState ) {
615
+ private static ContextState saveContext (WasmContext context , List <WasmInstance > instances ) {
616
+ return new ContextState (
617
+ instances .stream ().map (instance -> saveInstanceState (instance )).toList (),
618
+ context .fdManager ().size ());
619
+ }
620
+
621
+ private static void assertInstanceEqual (InstanceState expectedState , InstanceState actualState ) {
616
622
// Compare memories
617
623
final MemoryRegistry expectedMemories = expectedState .memories ();
618
624
final MemoryRegistry actualMemories = actualState .memories ();
@@ -643,28 +649,20 @@ private static void assertContextEqual(ContextState expectedState, ContextState
643
649
long last = lastGlobals .loadAsLong (address );
644
650
Assert .assertEquals ("Mismatch in global at " + address + ". " , first , last );
645
651
}
652
+ }
653
+
654
+ private static void assertContextEqual (ContextState expectedState , ContextState actualState ) {
655
+ for (int i = 0 ; i < expectedState .instanceState ().size (); i ++) {
656
+ assertInstanceEqual (expectedState .instanceState ().get (i ), actualState .instanceState ().get (i ));
657
+ }
646
658
647
659
// Check number of opened file descriptors
648
660
Assert .assertEquals ("Mismatch in file descriptor counts." , expectedState .openedFdCount , actualState .openedFdCount );
649
661
}
650
662
651
- private static final class ContextState {
652
- private final MemoryRegistry memories ;
653
- private final GlobalRegistry globals ;
654
- private final int openedFdCount ;
655
-
656
- private ContextState (MemoryRegistry memories , GlobalRegistry globals , int openedFdCount ) {
657
- this .memories = memories ;
658
- this .globals = globals ;
659
- this .openedFdCount = openedFdCount ;
660
- }
661
-
662
- public MemoryRegistry memories () {
663
- return memories ;
664
- }
663
+ private record InstanceState (MemoryRegistry memories , GlobalRegistry globals ) {
664
+ }
665
665
666
- public GlobalRegistry globals () {
667
- return globals ;
668
- }
666
+ private record ContextState (List <InstanceState > instanceState , int openedFdCount ) {
669
667
}
670
668
}
0 commit comments