upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/dstorage.cpp
diff options
context:
space:
mode:
authorTyrannys <80750971+Tyrannys@users.noreply.github.com>2026-11-24 01:40:07 -0600
committerTyrannys <80750971+Tyrannys@users.noreply.github.com>2026-11-24 01:50:32 -0600
commit779f901084774418f1f83d55c6d7fa075e71e98b (patch)
tree62e0fa043dafc99298230db3d3007e6507a9c819 /runtime/shims/dstorage.cpp
parent42987826128a8612f4e01d8c2beaf0ff2814410c (diff)
fs, dstorage: a ReadFile into first-touch title memory commits the pages and reads again (RT-READFILE-NOACCESS)
xmem maps the title's physical pages as views of a reserve-only section and commits each page in a vectored handler when user-mode code first faults on it. A kernel write raises no such fault: ReadFile into a page nobody touched yet fails with ERROR_NOACCESS (998). The retail title's file reader treats any error but 6, 0x26 and 0x57 as transient and calls Sleep(1) and ReadFile again forever, so the boot never leaves its first frame loop; DirectStorage reads into the same memory came back as E_DSTORAGE_ACCESS_VIOLATION and the title stopped on an int3. The ReadFile import of the file shims, and DirectStorage's read_file_range, now read one byte of every page of the destination from user mode on ERROR_NOACCESS (the handler commits them) and read again once. file_system_test checks it on a reserve-only section with a first-touch handler: the host ReadFile fails with ERROR_NOACCESS, the shimmed one reads every byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/dstorage.cpp')
-rw-r--r--runtime/shims/dstorage.cpp24
1 files changed, 23 insertions, 1 deletions
diff --git a/runtime/shims/dstorage.cpp b/runtime/shims/dstorage.cpp
index 81abaea3d..0f8345689 100644
--- a/runtime/shims/dstorage.cpp
+++ b/runtime/shims/dstorage.cpp
@@ -409,6 +409,19 @@ bool guarded_move(void* destination, const void* source, size_t size) {
409 } 409 }
410} 410}
411 411
412// Reads one byte of every 4 KiB page of the range from user mode. Title memory that xmem maps from physical pages is
413// committed by a vectored handler on first touch; a kernel write (ReadFile) raises no user-mode fault and fails with
414// ERROR_NOACCESS instead. No C++ objects here: the function has __try.
415void touch_pages(void* buffer, size_t size) {
416 const uintptr_t start = reinterpret_cast<uintptr_t>(buffer);
417 for (uintptr_t page = start & ~uintptr_t{0xfff}; page < start + size; page += 0x1000) {
418 __try {
419 (void)*reinterpret_cast<volatile const unsigned char*>(page < start ? start : page);
420 } __except (EXCEPTION_EXECUTE_HANDLER) {
421 }
422 }
423}
424
412HRESULT read_error(DWORD error) { 425HRESULT read_error(DWORD error) {
413 switch (error) { 426 switch (error) {
414 case ERROR_NOACCESS: 427 case ERROR_NOACCESS:
@@ -432,7 +445,16 @@ HRESULT read_file_range(HANDLE handle, uint64_t offset, void* destination, uint3
432 at.Offset = static_cast<DWORD>(position); 445 at.Offset = static_cast<DWORD>(position);
433 at.OffsetHigh = static_cast<DWORD>(position >> 32); 446 at.OffsetHigh = static_cast<DWORD>(position >> 32);
434 DWORD got = 0; 447 DWORD got = 0;
435 if (!ReadFile(handle, out + done, size - done, &got, &at)) return read_error(GetLastError()); 448 if (!ReadFile(handle, out + done, size - done, &got, &at)) {
449 const DWORD error = GetLastError();
450 if (error != ERROR_NOACCESS) return read_error(error);
451 // Destination pages not committed yet (see touch_pages): commit them from user mode and read again once.
452 touch_pages(out + done, size - done);
453 at = OVERLAPPED{};
454 at.Offset = static_cast<DWORD>(position);
455 at.OffsetHigh = static_cast<DWORD>(position >> 32);
456 if (!ReadFile(handle, out + done, size - done, &got, &at)) return read_error(GetLastError());
457 }
436 if (got == 0) return E_DSTORAGE_END_OF_FILE; // the file shrank since the size check 458 if (got == 0) return E_DSTORAGE_END_OF_FILE; // the file shrank since the size check
437 done += got; 459 done += got;
438 } 460 }