Commit Graph

7 Commits

Author SHA1 Message Date
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