upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/file_system.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/file_system.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/file_system.cpp')
-rw-r--r--runtime/shims/file_system.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index 720c106f3..75f5e9810 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -701,6 +701,41 @@ void note_open_failure(const char* api, LPCWSTR name, DWORD error, const void* c
701 rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)))); 701 rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller))));
702} 702}
703 703
704// Reads one byte of every 4 KiB page of [buffer, buffer + size) from user mode, so that pages committed on first
705// touch (xmem's physical-page views: a reserve-only section committed by its vectored handler) are committed before
706// the kernel writes into them. A page that is not valid at all is left for the caller's ReadFile to refuse. No C++
707// objects here: the function has __try.
708bool touch_pages(void* buffer, DWORD size) {
709 auto* p = static_cast<volatile const unsigned char*>(buffer);
710 const uintptr_t first = reinterpret_cast<uintptr_t>(buffer) & ~uintptr_t{0xfff};
711 const uintptr_t end = reinterpret_cast<uintptr_t>(buffer) + size;
712 bool all = true;
713 for (uintptr_t page = first; page < end; page += 0x1000) {
714 const uintptr_t at = page < reinterpret_cast<uintptr_t>(p) ? reinterpret_cast<uintptr_t>(p) : page;
715 __try {
716 (void)*reinterpret_cast<volatile const unsigned char*>(at);
717 } __except (EXCEPTION_EXECUTE_HANDLER) {
718 all = false;
719 }
720 }
721 return all;
722}
723
724// ReadFile with one retry for a destination the kernel cannot write yet. The retail title reads into title memory
725// that xmem maps from physical pages committed on first touch; the kernel's write raises no user-mode fault, so
726// ReadFile fails with ERROR_NOACCESS (998), and the title's read loop sleeps 1 ms and retries forever (measured on
727// 1.0.2.1, native Windows). Touching the pages from user mode commits them; the read is then retried once.
728BOOL WINAPI read_file(HANDLE file, LPVOID buffer, DWORD size, LPDWORD read, LPOVERLAPPED overlapped) {
729 if (ReadFile(file, buffer, size, read, overlapped)) return TRUE;
730 if (GetLastError() != ERROR_NOACCESS || !buffer || !size) return FALSE;
731 static std::atomic<bool> logged{false};
732 if (!logged.exchange(true))
733 std::fprintf(stderr, "[rt] fs: ReadFile into uncommitted title memory (ERROR_NOACCESS): committing the "
734 "destination pages and retrying (logged once)\n");
735 touch_pages(buffer, size);
736 return ReadFile(file, buffer, size, read, overlapped);
737}
738
704HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes, 739HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes,
705 DWORD disposition, DWORD flags, HANDLE template_file) { 740 DWORD disposition, DWORD flags, HANDLE template_file) {
706 if (!name) { 741 if (!name) {
@@ -1405,6 +1440,7 @@ using namespace rt::fs;
1405 {dll, "CreateFileA", reinterpret_cast<void*>(&create_file_a)}, \ 1440 {dll, "CreateFileA", reinterpret_cast<void*>(&create_file_a)}, \
1406 {dll, "CreateFileW", reinterpret_cast<void*>(&create_file_w)}, \ 1441 {dll, "CreateFileW", reinterpret_cast<void*>(&create_file_w)}, \
1407 {dll, "CreateFile2", reinterpret_cast<void*>(&create_file2)}, \ 1442 {dll, "CreateFile2", reinterpret_cast<void*>(&create_file2)}, \
1443 {dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \
1408 {dll, "FindFirstFileA", reinterpret_cast<void*>(&find_first_file_a)}, \ 1444 {dll, "FindFirstFileA", reinterpret_cast<void*>(&find_first_file_a)}, \
1409 {dll, "FindFirstFileW", reinterpret_cast<void*>(&find_first_file_w)}, \ 1445 {dll, "FindFirstFileW", reinterpret_cast<void*>(&find_first_file_w)}, \
1410 {dll, "FindFirstFileExA", reinterpret_cast<void*>(&find_first_file_ex_a)}, \ 1446 {dll, "FindFirstFileExA", reinterpret_cast<void*>(&find_first_file_ex_a)}, \