use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class WasmLateLinkingSuite method linkingFailureOnlyAux.
@Test
public void linkingFailureOnlyAux() throws IOException, InterruptedException {
final Context context = Context.newBuilder(WasmLanguage.ID).build();
final ByteSequence binaryAux = ByteSequence.create(compileWat("file1", "(import \"non_existing\" \"f\" (func))"));
final ByteSequence binaryMain = ByteSequence.create(compileWat("file2", "(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();
final Value module2Instance = context.eval(sourceMain);
context.eval(sourceAux);
// does not exist."
try {
final Value g = module2Instance.getMember("g");
g.execute();
Assert.assertFalse("Should not reach here.", true);
} catch (Throwable e) {
// We can later refine these semantics, and avoid a failure here.
// To do this, we should only lazily link exactly the required modules, instead of
// linking all of them.
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 'module1', does not exist"));
}
try {
final Value g2 = module2Instance.getMember("g");
final Value result2 = g2.execute();
Assert.assertEquals(42, result2.asInt());
} catch (Throwable e) {
Assert.assertFalse("Should not reach here, got: " + e.getMessage(), true);
}
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class WasmPolyglotTestSuite method extractKeys.
@Test
public void extractKeys() throws IOException {
ByteSequence test = ByteSequence.create(binaryReturnConst);
Source source = Source.newBuilder(WasmLanguage.ID, test, "main").build();
try (Context context = Context.create(WasmLanguage.ID)) {
context.eval(source);
Value instance = context.getBindings(WasmLanguage.ID).getMember("main");
Set<String> keys = instance.getMemberKeys();
Assert.assertTrue("Should contain function 'main'", keys.contains("main"));
Assert.assertTrue("Should contain memory 'memory'", keys.contains("memory"));
Assert.assertTrue("Should contain global '__heap_base'", keys.contains("__heap_base"));
Assert.assertTrue("Should contain global '__data_end'", keys.contains("__data_end"));
}
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class ByteSequenceTest method testByteArraySubSequence.
@Test
public void testByteArraySubSequence() {
ByteSequence sequence = ByteSequence.create(new byte[] { 1, 2, 3, 4 });
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, sequence.toByteArray());
ByteSequence subSequence = sequence.subSequence(1, 4);
assertArrayEquals(new byte[] { 2, 3, 4 }, subSequence.toByteArray());
ByteSequence subSubSequence = subSequence.subSequence(1, 3);
assertArrayEquals(new byte[] { 3, 4 }, subSubSequence.toByteArray());
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class BinaryParser method parseBitcode.
private ByteSequence parseBitcode(ByteSequence bytes, Source source) {
BitStream b = BitStream.create(bytes);
Magic magicWord = getMagic(b);
if (source != null) {
libraryName = source.getName();
}
switch(magicWord) {
case BC_MAGIC_WORD:
return bytes;
case WRAPPER_MAGIC_WORD:
// 0: magic word
// 32: version
// 64: offset32
long offset = b.read(64, Integer.SIZE);
// 96: size32
long size = b.read(96, Integer.SIZE);
return bytes.subSequence((int) offset, (int) (offset + size));
case ELF_MAGIC_WORD:
ElfFile elfFile = ElfFile.create(bytes);
ElfSectionHeaderTable.Entry llvmbc = elfFile.getSectionHeaderTable().getEntry(".llvmbc");
if (llvmbc == null) {
// ELF File does not contain an .llvmbc section
return null;
}
ElfDynamicSection dynamicSection = elfFile.getDynamicSection();
if (dynamicSection != null) {
List<String> elfLibraries = dynamicSection.getDTNeeded();
libraries.addAll(elfLibraries);
String soName = dynamicSection.getDTSOName();
if (soName != null) {
libraryName = soName;
}
locator = new ElfLibraryLocator(elfFile, source);
}
long elfOffset = llvmbc.getOffset();
long elfSize = llvmbc.getSize();
return bytes.subSequence((int) elfOffset, (int) (elfOffset + elfSize));
case MH_MAGIC:
case MH_CIGAM:
case MH_MAGIC_64:
case MH_CIGAM_64:
MachOFile machOFile = MachOFile.create(bytes);
String origin = getOrigin(source);
List<String> machoLibraries = machOFile.getDyLibs(origin);
locator = new MachOLibraryLocator(machOFile, source);
libraries.addAll(machoLibraries);
ByteSequence machoBitcode = machOFile.extractBitcode();
if (machoBitcode == null) {
return null;
}
return parseBitcode(machoBitcode, source);
case XAR_MAGIC:
Xar xarFile = Xar.create(bytes);
ByteSequence xarBitcode = xarFile.extractBitcode();
if (xarBitcode == null) {
return null;
}
return parseBitcode(xarBitcode, source);
case COFF_INTEL_AMD64:
CoffFile coffFile = CoffFile.create(bytes);
ImageSectionHeader coffBcSection = coffFile.getSection(".llvmbc");
if (coffBcSection == null) {
// COFF File does not contain an .llvmbc section
return null;
}
return parseBitcode(coffBcSection.getData(), source);
case MS_DOS:
PEFile peFile = PEFile.create(bytes);
ImageSectionHeader peBcSection = peFile.getCoffFile().getSection(".llvmbc");
if (peBcSection == null) {
// PE/COFF File does not contain an .llvmbc section
return null;
}
return parseBitcode(peBcSection.getData(), source);
default:
return null;
}
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class CoffFile method getReaderAtVirtualAddress.
public ObjectFileReader getReaderAtVirtualAddress(int virtualAddress) {
ImageSectionHeader section = lookupOffset(virtualAddress);
ByteSequence sectionBytes = bytes.subSequence(section.pointerToRawData, section.pointerToRawData + section.sizeOfRawData);
ObjectFileReader reader = new ObjectFileReader(sectionBytes, true);
reader.setPosition(virtualAddress - section.virtualAddress);
return reader;
}
Aggregations