Skip to content

Commit

Permalink
Use a library instead of an interface for WasmMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkamarsik committed Jan 21, 2025
1 parent eb8d427 commit e6ac41d
Show file tree
Hide file tree
Showing 45 changed files with 1,241 additions and 1,080 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -83,6 +83,7 @@
import org.graalvm.wasm.WasmInstance;
import org.graalvm.wasm.WasmLanguage;
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.memory.WasmMemoryLibrary;
import org.graalvm.wasm.test.options.WasmTestOptions;
import org.graalvm.wasm.utils.WasmBinaryTools;
import org.graalvm.wasm.utils.cases.WasmCase;
Expand Down Expand Up @@ -235,7 +236,7 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
final boolean reinitMemory = requiresZeroMemory || iterationNeedsStateCheck(i + 1);
if (reinitMemory) {
for (int j = 0; j < wasmContext.memories().count(); ++j) {
wasmContext.memories().memory(j).reset();
WasmMemoryLibrary.getUncached().reset(wasmContext.memories().memory(j));
}
for (int j = 0; j < wasmContext.tables().tableCount(); ++j) {
wasmContext.tables().table(j).reset();
Expand Down Expand Up @@ -602,11 +603,12 @@ private static void assertContextEqual(ContextState expectedState, ContextState
if (expectedMemory == null) {
Assert.assertNull("Memory should be null", actualMemory);
} else {
WasmMemoryLibrary memories = WasmMemoryLibrary.getUncached();
Assert.assertNotNull("Memory should not be null", actualMemory);
Assert.assertEquals("Mismatch in memory lengths", expectedMemory.byteSize(), actualMemory.byteSize());
for (int ptr = 0; ptr < expectedMemory.byteSize(); ptr++) {
byte expectedByte = (byte) expectedMemory.load_i32_8s(null, ptr);
byte actualByte = (byte) actualMemory.load_i32_8s(null, ptr);
Assert.assertEquals("Mismatch in memory lengths", memories.byteSize(expectedMemory), memories.byteSize(actualMemory));
for (int ptr = 0; ptr < memories.byteSize(expectedMemory); ptr++) {
byte expectedByte = (byte) memories.load_i32_8s(expectedMemory, null, ptr);
byte actualByte = (byte) memories.load_i32_8s(actualMemory, null, ptr);
Assert.assertEquals("Memory mismatch at offset " + ptr + ",", expectedByte, actualByte);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.graalvm.wasm.globals.DefaultWasmGlobal;
import org.graalvm.wasm.globals.WasmGlobal;
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.memory.WasmMemoryLibrary;
import org.graalvm.wasm.predefined.testutil.TestutilModule;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -163,6 +164,7 @@ public void testInstantiateWithImportMemory() throws IOException {
runMemoryTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmMemory memory = WebAssembly.memAlloc(4, 8, false);
final WasmMemoryLibrary memoryLib = WasmMemoryLibrary.getUncached();
final Dictionary importObject = Dictionary.create(new Object[]{
"host", Dictionary.create(new Object[]{
"defaultMemory", memory
Expand All @@ -171,9 +173,9 @@ public void testInstantiateWithImportMemory() throws IOException {
final WasmInstance instance = moduleInstantiate(wasm, binaryWithMemoryImport, importObject);
try {
final Object initZero = WebAssembly.instanceExport(instance, "initZero");
Assert.assertEquals("Must be zero initially.", 0, memory.load_i32(null, 0));
Assert.assertEquals("Must be zero initially.", 0, memoryLib.load_i32(memory, null, 0));
InteropLibrary.getUncached(initZero).execute(initZero);
Assert.assertEquals("Must be 174 after initialization.", 174, memory.load_i32(null, 0));
Assert.assertEquals("Must be 174 after initialization.", 174, memoryLib.load_i32(memory, null, 0));
} catch (InteropException e) {
throw new RuntimeException(e);
}
Expand All @@ -187,8 +189,9 @@ public void testInstantiateWithExportMemory() throws IOException {
WasmInstance instance = moduleInstantiate(wasm, binaryWithMemoryExport, null);
try {
final WasmMemory memory = (WasmMemory) WebAssembly.instanceExport(instance, "memory");
final WasmMemoryLibrary memoryLib = WasmMemoryLibrary.getUncached();
final Object readZero = WebAssembly.instanceExport(instance, "readZero");
memory.store_i32(null, 0, 174);
memoryLib.store_i32(memory, null, 0, 174);
final Object result = InteropLibrary.getUncached(readZero).execute(readZero);
Assert.assertEquals("Must be 174.", 174, InteropLibrary.getUncached(result).asInt(result));
} catch (InteropException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -66,6 +66,7 @@
import org.graalvm.wasm.exception.WasmJsApiException;
import org.graalvm.wasm.globals.WasmGlobal;
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.memory.WasmMemoryLibrary;
import org.graalvm.wasm.predefined.testutil.TestutilModule;
import org.graalvm.wasm.utils.WasmBinaryTools;
import org.junit.Assert;
Expand Down Expand Up @@ -192,7 +193,8 @@ public void testImportsAndExports() throws IOException, InterruptedException {
a.addMember("t", t);

final WasmMemory m = WebAssembly.memAlloc(1, 1, false);
m.store_i32_8(null, 0, (byte) 5);
final WasmMemoryLibrary memoryLib = WasmMemoryLibrary.getUncached();
memoryLib.store_i32_8(m, null, 0, (byte) 5);
a.addMember("m", m);

final WasmGlobal g = wasm.globalAlloc(ValueType.i32, false, 4);
Expand Down
10 changes: 6 additions & 4 deletions wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/Linker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -99,6 +99,7 @@
import org.graalvm.wasm.globals.WasmGlobal;
import org.graalvm.wasm.memory.NativeDataInstanceUtil;
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.memory.WasmMemoryLibrary;
import org.graalvm.wasm.nodes.WasmFunctionNode;

import com.oracle.truffle.api.CallTarget;
Expand Down Expand Up @@ -750,10 +751,11 @@ void resolveDataSegment(WasmContext context, WasmInstance instance, int dataSegm
baseAddress = offsetAddress;
}

Assert.assertUnsignedLongLessOrEqual(baseAddress, memory.byteSize(), Failure.OUT_OF_BOUNDS_MEMORY_ACCESS);
Assert.assertUnsignedLongLessOrEqual(baseAddress + byteLength, memory.byteSize(), Failure.OUT_OF_BOUNDS_MEMORY_ACCESS);
WasmMemoryLibrary memoryLib = WasmMemoryLibrary.getUncached();
Assert.assertUnsignedLongLessOrEqual(baseAddress, memoryLib.byteSize(memory), Failure.OUT_OF_BOUNDS_MEMORY_ACCESS);
Assert.assertUnsignedLongLessOrEqual(baseAddress + byteLength, memoryLib.byteSize(memory), Failure.OUT_OF_BOUNDS_MEMORY_ACCESS);
final byte[] bytecode = instance.module().bytecode();
memory.initialize(bytecode, bytecodeOffset, baseAddress, byteLength);
memoryLib.initialize(memory, bytecode, bytecodeOffset, baseAddress, byteLength);
instance.setDataInstance(dataSegmentId, droppedDataInstanceOffset);
};
final ArrayList<Sym> dependencies = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -42,6 +42,7 @@

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.memory.WasmMemoryLibrary;

public class MemoryRegistry {
private static final int INITIAL_MEMORIES_SIZE = 4;
Expand Down Expand Up @@ -86,7 +87,7 @@ public WasmMemory memory(int index) {
public MemoryRegistry duplicate() {
final MemoryRegistry other = new MemoryRegistry();
for (int i = 0; i < numMemories; i++) {
final WasmMemory memory = memory(i).duplicate();
final WasmMemory memory = WasmMemoryLibrary.getUncached().duplicate(memory(i));
other.register(memory);
}
return other;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.graalvm.wasm.api.WebAssembly;
import org.graalvm.wasm.exception.WasmJsApiException;
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.memory.WasmMemoryLibrary;
import org.graalvm.wasm.predefined.BuiltinModule;

import com.oracle.truffle.api.CallTarget;
Expand Down Expand Up @@ -204,7 +205,7 @@ protected void finalizeContext(WasmContext context) {
super.finalizeContext(context);
for (int i = 0; i < context.memories().count(); ++i) {
final WasmMemory memory = context.memories().memory(i);
memory.close();
WasmMemoryLibrary.getUncached().close(memory);
}
try {
context.fdManager().close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.Source;
import org.graalvm.wasm.memory.WasmMemoryLibrary;

public class WebAssembly extends Dictionary {
private final WasmContext currentContext;
Expand Down Expand Up @@ -737,10 +738,11 @@ private static Object memGrow(Object[] args) {
}

public static long memGrow(WasmMemory memory, int delta) {
final long previousSize = memory.grow(delta);
WasmMemoryLibrary memoryLib = WasmMemoryLibrary.getUncached();
final long previousSize = memoryLib.grow(memory, delta);
if (previousSize == -1) {
throw new WasmJsApiException(WasmJsApiException.Kind.RangeError,
StrictMath.addExact(memory.size(), delta) <= memory.declaredMaxSize() ? "Cannot grow memory above implementation limit" : "Cannot grow memory above max limit");
StrictMath.addExact(memoryLib.size(memory), delta) <= memory.declaredMaxSize() ? "Cannot grow memory above implementation limit" : "Cannot grow memory above max limit");
}
return previousSize;
}
Expand Down Expand Up @@ -865,7 +867,7 @@ private static Object memAsByteBuffer(Object[] args) {
checkArgumentCount(args, 1);
if (args[0] instanceof WasmMemory) {
WasmMemory memory = (WasmMemory) args[0];
ByteBuffer buffer = memory.asByteBuffer();
ByteBuffer buffer = WasmMemoryLibrary.getUncached().asByteBuffer(memory);
if (buffer != null) {
return WasmContext.get(null).environment().asGuestValue(buffer);
}
Expand Down
Loading

0 comments on commit e6ac41d

Please sign in to comment.