Search in sources :

Example 6 with ByteSequence

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);
    }
}
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 7 with ByteSequence

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"));
    }
}
Also used : WasmContext(org.graalvm.wasm.WasmContext) 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 8 with ByteSequence

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());
}
Also used : ByteSequence(org.graalvm.polyglot.io.ByteSequence) Test(org.junit.Test)

Example 9 with ByteSequence

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;
    }
}
Also used : BitStream(com.oracle.truffle.llvm.parser.scanner.BitStream) PEFile(com.oracle.truffle.llvm.parser.coff.PEFile) ElfFile(com.oracle.truffle.llvm.parser.elf.ElfFile) ElfDynamicSection(com.oracle.truffle.llvm.parser.elf.ElfDynamicSection) Magic(com.oracle.truffle.llvm.runtime.Magic) CoffFile(com.oracle.truffle.llvm.parser.coff.CoffFile) Xar(com.oracle.truffle.llvm.parser.macho.Xar) ImageSectionHeader(com.oracle.truffle.llvm.parser.coff.CoffFile.ImageSectionHeader) MachOLibraryLocator(com.oracle.truffle.llvm.parser.macho.MachOLibraryLocator) MachOFile(com.oracle.truffle.llvm.parser.macho.MachOFile) ElfLibraryLocator(com.oracle.truffle.llvm.parser.elf.ElfLibraryLocator) ElfSectionHeaderTable(com.oracle.truffle.llvm.parser.elf.ElfSectionHeaderTable) ByteSequence(org.graalvm.polyglot.io.ByteSequence)

Example 10 with ByteSequence

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;
}
Also used : ObjectFileReader(com.oracle.truffle.llvm.parser.filereader.ObjectFileReader) ByteSequence(org.graalvm.polyglot.io.ByteSequence)

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