Repository navigation
Record file views mapped at an address the program picks - #163
Conversation
mapViewOfFileInternal only claimed a range in the guest mapping table when it picked the address itself. With a caller-supplied base (MapViewOfFileEx with lpBaseAddress) it went straight to mmap: - On Linux it used MAP_FIXED_NOREPLACE, but free guest memory is normally covered by a PROT_NONE placeholder (VirtualFree(MEM_RELEASE) puts it back), so the call failed with ERROR_INVALID_ADDRESS even when the table showed the range as free. - On macOS there is no MAP_FIXED_NOREPLACE, so it used MAP_FIXED but left the view out of the table, and the range still looked free: a later VirtualAlloc could be given it, and its MAP_FIXED mmap silently replaced the view. Mapping over an existing allocation or view replaced it the same way. The Xbox 360 MSVC linker hits this: it reserves a range, releases it, maps its output file there, and then allocates more memory. Check and claim a caller-supplied range in the guest mapping table, as VirtualAlloc does for a requested address, then map it with MAP_FIXED. Every view is now recorded. UnmapViewOfFile puts the PROT_NONE placeholder back, as VirtualFree does, instead of unmapping, so an unrelated host mmap can't land in the freed range. Add a test for the reserve, release, map, allocate sequence. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
17a6eaa to
f42f316
Compare
|
I'm very confused about how the existing code and what it was meant to do. Is it just broken and nobody noticed until now? (I have vague memories of the memory blocking setup being different before, so maybe it used to work.) Or is it meant to be able to map memory in the upper 2 GB? Can you do some digging with git blame/Discord search? |
MapViewOfFileEx at an address that is still allocated must fail with ERROR_INVALID_ADDRESS and leave the allocation intact, both when the view would cover the whole allocation and when only the end of the requested range is allocated. Without the previous commit, macOS maps the view over the allocation and the test fails. This case comes from the fixed-view test in decompals#146. Co-authored-by: banteg <4562643+banteg@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MapViewOfFileEx with a base above the highest user address (0x7FFEFFFF for a process that isn't large-address-aware) fails on Windows with ERROR_INVALID_PARAMETER, unlike VirtualAlloc, which returns ERROR_INVALID_ADDRESS for the same address. Check for that before claiming the range, and test a base at 0x80000000. Checked on Windows Server 2025 (GitHub Actions windows-latest). Wine doesn't enforce the limit and maps the view, so the new test fails there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The base-address limit check matches Windows, not Wine, which maps the view above 0x7FFEFFFF. Say so next to the check, not only in the test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Had my agents run over everything a number of times to research and triple-check some stuff, and here's what they found. In short:
The agents searched through I've also updated the PR since your comment with some more tests (including one based on a related scenario also noticed in #146) and an additional upper-limit check to more exactly match Windows behavior (as described below). What changed
Why the issue went (partially) unnoticed, and where else it has been encounteredEvery existing wibo test that maps a view lets wibo pick the address, so no test exercises the fixed-base path. It had also only worked for about a month, so little could have come to rely on it. Two other contributors did run into it, but neither of their PRs has been merged:
Mapping above 2 GB: why it wasn't intended, and how Windows and this PR handle itWhy the fixed-base path wasn't meant to map there:
On current My agents checked what Windows does on a GitHub Actions
This PR matches that: a 64 KiB-aligned base address above Wine doesn't enforce Windows' Tested revisionsThe wibo builds below are 32-bit wibo before #124 (June 2026) didn't grow a file to the mapping size, so in old builds, writing to a view of the test's empty temp file raises SIGBUS. For the two 2025 builds (9367f00 and a59e002), my agents used a variant of that test that maps a pagefile-backed section (
On Windows Server 2025, the whole |
simonlindholm
left a comment
There was a problem hiding this comment.
Right, I see, thanks for the thorough investigation!
| } | ||
| // Windows rejects a base past the user address range with ERROR_INVALID_PARAMETER (VirtualAlloc uses | ||
| // ERROR_INVALID_ADDRESS). Wine doesn't enforce the limit and maps the view, so this differs from Wine. | ||
| if (baseAddr > kHighestUserAddress) { |
There was a problem hiding this comment.
while it doesn't really matter: oughtn't this check that the end of the mapping is in bounds rather than the start?
There was a problem hiding this comment.
Yeah, I suppose it conceptually makes sense to check the end as well. In wibo today, a view that runs past the end already fails with ERROR_INVALID_ADDRESS (the same error as on Windows), albeit for incidental reasons:
- On Linux, wibo reserves the guest stack (the Windows program's main-thread stack) as high as it can below 2 GB, so the stack covers
0x7FFE0000. - On macOS, the guest range ends at
0x7E000000, so those addresses are out of range.
So checking the end changes neither whether such a view fails nor which error it gets. But to make the logic more explicit, I've gone ahead and pushed commit 559afe3, which adds an explicit check: a view that covers 0x7FFE0000 will fail with ERROR_INVALID_ADDRESS.
The new check alone would also reject every view the PR's base check does, but it would use the ERROR_INVALID_ADDRESS error code in all cases. Windows, however, uses different errors depending on whether the base itself is past the limit or only the end of the view is (run):
- A view whose base is past
0x7FFEFFFFfails withERROR_INVALID_PARAMETER. The base check added by this PR implements that. - A view whose base is in range but that runs past the end fails with
ERROR_INVALID_ADDRESSinstead. Every such view covers the shared user data page at0x7FFE0000, and views that cover it fail the same way even when they end within the limit (e.g. a 128 KiB view at0x7FFD0000). So it behaves like an ordinary overlap failure.
Thus, I've kept the base check to better match the precise error codes used by Windows.
The testing added by the PR now covers both kinds of failure, and passes on Windows (run), Linux (32- and 64-bit builds) and macOS. Under Wine it still fails at the 0x80000000 case, as before, since Wine doesn't enforce the 0x7FFEFFFF limit and successfully maps a view there instead of refusing it.
There is a remaining caveat, though: the PR's changes only cover MapViewOfFileEx. On Windows, VirtualAlloc at 0x7FFE0000 or 0x7FFF0000 also fails with ERROR_INVALID_ADDRESS. In wibo, VirtualAlloc there currently fails only because of the stack on Linux, or the 0x7E000000 limit on macOS.
A more complete fix would be to mark 0x7FFE0000–0x7FFFFFFF as taken in the guest mapping table at startup on Linux. (Only Linux needs this, since on macOS the guest range already ends below it, at 0x7E000000.) Allocations and views would then both fail there through the usual overlap check, which would make the view check added in 559afe3 redundant, so it could be removed. But the guest stack could then no longer use that range, so wibo would place the stack 128 KiB lower, entirely below 0x7FFE0000. That seemed like it might be scope-creeping the PR a bit, though, so I was planning to leave it for a follow-up PR… unless you'd prefer it here?
There was a problem hiding this comment.
Huh, that's funny, interesting that Windows range-checks like that. No need to follow up on that or send further PRs I think, I doubt there are any applications that rely on the exact error code.
Co-authored-by: Simon Lindholm <simon.lindholm10@gmail.com>
Windows always maps the shared user data page at 0x7FFE0000, so a view that covers it fails with ERROR_INVALID_ADDRESS. That includes every view whose base is at or below 0x7FFEFFFF but that runs past it. A base past 0x7FFEFFFF instead gets ERROR_INVALID_PARAMETER, from the existing check. Such views already failed in practice: on Linux the guest stack is reserved at the top of the range, and on macOS the guest range ends at 0x7E000000. With this check, that no longer matters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
decompals/wibo#163 records views mapped at a fixed address, one of the three fixes link.exe /LTCG needs. It landed after 1.2.0; the other two are still to be proposed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Background
Windows programs can map a file into memory:
MapViewOfFileExmakes the file's contents appear at some address in the program's memory (a "view" of the file). Its last argument,lpBaseAddress, lets the program pick that address itself; if it passesNULL, the system picks one. When the program picks the address, Windows only maps the view there if that memory isn't already in use.wibo keeps the Windows program's memory (the guest memory) in the low 2 GB of its own address space, and manages that range itself. It keeps a table of which parts of the range are in use, the guest mapping table in
src/heap.cpp.VirtualAllocconsults the table to find free memory. Memory that isn't in use may be covered by an inaccessible placeholder mapping (PROT_NONE). On Linux, wibo covers all unused memory in the range with placeholders at startup. On both Linux and macOS,VirtualFreeputs a placeholder back over memory it releases.Problem
mapViewOfFileInternal(indll/kernel32/memoryapi.cpp, behind bothMapViewOfFileandMapViewOfFileEx) only adds a view to the table when wibo picks its address. When the program picks the address, the view is mapped without being recorded. What goes wrong depends on the platform:MapViewOfFileExfails withERROR_INVALID_ADDRESSeven when the table shows that memory as free. wibo maps the view withMAP_FIXED_NOREPLACE, which refuses to map over any existing host mapping, and free memory in the range is normally covered by a placeholder (always, in the linker's case below).MAP_FIXED_NOREPLACE, so wibo usesMAP_FIXED, which maps over whatever is there.MapViewOfFileExsucceeds, but the table still shows that memory as free. A laterVirtualAlloccan be given the same addresses, and its mmap silently replaces the view. Mapping a view on top of an existing allocation or view also silently replaces it, where Windows would refuse.I ran into this with the Xbox 360 linker,
link.exe10.00.10224 from "Visual Studio 2010 for Xbox 360", when linking with/LTCGand/EXPORT. It's in theX360/16.00.10224.00folder of decomp.dev's compiler bundle,compilers_20260305.zip; the folder is named after the compiler's version. The linker reserves some memory withVirtualAlloc, releases it, maps an output file at that address withMapViewOfFileEx, and then allocates more memory. On Linux theMapViewOfFileExcall fails. On macOS the later allocation lands on top of the output file's view and the link crashes.Fix
wibo::heap::reserveViewRangeAtchecks the address range the program asked for against the table and, if it's free, records it there as in use (claims it), the same wayVirtualAllochandles a requested address. The view is then mapped into that range withMAP_FIXED, replacing the placeholder if there is one; this is safe because the table has already confirmed the range is free.ERROR_INVALID_ADDRESS, as it does on Windows and under Wine.VirtualAlloccan no longer hand out memory that a view occupies.UnmapViewOfFilenow puts the placeholder back, asVirtualFreedoes, instead of callingmunmap. wibo does the same if mapping fails after the range was claimed. That way the freed range never becomes a hole where an unrelated mmap, by wibo itself or a library it loads, could land. This applies to every view, not only those at an address the program picked.0x7FFEFFFF, for a program that isn't large-address-aware) now fails withERROR_INVALID_PARAMETER, which is what Windows returns forMapViewOfFileEx(VirtualAllocreturnsERROR_INVALID_ADDRESSfor the same address). Before, this branch had no upper limit, and since the upper 2 GB is no longer blocked, it would try to map there.0x7FFE0000now fails withERROR_INVALID_ADDRESS, as on Windows, which always maps the shared user data page there. This includes every view whose base is in range but that runs past0x7FFEFFFF. Such views already failed in wibo with that error, because on Linux the guest stack is reserved there and on macOS the guest range ends at0x7E000000; the check makes this independent of where the stack lands.Testing
There are three new tests, all added to the existing
test_createfilemappingfixture (test/test_createfilemapping.c).test_fixed_address_view_is_not_reallocatedreproduces the linker's sequence: reserve memory withVirtualAlloc, release it, and map a file view at that address withMapViewOfFileEx. It then checks that:VirtualAlloc(NULL, ...)returns memory outside the view, and the view's contents survive.VirtualAllocandMapViewOfFileExat the view's address fail withERROR_INVALID_ADDRESS.UnmapViewOfFile, that address can be reserved again.test_fixed_address_view_does_not_replace_allocationchecks thatMapViewOfFileExat an address that is still allocated fails withERROR_INVALID_ADDRESSand leaves the allocation's contents intact. It checks both a view covering the whole allocation and a view whose start is free but whose end overlaps an allocation. This case comes from the test in #146, and its author is credited as a co-author of that commit.test_fixed_address_view_at_top_of_user_range_failschecks thatMapViewOfFileExfails withERROR_INVALID_PARAMETERfor a base past0x7FFEFFFF(0x7FFF0000and0x80000000), and withERROR_INVALID_ADDRESSfor a view that covers0x7FFE0000(64 KiB and 64 KiB + 1 byte at0x7FFE0000, and 128 KiB at0x7FFD0000).Windows Server 2025 (a GitHub Actions
windows-latestrunner, running the 32-bit test program built with mingw): the wholetest_createfilemappingfixture passes, including all three new tests (run). Small probe programs show that, for a program that isn't large-address-aware:MapViewOfFileExat0x7FFF0000,0x80000000or0x90000000fails withERROR_INVALID_PARAMETER, whileVirtualAllocat0x80000000fails withERROR_INVALID_ADDRESS(run).0x7FFE0000, and any view or allocation that covers it fails withERROR_INVALID_ADDRESS, including views that end at or below0x7FFEFFFF, such as 128 KiB at0x7FFD0000(run).Wine 9.0 (x86_64 Ubuntu 24.04 VM): the first two tests pass. The third fails, because Wine doesn't (yet in a stable release) enforce the 2 GB limit: it maps the test's view at
0x80000000(and, in a separate probe, letsVirtualAllocallocate there), although itsGetSystemInforeports0x7FFEFFFFas the highest application address, as Windows does.Local wibo builds:
debug(32-bit) anddebug64(64-bit) presets, same VMMapViewOfFileExfails withERROR_INVALID_ADDRESS(run on its own, the second passes, since each of its fixed-address views lands on a placeholder or a live allocation)ctestpasses, 55/55 for each presetVirtualAllocreturns the view's address, andMapViewOfFileExover a live allocation succeedstest_createfilemappingfixture passes, including all three new tests (full suite: see CI)CI (this repo's workflow, run on my fork): in the run with the fix, the new tests pass in every job that runs the test suite, including all three macOS builds. The two
Static, 64-bitjobs run no tests, onmaintoo. The only failure istest_namedpipe(test_namedpipe.c:64) inBuild Debug (32-bit)andBuild Release (32-bit). It fails the same way in a run of unmodifiedmain(f3570ca), so this PR didn't cause it.test_namedpipepasses in my local 32-bit Linuxdebugbuild, so the failure looks specific to the CI runner. (Themainrun shows commit0d36348rather thanf3570ca, because I pushed a copy of it, with identical files, to a temporary branch to start it.)Related PRs
MAP_FIXEDand records the view in the table. In review, a maintainer asked for Add Xbox 360 linker support (fake mspdb PDB interface) #110 to be split into smaller PRs. This PR was written separately and makes that fix on its own. It also goes further: it refuses to map over memory that's already in use (MAP_FIXEDalone silently replaces it), and it puts the placeholder back when a view is unmapped.MAP_FIXED_NOREPLACE, so on Linux the placeholder still makesMapViewOfFileExfail: with fix: support VC6 precompiled header creation and reuse #146'smemoryapi.cppchange applied tomain, both its own test and this PR's fail on Linux. fix: support VC6 precompiled header creation and reuse #146's test passes with this PR, and the second test here adds the case from it that this PR's first test didn't cover.mapViewOfFileInternalandtest/test_createfilemapping.c. The two don't depend on each other, but whichever lands second needs a small rebase.LLM disclosure
This PR was made with an LLM, Claude Opus 5.5 in Claude Code. It tracked down the bug, wrote the fix and the tests, ran the tests on Wine, Linux, macOS and Windows, and drafted this description. A second Claude agent reviewed the change, and its critiques were addressed. I directed the work and reviewed this description.
🤖 Generated with Claude Code