PE: add synthetic unit tests for implib.rs and layout.rs
(cherry picked from commit 131944d02dc0b37469f85f907234c664eb8027af)
This commit is contained in:
1 parent
c7541110d4
commit
ed07828ce2
2 files changed
+729
No files matched your search
@@ -64,3 +64,571 @@ impl ImportLibrary {
|
||||
Ok(lib)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::MACHINE_AMD64;
|
||||
|
||||
/// `Sig1`, which must be `IMAGE_FILE_MACHINE_UNKNOWN` (not a machine).
|
||||
const SIG1: u16 = 0;
|
||||
/// `Sig2` of a short-format import object (`IMPORT_OBJECT_HDR_SIG2`).
|
||||
const SIG2: u16 = 0xFFFF;
|
||||
|
||||
/// `Type` field of the header, a 2-bit value at bit 0.
|
||||
mod import_type {
|
||||
pub const CODE: u16 = 0;
|
||||
pub const DATA: u16 = 1;
|
||||
pub const CONST: u16 = 2;
|
||||
}
|
||||
|
||||
/// `NameType` field of the header, a 3-bit value at bit 2.
|
||||
mod name_type {
|
||||
pub const ORDINAL: u16 = 0;
|
||||
pub const NAME: u16 = 1;
|
||||
pub const NAME_NO_PREFIX: u16 = 2;
|
||||
pub const NAME_UNDECORATE: u16 = 3;
|
||||
pub const NAME_EXPORTAS: u16 = 4;
|
||||
}
|
||||
|
||||
/// One short-format import object: the 20-byte header followed by the
|
||||
/// NUL-terminated symbol, DLL and (for `NAME_EXPORTAS`) export names.
|
||||
fn import_object(
|
||||
import_type: u16,
|
||||
name_type: u16,
|
||||
ordinal_or_hint: u16,
|
||||
symbol: &str,
|
||||
dll: &str,
|
||||
export_as: Option<&str>,
|
||||
) -> Vec<u8> {
|
||||
let mut strings = Vec::new();
|
||||
for s in [Some(symbol), Some(dll), export_as].into_iter().flatten() {
|
||||
strings.extend_from_slice(s.as_bytes());
|
||||
strings.push(0);
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
out.extend_from_slice(&SIG1.to_le_bytes());
|
||||
out.extend_from_slice(&SIG2.to_le_bytes());
|
||||
out.extend_from_slice(&0u16.to_le_bytes()); // Version
|
||||
out.extend_from_slice(&MACHINE_AMD64.to_le_bytes());
|
||||
out.extend_from_slice(&0u32.to_le_bytes()); // TimeDateStamp
|
||||
out.extend_from_slice(&(strings.len() as u32).to_le_bytes());
|
||||
out.extend_from_slice(&ordinal_or_hint.to_le_bytes());
|
||||
out.extend_from_slice(&(import_type | name_type << 2).to_le_bytes());
|
||||
out.extend_from_slice(&strings);
|
||||
out
|
||||
}
|
||||
|
||||
/// One archive member: the 60-byte header, the data, and the pad byte the
|
||||
/// format adds so the next header starts even.
|
||||
fn archive_member(name: &str, data: &[u8]) -> Vec<u8> {
|
||||
assert!(name.len() <= 16, "an archive member name is 16 bytes");
|
||||
let mut out = vec![b' '; 60];
|
||||
out[..name.len()].copy_from_slice(name.as_bytes());
|
||||
let size = data.len().to_string();
|
||||
out[48..48 + size.len()].copy_from_slice(size.as_bytes());
|
||||
out[58] = b'`';
|
||||
out[59] = b'\n';
|
||||
out.extend_from_slice(data);
|
||||
if data.len() % 2 == 1 {
|
||||
out.push(b'\n');
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn archive(members: Vec<(&str, Vec<u8>)>) -> Vec<u8> {
|
||||
let mut out = b"!<arch>\n".to_vec();
|
||||
for (name, data) in members {
|
||||
out.extend_from_slice(&archive_member(name, &data));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_named_code_imports() {
|
||||
// Deliberately out of order, to show that the result is sorted.
|
||||
let lib = archive(vec![
|
||||
(
|
||||
"version.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"GetFileVersionInfoSizeW",
|
||||
"version.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"kernel32.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
7,
|
||||
"ExitProcess",
|
||||
"kernel32.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
assert_eq!(
|
||||
parsed.imports,
|
||||
[
|
||||
LibImport {
|
||||
dll: "kernel32.dll".into(),
|
||||
// `object` does not expose the hint of a short-format
|
||||
// import, so it is always 0 here.
|
||||
export: ImportSymbol::Name {
|
||||
hint: 0,
|
||||
name: "ExitProcess".into()
|
||||
},
|
||||
kind: LibImportKind::Code,
|
||||
},
|
||||
LibImport {
|
||||
dll: "version.dll".into(),
|
||||
export: ImportSymbol::Name {
|
||||
hint: 0,
|
||||
name: "GetFileVersionInfoSizeW".into()
|
||||
},
|
||||
kind: LibImportKind::Code,
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_imports_by_ordinal() {
|
||||
let lib = archive(vec![(
|
||||
"d3d12.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::ORDINAL,
|
||||
42,
|
||||
"Alpha",
|
||||
"d3d12.dll",
|
||||
None,
|
||||
),
|
||||
)]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
assert_eq!(parsed.imports.len(), 1);
|
||||
assert_eq!(parsed.imports[0].dll, "d3d12.dll");
|
||||
assert_eq!(parsed.imports[0].export, ImportSymbol::Ordinal(42));
|
||||
assert_eq!(parsed.imports[0].kind, LibImportKind::Code);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maps_the_import_kinds() {
|
||||
let lib = archive(vec![
|
||||
(
|
||||
"a.dll/",
|
||||
import_object(
|
||||
import_type::DATA,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"tls_index",
|
||||
"a.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"b.dll/",
|
||||
import_object(
|
||||
import_type::CONST,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"limit",
|
||||
"b.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
let kinds: Vec<_> = parsed.imports.iter().map(|i| i.kind).collect();
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
assert_eq!(parsed.imports.len(), 2);
|
||||
assert!(kinds.contains(&LibImportKind::Data));
|
||||
assert!(kinds.contains(&LibImportKind::Const));
|
||||
assert!(!kinds.contains(&LibImportKind::Code));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_name_exported_under_another_name_uses_the_export() {
|
||||
let lib = archive(vec![(
|
||||
"a.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME_EXPORTAS,
|
||||
0,
|
||||
"__imp_local",
|
||||
"a.dll",
|
||||
Some("Remote"),
|
||||
),
|
||||
)]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
assert_eq!(
|
||||
parsed.imports[0].export,
|
||||
ImportSymbol::Name {
|
||||
hint: 0,
|
||||
name: "Remote".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefixed_and_decorated_symbols_are_cleaned_up() {
|
||||
// `NAME_NO_PREFIX` drops a leading `?`, `@` or `_`; `NAME_UNDECORATE`
|
||||
// also stops at the first `@`. Both are what a linker writes for C
|
||||
// symbols, so the map holds the plain export name.
|
||||
let lib = archive(vec![
|
||||
(
|
||||
"a.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME_NO_PREFIX,
|
||||
0,
|
||||
"_Remap",
|
||||
"a.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"b.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME_UNDECORATE,
|
||||
0,
|
||||
"?Thing@@YAHXZ",
|
||||
"b.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"c.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME_NO_PREFIX,
|
||||
0,
|
||||
"plain",
|
||||
"c.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
let names: Vec<_> = parsed
|
||||
.imports
|
||||
.iter()
|
||||
.map(|i| i.export.to_string())
|
||||
.collect();
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
assert_eq!(names, ["Remap", "Thing", "plain"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_same_import_twice_is_listed_once() {
|
||||
let exit_process = || {
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"ExitProcess",
|
||||
"kernel32.dll",
|
||||
None,
|
||||
)
|
||||
};
|
||||
let lib = archive(vec![
|
||||
("kernel32.dll/", exit_process()),
|
||||
("kernel32.dll/", exit_process()),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(parsed.imports.len(), 1);
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_same_symbol_of_a_different_kind_is_kept() {
|
||||
// A data import (`__imp_`) and a code import of one name differ.
|
||||
let lib = archive(vec![
|
||||
(
|
||||
"a.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"table",
|
||||
"a.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"a.dll/",
|
||||
import_object(
|
||||
import_type::DATA,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"table",
|
||||
"a.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(parsed.imports.len(), 2);
|
||||
assert_ne!(parsed.imports[0].kind, parsed.imports[1].kind);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sorted_ordinal_and_named_exports_order_by_dll_then_name() {
|
||||
let lib = archive(vec![
|
||||
(
|
||||
"b.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::ORDINAL,
|
||||
1,
|
||||
"second",
|
||||
"b.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"a.dll/",
|
||||
import_object(import_type::CODE, name_type::NAME, 0, "zeta", "a.dll", None),
|
||||
),
|
||||
(
|
||||
"a.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"alpha",
|
||||
"a.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"a.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::ORDINAL,
|
||||
9,
|
||||
"first",
|
||||
"a.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
let pairs: Vec<_> = parsed
|
||||
.imports
|
||||
.iter()
|
||||
.map(|i| (i.dll.as_str(), i.export.clone()))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
pairs,
|
||||
[
|
||||
(
|
||||
"a.dll",
|
||||
ImportSymbol::Name {
|
||||
hint: 0,
|
||||
name: "alpha".into()
|
||||
}
|
||||
),
|
||||
(
|
||||
"a.dll",
|
||||
ImportSymbol::Name {
|
||||
hint: 0,
|
||||
name: "zeta".into()
|
||||
}
|
||||
),
|
||||
("a.dll", ImportSymbol::Ordinal(9)),
|
||||
("b.dll", ImportSymbol::Ordinal(1)),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_archive_has_no_imports() {
|
||||
let parsed = ImportLibrary::parse(b"!<arch>\n").unwrap();
|
||||
assert!(parsed.imports.is_empty());
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linker_and_names_members_are_skipped_not_counted() {
|
||||
// What a COFF archive looks like: two linker members, an optional
|
||||
// names table, then the import objects.
|
||||
let lib = archive(vec![
|
||||
("/", vec![0; 8]),
|
||||
("/", vec![0; 8]),
|
||||
("//", b"a_long_member_name/\n".to_vec()),
|
||||
(
|
||||
"kernel32.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"ExitProcess",
|
||||
"kernel32.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"user32.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"MessageBoxW",
|
||||
"user32.dll",
|
||||
None,
|
||||
),
|
||||
),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(parsed.imports.len(), 2);
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn members_that_are_not_import_objects_are_counted() {
|
||||
let lib = archive(vec![
|
||||
// A regular COFF object starts with a machine, not a zero `Sig1`.
|
||||
("object.obj/", {
|
||||
let mut object = Vec::new();
|
||||
object.extend_from_slice(&MACHINE_AMD64.to_le_bytes());
|
||||
object.extend_from_slice(&1u16.to_le_bytes()); // NumberOfSections
|
||||
object.extend_from_slice(&0u32.to_le_bytes()); // TimeDateStamp
|
||||
object.resize(60, 0);
|
||||
object
|
||||
}),
|
||||
// A long-format descriptor: no short-format `Sig2`.
|
||||
("long.lib/", {
|
||||
let mut long = import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"Old",
|
||||
"old.dll",
|
||||
None,
|
||||
);
|
||||
long[2..4].copy_from_slice(&0u16.to_le_bytes());
|
||||
long
|
||||
}),
|
||||
// An import header whose string table is cut short.
|
||||
("short.lib/", {
|
||||
let mut short = import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"Cut",
|
||||
"cut.dll",
|
||||
None,
|
||||
);
|
||||
short.truncate(24);
|
||||
short
|
||||
}),
|
||||
(
|
||||
"a.dll/",
|
||||
import_object(import_type::CODE, name_type::NAME, 0, "Kept", "a.dll", None),
|
||||
),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(parsed.other_members, 3);
|
||||
assert_eq!(parsed.imports.len(), 1);
|
||||
assert_eq!(parsed.imports[0].dll, "a.dll");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn odd_sized_members_are_padded_aligned() {
|
||||
// 20-byte header plus 9 bytes of strings: the archive pads one byte,
|
||||
// and the next member must still be found.
|
||||
let odd = import_object(import_type::CODE, name_type::NAME, 0, "f", "ab", None);
|
||||
assert_eq!(odd.len() % 2, 1);
|
||||
let lib = archive(vec![
|
||||
("a.dll/", odd),
|
||||
(
|
||||
"b.dll/",
|
||||
import_object(import_type::CODE, name_type::NAME, 0, "Next", "b.dll", None),
|
||||
),
|
||||
]);
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(parsed.imports.len(), 2);
|
||||
assert_eq!(parsed.other_members, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn names_that_are_not_utf8_are_reported_lossily() {
|
||||
// The symbol name "Café" is stored as UTF-8; replacing its lead byte
|
||||
// leaves a stray continuation byte, so the member holds a name that
|
||||
// is not valid UTF-8.
|
||||
let mut lib = archive(vec![(
|
||||
"x.dll/",
|
||||
import_object(
|
||||
import_type::CODE,
|
||||
name_type::NAME,
|
||||
0,
|
||||
"Caf\u{e9}",
|
||||
"x.dll",
|
||||
None,
|
||||
),
|
||||
)]);
|
||||
let at = lib
|
||||
.windows(2)
|
||||
.position(|w| w == [0xC3, 0xA9])
|
||||
.expect("the name is in the member");
|
||||
lib[at] = 0xFF;
|
||||
let parsed = ImportLibrary::parse(&lib).unwrap();
|
||||
assert_eq!(
|
||||
parsed.imports[0].export,
|
||||
// One replacement character for the invalid byte, one for the
|
||||
// continuation byte that is then unexpected.
|
||||
ImportSymbol::Name {
|
||||
hint: 0,
|
||||
name: "Caf\u{fffd}\u{fffd}".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_input_that_is_not_an_archive() {
|
||||
let cases: [&[u8]; 3] = [
|
||||
b"",
|
||||
b"MZ a PE image, not a library",
|
||||
b"!<arch>", // truncated magic
|
||||
];
|
||||
for data in cases {
|
||||
assert!(
|
||||
matches!(ImportLibrary::parse(data), Err(PeError::Malformed(_))),
|
||||
"{data:?} must be rejected"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_a_truncated_member_header() {
|
||||
let mut data = b"!<arch>\n".to_vec();
|
||||
data.extend_from_slice(&[b'x'; 20]);
|
||||
assert!(matches!(
|
||||
ImportLibrary::parse(&data),
|
||||
Err(PeError::Malformed(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_a_member_whose_data_is_cut_off() {
|
||||
// The header promises 0x100 bytes, the archive holds 4.
|
||||
let mut data = b"!<arch>\n".to_vec();
|
||||
let mut member = archive_member("a.dll/", &[0u8; 0x100]);
|
||||
member.truncate(60 + 4);
|
||||
data.extend_from_slice(&member);
|
||||
assert!(matches!(
|
||||
ImportLibrary::parse(&data),
|
||||
Err(PeError::Malformed(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -101,10 +101,171 @@ pub fn read_u64(data: &[u8], at: usize) -> Option<u64> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Length of a synthetic PE32+ prefix whose `e_lfanew` is 0x40: it ends
|
||||
/// right after the 16th (last) data directory entry.
|
||||
const IMAGE_LEN: usize =
|
||||
0x40 + NT_SIGNATURE_SIZE + FILE_HEADER_SIZE + OPT_DATA_DIRECTORY + 16 * DATA_DIRECTORY_SIZE;
|
||||
|
||||
/// A synthetic PE32+ prefix: DOS header pointing at the NT headers, the
|
||||
/// PE32+ magic, and a full data directory table of 16 entries.
|
||||
fn image() -> Vec<u8> {
|
||||
let mut data = vec![0u8; IMAGE_LEN];
|
||||
data[..2].copy_from_slice(b"MZ");
|
||||
data[DOS_E_LFANEW..DOS_E_LFANEW + 4].copy_from_slice(&0x40u32.to_le_bytes());
|
||||
let opt = optional_header_offset(&data).unwrap();
|
||||
data[opt..opt + 2].copy_from_slice(&0x20Bu16.to_le_bytes());
|
||||
data[opt + OPT_NUMBER_OF_RVA_AND_SIZES..opt + OPT_NUMBER_OF_RVA_AND_SIZES + 4]
|
||||
.copy_from_slice(&16u32.to_le_bytes());
|
||||
data
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_reject_overflowing_offsets() {
|
||||
let data = [0u8; 16];
|
||||
assert_eq!(read_u32(&data, usize::MAX - 1), None);
|
||||
assert_eq!(read_u64(&data, usize::MAX - 2), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_little_endian_values_at_the_edges_of_the_buffer() {
|
||||
let mut data = [0u8; 8];
|
||||
data.copy_from_slice(&0x0123_4567_89AB_CDEFu64.to_le_bytes());
|
||||
assert_eq!(read_u32(&data, 0), Some(0x89AB_CDEF));
|
||||
assert_eq!(read_u32(&data, 4), Some(0x0123_4567));
|
||||
// One byte short of a field is not a field.
|
||||
assert_eq!(read_u32(&data, 5), None);
|
||||
assert_eq!(read_u32(&data, 8), None);
|
||||
assert_eq!(read_u64(&data, 0), Some(0x0123_4567_89AB_CDEF));
|
||||
assert_eq!(read_u64(&data, 1), None);
|
||||
assert_eq!(read_u64(&data, 8), None);
|
||||
assert_eq!(read_u32(b"", 0), None);
|
||||
assert_eq!(read_u64(b"", 0), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_offsets_follow_e_lfanew() {
|
||||
let data = image();
|
||||
assert_eq!(file_header_offset(&data), Some(0x40 + NT_SIGNATURE_SIZE));
|
||||
assert_eq!(
|
||||
optional_header_offset(&data),
|
||||
Some(0x40 + NT_SIGNATURE_SIZE + FILE_HEADER_SIZE)
|
||||
);
|
||||
let fh = file_header_offset(&data).unwrap();
|
||||
let opt = optional_header_offset(&data).unwrap();
|
||||
assert_eq!(opt - fh, FILE_HEADER_SIZE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_offsets_need_the_dos_header() {
|
||||
assert_eq!(optional_header_offset(b""), None);
|
||||
assert_eq!(file_header_offset(b""), None);
|
||||
// `e_lfanew` is the last field the offsets read.
|
||||
assert_eq!(optional_header_offset(&[0u8; DOS_E_LFANEW]), None);
|
||||
let mut data = [0u8; DOS_E_LFANEW + 4];
|
||||
data[DOS_E_LFANEW..].copy_from_slice(&0x40u32.to_le_bytes());
|
||||
assert_eq!(file_header_offset(&data), Some(0x44));
|
||||
assert_eq!(optional_header_offset(&data), Some(0x58));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hostile_e_lfanew_is_passed_on_but_the_readers_refuse_it() {
|
||||
// The offsets are pure arithmetic, so an `e_lfanew` past the end of
|
||||
// the buffer is reported unchanged; every read through it is
|
||||
// bounds-checked. (A value near `u32::MAX` would also overflow the
|
||||
// addition on a 32-bit host, which is why it is a `checked_add`.)
|
||||
let mut data = [0u8; DOS_E_LFANEW + 4];
|
||||
data[DOS_E_LFANEW..].copy_from_slice(&0x10_0000u32.to_le_bytes());
|
||||
assert_eq!(file_header_offset(&data), Some(0x10_0004));
|
||||
assert_eq!(optional_header_offset(&data), Some(0x10_0018));
|
||||
assert_eq!(
|
||||
optional_header_offset(&data).and_then(|at| read_u32(&data, at)),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
file_header_offset(&data).and_then(|at| read_u32(&data, at)),
|
||||
None
|
||||
);
|
||||
assert_eq!(data_directory_offset(&data, dir::IMPORT), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_directory_offsets_are_indexed_from_the_optional_header() {
|
||||
let data = image();
|
||||
let opt = optional_header_offset(&data).unwrap();
|
||||
assert_eq!(
|
||||
data_directory_offset(&data, 0),
|
||||
Some(opt + OPT_DATA_DIRECTORY)
|
||||
);
|
||||
assert_eq!(
|
||||
data_directory_offset(&data, dir::IMPORT),
|
||||
Some(opt + OPT_DATA_DIRECTORY + DATA_DIRECTORY_SIZE)
|
||||
);
|
||||
assert_eq!(
|
||||
data_directory_offset(&data, 15),
|
||||
Some(opt + OPT_DATA_DIRECTORY + 15 * DATA_DIRECTORY_SIZE)
|
||||
);
|
||||
// The table holds 16 entries in this image.
|
||||
assert_eq!(data_directory_offset(&data, 16), None);
|
||||
assert_eq!(data_directory_offset(&data, usize::MAX), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_directory_offset_honours_a_shorter_table() {
|
||||
let mut data = image();
|
||||
let opt = optional_header_offset(&data).unwrap();
|
||||
data[opt + OPT_NUMBER_OF_RVA_AND_SIZES..opt + OPT_NUMBER_OF_RVA_AND_SIZES + 4]
|
||||
.copy_from_slice(&3u32.to_le_bytes());
|
||||
assert!(data_directory_offset(&data, 2).is_some());
|
||||
assert_eq!(data_directory_offset(&data, 3), None);
|
||||
assert_eq!(data_directory_offset(&data, dir::IAT), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_directory_offset_needs_the_count_and_the_header() {
|
||||
let full = image();
|
||||
let opt = optional_header_offset(&full).unwrap();
|
||||
// Ends exactly where `NumberOfRvaAndSizes` starts.
|
||||
let cut = &full[..opt + OPT_NUMBER_OF_RVA_AND_SIZES];
|
||||
assert_eq!(
|
||||
data_directory_offset(cut, dir::IMPORT),
|
||||
None,
|
||||
"a header without the count field has no directories"
|
||||
);
|
||||
assert_eq!(data_directory_offset(b"", dir::IMPORT), None);
|
||||
assert_eq!(data_directory_offset(b"MZ", dir::IMPORT), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_data_directory_entry_reads_back_as_the_image_wrote_it() {
|
||||
let mut data = image();
|
||||
let at = data_directory_offset(&data, dir::IMPORT).unwrap();
|
||||
data[at..at + 4].copy_from_slice(&0x3000u32.to_le_bytes());
|
||||
data[at + 4..at + 8].copy_from_slice(&0x28u32.to_le_bytes());
|
||||
assert_eq!(read_u32(&data, at), Some(0x3000));
|
||||
assert_eq!(read_u32(&data, at + 4), Some(0x28));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constants_match_the_pe32_layout() {
|
||||
// Offsets of fields inside the structures they are named after.
|
||||
assert_eq!(DOS_E_LFANEW, 0x3C);
|
||||
assert_eq!(NT_SIGNATURE_SIZE, 4);
|
||||
assert_eq!(FILE_HEADER_SIZE, 20);
|
||||
assert_eq!(FILE_HEADER_CHARACTERISTICS, FILE_HEADER_SIZE - 2);
|
||||
assert_eq!(OPT_ADDRESS_OF_ENTRY_POINT, 16);
|
||||
assert_eq!(OPT_CHECKSUM, 64);
|
||||
assert_eq!(OPT_DLL_CHARACTERISTICS, 70);
|
||||
assert_eq!(OPT_NUMBER_OF_RVA_AND_SIZES, 108);
|
||||
assert_eq!(OPT_DATA_DIRECTORY, OPT_NUMBER_OF_RVA_AND_SIZES + 4);
|
||||
assert_eq!(DATA_DIRECTORY_SIZE, 8);
|
||||
assert_eq!(TLS64_ADDRESS_OF_CALLBACKS, 24);
|
||||
|
||||
// Data directory indices, which `prepare` patches by number.
|
||||
assert_eq!(dir::IMPORT, 1);
|
||||
assert_eq!(dir::EXCEPTION, 3);
|
||||
assert_eq!(dir::BASERELOC, 5);
|
||||
assert_eq!(dir::LOAD_CONFIG, 10);
|
||||
assert_eq!(dir::BOUND_IMPORT, 11);
|
||||
assert_eq!(dir::IAT, 12);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user