Search in sources :

Example 1 with ImportMemorySym

use of org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym in project graal by oracle.

the class Linker method resolveDataSegment.

void resolveDataSegment(WasmContext context, WasmInstance instance, int dataSegmentId, int offsetAddress, int offsetGlobalIndex, int byteLength, byte[] data) {
    assertTrue(instance.symbolTable().memoryExists(), String.format("No memory declared or imported in the module '%s'", instance.name()), Failure.UNSPECIFIED_MALFORMED);
    final Runnable resolveAction = () -> {
        WasmMemory memory = instance.memory();
        Assert.assertNotNull(memory, String.format("No memory declared or imported in the module '%s'", instance.name()), Failure.UNSPECIFIED_MALFORMED);
        int baseAddress;
        if (offsetGlobalIndex != -1) {
            final int offsetGlobalAddress = instance.globalAddress(offsetGlobalIndex);
            assertTrue(offsetGlobalAddress != -1, "The global variable '" + offsetGlobalIndex + "' for the offset of the data segment " + dataSegmentId + " in module '" + instance.name() + "' was not initialized.", Failure.UNSPECIFIED_MALFORMED);
            baseAddress = context.globals().loadAsInt(offsetGlobalAddress);
        } else {
            baseAddress = offsetAddress;
        }
        Assert.assertUnsignedIntLessOrEqual(baseAddress, WasmMath.toUnsignedIntExact(memory.byteSize()), Failure.DATA_SEGMENT_DOES_NOT_FIT);
        Assert.assertUnsignedIntLessOrEqual(baseAddress + byteLength, WasmMath.toUnsignedIntExact(memory.byteSize()), Failure.DATA_SEGMENT_DOES_NOT_FIT);
        for (int writeOffset = 0; writeOffset != byteLength; ++writeOffset) {
            byte b = data[writeOffset];
            memory.store_i32_8(null, baseAddress + writeOffset, b);
        }
    };
    final ArrayList<Sym> dependencies = new ArrayList<>();
    if (instance.symbolTable().importedMemory() != null) {
        dependencies.add(new ImportMemorySym(instance.name(), instance.symbolTable().importedMemory()));
    }
    if (dataSegmentId > 0) {
        dependencies.add(new DataSym(instance.name(), dataSegmentId - 1));
    }
    if (offsetGlobalIndex != -1) {
        dependencies.add(new InitializeGlobalSym(instance.name(), offsetGlobalIndex));
    }
    resolutionDag.resolveLater(new DataSym(instance.name(), dataSegmentId), dependencies.toArray(new Sym[dependencies.size()]), resolveAction);
}
Also used : DataSym(org.graalvm.wasm.Linker.ResolutionDag.DataSym) ImportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym) ElemSym(org.graalvm.wasm.Linker.ResolutionDag.ElemSym) ExportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym) ExportTableSym(org.graalvm.wasm.Linker.ResolutionDag.ExportTableSym) InitializeGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym) ExportFunctionSym(org.graalvm.wasm.Linker.ResolutionDag.ExportFunctionSym) CodeEntrySym(org.graalvm.wasm.Linker.ResolutionDag.CodeEntrySym) CallsiteSym(org.graalvm.wasm.Linker.ResolutionDag.CallsiteSym) ImportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym) ExportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ExportGlobalSym) ImportFunctionSym(org.graalvm.wasm.Linker.ResolutionDag.ImportFunctionSym) Sym(org.graalvm.wasm.Linker.ResolutionDag.Sym) ImportTableSym(org.graalvm.wasm.Linker.ResolutionDag.ImportTableSym) ImportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ImportGlobalSym) DataSym(org.graalvm.wasm.Linker.ResolutionDag.DataSym) ArrayList(java.util.ArrayList) InitializeGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym) WasmMemory(org.graalvm.wasm.memory.WasmMemory)

Example 2 with ImportMemorySym

use of org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym in project graal by oracle.

the class Linker method resolveMemoryExport.

void resolveMemoryExport(WasmInstance instance, String exportedMemoryName) {
    WasmModule module = instance.module();
    final ImportDescriptor importDescriptor = module.symbolTable().importedMemory();
    final Sym[] dependencies = importDescriptor != null ? new Sym[] { new ImportMemorySym(module.name(), importDescriptor) } : ResolutionDag.NO_DEPENDENCIES;
    resolutionDag.resolveLater(new ExportMemorySym(module.name(), exportedMemoryName), dependencies, () -> {
    });
}
Also used : ImportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym) ElemSym(org.graalvm.wasm.Linker.ResolutionDag.ElemSym) ExportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym) ExportTableSym(org.graalvm.wasm.Linker.ResolutionDag.ExportTableSym) InitializeGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym) ExportFunctionSym(org.graalvm.wasm.Linker.ResolutionDag.ExportFunctionSym) CodeEntrySym(org.graalvm.wasm.Linker.ResolutionDag.CodeEntrySym) CallsiteSym(org.graalvm.wasm.Linker.ResolutionDag.CallsiteSym) ImportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym) ExportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ExportGlobalSym) ImportFunctionSym(org.graalvm.wasm.Linker.ResolutionDag.ImportFunctionSym) Sym(org.graalvm.wasm.Linker.ResolutionDag.Sym) ImportTableSym(org.graalvm.wasm.Linker.ResolutionDag.ImportTableSym) ImportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ImportGlobalSym) DataSym(org.graalvm.wasm.Linker.ResolutionDag.DataSym) ExportMemorySym(org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym)

Example 3 with ImportMemorySym

use of org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym 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)

Aggregations

ExportMemorySym (org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym)3 ImportMemorySym (org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym)3 ArrayList (java.util.ArrayList)2 CallsiteSym (org.graalvm.wasm.Linker.ResolutionDag.CallsiteSym)2 CodeEntrySym (org.graalvm.wasm.Linker.ResolutionDag.CodeEntrySym)2 DataSym (org.graalvm.wasm.Linker.ResolutionDag.DataSym)2 ElemSym (org.graalvm.wasm.Linker.ResolutionDag.ElemSym)2 ExportFunctionSym (org.graalvm.wasm.Linker.ResolutionDag.ExportFunctionSym)2 ExportGlobalSym (org.graalvm.wasm.Linker.ResolutionDag.ExportGlobalSym)2 ExportTableSym (org.graalvm.wasm.Linker.ResolutionDag.ExportTableSym)2 ImportFunctionSym (org.graalvm.wasm.Linker.ResolutionDag.ImportFunctionSym)2 ImportGlobalSym (org.graalvm.wasm.Linker.ResolutionDag.ImportGlobalSym)2 ImportTableSym (org.graalvm.wasm.Linker.ResolutionDag.ImportTableSym)2 InitializeGlobalSym (org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym)2 Sym (org.graalvm.wasm.Linker.ResolutionDag.Sym)2 WasmMemory (org.graalvm.wasm.memory.WasmMemory)2 List (java.util.List)1