Search in sources :

Example 16 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 17 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 18 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)

Example 19 with MappedByteBuffer

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

the class ElfTest method le32.

@Test
public void le32() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "samples", tmp);
    workspace.setUp();
    Path elfPath = workspace.resolve(Paths.get("le32.o"));
    try (FileChannel channel = FileChannel.open(elfPath)) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        assertEquals(ElfHeader.EIClass.ELFCLASS32, elf.header.ei_class);
        assertEquals(ElfHeader.EIData.ELFDATA2LSB, elf.header.ei_data);
        assertEquals(9, elf.getNumberOfSections());
        assertTrue(elf.getSectionByName(".text").isPresent());
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 20 with MappedByteBuffer

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

the class ElfTest method sectionTypes.

@Test
public void sectionTypes() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "samples", tmp);
    workspace.setUp();
    Path elfPath = workspace.resolve(Paths.get("section_types.o"));
    try (FileChannel channel = FileChannel.open(elfPath)) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        Optional<ElfSection> section;
        section = elf.getSectionByName(".text").map(Pair::getSecond);
        assertTrue(section.isPresent());
        assertEquals(ElfSectionHeader.SHType.SHT_PROGBITS, section.get().header.sh_type);
        section = elf.getSectionByName(".bss").map(Pair::getSecond);
        assertTrue(section.isPresent());
        assertEquals(ElfSectionHeader.SHType.SHT_NOBITS, section.get().header.sh_type);
        section = elf.getSectionByName(".strtab").map(Pair::getSecond);
        assertTrue(section.isPresent());
        assertEquals(ElfSectionHeader.SHType.SHT_STRTAB, section.get().header.sh_type);
        section = elf.getSectionByName(".symtab").map(Pair::getSecond);
        assertTrue(section.isPresent());
        assertEquals(ElfSectionHeader.SHType.SHT_SYMTAB, section.get().header.sh_type);
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Aggregations

MappedByteBuffer (java.nio.MappedByteBuffer)147 FileChannel (java.nio.channels.FileChannel)71 IOException (java.io.IOException)40 File (java.io.File)34 RandomAccessFile (java.io.RandomAccessFile)33 FileInputStream (java.io.FileInputStream)27 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)7 ElfSection (com.facebook.buck.cxx.elf.ElfSection)6 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 FileNotFoundException (java.io.FileNotFoundException)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