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);
}
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, () -> {
});
}
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);
}
Aggregations