Search in sources :

Example 1 with InitializeGlobalSym

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

the class Linker method resolveGlobalImport.

void resolveGlobalImport(WasmContext context, WasmInstance instance, ImportDescriptor importDescriptor, int globalIndex, byte valueType, byte mutability) {
    final String importedGlobalName = importDescriptor.memberName;
    final String importedModuleName = importDescriptor.moduleName;
    final Runnable resolveAction = () -> {
        final WasmInstance importedInstance = context.moduleInstances().get(importedModuleName);
        if (importedInstance == null) {
            throw WasmException.create(Failure.UNKNOWN_IMPORT, "Module '" + importedModuleName + "', referenced in the import of global variable '" + importedGlobalName + "' into module '" + instance.name() + "', does not exist.");
        }
        // Check that the imported global is resolved in the imported module.
        Integer exportedGlobalIndex = importedInstance.symbolTable().exportedGlobals().get(importedGlobalName);
        if (exportedGlobalIndex == null) {
            throw WasmException.create(Failure.UNKNOWN_IMPORT, "Global variable '" + importedGlobalName + "', imported into module '" + instance.name() + "', was not exported in the module '" + importedModuleName + "'.");
        }
        int exportedValueType = importedInstance.symbolTable().globalValueType(exportedGlobalIndex);
        if (exportedValueType != valueType) {
            throw WasmException.create(Failure.INCOMPATIBLE_IMPORT_TYPE, "Global variable '" + importedGlobalName + "' is imported into module '" + instance.name() + "' with the type " + WasmType.toString(valueType) + ", " + "'but it was exported in the module '" + importedModuleName + "' with the type " + WasmType.toString(exportedValueType) + ".");
        }
        int exportedMutability = importedInstance.symbolTable().globalMutability(exportedGlobalIndex);
        if (exportedMutability != mutability) {
            throw WasmException.create(Failure.INCOMPATIBLE_IMPORT_TYPE, "Global variable '" + importedGlobalName + "' is imported into module '" + instance.name() + "' with the modifier " + GlobalModifier.asString(mutability) + ", " + "'but it was exported in the module '" + importedModuleName + "' with the modifier " + GlobalModifier.asString(exportedMutability) + ".");
        }
        int address = importedInstance.globalAddress(exportedGlobalIndex);
        instance.setGlobalAddress(globalIndex, address);
    };
    final ImportGlobalSym importGlobalSym = new ImportGlobalSym(instance.name(), importDescriptor, globalIndex);
    final Sym[] dependencies = new Sym[] { new ExportGlobalSym(importedModuleName, importedGlobalName) };
    resolutionDag.resolveLater(importGlobalSym, dependencies, resolveAction);
    resolutionDag.resolveLater(new InitializeGlobalSym(instance.name(), globalIndex), new Sym[] { importGlobalSym }, NO_RESOLVE_ACTION);
}
Also used : ExportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ExportGlobalSym) ImportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ImportGlobalSym) 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) InitializeGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym)

Example 2 with InitializeGlobalSym

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

the class Linker method resolveElemSegment.

void resolveElemSegment(WasmContext context, WasmInstance instance, int elemSegmentId, int offsetAddress, int offsetGlobalIndex, int[] functionsIndices) {
    final Runnable resolveAction = () -> immediatelyResolveElemSegment(context, instance, elemSegmentId, offsetAddress, offsetGlobalIndex, functionsIndices);
    final ArrayList<Sym> dependencies = new ArrayList<>();
    if (instance.symbolTable().importedTable() != null) {
        dependencies.add(new ImportTableSym(instance.name(), instance.symbolTable().importedTable()));
    }
    if (elemSegmentId > 0) {
        dependencies.add(new ElemSym(instance.name(), elemSegmentId - 1));
    }
    if (offsetGlobalIndex != -1) {
        dependencies.add(new InitializeGlobalSym(instance.name(), offsetGlobalIndex));
    }
    for (final int functionIndex : functionsIndices) {
        final WasmFunction function = instance.module().function(functionIndex);
        if (function.importDescriptor() != null) {
            dependencies.add(new ImportFunctionSym(instance.name(), function.importDescriptor(), function.index()));
        }
    }
    resolutionDag.resolveLater(new ElemSym(instance.name(), elemSegmentId), dependencies.toArray(new Sym[dependencies.size()]), resolveAction);
}
Also used : ElemSym(org.graalvm.wasm.Linker.ResolutionDag.ElemSym) ImportTableSym(org.graalvm.wasm.Linker.ResolutionDag.ImportTableSym) 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) ImportFunctionSym(org.graalvm.wasm.Linker.ResolutionDag.ImportFunctionSym)

Example 3 with InitializeGlobalSym

use of org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym 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 4 with InitializeGlobalSym

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

the class Linker method resolveGlobalExport.

void resolveGlobalExport(WasmModule module, String globalName, int globalIndex) {
    final Sym[] dependencies;
    dependencies = new Sym[] { new InitializeGlobalSym(module.name(), globalIndex) };
    resolutionDag.resolveLater(new ExportGlobalSym(module.name(), globalName), dependencies, NO_RESOLVE_ACTION);
}
Also used : ExportGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.ExportGlobalSym) 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) InitializeGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym)

Example 5 with InitializeGlobalSym

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

the class Linker method resolveGlobalInitialization.

void resolveGlobalInitialization(WasmContext context, WasmInstance instance, int globalIndex, int sourceGlobalIndex) {
    final Runnable resolveAction = () -> {
        final int sourceAddress = instance.globalAddress(sourceGlobalIndex);
        final int address = instance.globalAddress(globalIndex);
        final GlobalRegistry globals = context.globals();
        globals.storeLong(address, globals.loadAsLong(sourceAddress));
    };
    final Sym[] dependencies = new Sym[] { new InitializeGlobalSym(instance.name(), sourceGlobalIndex) };
    resolutionDag.resolveLater(new InitializeGlobalSym(instance.name(), globalIndex), dependencies, resolveAction);
}
Also used : 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) InitializeGlobalSym(org.graalvm.wasm.Linker.ResolutionDag.InitializeGlobalSym)

Aggregations

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