use anyhow::{anyhow, Context, Result};
use goblin::{elf, mach, pe, Object};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryFormat {
Elf,
Pe,
MachO,
}
#[derive(Debug, Clone)]
pub struct Section {
pub name: String,
pub vaddr: u64,
pub size: u64,
pub data: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct Symbol {
pub vaddr: u64,
pub name: String,
pub size: u64,
}
pub struct Binary {
pub path: Option<String>,
pub format: BinaryFormat,
pub arch: String,
pub is_64: bool,
pub little_endian: bool,
pub bytes: Vec<u8>,
pub sections: Vec<Section>,
pub symbols: Vec<Symbol>,
#[doc(hidden)]
pub sec_by_vaddr: Vec<(u64, u64, usize)>,
#[doc(hidden)]
pub string_sections: Vec<usize>,
}
impl Binary {
pub fn parse(path: Option<&str>, bytes: Vec<u8>) -> Result<Self> {
let parse_buffer = bytes.clone();
let obj = Object::parse(&parse_buffer).context("goblin: failed to parse object")?;
match obj {
Object::Elf(elf) => load_elf(path, &elf, bytes),
Object::PE(pe) => load_pe(path, &pe, bytes),
Object::Mach(mach) => load_mach(path, &mach, bytes),
_ => Err(anyhow!("unsupported object kind (likely archive)")),
}
}
#[must_use]
pub fn vaddr_to_offset(&self, vaddr: u64) -> Option<(usize, usize)> {
let idx = self.sec_by_vaddr.binary_search_by(|(start, end, _)| {
if vaddr < *start {
std::cmp::Ordering::Greater
} else if vaddr >= *end {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Equal
}
});
idx.ok().map(|i| {
let (start, _end, sec_i) = self.sec_by_vaddr[i];
let off_in_sec = usize::try_from(vaddr - start)
.expect("vaddr-to-offset: offset within section fits usize on supported targets");
(sec_i, off_in_sec)
})
}
#[must_use]
pub fn read_at_vaddr(&self, vaddr: u64, len: usize) -> Option<&[u8]> {
let (sec_i, off) = self.vaddr_to_offset(vaddr)?;
let sec = &self.sections[sec_i];
if off.checked_add(len)? > sec.data.len() {
return None;
}
Some(&sec.data[off..off + len])
}
#[must_use]
pub fn read_ptr(&self, vaddr: u64) -> Option<u64> {
let sz = if self.is_64 { 8 } else { 4 };
let bytes = self.read_at_vaddr(vaddr, sz)?;
Some(if self.little_endian {
read_le_ptr(bytes, self.is_64)
} else {
read_be_ptr(bytes, self.is_64)
})
}
#[must_use]
pub fn read_u32(&self, vaddr: u64) -> Option<u32> {
let bytes = self.read_at_vaddr(vaddr, 4)?;
Some(if self.little_endian {
u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
} else {
u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
})
}
#[must_use]
pub fn vaddr_in_string_section(&self, vaddr: u64, len: u64) -> bool {
for &i in &self.string_sections {
let sec = &self.sections[i];
if vaddr >= sec.vaddr
&& vaddr
.checked_add(len)
.is_some_and(|e| e <= sec.vaddr + sec.size)
{
return true;
}
}
false
}
#[must_use]
pub fn string_section_indices(&self) -> &[usize] {
&self.string_sections
}
#[must_use]
pub fn sections(&self) -> &[Section] {
&self.sections
}
#[doc(hidden)]
#[must_use]
pub fn from_test_parts(
format: BinaryFormat,
is_64: bool,
little_endian: bool,
sections: Vec<Section>,
symbols: Vec<Symbol>,
force_string_sections: Vec<usize>,
) -> Self {
let mut bin = Self {
path: None,
format,
arch: "test".into(),
is_64,
little_endian,
bytes: vec![],
sections,
symbols,
sec_by_vaddr: Vec::new(),
string_sections: Vec::new(),
};
bin.build_indices();
bin.string_sections = force_string_sections;
bin
}
}
fn extract_section_data(off: usize, sz: usize, cap: usize, bytes: &[u8]) -> Vec<u8> {
if sz == 0 {
Vec::new()
} else if let Some(end) = off.checked_add(sz) {
if off <= cap && end <= cap {
bytes[off..end].to_vec()
} else if off < cap {
bytes[off..cap].to_vec()
} else {
Vec::new()
}
} else {
Vec::new()
}
}
#[allow(clippy::unnecessary_wraps)]
fn load_elf(path: Option<&str>, elf: &elf::Elf<'_>, bytes: Vec<u8>) -> Result<Binary> {
let is_64 = elf.is_64;
let little_endian = elf.little_endian;
let arch = arch_name_elf(elf.header.e_machine);
let cap = bytes.len();
let mut sections: Vec<Section> = Vec::new();
for sc in &elf.section_headers {
let name = elf.shdr_strtab.get_at(sc.sh_name).unwrap_or("").to_string();
let off = usize::try_from(sc.sh_offset)
.expect("ELF: section file offset exceeds usize on this host");
let sz = usize::try_from(sc.sh_size).expect("ELF: section size exceeds usize on this host");
let data = extract_section_data(off, sz, cap, &bytes);
sections.push(Section {
name,
vaddr: sc.sh_addr,
size: sc.sh_size,
data,
});
}
let mut symbols: Vec<Symbol> = Vec::new();
for sym in elf.syms.iter().chain(elf.dynsyms.iter()) {
if sym.st_value == 0 {
continue;
}
let Some(raw) = elf.strtab.get_at(sym.st_name) else {
continue;
};
if raw.is_empty() {
continue;
}
symbols.push(Symbol {
vaddr: sym.st_value,
name: raw.to_string(),
size: sym.st_size,
});
}
let mut bin = Binary {
path: path.map(String::from),
format: BinaryFormat::Elf,
arch,
is_64,
little_endian,
bytes,
sections,
symbols,
sec_by_vaddr: Vec::new(),
string_sections: Vec::new(),
};
bin.build_indices();
Ok(bin)
}
#[allow(clippy::unnecessary_wraps)]
fn load_pe(path: Option<&str>, pe: &pe::PE<'_>, bytes: Vec<u8>) -> Result<Binary> {
let is_64 = pe.is_64;
let arch = arch_name_pe(pe.header.coff_header.machine);
let cap = bytes.len();
let base = pe.image_base as u64;
let mut sections: Vec<Section> = Vec::new();
for sc in &pe.sections {
let name = sc.name().unwrap_or("").to_string();
let off = usize::try_from(sc.pointer_to_raw_data)
.expect("PE: section raw pointer exceeds usize on this host");
let sz = usize::try_from(sc.size_of_raw_data)
.expect("PE: section raw size exceeds usize on this host");
let data = extract_section_data(off, sz, cap, &bytes);
let vaddr = base.wrapping_add(u64::from(sc.virtual_address));
sections.push(Section {
name,
vaddr,
size: u64::from(sc.virtual_size),
data,
});
}
let mut symbols: Vec<Symbol> = Vec::new();
for exp in &pe.exports {
let Some(raw) = exp.name else { continue };
if raw.is_empty() {
continue;
}
symbols.push(Symbol {
vaddr: base.wrapping_add(exp.rva as u64),
name: raw.to_string(),
size: 0,
});
}
let mut bin = Binary {
path: path.map(String::from),
format: BinaryFormat::Pe,
arch,
is_64,
little_endian: true,
bytes,
sections,
symbols,
sec_by_vaddr: Vec::new(),
string_sections: Vec::new(),
};
bin.build_indices();
Ok(bin)
}
fn load_mach(path: Option<&str>, mach: &mach::Mach<'_>, bytes: Vec<u8>) -> Result<Binary> {
let mach_bin = match mach {
mach::Mach::Binary(b) => b,
mach::Mach::Fat(_) => return Err(anyhow!("Mach-O fat archives are not supported")),
};
let is_64 = mach_bin.is_64;
let mut sections: Vec<Section> = Vec::new();
for seg_iter in mach_bin.segments.sections() {
for entry in seg_iter {
let (sc, data) = entry.context("mach-o section parse")?;
let sectname = sc.name().unwrap_or("").to_string();
let segname = sc.segname().unwrap_or("").to_string();
let name = if segname.is_empty() {
sectname
} else {
format!("{segname}.{sectname}")
};
sections.push(Section {
name,
vaddr: sc.addr,
size: sc.size,
data: data.to_vec(),
});
}
}
let mut bin = Binary {
path: path.map(String::from),
format: BinaryFormat::MachO,
arch: String::from("macho"),
is_64,
little_endian: true,
bytes,
sections,
symbols: Vec::new(),
sec_by_vaddr: Vec::new(),
string_sections: Vec::new(),
};
bin.build_indices();
Ok(bin)
}
impl Binary {
fn build_indices(&mut self) {
for (i, sec) in self.sections.iter().enumerate() {
let end = sec.vaddr.wrapping_add(sec.size);
self.sec_by_vaddr.push((sec.vaddr, end, i));
}
self.sec_by_vaddr.sort_by_key(|(start, _end, _i)| *start);
for (i, sec) in self.sections.iter().enumerate() {
if is_string_section_name(&sec.name) {
self.string_sections.push(i);
}
}
}
}
fn read_le_ptr(bytes: &[u8], is_64: bool) -> u64 {
if is_64 {
let mut a = [0u8; 8];
a.copy_from_slice(&bytes[..8]);
u64::from_le_bytes(a)
} else {
let mut a = [0u8; 4];
a.copy_from_slice(&bytes[..4]);
u64::from(u32::from_le_bytes(a))
}
}
fn read_be_ptr(bytes: &[u8], is_64: bool) -> u64 {
if is_64 {
let mut a = [0u8; 8];
a.copy_from_slice(&bytes[..8]);
u64::from_be_bytes(a)
} else {
let mut a = [0u8; 4];
a.copy_from_slice(&bytes[..4]);
u64::from(u32::from_be_bytes(a))
}
}
fn is_string_section_name(name: &str) -> bool {
if name == ".rodata" || name.starts_with(".rodata.") {
return true;
}
if name == ".data.rel.ro" || name.starts_with(".data.rel.ro.") {
return true;
}
if name == ".rdata" || name.starts_with(".rdata.") {
return true;
}
if name == "__cstring" || name.starts_with("__cstring,") {
return true;
}
if name == "__const" || name.starts_with("__const,") {
return true;
}
false
}
fn arch_name_elf(machine: u16) -> String {
use goblin::elf::header::{EM_386, EM_AARCH64, EM_ARM, EM_MIPS, EM_PPC64, EM_RISCV, EM_X86_64};
match machine {
EM_X86_64 => "x86_64".into(),
EM_386 => "x86".into(),
EM_AARCH64 => "aarch64".into(),
EM_ARM => "arm".into(),
EM_RISCV => "riscv".into(),
EM_PPC64 => "ppc64".into(),
EM_MIPS => "mips".into(),
_ => format!("elf:{machine}"),
}
}
fn arch_name_pe(machine: u16) -> String {
use goblin::pe::header::{
COFF_MACHINE_ARM, COFF_MACHINE_ARM64, COFF_MACHINE_X86, COFF_MACHINE_X86_64,
};
match machine {
COFF_MACHINE_X86_64 => "x86_64".into(),
COFF_MACHINE_X86 => "x86".into(),
COFF_MACHINE_ARM64 => "aarch64".into(),
COFF_MACHINE_ARM => "arm".into(),
_ => format!("pe:{machine}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn fake_binary_empty() -> Binary {
Binary {
path: None,
format: BinaryFormat::Elf,
arch: "x86_64".into(),
is_64: true,
little_endian: true,
bytes: vec![],
sections: vec![Section {
name: ".rodata".into(),
vaddr: 0x2000,
size: 0x1000,
data: vec![0; 0x1000],
}],
symbols: vec![],
sec_by_vaddr: vec![(0x2000, 0x3000, 0)],
string_sections: vec![0],
}
}
#[test]
fn arch_name_elf_x86_64() {
assert_eq!(arch_name_elf(goblin::elf::header::EM_X86_64), "x86_64");
}
#[test]
fn arch_name_elf_x86() {
assert_eq!(arch_name_elf(goblin::elf::header::EM_386), "x86");
}
#[test]
fn arch_name_elf_aarch64() {
assert_eq!(arch_name_elf(goblin::elf::header::EM_AARCH64), "aarch64");
}
#[test]
fn arch_name_elf_arm() {
assert_eq!(arch_name_elf(goblin::elf::header::EM_ARM), "arm");
}
#[test]
fn arch_name_elf_riscv() {
assert_eq!(arch_name_elf(goblin::elf::header::EM_RISCV), "riscv");
}
#[test]
fn arch_name_elf_ppc64() {
assert_eq!(arch_name_elf(goblin::elf::header::EM_PPC64), "ppc64");
}
#[test]
fn arch_name_elf_mips() {
assert_eq!(arch_name_elf(goblin::elf::header::EM_MIPS), "mips");
}
#[test]
fn arch_name_elf_unknown_emits_default() {
assert_eq!(arch_name_elf(0xFFFF), "elf:65535");
}
#[test]
fn arch_name_pe_x86_64() {
assert_eq!(
arch_name_pe(goblin::pe::header::COFF_MACHINE_X86_64),
"x86_64"
);
}
#[test]
fn arch_name_pe_x86() {
assert_eq!(arch_name_pe(goblin::pe::header::COFF_MACHINE_X86), "x86");
}
#[test]
fn arch_name_pe_aarch64() {
assert_eq!(
arch_name_pe(goblin::pe::header::COFF_MACHINE_ARM64),
"aarch64"
);
}
#[test]
fn arch_name_pe_arm() {
assert_eq!(arch_name_pe(goblin::pe::header::COFF_MACHINE_ARM), "arm");
}
#[test]
fn arch_name_pe_unknown_emits_default() {
assert_eq!(arch_name_pe(0xBEEF), "pe:48879");
}
#[test]
fn is_string_section_name_full_matrix_positive() {
let positives = [
".rodata",
".rodata.1",
".rodata.foo.bar",
".data.rel.ro",
".data.rel.ro.local",
".rdata",
".rdata.0",
"__cstring",
"__cstring,FOO",
"__const",
"__const,FUNC",
];
for n in positives {
assert!(
super::is_string_section_name(n),
"{n} should be classified as string-hosting",
);
}
}
#[test]
fn vaddr_lookup_in_range() {
let b = fake_binary_empty();
let r = b.vaddr_to_offset(0x2000 + 5).unwrap();
assert_eq!(r, (0, 5));
assert!(b.vaddr_to_offset(0x3000).is_none());
assert!(b.vaddr_to_offset(0x1FFF).is_none());
}
#[test]
fn read_ptr_endianness_le() {
let mut b = fake_binary_empty();
b.sections[0].data[..8].copy_from_slice(&[0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11]);
assert_eq!(b.read_ptr(0x2000).unwrap(), 0x1122_3344_5566_7788);
}
#[test]
fn string_section_recognized() {
let b = fake_binary_empty();
assert!(b.vaddr_in_string_section(0x2000, 1));
assert!(b.vaddr_in_string_section(0x2800, 0x800));
assert!(!b.vaddr_in_string_section(0x3000, 1));
}
#[test]
fn read_be_ptr_64bit() {
let bytes = [0x11u8, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
assert_eq!(read_be_ptr(&bytes, true), 0x1122_3344_5566_7788);
}
#[test]
fn read_be_ptr_32bit() {
let bytes = [0x11u8, 0x22, 0x33, 0x44];
assert_eq!(read_be_ptr(&bytes, false), 0x1122_3344);
}
#[test]
fn read_ptr_endianness_be() {
let mut b = fake_binary_empty();
b.little_endian = false;
b.sections[0].data[..8].copy_from_slice(&[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]);
assert_eq!(b.read_ptr(0x2000).unwrap(), 0x1122_3344_5566_7788);
}
#[test]
fn is_string_section_name_rejects_non_string_sections() {
assert!(!is_string_section_name(".text"));
assert!(!is_string_section_name(".data"));
assert!(!is_string_section_name(".bss"));
assert!(!is_string_section_name(".got"));
assert!(!is_string_section_name(".dynsym"));
}
#[test]
fn string_section_indices_correct_with_multiple_sections() {
let mut bin = Binary {
path: None,
format: BinaryFormat::Elf,
arch: "x86_64".into(),
is_64: true,
little_endian: true,
bytes: vec![],
sections: vec![
Section {
name: ".text".into(),
vaddr: 0x1000,
size: 0x100,
data: vec![0; 0x100],
},
Section {
name: ".rodata".into(),
vaddr: 0x2000,
size: 0x100,
data: vec![0; 0x100],
},
Section {
name: ".data".into(),
vaddr: 0x3000,
size: 0x100,
data: vec![0; 0x100],
},
Section {
name: ".rdata".into(),
vaddr: 0x4000,
size: 0x100,
data: vec![0; 0x100],
},
],
symbols: vec![],
sec_by_vaddr: Vec::new(),
string_sections: Vec::new(),
};
bin.build_indices();
let indices = bin.string_section_indices();
assert!(indices.contains(&1), ".rodata must be in string_sections");
assert!(indices.contains(&3), ".rdata must be in string_sections");
assert!(
!indices.contains(&0),
".text must NOT be in string_sections"
);
}
#[test]
fn string_section_indices_empty_when_no_string_sections() {
let bin = Binary::from_test_parts(
BinaryFormat::Elf,
true,
true,
vec![
Section {
name: ".text".into(),
vaddr: 0x1000,
size: 0x100,
data: vec![0; 0x100],
},
Section {
name: ".data".into(),
vaddr: 0x2000,
size: 0x100,
data: vec![0; 0x100],
},
],
vec![],
vec![],
);
assert!(bin.string_section_indices().is_empty());
}
fn minimal_elf64() -> Vec<u8> {
let mut e = vec![0u8; 64];
e[0..4].copy_from_slice(b"\x7fELF");
e[4] = 2;
e[5] = 1;
e[6] = 1;
e[16..18].copy_from_slice(&2u16.to_le_bytes());
e[18..20].copy_from_slice(&62u16.to_le_bytes());
e[20..24].copy_from_slice(&1u32.to_le_bytes());
e[52..54].copy_from_slice(&64u16.to_le_bytes());
e[58..60].copy_from_slice(&64u16.to_le_bytes());
e
}
#[test]
fn parse_minimal_elf_succeeds() {
let elf = minimal_elf64();
let bin = Binary::parse(Some("test.elf"), elf).expect("minimal ELF must parse");
assert_eq!(bin.format, BinaryFormat::Elf);
assert!(bin.is_64);
assert!(bin.little_endian);
assert_eq!(bin.arch, "x86_64");
}
fn elf64_with_rodata() -> Vec<u8> {
let mut buf = vec![0u8; 64]; buf[0..4].copy_from_slice(b"\x7fELF");
buf[4] = 2;
buf[5] = 1;
buf[6] = 1;
buf[16..18].copy_from_slice(&2u16.to_le_bytes());
buf[18..20].copy_from_slice(&62u16.to_le_bytes());
buf[20..24].copy_from_slice(&1u32.to_le_bytes());
buf[40..48].copy_from_slice(&64u64.to_le_bytes()); buf[52..54].copy_from_slice(&64u16.to_le_bytes()); buf[58..60].copy_from_slice(&64u16.to_le_bytes()); buf[60..62].copy_from_slice(&2u16.to_le_bytes()); buf.extend_from_slice(&[0u8; 64]);
let data_off = 192u64;
let sh1 = 64 + 64; buf.extend_from_slice(&[0u8; 64]);
buf[sh1 + 4..sh1 + 8].copy_from_slice(&1u32.to_le_bytes()); buf[sh1 + 16..sh1 + 24].copy_from_slice(&0x1000u64.to_le_bytes()); buf[sh1 + 24..sh1 + 32].copy_from_slice(&data_off.to_le_bytes()); buf[sh1 + 32..sh1 + 40].copy_from_slice(&5u64.to_le_bytes()); buf.extend_from_slice(b"hello");
buf
}
#[test]
fn parse_elf_section_data_not_empty() {
let elf = elf64_with_rodata();
let bin = Binary::parse(Some("test.elf"), elf).expect("parse");
let has_data = bin.sections.iter().any(|s| !s.data.is_empty());
assert!(
has_data,
"at least one section must have non-empty data (kills sz==0 → != mutation)"
);
}
#[test]
fn parse_elf_truncated_section_clamps_data() {
let mut elf = elf64_with_rodata();
let sh1_size_off = 64 + 64 + 32; elf[sh1_size_off..sh1_size_off + 8].copy_from_slice(&100u64.to_le_bytes());
let bin = Binary::parse(Some("truncated.elf"), elf).expect("parse");
let rodata = bin.sections.iter().find(|s| s.size == 100);
assert!(rodata.is_some(), "section with size=100 must exist");
let rodata = rodata.unwrap();
assert_eq!(
rodata.data.len(),
5,
"truncated section data must be clamped to available bytes"
);
}
fn minimal_macho64() -> Vec<u8> {
let mut m = vec![0u8; 32];
m[0..4].copy_from_slice(&0xfeed_facf_u32.to_le_bytes());
m[4..8].copy_from_slice(&0x0100_0007_u32.to_le_bytes());
m[8..12].copy_from_slice(&3u32.to_le_bytes());
m[12..16].copy_from_slice(&1u32.to_le_bytes());
m
}
#[test]
fn parse_minimal_macho_succeeds() {
let macho = minimal_macho64();
let bin = Binary::parse(Some("test.macho"), macho).expect("minimal Mach-O must parse");
assert_eq!(bin.format, BinaryFormat::MachO);
assert!(bin.is_64);
}
#[test]
fn vaddr_to_offset_finds_correct_section() {
let bin = Binary::from_test_parts(
BinaryFormat::Elf,
true,
true,
vec![
Section {
name: ".text".into(),
vaddr: 0x1000,
size: 0x100,
data: vec![0xAA; 0x100],
},
Section {
name: ".rodata".into(),
vaddr: 0x2000,
size: 0x100,
data: vec![0xBB; 0x100],
},
],
vec![],
vec![],
);
let (sec_i, off) = bin.vaddr_to_offset(0x2005).unwrap();
assert_eq!(sec_i, 1);
assert_eq!(off, 5);
assert_eq!(bin.read_at_vaddr(0x2005, 1).unwrap()[0], 0xBB);
}
#[test]
fn parse_rejects_empty_input() {
assert!(Binary::parse(Some("empty"), vec![]).is_err());
}
#[test]
fn parse_rejects_garbage() {
assert!(Binary::parse(Some("garbage"), vec![0x42; 128]).is_err());
}
#[test]
fn extract_data_full_section_within_file() {
let bytes = b"0123456789HELLO-----".to_vec();
let data = extract_section_data(10, 5, bytes.len(), &bytes);
assert_eq!(data, b"HELLO");
}
#[test]
fn extract_data_section_extends_past_file() {
let bytes = b"0123456789ABCDEPARTI".to_vec();
let data = extract_section_data(15, 10, bytes.len(), &bytes);
assert_eq!(data, b"PARTI");
}
#[test]
fn extract_data_offset_at_exact_file_end() {
let bytes = b"0123456789ABCDEFGHIJ".to_vec(); let data = extract_section_data(20, 5, bytes.len(), &bytes);
assert!(data.is_empty(), "offset at file end must yield empty data");
}
#[test]
fn extract_data_offset_past_file_end() {
let bytes = b"0123456789ABCDEFGHIJ".to_vec();
let data = extract_section_data(25, 5, bytes.len(), &bytes);
assert!(
data.is_empty(),
"offset past file end must yield empty data"
);
}
#[test]
fn extract_data_zero_size_returns_empty() {
let bytes = b"0123456789".to_vec();
let data = extract_section_data(0, 0, bytes.len(), &bytes);
assert!(data.is_empty(), "zero-size section must yield empty data");
}
#[test]
fn extract_data_offset_zero_full_file() {
let bytes = b"0123456789".to_vec();
let data = extract_section_data(0, 10, bytes.len(), &bytes);
assert_eq!(data, b"0123456789");
}
#[test]
fn extract_data_section_at_exact_end_boundary() {
let bytes = b"0123456789ABCDEEND!!".to_vec(); let data = extract_section_data(15, 5, bytes.len(), &bytes);
assert_eq!(data, b"END!!");
}
#[test]
fn extract_data_overflow_protection() {
let bytes = b"0123456789".to_vec();
let data = extract_section_data(usize::MAX - 2, 10, bytes.len(), &bytes);
assert!(data.is_empty(), "overflow in off+sz must yield empty data");
}
}