PE/loader tests: hermetic synthetic images for ImageInfo, ImportLibrary and prepare #287
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!287
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/287/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@SkippySteve on GitHub · 2026-11-24 02:03 UTC · original
SkippySteve/victory-pr-own-next15→main· +6158 −30 · 5 files · 56 commits · closed without mergingSeveral
victory-peandvictory-loaderunit tests parse the test binary itself. That binary is a PE image only on Windows, so on Linux they fail withUnknown file magic, and #235 limits them to Windows. This PR adds hermetic tests that run on every host. Each one builds its PE image or import library byte by byte in memory, with no game data, fixtures or files on disk. It changes no library code and doesn't touch the existing tests.This PR combines four earlier PRs: #287 (loader
prepare), #257 (ImageInfo), #268 (ImportLibrary) and #344 (a relocation fix inprepare). It has one commit for each, plus three that widen theImageInfotests, one that widens theImportLibrarytests and four that widen thepreparetests. #344 is the only library change.victory-loader:crates/victory-loader/tests/synthetic_prepare.rs(43 tests)The tests build a PE32+ executable in memory. It has one section, a
KERNEL32.dllimport by name and by ordinal, a TLS directory with one callback, DIR64 base relocations for the TLS pointers, a bound-import directory entry, a checksum, andFORCE_INTEGRITYset. They check that:infodescribes the original image correctly: it is not a DLL, and the entry point, import IAT slots and TLS callbacks match.IMAGE_FILE_DLLset, entry point 0 and checksum 0;FORCE_INTEGRITYcleared, with the other DLL flags unchanged;IMAGE_REL_BASED_ABSOLUTE.keeps_iat_slots_when_import_size_excludes_the_terminator.kernel32.dllwith no name table), each is read as its own module with its IAT slots, and the import directory points at the terminator 60 bytes in, not a fixed 20.file_name()is a stablevictory-image-<16 hex of BLAKE3>.dll.DYNAMIC_BASEimages), the cleared TLS callbacks pointer stays 0 while the TLS index pointer and the callback array move. Without the no-op relocation the same rebase would leavedeltathere.HIGHLOWentry for the same RVA and the other DIR64 entries are left alone.preparestill clears the callbacks pointer and rewrites no relocation entry..relocsection of their own (a different file offset from.data), the callbacks pointer's entry is found through the section table; a block for another page with an entry at the same page offset is left alone, as is a stale copy of the old block in.data, and a rebase keeps the pointer at 0.0x00and to0xFFin turn, returns a result without panicking; a patched copy keeps the original's length and hash.victory-pe:crates/victory-pe/tests/synthetic_image.rs(144 tests)The tests build a PE32+ executable in memory with one section, a
KERNEL32.dllimport by name (with a hint) and by ordinal, and two TLS callbacks. They check:rva_to_file_offset:SizeOfImage, including at the 4 GiB limit, isMalformed.Unsupported, and truncated input returns an error without panicking.Unsupported.ImportSymboldisplays as its name or#<ordinal>..data, virtual size 0),rva_to_file_offsetmaps each RVA through the section that holds it, uses the raw size as the size in memory, and maps neither the gap between the sections nor the end of.data.OriginalFirstThunkis 0, as old linkers leave it, import names are read from the IAT, with the same slots.Malformed.Each of these tests adds one optional directory to the same image, and only that test uses it:
USER32.dll!MessageBoxWis read with its hint and IAT slot, separately from the ordinary imports, and by ordinal (#9) when its name-table entry is an ordinal. A delay IAT that runs pastSizeOfImage, including at the 4 GiB limit, isMalformed.NTDLL.RtlFoo) and by ordinal (NTDLL.#7). Forwarders have no RVA. With one name fewer, the local export is read by ordinal only, with no name.symbol_key()gives the symbol-server key (the first three GUID fields are little-endian; the age is unpadded hex). A record that isn't RSDS (NB10) is ignored, and an RSDS record in a later debug entry is still found. With two RSDS records the first is reported. A record whoseSizeOfDatais too short for the signature, GUID or age is malformed, while a short non-CodeView entry pointing past the file is never read.has_relocations,has_exception_tableandhas_load_configfollow their directory entries.RELOCS_STRIPPEDoverrides a leftover relocation entry, and an entry with size 0 counts as absent.other.AddressOfIndexofu64::MAX), a delay descriptor's module handle, bound IAT, unload IAT and timestamp (all RVAs pastSizeOfImage), the export directory's characteristics, timestamp and version, and the CodeView debug entry's characteristics, timestamp and version to garbage changes nothingImageInforeports. The same holds for the DOS header bytes between the magic ande_lfanew, a COFF symbol table pointer past the file with a huge symbol count, the optional-header fieldsImageInfodoesn't keep (linker, OS, image and subsystem versions, code and data sizes, base of code,Win32VersionValue, checksum, heap sizes, loader flags), a section's relocation and line-number fields, and an import descriptor's timestamp and forwarder chain. Filling the unused bytes of.textwith0xEEchanges nothing either, and parsing the same bytes twice (or cloning the result) gives the sameImageInfo.layouthelpers: the file header, optional header and data directory offsets match the image. An index at or pastNumberOfRvaAndSizes, a truncated DOS header and ane_lfanewofu32::MAXall returnNone.victory-pe:crates/victory-pe/src/implib.rs(4 unit tests)implib.rs(the reader behindvictory lib-exports) had no tests. The new tests build an in-memory!<arch>archive of short-format import objects. They check:_Prefixed→Prefixed), undecorated (_Stdcall@8→Stdcall) and by ordinal.other_members.lib.exewrites, with two/linker members, a//long-names member and imports under/<n>names. The imports are read, and the linker and long-names members aren't counted as imports or inother_members.SizeOfDataruns past the member, or one cut short inside its header, is counted inother_members.victory-loader: a relocation past the top of the address space is not wrapped (was #344)prepare()walks the base relocations to find the one that patches the TLS callback field. It added each DIR64 entry's page offset to the block's page RVA unchecked: a damaged block nearu32::MAXpanicked with an overflow in debug builds and, in release, wrapped to a low RVA that could match the field and zero an unrelated relocation entry.Targets that do not fit in an RVA are now left out, the TLS field's own RVA is computed with
checked_add, and the block walk is split intodir64_entriesover plain bytes. Two new unit tests (dir64_entries_walk_blocks_and_skip_other_types,dir64_entries_drop_targets_past_the_top_of_the_address_space) build relocation blocks in memory and pass on Linux.Testing
I ran these on Arch Linux (x86_64-unknown-linux-gnu), on top of current
main:cargo test -p victory-loader --test synthetic_prepare: 43 passed.cargo test -p victory-pe --test synthetic_image: 135 passed.cargo test -p victory-pe --lib implib: 34 passed.cargo clippy -p victory-pe -p victory-loader --all-targets -- -D warnings: clean.rustfmt --checkon the three touched files: clean.cargo test -p victory-loader --lib: the 2 new relocation tests pass; the 3 existingprepare::teststhat parse the test binary still fail on Linux, as onmain(#235).cargo xtask policy: ok.To check that the tests catch regressions, I broke checks in
prepare.rs(12 ways; matching relocations by page offset alone or reading the relocation directory as if it were in the first section fails only the.reloctest, writing the import slot when its RVA is 0 fails the no-RVA test; dropping the relocation no-op fails 3 tests, including the rebase one, placing the import terminator a fixed 20 bytes in fails only the three-descriptor test, writing the bound-import slot regardless ofNumberOfRvaAndSizesfails the short-header test, and stopping after the first callback relocation fails the duplicate-relocation test) and inimage.rs(13 ways), one at a time. Each change made a test fail.TLS callbacks behind a damaged section table. A section with no virtual size, listed before the real one and over the same RVAs, makes
object(which skips it) andImageInfo::rva_to_file_offset(which maps it by its raw size) find the TLS directory in different places.preparethen cleared 8 bytes of the headers, left the real callback pointer set, and returnedOk: the OS would have run the callbacks before any import was bound.preparenow re-parses the patched copy and returnsPrepareError::Patchif the callback pointer is still set (a_section_table_that_hides_the_tls_directory_is_an_error; without the fix the copy keeps0x140001140).Data in a second section. In the other tests, all import, TLS and export data sits in
.text. New tests add a.datasection whose file offset is not the.textshift, and fill the file with0xEEwhere that shift would point. Imports, delay imports and TLS are read at.data's own file offset. A TLS directory in one section can point at a callback array in the other, and reading the array stops at the end of.data's raw data. Listing.datafirst in the section table changes only the order ofsections.objectreads DLL names, name tables and hint/name entries only from the section that holds the import (or delay-import) directory: a descriptor that points into.textfor any of them isMalformed, even though the same bytes are there. Only the IAT may be in another section. Ifrva_to_file_offsetchecks only the first section, 4 tests fail, including the new import and table-order tests.Import library names. New
implibtests coverIMPORT_OBJECT_NAME_EXPORTAS(the third string is the export name; a member without it is counted as another member), undefined import type 3 and name types 5 to 7 (counted, with the members around them still read), and prefix stripping (NO_PREFIXdrops a single?,@or_, andUNDECORATEalso drops everything from the next@).On Linux,
mainstill has 7 failing tests: 4 inimage::testsand 3 inprepare::tests, all of which parse the test binary itself. #235 handles those. I haven't run the new tests on Windows. They use only the public API and don't depend on the host.@jeremygold02 on GitHub · 2026-11-24 04:16 UTC · original · edited 2026-11-24 04:31 UTC
Tested the current open PR at head
d6f7fd2616fd8bf592c876b61d6851ae12323abc(head and discussion refreshed before this edit).cargo test --locked -p victory-loader --lib relocation— passed 4 tests:a_relocation_overlaps_the_bytes_it_writes,relocation_entries_know_each_width_and_the_highadj_parameter,relocation_entries_walk_blocks_and_skip_padding, andrelocation_entries_drop_targets_past_the_top_of_the_address_space.cargo test --locked -p victory-loader --test synthetic_prepare— passed 28 tests, including HIGHADJ, 32-bit callback-field relocations, and refusal of relocation/TLS RVAs outside valid headers/sections.cargo test --locked -p victory-pe --test synthetic_image— passed 41 tests.The branch advanced from
b95118f43883654db068e028a35e1ae3fbabe6a1after the earlier 22/37 synthetic test pass; the results above are a fresh run on the current head. The earlierdir64_entries_filters (invictory-pe, then invictory-loader) each matched zero tests after the helper was renamed torelocation_entries; the loader relocation tests above provide the actual coverage. These tests use synthetic PE data only.Code: prepare.rs, synthetic_prepare.rs, synthetic_image.rs.
Pull request closed