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);
}
}
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());
}
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.
}
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"));
}
}
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());
}
}
}
}
Aggregations