Search in sources :

Example 11 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WasmJsApiSuite method testMemoryGrowLimit.

@Test
public void testMemoryGrowLimit() throws IOException {
    runTest(context -> {
        try {
            WasmMemory memory = WebAssembly.memAlloc(1, 1);
            WebAssembly.memGrow(memory, 1);
            Assert.fail("Should have failed - try to grow memory beyond max size");
        } catch (WasmJsApiException e) {
            Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
        }
    });
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WasmMemory(org.graalvm.wasm.memory.WasmMemory) Test(org.junit.Test)

Example 12 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WasmJsApiSuite method testInvalidEmbedderData.

@Test
public void testInvalidEmbedderData() throws IOException {
    runTest(context -> {
        Object notEmbedderDataHolder = new Object();
        try {
            WebAssembly.embedderDataGet(new Object[] { notEmbedderDataHolder });
            Assert.fail("embedderDataGet failed to throw");
        } catch (WasmJsApiException ex) {
            Assert.assertEquals("Type error expected", WasmJsApiException.Kind.TypeError, ex.kind());
        }
        try {
            WebAssembly.embedderDataSet(new Object[] { notEmbedderDataHolder, 42 });
            Assert.fail("embedderDataSet failed to throw");
        } catch (WasmJsApiException ex) {
            Assert.assertEquals("Type error expected", WasmJsApiException.Kind.TypeError, ex.kind());
        }
    });
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 13 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WasmJsApiSuite method testTableInstanceGrowLimit.

@Test
public void testTableInstanceGrowLimit() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        // We should be able to grow the table to 10,000,000.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object grow = wasm.readMember("table_grow");
            lib.execute(grow, table, 9999999);
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
        // But growing to 10,000,001 should fail.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object grow = wasm.readMember("table_grow");
            lib.execute(grow, table, 1);
            Assert.fail("Should have failed - export count exceeds the limit");
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        } catch (WasmJsApiException e) {
            Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) 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) ArityException(com.oracle.truffle.api.interop.ArityException) Test(org.junit.Test)

Example 14 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WasmJsApiSuite method testTableInstanceOutOfBoundsGet.

@Test
public void testTableInstanceOutOfBoundsGet() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        // We should be able to get element 1.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object readTable = wasm.readMember("table_read");
            lib.execute(readTable, table, 0);
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
        // But not element 2.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object readTable = wasm.readMember("table_read");
            lib.execute(readTable, table, 1);
            Assert.fail("Should have failed - export count exceeds the limit");
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        } catch (WasmJsApiException e) {
            Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) 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) ArityException(com.oracle.truffle.api.interop.ArityException) Test(org.junit.Test)

Example 15 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WasmJsApiSuite method testTableGrowLimit.

@Test
public void testTableGrowLimit() throws IOException {
    runTest(context -> {
        try {
            WasmTable table = WebAssembly.tableAlloc(1, 1);
            WebAssembly.tableGrow(table, 1);
            Assert.fail("Should have failed - try to grow table beyond max size");
        } catch (WasmJsApiException e) {
            Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
        }
    });
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WasmTable(org.graalvm.wasm.WasmTable) Test(org.junit.Test)

Aggregations

WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)20 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)6 WasmTable (org.graalvm.wasm.WasmTable)6 Test (org.junit.Test)6 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)4 WasmInstance (org.graalvm.wasm.WasmInstance)4 ExportedWasmGlobal (org.graalvm.wasm.globals.ExportedWasmGlobal)4 WasmMemory (org.graalvm.wasm.memory.WasmMemory)4 ArityException (com.oracle.truffle.api.interop.ArityException)3 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)3 WebAssembly (org.graalvm.wasm.api.WebAssembly)3 DefaultWasmGlobal (org.graalvm.wasm.globals.DefaultWasmGlobal)3 WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)3 ByteArrayWasmMemory (org.graalvm.wasm.memory.ByteArrayWasmMemory)3 UnsafeWasmMemory (org.graalvm.wasm.memory.UnsafeWasmMemory)3 WasmFunction (org.graalvm.wasm.WasmFunction)2 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)1