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