Commit Graph

19 Commits

Author SHA1 Message Date
83d973a424 pe_loader: read the TLS directory through directory(), honouring NumberOfRvaAndSizes
Every data directory the loader reads goes through directory(), which
returns an empty entry past OptionalHeader.NumberOfRvaAndSizes (PE/COFF: those
slots are not directories, whatever bytes they hold) - except the TLS one,
which read DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] directly. An image
declaring fewer than 10 directories with leftover bytes in slot 9 then failed
to load ("the TLS directory runs past the image") or, with plausible bytes,
had a TLS directory the Windows loader would ignore.

loader_test: with NumberOfRvaAndSizes = 9 and junk in the TLS slot the image
loads (it was rejected before); with all 16 the same slot is still checked
and refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:43:32 +00:00
4bc2fd2870 Merge pull request #1973 from Dominik 2026-11-22 10:39:59 -05:00
56fd84b274 pe_loader: a forwarded CRT export binds the host copy, as a direct import does
resolve_forwarder checked prefer_host_copy() on the bare forwarder module
name, but forwarder strings normally leave out ".dll" ("MSVCP140.?foo")
and the host-copy list is named with it. So a bundled module forwarding to
one of the CRT DLLs the package ships (vcruntime140, msvcp140, concrt140,
...) mapped the Xbox build of that DLL from the game folder instead of the
host's, unlike a direct import of the same symbol. It now uses shim_key,
the name with ".dll" the function already builds for the shim lookup.

loader_test: a fake bundled vcruntime140.dll plus a provider forwarding
"vcruntime140.memset" binds to the host memset and never maps the fake
(fails before the change: the import ends unimplemented).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 15:26:25 +00:00
b805f191da pe_loader: refuse an export whose address is outside the image
parse_exports range-checks the export directory, its three tables and every
name, but stored each code export as base + rva without checking rva. An
address-table entry at or past SizeOfImage (and outside the export
directory, so not a forwarder) became a pointer past the mapping, which an
import or GetProcAddress through the shims would hand out and a caller would
jump to. The load now fails with the export's name, like every other RVA the
loader follows.

loader_test: an export at SizeOfImage is rejected (it loaded before this
change); one at the image's last byte still loads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 15:26:21 +00:00
eda43133f8 Merge pull request #1172 from farhat 2026-11-22 03:14:21 -05:00
cc76514229 fix(runtime): detach initialised PE modules before unmapping them
Deliver TLS and DllMain process-detach notifications in reverse dependency order while every mapped image and TLS block is still live. This lets mapped DLLs unregister FLS callbacks before their code is freed. Cover failed attach, map-only loads, partial initialisation, teardown ordering and malformed TLS tables with synthetic PE tests.
2026-11-22 14:56:50 +07:00
236b0acec3 pe_loader: honour NumberOfRvaAndSizes, and refuse a forwarder ordinal past 65535
Data directories past NumberOfRvaAndSizes were read anyway (only the
exception directory checked the count), so stale bytes there were parsed
as import, delay-import, export or relocation tables. And resolve_forwarder
range-checked the ordinal before adding each digit, so '#65537' passed and
was cast to ordinal 1. The hand-built test images now declare their 16
directories.
2026-11-22 12:48:35 +05:30
6ba626606b tests: loader's scan probe and the D3D12 host comparisons under Wine
loader_test: the probe for security software that holds newly written
images also stops when writing and reading one back averages over
25 ms. Under Wine there is no such software and its file I/O alone took
38-40 ms in the wine job, so there only the refusals and contents are
checked.

The host comparisons move from d3d12_vulkan_device_test's
create_host_device to copycmp::create_host, so the copy comparisons are
skipped under Wine too (with them, winevulkan asserted in
vkBindBufferMemory).
2026-11-22 03:55:16 +00:00
f6b3d3a24c Loader: reject a delay-import descriptor with no name table
An IAT without an INT still holds delay thunks, not name RVAs, so eager binding has no symbol to resolve. Reject the form with a clear error. A scan of the installed x64 System32 tree found 4,620 DLLs and 17,558 delay descriptors, none with a missing INT or without dlattrRva. The test now expects the rejection.
2026-11-21 15:51:43 -08:00
ed6b5236e5 Merge pull request #348 from traeb0n 2026-11-21 18:37:58 -05:00
993a2cbf72 Runtime: install static TLS for mapped images 2026-11-21 23:29:24 +00:00
d3a02408ff Tests: loader_test on machines whose security software holds new PE files
On Windows with Trend Micro Maximum Security (Defender and Smart App Control
off), images the test writes to %TEMP% can be written but not opened for a
while: each takes 50-800 ms, the first case failed, the truncation case
stalled for about ten minutes, and an uncaught write error then ended the
process with 0xc0000409. Held images from one run also made the next fail.

- Each run writes to leonida_loader_test\<process id>, so the previous run's
  held images cannot collide; LEONIDA_TEST_DIR picks the parent folder.
- A start-up check writes and reads back 8 images and stops with the reason
  and the folder to exclude if one is refused or they average over 25 ms.
- A failed write ends the run as one FAIL instead of std::terminate.
- A run folder that cannot be removed is reported as a note.

The loader and its 48 checks are unchanged. ctest 15/15 on Windows 11 with
the antivirus active. AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 17:11:10 -06:00
4d010bf4bd Merge pull request #176 from Spaghetih 2026-11-21 17:38:46 -05:00
27d1b11b64 Runtime: register each mapped image's x64 unwind table (.pdata) with RtlAddFunctionTable
Images the loader maps by hand are unknown to the system unwinder, so an exception raised in or passing through one couldn't be dispatched and killed the process. load() now validates the exception directory (entries in bounds, sorted, non-overlapping; zero-length entries allowed, as in System32's symcryptk.dll) and registers it; the loader removes it before freeing the image. Rules measured over 3,780 x64 System32 images (5,023,071 entries) and the 16 package DLLs.

Tests: loader_test covers registration, removal and six corrupt tables on hand-built images; pe_loader_test checks a fixture SEH fault is caught by the image's own handler (fails without registration) and, with LEONIDA_GAME_DIR, every package DLL's table.

AI-assisted.
2026-11-21 23:18:59 +01:00
8d8ebee384 Loader: resolve forwarders to shimmed modules through the shim registry
A forwarder target is resolved through game_dir and System32, but not through shims::find, so a forwarder to xmem or d3d12_xs became an unimplemented thunk. Check the normalized module name against the shim registry first, as a direct import already does. A synthetic shim table and a forwarder-to-shim test cover it.
2026-11-21 13:20:30 -08:00
530246a6e7 tests: loader_test fills its byte buffer with uint8_t{0} (C4244 x2 under /W4)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 15:55:49 -05:00
ff573ee55e loader: follow forwarded exports and validate delay-import descriptors
parse_exports stored base + RVA for every export, but an export whose RVA lands inside the export
directory is a forwarder string ("KERNEL32.Sleep"), not code, so an import of one received a pointer
into the string. Forwarders are now recorded separately and followed when an import asks for one:
bundled modules first, then LoadLibraryEx/GetProcAddress for system DLLs, with a depth cap so two
modules re-exporting each other stop instead of recursing. An unresolvable forwarder records the
import as unimplemented.

The delay-import walk also assumed every descriptor is RVA-based and has a name table. A descriptor
without dlattrRva stores 32-bit virtual addresses, which cannot address an x64 image base, so it is
rejected with a message instead of being misread. A missing address table is rejected, and a missing
name table now reads its entries back through the address table.
2026-11-21 19:56:27 +00:00
126062ae4a Runtime: initialise a loaded module's TLS and DllMain
Mapping an image never ran anything. TLS callbacks and DllMain were documented as the caller's, and
nothing called them, so a module that needs either could not be brought up at all.

Loader::initialise() now allocates the module's TLS slot, copies the template and SizeOfZeroFill into
it, writes the slot index into the image, runs the TLS callbacks with DLL_PROCESS_ATTACH, then calls
the entry point of a DLL as DllMain. Every address it follows is bounded like the rest of the loader:
the directory, the template, the index, the callback table and each callback, with a 1 MiB ceiling on
the block. Only DLLs get DllMain; an EXE's entry point has a different signature and stays the
caller's to run, and --dry-run still runs nothing.

The tests build an image whose TLS callback and DllMain are real machine-code stubs that store a
sentinel, so they check the code actually executed rather than merely being parsed.
2026-11-21 19:00:06 +00:00
47dec11677 Runtime: bound every RVA the PE loader follows, and test it
The loader trusted the image it was handed: e_lfanew was dereferenced without
checking it against the file size, section copies were bounded by the file but
not by SizeOfImage, and the relocation, export and import walks put no limit on
where an image's own RVAs sent them. A truncated or corrupt image could read or
write outside the mapping the loader had just allocated, and a PE32 image was
read through a PE32+ header instead of being rejected.

Every RVA is now checked first, and a bad one fails with the file's name and
what did not fit. loader_test builds its own images, so ctest covers the good
and the corrupt cases without needing any game files.
2026-11-21 18:32:22 +00:00