Search in sources :

Example 41 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project LuaViewSDK by alibaba.

the class ChannelTools method toBytes.

/**
     * file path to
     *
     * @param filepath
     * @param sizes
     * @return
     */
public static List<byte[]> toBytes(String filepath, int[] sizes) {
    List<byte[]> result = new ArrayList<byte[]>();
    try {
        RandomAccessFile randomAccessFile = new RandomAccessFile(filepath, "r");
        MappedByteBuffer buffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length());
        if (sizes != null && sizes.length > 0) {
            for (int size : sizes) {
                byte[] r = new byte[size];
                //fill buffer
                buffer.get(r);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 42 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfDynamicSectionScrubberStepTest method test.

@Test
public void test() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "elf_shared_lib", tmp);
    workspace.setUp();
    ElfDynamicSectionScrubberStep step = ElfDynamicSectionScrubberStep.of(new ProjectFilesystem(tmp.getRoot()), tmp.getRoot().getFileSystem().getPath("libfoo.so"));
    step.execute(TestExecutionContext.newInstance());
    // Verify that the relevant dynamic section tag have been zero'd out.
    try (FileChannel channel = FileChannel.open(step.getFilesystem().resolve(step.getPath()), StandardOpenOption.READ)) {
        MappedByteBuffer buffer = channel.map(READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        Optional<ElfSection> section = elf.getSectionByName(ElfDynamicSectionScrubberStep.SECTION).map(Pair::getSecond);
        for (ByteBuffer body = section.get().body; body.hasRemaining(); ) {
            ElfDynamicSection.DTag dTag = ElfDynamicSection.DTag.valueOf(elf.header.ei_class == ElfHeader.EIClass.ELFCLASS32 ? Elf.Elf32.getElf32Sword(body) : (int) Elf.Elf64.getElf64Sxword(body));
            long dPtr = elf.header.ei_class == ElfHeader.EIClass.ELFCLASS32 ? Elf.Elf32.getElf32Addr(body) : Elf.Elf64.getElf64Addr(body);
            if (!ElfDynamicSectionScrubberStep.WHITELISTED_TAGS.contains(dTag)) {
                assertThat(dPtr, Matchers.equalTo(0L));
            }
        }
    }
}
Also used : ElfDynamicSection(com.facebook.buck.cxx.elf.ElfDynamicSection) FileChannel(java.nio.channels.FileChannel) ElfSection(com.facebook.buck.cxx.elf.ElfSection) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Elf(com.facebook.buck.cxx.elf.Elf) Pair(com.facebook.buck.model.Pair) Test(org.junit.Test)

Example 43 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfSymbolTableScrubberStepTest method test.

@Test
public void test() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "elf_shared_lib", tmp);
    workspace.setUp();
    ElfSymbolTableScrubberStep step = ElfSymbolTableScrubberStep.of(new ProjectFilesystem(tmp.getRoot()), tmp.getRoot().getFileSystem().getPath("libfoo.so"), ".dynsym", /* allowMissing */
    false);
    step.execute(TestExecutionContext.newInstance());
    // Verify that the symbol table values and sizes are zero.
    try (FileChannel channel = FileChannel.open(step.getFilesystem().resolve(step.getPath()), StandardOpenOption.READ)) {
        MappedByteBuffer buffer = channel.map(READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        ElfSection section = elf.getSectionByName(SECTION).orElseThrow(AssertionError::new).getSecond();
        ElfSymbolTable table = ElfSymbolTable.parse(elf.header.ei_class, section.body);
        Set<Long> addresses = new HashSet<>();
        table.entries.forEach(entry -> {
            assertTrue(entry.st_value == 0 || addresses.add((entry.st_value)));
            assertThat(entry.st_shndx, Matchers.equalTo(entry.st_shndx != 0 ? ElfSymbolTableScrubberStep.STABLE_SECTION : entry.st_shndx));
            assertThat(entry.st_size, Matchers.equalTo(entry.st_info.st_type == ElfSymbolTable.Entry.Info.Type.STT_FUNC ? 0 : entry.st_size));
        });
    }
}
Also used : ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ElfSymbolTable(com.facebook.buck.cxx.elf.ElfSymbolTable) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ElfSection(com.facebook.buck.cxx.elf.ElfSection) Elf(com.facebook.buck.cxx.elf.Elf) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 44 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfVerDefTest method test.

@Test
public void test() throws IOException {
    try (FileChannel channel = FileChannel.open(workspace.resolve("libfoo.so"))) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        ElfSection stringTable = elf.getSectionByName(".dynstr").get().getSecond();
        ElfVerDef verDef = ElfVerDef.parse(elf.header.ei_class, elf.getSectionByName(".gnu.version_d").get().getSecond().body);
        assertThat(verDef.entries, Matchers.hasSize(2));
        assertThat(stringTable.lookupString(verDef.entries.get(0).getSecond().get(0).vda_name), Matchers.equalTo("libfoo.so"));
        assertThat(stringTable.lookupString(verDef.entries.get(1).getSecond().get(0).vda_name), Matchers.equalTo("VERS_1.0"));
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 45 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfVerNeedTest method test.

@Test
public void test() throws IOException {
    try (FileChannel channel = FileChannel.open(workspace.resolve("libfoo.so"))) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        ElfSection stringTable = elf.getSectionByName(".dynstr").get().getSecond();
        ElfVerNeed verNeed = ElfVerNeed.parse(elf.header.ei_class, elf.getSectionByName(".gnu.version_r").get().getSecond().body);
        assertThat(verNeed.entries, Matchers.hasSize(1));
        assertThat(stringTable.lookupString(verNeed.entries.get(0).getFirst().vn_file), Matchers.equalTo("libc.so.6"));
        assertThat(verNeed.entries.get(0).getSecond(), Matchers.hasSize(1));
        assertThat(stringTable.lookupString(verNeed.entries.get(0).getSecond().get(0).vna_name), Matchers.equalTo("GLIBC_2.2.5"));
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Aggregations

MappedByteBuffer (java.nio.MappedByteBuffer)154 FileChannel (java.nio.channels.FileChannel)75 IOException (java.io.IOException)44 File (java.io.File)38 RandomAccessFile (java.io.RandomAccessFile)36 FileInputStream (java.io.FileInputStream)29 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.Test)18 Path (java.nio.file.Path)11 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9 Elf (com.facebook.buck.cxx.elf.Elf)8 FileOutputStream (java.io.FileOutputStream)8 ElfSection (com.facebook.buck.cxx.elf.ElfSection)6 FileNotFoundException (java.io.FileNotFoundException)5 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)4 Pair (com.facebook.buck.model.Pair)3 Date (java.util.Date)3 NulTerminatedCharsetDecoder (com.facebook.buck.charset.NulTerminatedCharsetDecoder)2 ElfDynamicSection (com.facebook.buck.cxx.elf.ElfDynamicSection)2