Skip to content

Commit a180d4f

Browse files
committed
[GR-65863] Avoid global variable linkage run-time type dispatch.
PullRequest: graal/21169
2 parents e98678b + 83735fc commit a180d4f

18 files changed

+503
-589
lines changed

wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmFileSuite.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
import org.graalvm.wasm.WasmFunctionInstance;
8383
import org.graalvm.wasm.WasmInstance;
8484
import org.graalvm.wasm.WasmLanguage;
85-
import org.graalvm.wasm.WasmStore;
8685
import org.graalvm.wasm.memory.WasmMemory;
8786
import org.graalvm.wasm.memory.WasmMemoryLibrary;
8887
import org.graalvm.wasm.test.options.WasmTestOptions;
@@ -210,7 +209,6 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
210209
final WasmContext wasmContext = WasmContext.get(null);
211210
final Value mainFunction = findMain(moduleInstances);
212211
final List<WasmInstance> instanceList = moduleInstances.stream().map(i -> toWasmInstance(i)).toList();
213-
final var contextStore = instanceList.get(0).store();
214212

215213
resetStatus(System.out, phaseIcon, phaseLabel);
216214

@@ -244,7 +242,7 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
244242
if (!wasmContext.environment().getContext().isClosed()) {
245243
// Save context state, and check that it's consistent with the previous one.
246244
if (iterationNeedsStateCheck(i)) {
247-
final ContextState contextState = saveContext(contextStore);
245+
final ContextState contextState = saveContext(wasmContext, instanceList);
248246
if (firstIterationContextState == null) {
249247
firstIterationContextState = contextState;
250248
} else {
@@ -255,11 +253,13 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
255253
// Reset context state.
256254
final boolean reinitMemory = requiresZeroMemory || iterationNeedsStateCheck(i + 1);
257255
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+
}
263263
}
264264
}
265265

@@ -606,13 +606,19 @@ protected String suiteName() {
606606
return getClass().getSimpleName();
607607
}
608608

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);
613613
}
614614

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) {
616622
// Compare memories
617623
final MemoryRegistry expectedMemories = expectedState.memories();
618624
final MemoryRegistry actualMemories = actualState.memories();
@@ -643,28 +649,20 @@ private static void assertContextEqual(ContextState expectedState, ContextState
643649
long last = lastGlobals.loadAsLong(address);
644650
Assert.assertEquals("Mismatch in global at " + address + ". ", first, last);
645651
}
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+
}
646658

647659
// Check number of opened file descriptors
648660
Assert.assertEquals("Mismatch in file descriptor counts.", expectedState.openedFdCount, actualState.openedFdCount);
649661
}
650662

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+
}
665665

666-
public GlobalRegistry globals() {
667-
return globals;
668-
}
666+
private record ContextState(List<InstanceState> instanceState, int openedFdCount) {
669667
}
670668
}

wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmJsApiSuite.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
import org.graalvm.wasm.constants.Sizes;
8080
import org.graalvm.wasm.exception.WasmException;
8181
import org.graalvm.wasm.exception.WasmJsApiException;
82-
import org.graalvm.wasm.globals.DefaultWasmGlobal;
8382
import org.graalvm.wasm.globals.WasmGlobal;
8483
import org.graalvm.wasm.memory.WasmMemory;
8584
import org.graalvm.wasm.memory.WasmMemoryLibrary;
@@ -504,7 +503,7 @@ public void testGlobalWriteNull() throws IOException {
504503
public void testGlobalWriteAnyfuncRefTypesDisabled() throws IOException {
505504
runTest(WasmJsApiSuite::disableRefTypes, context -> {
506505
final WebAssembly wasm = new WebAssembly(context);
507-
final WasmGlobal global = new DefaultWasmGlobal(ValueType.anyfunc, true, WasmConstant.NULL);
506+
final WasmGlobal global = new WasmGlobal(ValueType.anyfunc, true, WasmConstant.NULL);
508507
try {
509508
wasm.globalWrite(global, WasmConstant.NULL);
510509
Assert.fail("Should have failed - ref types not enabled");
@@ -518,7 +517,7 @@ public void testGlobalWriteAnyfuncRefTypesDisabled() throws IOException {
518517
public void testGlobalWriteExternrefRefTypesDisabled() throws IOException {
519518
runTest(WasmJsApiSuite::disableRefTypes, context -> {
520519
final WebAssembly wasm = new WebAssembly(context);
521-
final WasmGlobal global = new DefaultWasmGlobal(ValueType.externref, true, WasmConstant.NULL);
520+
final WasmGlobal global = new WasmGlobal(ValueType.externref, true, WasmConstant.NULL);
522521
try {
523522
wasm.globalWrite(global, WasmConstant.NULL);
524523
Assert.fail("Should have failed - ref types not enabled");

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private void readSymbolSections() {
231231
module.checkDataSegmentCount(0);
232232
}
233233
module.setBytecode(bytecode.toArray());
234-
module.removeFunctionReferences();
234+
module.finishSymbolTable();
235235
module.setCustomData(customData.toArray());
236236
if (module.hasDebugInfo()) {
237237
functionDebugData.add(codeSectionLength);
@@ -2828,15 +2828,6 @@ private void readGlobalSection() {
28282828
final boolean isInitialized = initBytecode == null;
28292829

28302830
module.symbolTable().declareGlobal(globalIndex, type, mutability, isInitialized, initBytecode, initValue);
2831-
final int currentGlobalIndex = globalIndex;
2832-
module.addLinkAction((context, store, instance, imports) -> {
2833-
if (isInitialized) {
2834-
store.globals().store(type, instance.globalAddress(currentGlobalIndex), initValue);
2835-
store.linker().resolveGlobalInitialization(instance, currentGlobalIndex);
2836-
} else {
2837-
store.linker().resolveGlobalInitialization(store, instance, currentGlobalIndex, initBytecode);
2838-
}
2839-
});
28402831
}
28412832
}
28422833

0 commit comments

Comments
 (0)