#![deny(unsafe_code)]
use core::marker::PhantomData;
use puns::{
Error, Pun, from_bytes, from_head,
num::{u8c, u16be, u16le, u16lec, u32be, u32bec, u32le, u32lec, u64be, u64le},
try_pun_ref,
};
#[derive(Pun)]
#[repr(C)]
#[pun(assert(e_version == 1))]
#[pun(assert(e_ehsize == if class == 2 { 64 } else { 52 }))]
#[pun(assert(e_phnum == 0 || e_phentsize == size_of::<P>()))]
#[pun(assert(e_shnum == 0 || e_shentsize == size_of::<S>()))]
#[pun(assert(e_shnum == 0 || e_shstrndx < e_shnum))]
#[pun(assert(e_phnum == 0 || e_phoff >= e_ehsize))]
#[pun(assert(e_shnum == 0 || e_shoff >= e_phoff + e_phnum * e_phentsize))]
#[pun(get(is_executable: bool = e_type == 2, is_shared: bool = e_type == 3))]
pub struct Elf<C, D, H: Copy, W, A, P, S> {
pub magic: u32bec<0x7f45_4c46>,
pub class: C,
pub data: D,
pub ident_version: u8c<1>,
pub os_abi: u8,
pub abi_version: u8,
pub padding: [u8; 7],
#[pun(valid(0..=4, 0xfe00..))]
pub e_type: H,
pub e_machine: H,
pub e_version: W,
pub e_entry: A,
pub e_phoff: A,
pub e_shoff: A,
pub e_flags: W,
pub e_ehsize: H,
pub e_phentsize: H,
pub e_phnum: H,
pub e_shentsize: H,
pub e_shnum: H,
pub e_shstrndx: H,
pub header_types: PhantomData<(P, S)>,
#[pun(regions(
before_program_headers = if e_phnum == 0 { 0 } else { e_phoff - e_ehsize },
program_headers: [P] = e_phnum * e_phentsize,
before_section_headers = if e_shnum == 0 {
0
} else {
e_shoff
- e_ehsize
- (if e_phnum == 0 { 0 } else { e_phoff - e_ehsize })
- e_phnum * e_phentsize
},
section_headers: [S] = e_shnum * e_shentsize,
rest,
))]
pub tail: [u8],
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
#[pun(assert(p_filesz <= p_memsz || p_type != 1))]
#[pun(assert(p_align == 0 || p_align & (p_align - 1) == 0))]
pub struct Phdr64<W, X> {
pub p_type: W,
pub p_flags: W,
pub p_offset: X,
pub p_vaddr: X,
pub p_paddr: X,
pub p_filesz: X,
pub p_memsz: X,
pub p_align: X,
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
#[pun(assert(p_filesz <= p_memsz || p_type != 1))]
#[pun(assert(p_align == 0 || p_align & (p_align - 1) == 0))]
pub struct Phdr32<W> {
pub p_type: W,
pub p_offset: W,
pub p_vaddr: W,
pub p_paddr: W,
pub p_filesz: W,
pub p_memsz: W,
pub p_flags: W,
pub p_align: W,
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
pub struct Shdr<W, X> {
pub sh_name: W,
pub sh_type: W,
pub sh_flags: X,
pub sh_addr: X,
pub sh_offset: X,
pub sh_size: X,
pub sh_link: W,
pub sh_info: W,
pub sh_addralign: X,
pub sh_entsize: X,
}
pub type Elf64Le =
Elf<u8c<2>, u8c<1>, u16le, u32le, u64le, Phdr64<u32le, u64le>, Shdr<u32le, u64le>>;
pub type Elf64Be =
Elf<u8c<2>, u8c<2>, u16be, u32be, u64be, Phdr64<u32be, u64be>, Shdr<u32be, u64be>>;
pub type Elf32Le = Elf<u8c<1>, u8c<1>, u16le, u32le, u32le, Phdr32<u32le>, Shdr<u32le, u32le>>;
pub type Elf32Be = Elf<u8c<1>, u8c<2>, u16be, u32be, u32be, Phdr32<u32be>, Shdr<u32be, u32be>>;
#[derive(Pun)]
pub enum AnyElf<'a> {
Le64(&'a Elf64Le),
Be64(&'a Elf64Be),
Le32(&'a Elf32Le),
Be32(&'a Elf32Be),
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert(e_lfanew >= 64 && e_lfanew % 8 == 0))]
#[pun(assert(le(e_lfanew + 24, 2) == MAGIC))]
pub struct Pe<O: ?Sized, const MAGIC: u16> {
pub e_magic: u16lec<0x5a4d>,
pub dos: [u16le; 29],
pub e_lfanew: u32le,
pub headers_type: PhantomData<O>,
#[pun(regions(stub = e_lfanew - 64, headers: O))]
pub rest: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert(size_of_optional_header >= size_of::<H>()))]
#[pun(get(dll: bool = characteristics & 0x2000 != 0, executable: bool = characteristics & 2 != 0))]
pub struct Headers<H> {
pub signature: u32lec<0x4550>,
#[pun(valid(0x14c, 0x8664, 0xaa64, 0x1c0, 0x1c4))]
pub machine: u16le,
#[pun(valid(1..=96))]
pub number_of_sections: u16le,
pub time_date_stamp: u32le,
pub pointer_to_symbol_table: u32le,
pub number_of_symbols: u32le,
pub size_of_optional_header: u16le,
pub characteristics: u16le,
pub optional: H,
#[pun(regions(
data_directories: [DataDirectory] = size_of_optional_header - size_of::<H>(),
sections: [SectionHeader] = number_of_sections * 40,
rest,
))]
pub rest: [u8],
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
#[pun(assert(section_alignment >= file_alignment
&& file_alignment >= 512
&& file_alignment & (file_alignment - 1) == 0))]
#[pun(assert(size_of_image % section_alignment == 0))]
pub struct Optional64 {
pub magic: u16lec<0x20b>,
pub linker: [u8; 2],
pub size_of_code: u32le,
pub size_of_initialized_data: u32le,
pub size_of_uninitialized_data: u32le,
pub address_of_entry_point: u32le,
pub base_of_code: u32le,
pub image_base: u64le,
pub section_alignment: u32le,
pub file_alignment: u32le,
pub versions: [u16le; 6],
pub win32_version: u32lec<0>,
pub size_of_image: u32le,
pub size_of_headers: u32le,
pub checksum: u32le,
#[pun(valid(1..=3, 9..=14, 16))]
pub subsystem: u16le,
pub dll_characteristics: u16le,
pub stack_reserve: u64le,
pub stack_commit: u64le,
pub heap_reserve: u64le,
pub heap_commit: u64le,
pub loader_flags: u32lec<0>,
#[pun(valid(..=16))]
pub number_of_rva_and_sizes: u32le,
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
pub struct Optional32 {
pub magic: u16lec<0x10b>,
pub linker: [u8; 2],
pub sizes: [u32le; 3],
pub address_of_entry_point: u32le,
pub base_of_code: u32le,
pub base_of_data: u32le,
pub image_base: u32le,
pub section_alignment: u32le,
pub file_alignment: u32le,
pub versions: [u16le; 6],
pub win32_version: u32lec<0>,
pub size_of_image: u32le,
pub size_of_headers: u32le,
pub checksum: u32le,
pub subsystem: u16le,
pub dll_characteristics: u16le,
pub reserves: [u32le; 4],
pub loader_flags: u32lec<0>,
#[pun(valid(..=16))]
pub number_of_rva_and_sizes: u32le,
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
pub struct DataDirectory {
pub virtual_address: u32le,
pub size: u32le,
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
#[pun(assert(size_of_raw_data == 0 || pointer_to_raw_data % 512 == 0))]
pub struct SectionHeader {
pub name: [u8; 8],
pub virtual_size: u32le,
pub virtual_address: u32le,
pub size_of_raw_data: u32le,
pub pointer_to_raw_data: u32le,
pub pointer_to_relocations: u32le,
pub pointer_to_line_numbers: u32le,
pub number_of_relocations: u16le,
pub number_of_line_numbers: u16le,
pub characteristics: u32le,
}
pub type Pe64 = Pe<Headers<Optional64>, 0x20b>;
pub type Pe32 = Pe<Headers<Optional32>, 0x10b>;
#[derive(Pun)]
pub enum AnyPe<'a> {
Pe64(&'a Pe64),
Pe32(&'a Pe32),
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert({
let end = walk(
32,
|s| s >> 32 < ncmds,
|s| {
let at = s & 0xffff_ffff;
let size = le(at + 4, 4);
if size < 8 || size % 8 != 0 { 1 / 0 } else { (at + size) + (((s >> 32) + 1) << 32) }
},
);
end & 0xffff_ffff == 32 + sizeofcmds
}))]
pub struct MachO64 {
pub magic: u32lec<0xfeed_facf>,
pub cputype: u32le,
pub cpusubtype: u32le,
#[pun(valid(1..=12))]
pub filetype: u32le,
pub ncmds: u32le,
pub sizeofcmds: u32le,
pub flags: u32le,
pub reserved: u32le,
#[pun(regions(commands = sizeofcmds, rest))]
pub tail: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = cmdsize)]
pub struct LoadCommand {
pub cmd: u32le,
#[pun(valid(8..))]
pub cmdsize: u32le,
pub data: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(assert(cmdsize == 72 + nsects * 80))]
#[pun(assert(filesize <= vmsize))]
pub struct Segment64 {
pub cmd: u32lec<0x19>,
pub cmdsize: u32le,
pub segname: [u8; 16],
pub vmaddr: u64le,
pub vmsize: u64le,
pub fileoff: u64le,
pub filesize: u64le,
pub maxprot: u32le,
pub initprot: u32le,
pub nsects: u32le,
pub flags: u32le,
#[pun(len = nsects)]
pub sections: [Section64],
}
#[derive(Pun, Debug, Clone, Copy)]
#[repr(C)]
pub struct Section64 {
pub sectname: [u8; 16],
pub segname: [u8; 16],
pub addr: u64le,
pub size: u64le,
pub offset: u32le,
#[pun(valid(0..=15))]
pub align: u32le,
pub reloff: u32le,
pub nreloc: u32le,
pub flags: u32le,
pub reserved: [u32le; 3],
}
pub const ENTRY_SIZES: u128 = 0x3_3553_4005_5553_3995_5010;
#[derive(Pun)]
#[repr(C)]
#[pun(assert({
let end = walk(
10 + (1 << 32),
|s| s >> 32 < constant_pool_count,
|s| {
let at = s & 0xffff_ffff;
let tag = be(at, 1);
let size = (ENTRY_SIZES >> (tag * 4)) & 15;
// (`2 << 32` is 3 operations, a literal is 1: with shifts, this step is 50.)
if size == 0 {
1 / 0
} else {
s + (if size == 1 { 3 + be(at + 1, 2) } else { size })
+ (if tag == 5 || tag == 6 { 0x2_0000_0000 } else { 0x1_0000_0000 })
}
},
);
let at = end & 0xffff_ffff;
end >> 32 == constant_pool_count
&& be(at + 2, 2) >= 1 && be(at + 2, 2) < constant_pool_count
&& be(at + 4, 2) < constant_pool_count
&& at + 8 + 2 * be(at + 6, 2) <= size_of_val(self)
// An interface is abstract.
&& (be(at, 2) & 0x0200 == 0 || be(at, 2) & 0x0400 != 0)
}))]
pub struct Class {
pub magic: u32bec<0xcafe_babe>,
pub minor: u16be,
#[pun(valid(45..=70))]
pub major: u16be,
#[pun(valid(1..))]
pub constant_pool_count: u16be,
pub rest: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = length + 3)]
pub struct Utf8Entry {
pub tag: u8c<1>,
pub length: u16be,
pub bytes: [u8],
}
pub const RANKS: u128 = 0x6b_dca9_8754_3210;
#[derive(Pun)]
#[repr(C)]
#[pun(assert(walk(
8,
|s| s & 0xffff_ffff < size_of_val(self),
|s| {
let at = s & 0xffff_ffff;
let id = be(at, 1);
let rank = (RANKS >> (id * 4)) & 15;
let next = at + 1 + leb128_len(at + 1) + leb128(at + 1);
if id > 13 || leb128_len(at + 1) > 5 {
1 / 0
} else if id == 0 {
next | (s >> 40 << 40)
} else if rank > s >> 40 {
next | (rank << 40)
} else {
1 / 0
}
},
) & 0xffff_ffff == size_of_val(self)))]
pub struct Module {
pub magic: u32bec<0x0061_736d>,
pub version: u32lec<1>,
pub sections: [u8],
}
#[derive(Pun)]
#[repr(C)]
#[pun(size = 1 + leb128_len(1) + leb128(1))]
#[pun(assert(leb128_len(1) <= 5))]
pub struct Section {
#[pun(valid(0..=13))]
pub id: u8,
#[pun(regions(size = leb128_len(1), payload))]
pub rest: [u8],
}
#[derive(Pun)]
#[repr(C)]
pub struct FuncType {
pub form: u8c<0x60>,
#[pun(valid(..0x80))]
pub params: u8,
#[pun(len = params)]
pub types: [ValueType],
}
#[derive(Pun, Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ValueType {
I32 = 0x7f,
I64 = 0x7e,
F32 = 0x7d,
F64 = 0x7c,
V128 = 0x7b,
FuncRef = 0x70,
ExternRef = 0x6f,
}
const ELF64_LE: [u8; 256] = [
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00, 0x01, 0x00, 0x40, 0x00, 0x02, 0x00, 0x01, 0x00,
0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x10, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
const ELF64_BE: [u8; 256] = [
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00, 0x01, 0x00, 0x40, 0x00, 0x02, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78,
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
const ELF32_LE: [u8; 172] = [
0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, 0x00, 0x34, 0x00, 0x00, 0x00,
0x5c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x20, 0x00, 0x01, 0x00, 0x28, 0x00,
0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, 0x00,
0x00, 0x10, 0x40, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
0x00, 0x10, 0x00, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
const ELF32_BE: [u8; 172] = [
0x7f, 0x45, 0x4c, 0x46, 0x01, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x34,
0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x20, 0x00, 0x01, 0x00, 0x28,
0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x54, 0x00, 0x40, 0x10, 0x00,
0x00, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x05,
0x00, 0x00, 0x10, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
];
const PE64: [u8; 320] = [
0x4d, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x50, 0x45, 0x00, 0x00, 0x64, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x22, 0x00, 0x0b, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, b'.', b't', b'e', b'x', b't', 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
const MACHO: [u8; 200] = [
0xcf, 0xfa, 0xed, 0xfe, 0x0c, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0xa8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x19, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, b'_', b'_', b'T', b'E', b'X', b'T', 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, b'_', b'_', b't', b'e', b'x', b't', 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, b'_', b'_', b'T', b'E', b'X', b'T', 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
];
const CLASS: [u8; 66] = [
0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x34, 0x00, 0x07, 0x01, 0x00, 0x05, b'H', b'e', b'l',
b'l', b'o', 0x07, 0x00, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x01, 0x00,
0x10, b'j', b'a', b'v', b'a', b'/', b'l', b'a', b'n', b'g', b'/', b'O', b'b', b'j', b'e', b'c',
b't', 0x07, 0x00, 0x05, 0x00, 0x21, 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];
const WASM: [u8; 31] = [
0x00, b'a', b's', b'm', 0x01, 0x00, 0x00, 0x00, 1, 7, 1, 0x60, 2, 0x7f, 0x7f, 1, 0x7f, 0, 5, 4,
b'n', b'a', b'm', b'e', 3, 2, 1, 0, 10, 0xc8, 0x01,
];
pub fn main() {
for bytes in [&ELF64_LE[..], &ELF64_BE, &ELF32_LE, &ELF32_BE] {
let (kind, entry, load, code) = match AnyElf::from_bytes(bytes).unwrap() {
AnyElf::Le64(elf) => {
assert!(elf.is_executable() && !elf.is_shared());
assert_eq!(elf.section_headers().unwrap()[1].sh_type.get(), 3);
let load = elf.program_headers().unwrap()[0];
(
"64 LE",
elf.e_entry.get(),
(load.p_flags.get(), load.p_filesz.get()),
elf.before_section_headers(),
)
}
AnyElf::Be64(elf) => {
let load = elf.program_headers().unwrap()[0];
(
"64 BE",
elf.e_entry.get(),
(load.p_flags.get(), load.p_filesz.get()),
elf.before_section_headers(),
)
}
AnyElf::Le32(elf) => {
let load = elf.program_headers().unwrap()[0];
let flags = (load.p_flags.get(), u64::from(load.p_filesz.get()));
("32 LE", u64::from(elf.e_entry.get()), flags, elf.before_section_headers())
}
AnyElf::Be32(elf) => {
let load = elf.program_headers().unwrap()[0];
let flags = (load.p_flags.get(), u64::from(load.p_filesz.get()));
("32 BE", u64::from(elf.e_entry.get()), flags, elf.before_section_headers())
}
};
assert_eq!((entry, load, code), (0x40_1000, (5, 8), &[0x90; 8][..]));
assert_eq!(
kind,
match bytes[4..6] {
[2, 1] => "64 LE",
[2, 2] => "64 BE",
[1, 1] => "32 LE",
_ => "32 BE",
}
);
}
for (at, value) in
[(0, 0x7e), (4, 3), (5, 0), (6, 2), (16, 5), (20, 2), (52, 63), (54, 55), (60, 3)]
{
let mut bytes = ELF64_LE;
bytes[at] = value;
assert_eq!(AnyElf::from_bytes(&bytes).err(), Some(Error::Validity));
}
let mut bytes = ELF64_LE;
bytes[64 + 48] = 3;
let elf: &Elf64Le = try_pun_ref(&bytes[..]).unwrap();
assert_eq!(elf.program_headers().err(), Some(Error::Validity));
let mut bytes = ELF64_LE;
bytes[40] = 0xf0;
assert_eq!(AnyElf::from_bytes(&bytes).err(), Some(Error::Validity));
let Ok(AnyPe::Pe64(pe)) = AnyPe::from_bytes(&PE64) else { panic!() };
assert_eq!((pe.e_lfanew.get(), pe.stub().len()), (0x80, 64));
let headers = pe.headers().unwrap();
assert_eq!((headers.machine.get(), headers.executable(), headers.dll()), (0x8664, true, false));
let optional = &headers.optional;
assert_eq!(
(optional.address_of_entry_point.get(), optional.image_base.get()),
(0x1000, 0x1_4000_0000)
);
assert_eq!((optional.subsystem.get(), headers.data_directories().unwrap().len()), (3, 2));
let sections = headers.sections().unwrap();
assert_eq!(
(§ions[0].name[..5], sections[0].pointer_to_raw_data.get()),
(&b".text"[..], 0x400)
);
assert!(try_pun_ref::<_, Pe32>(&PE64[..]).is_err());
for (at, value) in [(0, b'N'), (0x3c, 0x81), (0x80, b'Q'), (0x84, 0x4d), (0x86, 0), (0x98, 0x0c)]
{
let mut bytes = PE64;
bytes[at] = value;
let rejected = match AnyPe::from_bytes(&bytes) {
Err(_) => true,
Ok(AnyPe::Pe64(pe)) => pe.headers().is_err(),
Ok(AnyPe::Pe32(pe)) => pe.headers().is_err(),
};
assert!(rejected);
}
let mut bytes = PE64;
bytes[0x98 + 36..0x98 + 38].copy_from_slice(&[0x01, 0x03]);
let Ok(AnyPe::Pe64(pe)) = AnyPe::from_bytes(&bytes) else { panic!() };
assert_eq!(pe.headers().err(), Some(Error::Validity));
let file: &MachO64 = try_pun_ref(&MACHO[..]).unwrap();
assert_eq!((file.ncmds.get(), file.commands().len(), file.rest().len()), (2, 168, 0));
let (command, rest) = from_head::<LoadCommand>(file.commands()).unwrap();
assert_eq!(command.cmd.get(), 0x19);
let segment: &Segment64 = try_pun_ref(&file.commands()[..152]).unwrap();
assert_eq!(
(&segment.segname[..6], segment.nsects.get(), segment.sections[0].align.get()),
(&b"__TEXT"[..], 1, 4)
);
let (command, rest) = from_head::<LoadCommand>(rest).unwrap();
assert_eq!((command.cmd.get(), command.data.len(), rest.len()), (0x1b, 8, 0));
for (at, value) in [(16, 3), (188, 20), (20, 0xf8)] {
let mut bytes = MACHO;
bytes[at] = value;
assert_eq!(try_pun_ref::<_, MachO64>(&bytes[..]).err(), Some(Error::Validity));
}
let mut bytes = MACHO;
bytes[32 + 4] = 160;
assert_eq!(try_pun_ref::<_, Segment64>(&bytes[32..32 + 152]).err(), Some(Error::Validity));
let class: &Class = try_pun_ref(&CLASS[..]).unwrap();
assert_eq!((class.major.get(), class.constant_pool_count.get()), (52, 7));
let (utf8, _) = from_head::<Utf8Entry>(&class.rest).unwrap();
assert_eq!(&utf8.bytes, b"Hello");
for (at, value) in [(9, 8), (10, 2), (CLASS.len() - 11, 9), (9, 6), (7, 71)] {
let mut bytes = CLASS;
bytes[at] = value;
assert_eq!(try_pun_ref::<_, Class>(&bytes[..]).err(), Some(Error::Validity));
}
let mut module = WASM.to_vec();
module.resize(31 + 200, 0);
let wasm: &Module = try_pun_ref(&module[..]).unwrap();
let (mut rest, mut sections) = (&wasm.sections[..], vec![]);
while let Ok((section, next)) = from_head::<Section>(rest) {
sections.push((section.id, section.payload().len()));
rest = next;
}
assert_eq!((sections, rest.len()), (vec![(1, 7), (0, 5), (3, 2), (10, 200)], 0));
let (types, _) = from_head::<Section>(&wasm.sections).unwrap();
let (function, _) = from_head::<FuncType>(&types.payload()[1..]).unwrap();
assert_eq!(function.types, [ValueType::I32, ValueType::I32]);
assert_eq!(from_head::<FuncType>(&[0x60, 1, 0x7a]).err(), Some(Error::Validity));
for (at, value) in [(8, 14), (29, 0xc9), (24, 1)] {
let mut bytes = module.clone();
bytes[at] = value;
assert_eq!(try_pun_ref::<_, Module>(&bytes[..]).err(), Some(Error::Validity));
}
let mut large = [&WASM[..8], &[0, 0x80, 0x80, 0x01]].concat();
large.resize(12 + 16384, 0);
let wasm: &Module = try_pun_ref(&large[..]).unwrap();
assert_eq!(
from_bytes::<Section>(&wasm.sections).map(|section| section.payload().len()),
Ok(16384)
);
println!("ok");
}