Walk import descriptors to the terminator in pe_map, as Windows does #84
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/85"
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?
Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T15:41:15Z.
Change
pe_map_imageused the import directorySizeto decide whether an image has imports and to bound the descriptor walk. The Windows loader finds the table by its address and walks to the null descriptor, so copies of a source-built fixture withSize0, 19 or 20 still load and bind every slot.pe_maprejected the first two ("import directory smaller than one descriptor") and left the slots of a descriptor pastSizeunbound.pe_mapnow treats a directory with an address as present and walks it to the null descriptor, bounds-checking each descriptor against the image. IAT writes may not overlap the walked table (terminator included). An import directory that runs past the image is still rejected. Refs #46.Verification
Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, at
4cac5e6:The full
ctest --preset dev --no-tests=errorpassed 78/78 (bvh_wave64_hardwareandbvh_wave64_warpskipped, lane range 32..32), includingcpu_native_seh,cpu_native_sectionandcpu_native_tls.With the previous
pe_map, the new check fails withSize 0: unexpected import requests []: no import is requested and both slots keep their hint/name RVAs, while Windows binds both.Evidence and follow-up
tests/cpu/two_imports.asmis new source (MIT): two jump stubs that importmix_argsfromcpu_import_providerandprobefromcpu_register_probe. It has no entry point, and the test only reads the two IAT slots afterLoadLibraryExWorpe_map; nothing in the image runs. The test patches the import directorySizeof a temporary copy to 60 (as built), 0, 19 and 20 (ending after the first descriptor), and requires each slot to hold the export of its provider andpe_mapto request exactly the two imports.The size-19 rejection control in
check_rejectionsis dropped, because Windows binds that copy. The entry-point control from #78 and the outside-image control are unchanged. Thanks to @Leyukaka for theLoadLibraryExWmeasurements on build 26300, which this matches on 26200.The TLS and delay-import directory checks still test
Size; #103 covers them. Docs:docs/experiments.md#mapping-an-image-without-loadlibrary.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T15:46:39Z.
Thanks for the import-directory checks. #78 also touches the mapper and native-map tests. Please keep this draft until the branches are reconciled, then update the verification section with the Windows result.
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T15:55:19Z.
Thanks. I wrote
pe_map, so I checked the Windows side your verification section leaves open. I patched only the import-directorySizefield ofcpu_import_original.dll(import directory at RVA0x22b4, Size 40, one descriptor plus terminator, IAT slot at RVA0x2000). Each copy was loaded withLoadLibraryExW(path, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS), withcpu_import_provider.dllnext to it. I only read the IAT slot afterwards; nothing in the image was called. Windows 11 build 26300:LoadLibraryExWmix_argsfrom the providermix_argsfrom the providermix_argsfrom the providermix_argsfrom the providerThis confirms your point that the loader does not use
Sizefor the import walk: the zero-size copy is bound normally. It also means the fix in this PR is stricter than Windows rather than matching it. The Size 0 and Size 19 copies, which Windows loads and binds, both fail here with "import directory smaller than one descriptor". The Size 19 rejection was added in #49 at the maintainer's request.I didn't measure the two-DLL case, where a second live descriptor lies past
Size.There seem to be two consistent options, and I think it's the maintainer's call:
Size, walk fromVirtualAddressto the null descriptor with each descriptor bounds-checked againstSizeOfImage, and bind everything found. The Size 0/19/20 copies and your hidden-descriptor case would then map with every slot bound.docs/experiments.md, for example: "pe_map rejects an import directory whose Size is zero or stops before a live descriptor; Windows ignores Size and binds them."A merge note: this PR and #78 both edit the entry-point control and the summary
printincheck_rejections(tests/cpu/native_map_check.py). #78 changes the control to "entry point outside executable section", because entry points are now called. They also edit the same paragraph of thepe_mapsection. Whichever lands second will need a small rebase, and I'm happy to do it on #78's side. If it helps, I can also run the Windowsctestline that your verification section leaves open.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T16:23:01Z.
Thanks for measuring the Windows behavior. Please use the Windows-compatible interpretation for ordinary imports, with independent bounds checks. Please include a source-built two-provider comparison that confirms both slots are bound when the listed size ends early, and reconcile the existing size-19 expectation and #78 tests before requesting another review.
Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T16:54:28Z.
Updated (
4cac5e6, rebased on current main with #78):pe_mapnow uses the Windows-compatible interpretation for ordinary imports: the import table is found by address and walked to the null descriptor, with each descriptor bounds-checked against the image independently.Sizeis no longer used, and IAT writes may not overlap the walked table.cpu_two_imports(mix_argsfromcpu_import_provider,probefromcpu_register_probe).cpu_native_mappatches its import directorySizeto 60, 0, 19 and 20 and requires both slots to be bound underLoadLibraryExWand underpe_map. On Windows 11 26200.9974 both bind both slots in all four cases; the previouspe_mapfails at Size 0 with no import requested.Full
ctest --preset dev --no-tests=erroron this head: 78/78 passed (two wave64 skips). The PR title and description are updated to match.