Core: Fix file position after read-only file mmaps (#4442)

* Fix file position after file mmaps

* Oops
This commit is contained in:
Stephen Miller 2026-05-17 14:29:13 -05:00 committed by GitHub
parent 482d17c6e1
commit c7686e33a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -241,19 +241,28 @@ struct AddressSpace::Impl {
if (phys_addr != -1) {
HANDLE backing = fd != -1 ? reinterpret_cast<HANDLE>(fd) : backing_handle;
if (fd != -1 && prot == PAGE_READONLY) {
// Allocate the memory for the mapping
DWORD resultvar;
ptr = VirtualAlloc2(process, reinterpret_cast<PVOID>(virtual_addr), size,
MEM_RESERVE | MEM_COMMIT | MEM_REPLACE_PLACEHOLDER,
PAGE_READWRITE, nullptr, 0);
// phys_addr serves as an offset for file mmaps.
// Create an OVERLAPPED with the offset, then supply that to ReadFile
// Use ReadFile to read file contents into the memory area.
// Create an OVERLAPPED with the file offset, then supply that to ReadFile
OVERLAPPED param{};
// Offset is the least-significant 32 bits, OffsetHigh is the most-significant.
param.Offset = phys_addr & 0xffffffffull;
param.OffsetHigh = (phys_addr & 0xffffffff00000000ull) >> 32;
bool ret = ReadFile(backing, ptr, size, &resultvar, &param);
ASSERT_MSG(ret, "ReadFile failed. {}", Common::GetLastErrorMsg());
// ReadFile moves the file pointer, restore it with SetFilePointer
s64 size_to_move = -size;
LONG size_low = size_to_move & 0xffffffffull;
LONG size_high = (size_to_move & 0xffffffff00000000ull) >> 32;
ret = SetFilePointer(backing, size_low, &size_high, FILE_CURRENT);
// Protect the memory area appropriately
ret = VirtualProtect(ptr, size, prot, &resultvar);
ASSERT_MSG(ret, "VirtualProtect failed. {}", Common::GetLastErrorMsg());
} else {