Search in sources :

Example 16 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class ExtractBitcode method main.

public static void main(String[] args) {
    if (args.length != 2) {
        usage(System.err);
        System.exit(1);
    }
    try {
        String inName = args[0];
        String outName = args[1];
        InputStream in = inName.equals("-") ? System.in : new FileInputStream(inName);
        OutputStream out = outName.equals("-") ? System.out : new FileOutputStream(outName);
        ByteSequence bytes = readFully(in);
        BinaryParserResult result = BinaryParser.parse(bytes, null, null);
        if (result == null) {
            throw new IOException("No bitcode found in file '" + inName + "'");
        }
        out.write(result.getBitcode().toByteArray());
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(2);
    }
}
Also used : BinaryParserResult(com.oracle.truffle.llvm.parser.binary.BinaryParserResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ByteSequence(org.graalvm.polyglot.io.ByteSequence)

Example 17 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class SourceAPITest method testBuildBinarySources.

@Test
public void testBuildBinarySources() throws IOException {
    ByteSequence bytes = ByteSequence.create(new byte[8]);
    Source source = Source.newBuilder("", bytes, null).build();
    assertTrue(source.hasBytes());
    assertFalse(source.hasCharacters());
    source = Source.newBuilder("", "", null).content(bytes).build();
    assertTrue(source.hasBytes());
    assertFalse(source.hasCharacters());
    source = Source.newBuilder("", bytes, null).content("").build();
    assertFalse(source.hasBytes());
    assertTrue(source.hasCharacters());
    File file = File.createTempFile("Hello", ".bin").getCanonicalFile();
    file.deleteOnExit();
    // mime-type not specified + invalid langauge -> characters
    source = Source.newBuilder("", file).build();
    assertFalse(source.hasBytes());
    assertTrue(source.hasCharacters());
    // mime-type not specified + invalid langauge -> characters
    source = Source.newBuilder("", file).content(bytes).build();
    assertTrue(source.hasBytes());
    assertFalse(source.hasCharacters());
    source = Source.newBuilder("", file).content("").build();
    assertFalse(source.hasBytes());
    assertTrue(source.hasCharacters());
}
Also used : File(java.io.File) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 18 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class WasmLateLinkingSuite method lazyLinkEquivalenceClasses.

@Test
public void lazyLinkEquivalenceClasses() throws IOException, InterruptedException {
    // Exports table with a function
    final byte[] exportBytes = compileWat("exportTable", "(module" + "(func $f0 (result i32) i32.const 42)" + "(table 1 1 funcref)" + "(export \"table\" (table 0))" + "(elem (i32.const 0) $f0)" + ")");
    // Imports table and exports function that invokes functions from the table
    final byte[] importBytes = compileWat("importTable", "(module" + "(type (func (param i32) (result i32)))" + "(type (func (result i32)))" + "(import \"main\" \"table\" (table 1 1 funcref))" + "(func (type 0) (param i32) (result i32) local.get 0 call_indirect (type 1))" + "(export \"testFunc\" (func 0))" + ")");
    final Context context = Context.newBuilder(WasmLanguage.ID).build();
    final ByteSequence exportByteSeq = ByteSequence.create(exportBytes);
    final ByteSequence importByteSeq = ByteSequence.create(importBytes);
    final Source exportSource = Source.newBuilder(WasmLanguage.ID, exportByteSeq, "exportModule").build();
    final Source importSource = Source.newBuilder(WasmLanguage.ID, importByteSeq, "importModule").build();
    final Value exportModuleInstance = context.eval(exportSource);
    exportModuleInstance.getMember("table");
    // Linking of the first module was triggered by this point.
    final Value importModuleInstance = context.eval(importSource);
    importModuleInstance.getMember("testFunc").execute(0);
// Linking of the second module was triggered by this point.
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 19 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class WasmLateLinkingSuite method linkingFailureDueToDependency.

@Test
public void linkingFailureDueToDependency() throws IOException, InterruptedException {
    final Context context = Context.newBuilder(WasmLanguage.ID).build();
    final ByteSequence binaryAux = ByteSequence.create(compileWat("file1", "(import \"non_existing\" \"f\" (func)) (func (export \"h\") (result i32) (i32.const 42))"));
    final ByteSequence binaryMain = ByteSequence.create(compileWat("file2", "(import \"main\" \"h\" (func)) (func (export \"g\") (result i32) (i32.const 42))"));
    final Source sourceAux = Source.newBuilder(WasmLanguage.ID, binaryAux, "module1").build();
    final Source sourceMain = Source.newBuilder(WasmLanguage.ID, binaryMain, "module2").build();
    context.eval(sourceAux);
    final Value module2Instance = context.eval(sourceMain);
    try {
        final Value g = module2Instance.getMember("g");
        g.execute();
        Assert.assertFalse("Should not reach here.", true);
    } catch (Throwable e) {
        Assert.assertTrue("Should fail due to unresolved import in the other module, got: " + e.getMessage(), e.getMessage().contains("module 'non_existing', referenced by the import 'f' in the module 'main', does not exist"));
    }
    try {
        final Value g2 = module2Instance.getMember("g");
        g2.execute();
        Assert.assertFalse("Should not reach here.", true);
    } catch (Throwable e) {
        Assert.assertTrue("Should fail due to both modules being in a failed linking state, got: " + e.getMessage(), e.getMessage().contains("Linking of module wasm-module(module2) previously failed"));
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 20 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class WasmPolyglotTestSuite method divisionByZeroStressTest.

@Test
public void divisionByZeroStressTest() throws IOException, InterruptedException {
    String divisionByZeroWAT = "(module (func (export \"main\") (result i32) i32.const 1 i32.const 0 i32.div_s))";
    ByteSequence test = ByteSequence.create(compileWat("test", divisionByZeroWAT));
    Source source = Source.newBuilder(WasmLanguage.ID, test, "main").build();
    try (Context context = Context.create(WasmLanguage.ID)) {
        context.eval(source);
        Value mainFunction = context.getBindings(WasmLanguage.ID).getMember("main").getMember("main");
        for (int iteration = 0; iteration < 20000; iteration++) {
            try {
                mainFunction.execute();
                Assert.fail("Should have thrown");
            } catch (PolyglotException pex) {
                Assert.assertTrue("Should not throw internal error", !pex.isInternalError());
            }
        }
    }
}
Also used : WasmContext(org.graalvm.wasm.WasmContext) Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Aggregations

ByteSequence (org.graalvm.polyglot.io.ByteSequence)26 Test (org.junit.Test)20 Source (org.graalvm.polyglot.Source)10 Context (org.graalvm.polyglot.Context)8 Value (org.graalvm.polyglot.Value)8 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)5 Source (com.oracle.truffle.api.source.Source)4 File (java.io.File)3 WasmContext (org.graalvm.wasm.WasmContext)3 TruffleFile (com.oracle.truffle.api.TruffleFile)2 SourceSection (com.oracle.truffle.api.source.SourceSection)2 ObjectFileReader (com.oracle.truffle.llvm.parser.filereader.ObjectFileReader)2 TruffleTestAssumptions (com.oracle.truffle.tck.tests.TruffleTestAssumptions)2 IOException (java.io.IOException)2 Assert.assertArrayEquals (org.junit.Assert.assertArrayEquals)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Assert.fail (org.junit.Assert.fail)2