Search in sources :

Example 1 with InvalidArrayIndexException

use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.

the class WebAssembly method readModuleImports.

private static HashMap<String, ImportModule> readModuleImports(WasmModule module, Object importObject) {
    CompilerAsserts.neverPartOfCompilation();
    final Sequence<ModuleImportDescriptor> imports = moduleImports(module);
    if (imports.getArraySize() != 0 && importObject == null) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Module requires imports, but import object is undefined.");
    }
    HashMap<String, ImportModule> importModules = new HashMap<>();
    final InteropLibrary lib = InteropLibrary.getUncached();
    try {
        int i = 0;
        while (i < imports.getArraySize()) {
            final ModuleImportDescriptor d = (ModuleImportDescriptor) imports.readArrayElement(i);
            final Object importedModule = getMember(importObject, d.module());
            final Object member = getMember(importedModule, d.name());
            switch(d.kind()) {
                case function:
                    if (!lib.isExecutable(member)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not callable.");
                    }
                    WasmFunction f = module.importedFunction(d.name());
                    ensureImportModule(importModules, d.module()).addFunction(d.name(), Pair.create(f, member));
                    break;
                case memory:
                    if (!(member instanceof WasmMemory)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid memory.");
                    }
                    ensureImportModule(importModules, d.module()).addMemory(d.name(), (WasmMemory) member);
                    break;
                case table:
                    if (!(member instanceof WasmTable)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid table.");
                    }
                    ensureImportModule(importModules, d.module()).addTable(d.name(), (WasmTable) member);
                    break;
                case global:
                    if (!(member instanceof WasmGlobal)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid global.");
                    }
                    ensureImportModule(importModules, d.module()).addGlobal(d.name(), (WasmGlobal) member);
                    break;
                default:
                    throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unimplemented case: " + d.kind());
            }
            i += 1;
        }
    } catch (InvalidArrayIndexException | UnknownIdentifierException | ClassCastException | UnsupportedMessageException e) {
        throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unexpected state.");
    }
    return importModules;
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) HashMap(java.util.HashMap) WasmTable(org.graalvm.wasm.WasmTable) WasmFunction(org.graalvm.wasm.WasmFunction) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal) DefaultWasmGlobal(org.graalvm.wasm.globals.DefaultWasmGlobal) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory)

Example 2 with InvalidArrayIndexException

use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.

the class HoverRequestHandler method tryFrameScope.

private Hover tryFrameScope(MaterializedFrame frame, CoverageEventNode node, String textAtHoverPosition, LanguageInfo langInfo, SourceSection hoverSection) {
    Node instrumentedNode = node.getInstrumentedNode();
    NodeLibrary nodeLibrary = NodeLibrary.getUncached(instrumentedNode);
    if (nodeLibrary.hasScope(instrumentedNode, frame)) {
        try {
            Object scope = nodeLibrary.getScope(instrumentedNode, frame, true);
            Object keys = INTEROP.getMembers(scope);
            long size = INTEROP.getArraySize(keys);
            for (long i = 0; i < size; i++) {
                String key = INTEROP.asString(INTEROP.readArrayElement(keys, i));
                if (key.equals(textAtHoverPosition)) {
                    Object var = INTEROP.readMember(scope, key);
                    return Hover.create(createDefaultHoverInfos(textAtHoverPosition, var, langInfo)).setRange(SourceUtils.sourceSectionToRange(hoverSection));
                }
            }
        } catch (UnsupportedMessageException | UnknownIdentifierException | InvalidArrayIndexException e) {
        }
    }
    return null;
}
Also used : NodeLibrary(com.oracle.truffle.api.interop.NodeLibrary) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) ExecutableNode(com.oracle.truffle.api.nodes.ExecutableNode) Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 3 with InvalidArrayIndexException

use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.

the class HostProxy method getMembers.

@ExportMessage
@TruffleBoundary
Object getMembers(@SuppressWarnings("unused") boolean includeInternal, @CachedLibrary("this") InteropLibrary library, @Shared("cache") @Cached(value = "this.context.getGuestToHostCache()", allowUncached = true) GuestToHostCodeCache cache) throws UnsupportedMessageException {
    if (proxy instanceof ProxyObject) {
        Object result = guestToHostCall(library, cache.memberKeys, context, proxy);
        if (result == null) {
            result = EMPTY;
        }
        Object guestValue = context.toGuestValue(library, result);
        InteropLibrary interop = InteropLibrary.getFactory().getUncached();
        if (!interop.hasArrayElements(guestValue)) {
            if (guestValue instanceof HostObject) {
                HostObject hostObject = (HostObject) guestValue;
                if (hostObject.obj.getClass().isArray() && !hostObject.getHostClassCache().isArrayAccess()) {
                    throw illegalProxy(context, "getMemberKeys() returned a Java array %s, but allowArrayAccess in HostAccess is false.", context.asValue(library, guestValue).toString());
                } else if (hostObject.obj instanceof List && !hostObject.getHostClassCache().isListAccess()) {
                    throw illegalProxy(context, "getMemberKeys() returned a Java List %s, but allowListAccess in HostAccess is false.", context.asValue(library, guestValue).toString());
                }
            }
            throw illegalProxy(context, "getMemberKeys() returned invalid value %s but must return an array of member key Strings.", context.asValue(library, guestValue).toString());
        }
        // Todo: Use interop to determine an array element type when the GR-5737 is resolved.
        for (int i = 0; i < interop.getArraySize(guestValue); i++) {
            try {
                Object element = interop.readArrayElement(guestValue, i);
                if (!interop.isString(element)) {
                    throw illegalProxy(context, "getMemberKeys() returned invalid value %s but must return an array of member key Strings.", context.asValue(library, guestValue).toString());
                }
            } catch (UnsupportedOperationException e) {
                CompilerDirectives.shouldNotReachHere(e);
            } catch (InvalidArrayIndexException e) {
                continue;
            }
        }
        return guestValue;
    } else {
        throw UnsupportedMessageException.create();
    }
}
Also used : ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyNativeObject(org.graalvm.polyglot.proxy.ProxyNativeObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) List(java.util.List) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 4 with InvalidArrayIndexException

use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.

the class WasmJsApiSuite method testCompile.

@Test
public void testCompile() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmModule module = wasm.moduleDecode(binaryWithExports);
        try {
            HashMap<String, ModuleExportDescriptor> exports = new HashMap<>();
            int i = 0;
            while (i < WebAssembly.moduleExports(module).getArraySize()) {
                final ModuleExportDescriptor d = (ModuleExportDescriptor) WebAssembly.moduleExports(module).readArrayElement(i);
                exports.put(d.name(), d);
                i++;
            }
            Assert.assertEquals("Should export main.", ImportExportKind.function, exports.get("main").kind());
            Assert.assertEquals("Should export memory.", ImportExportKind.memory, exports.get("memory").kind());
            Assert.assertEquals("Should export global __heap_base.", ImportExportKind.global, exports.get("__heap_base").kind());
            Assert.assertEquals("Should export global __data_end.", ImportExportKind.global, exports.get("__data_end").kind());
            Assert.assertEquals("Should have empty imports.", 0L, WebAssembly.moduleImports(module).getArraySize());
        } catch (InvalidArrayIndexException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmModule(org.graalvm.wasm.WasmModule) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) HashMap(java.util.HashMap) WebAssembly(org.graalvm.wasm.api.WebAssembly) ModuleExportDescriptor(org.graalvm.wasm.api.ModuleExportDescriptor) Test(org.junit.Test)

Example 5 with InvalidArrayIndexException

use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.

the class WasmJsApiSuite method testExportMemoryTwice.

@Test
public void testExportMemoryTwice() throws IOException, InterruptedException {
    final byte[] exportMemoryTwice = compileWat("exportMemoryTwice", "(memory 1) (export \"a\" (memory 0)) (export \"b\" (memory 0))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, exportMemoryTwice, null);
        try {
            final InteropLibrary lib = InteropLibrary.getUncached();
            final Object memoryABuffer = WebAssembly.instanceExport(instance, "a");
            final Object memoryBBuffer = WebAssembly.instanceExport(instance, "b");
            lib.writeArrayElement(memoryABuffer, 0, (byte) 42);
            final byte readValue = lib.asByte(lib.readArrayElement(memoryBBuffer, 0));
            Assert.assertEquals("Written value should correspond to read value", (byte) 42, readValue);
        } catch (UnsupportedMessageException | UnsupportedTypeException | InvalidArrayIndexException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Aggregations

InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)17 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)12 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)9 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)8 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)8 Test (org.junit.Test)4 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)3 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)3 WebAssembly (org.graalvm.wasm.api.WebAssembly)3 JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 WasmModule (org.graalvm.wasm.WasmModule)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)1 StandardTags (com.oracle.truffle.api.instrumentation.StandardTags)1 Tag (com.oracle.truffle.api.instrumentation.Tag)1 NodeLibrary (com.oracle.truffle.api.interop.NodeLibrary)1 StopIterationException (com.oracle.truffle.api.interop.StopIterationException)1 ExportMessage (com.oracle.truffle.api.library.ExportMessage)1 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)1