Search in sources :

Example 11 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class Linker method resolveMemoryImport.

void resolveMemoryImport(WasmContext context, WasmInstance instance, ImportDescriptor importDescriptor, int declaredMinSize, int declaredMaxSize) {
    final String importedModuleName = importDescriptor.moduleName;
    final String importedMemoryName = importDescriptor.memberName;
    final Runnable resolveAction = () -> {
        final WasmInstance importedInstance = context.moduleInstances().get(importedModuleName);
        if (importedInstance == null) {
            throw WasmException.create(Failure.UNKNOWN_IMPORT, String.format("The module '%s', referenced in the import of memory '%s' in module '%s', does not exist", importedModuleName, importedMemoryName, instance.name()));
        }
        final List<String> exportedMemory = importedInstance.symbolTable().exportedMemoryNames();
        if (exportedMemory.size() == 0) {
            throw WasmException.create(Failure.UNKNOWN_IMPORT, String.format("The imported module '%s' does not export any memories, so cannot resolve memory '%s' imported in module '%s'.", importedModuleName, importedMemoryName, instance.name()));
        }
        if (!exportedMemory.contains(importedMemoryName)) {
            throw WasmException.create(Failure.UNKNOWN_IMPORT, String.format("The imported module '%s' exports a memory '%s', but module '%s' imports a memory '%s'.", importedModuleName, exportedMemory, instance.name(), importedModuleName));
        }
        final WasmMemory memory = importedInstance.memory();
        // Rules for limits matching:
        // https://webassembly.github.io/spec/core/exec/modules.html#limits
        // If no max size is declared, then declaredMaxSize value will be
        // MAX_TABLE_DECLARATION_SIZE, so this condition will pass.
        assertUnsignedIntLessOrEqual(declaredMinSize, memory.declaredMinSize(), Failure.INCOMPATIBLE_IMPORT_TYPE);
        assertUnsignedIntGreaterOrEqual(declaredMaxSize, memory.declaredMaxSize(), Failure.INCOMPATIBLE_IMPORT_TYPE);
        instance.setMemory(memory);
    };
    resolutionDag.resolveLater(new ImportMemorySym(instance.name(), importDescriptor), new Sym[] { new ExportMemorySym(importedModuleName, importedMemoryName) }, resolveAction);
}
Also used : ImportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym) ArrayList(java.util.ArrayList) List(java.util.List) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ExportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym)

Example 12 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class WasmJsApiSuite method testInstantiateWithExportMemory.

@Test
public void testInstantiateWithExportMemory() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        WasmInstance instance = moduleInstantiate(wasm, binaryWithMemoryExport, null);
        try {
            final WasmMemory memory = (WasmMemory) WebAssembly.instanceExport(instance, "memory");
            final Object readZero = WebAssembly.instanceExport(instance, "readZero");
            memory.store_i32(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) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) WasmMemory(org.graalvm.wasm.memory.WasmMemory) Test(org.junit.Test)

Example 13 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class SymbolTable method allocateExternalMemory.

public void allocateExternalMemory(WasmMemory externalMemory) {
    checkNotParsed();
    validateSingleMemory();
    memory = new MemoryInfo(externalMemory.declaredMinSize(), externalMemory.declaredMaxSize());
    module().addLinkAction((context, instance) -> {
        final int memoryIndex = context.memories().registerExternal(externalMemory);
        final WasmMemory allocatedMemory = context.memories().memory(memoryIndex);
        instance.setMemory(allocatedMemory);
    });
}
Also used : UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory)

Example 14 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class SymbolTable method allocateMemory.

public void allocateMemory(int declaredMinSize, int declaredMaxSize) {
    checkNotParsed();
    validateSingleMemory();
    memory = new MemoryInfo(declaredMinSize, declaredMaxSize);
    module().addLinkAction((context, instance) -> {
        final int maxAllowedSize = minUnsigned(declaredMaxSize, module().limits().memoryInstanceSizeLimit());
        module().limits().checkMemoryInstanceSize(declaredMinSize);
        final WasmMemory wasmMemory;
        if (context.environment().getOptions().get(WasmOptions.UseUnsafeMemory)) {
            wasmMemory = new UnsafeWasmMemory(declaredMinSize, declaredMaxSize, maxAllowedSize);
        } else {
            wasmMemory = new ByteArrayWasmMemory(declaredMinSize, declaredMaxSize, maxAllowedSize);
        }
        final int memoryIndex = context.memories().register(wasmMemory);
        final WasmMemory allocatedMemory = context.memories().memory(memoryIndex);
        instance.setMemory(allocatedMemory);
    });
}
Also used : UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory)

Example 15 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class WasmJsApiSuite method testMemoryEmbedderData.

@Test
public void testMemoryEmbedderData() throws IOException {
    runTest(context -> {
        WasmMemory memory = WebAssembly.memAlloc(1, 1);
        checkEmbedderData(memory);
    });
}
Also used : WasmMemory(org.graalvm.wasm.memory.WasmMemory) Test(org.junit.Test)

Aggregations

WasmMemory (org.graalvm.wasm.memory.WasmMemory)23 ByteArrayWasmMemory (org.graalvm.wasm.memory.ByteArrayWasmMemory)6 UnsafeWasmMemory (org.graalvm.wasm.memory.UnsafeWasmMemory)6 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)4 Test (org.junit.Test)4 WasmFunction (org.graalvm.wasm.WasmFunction)3 WasmInstance (org.graalvm.wasm.WasmInstance)3 WasmTable (org.graalvm.wasm.WasmTable)3 InteropException (com.oracle.truffle.api.interop.InteropException)2 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 GlobalRegistry (org.graalvm.wasm.GlobalRegistry)2 ExportMemorySym (org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym)2 ImportMemorySym (org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym)2 SymbolTable (org.graalvm.wasm.SymbolTable)2 WasmContext (org.graalvm.wasm.WasmContext)2 WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)2