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:
Diffstat (limited to 'runtime/shims/file_system.cpp')
-rw-r--r--runtime/shims/file_system.cpp39
1 files changed, 37 insertions, 2 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index 995f55a0b..1eaf0bda9 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -23,6 +23,7 @@
23// Only images mapped by the loader see these: they are bound through the registry, the runtime's own code calls the 23// Only images mapped by the loader see these: they are bound through the registry, the runtime's own code calls the
24// host directly. 24// host directly.
25#include <windows.h> 25#include <windows.h>
26#include <intrin.h>
26 27
27#include <atomic> 28#include <atomic>
28#include <cstring> 29#include <cstring>
@@ -39,6 +40,7 @@
39 40
40#include "file_system.h" 41#include "file_system.h"
41#include "shims.h" 42#include "shims.h"
43#include "startup_trace.h"
42 44
43namespace rt::fs { 45namespace rt::fs {
44namespace { 46namespace {
@@ -629,6 +631,25 @@ std::filesystem::path overlay_dir() {
629 return g_overlay_dir; 631 return g_overlay_dir;
630} 632}
631 633
634// gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the
635// path the title passed, the Win32 error, the caller as module+RVA), so a boot that stops on a missing or refused
636// file names it. Failures only, and at most 256 of them, so a title probing many optional files can't fill the file.
637void note_open_failure(const char* api, LPCWSTR name, DWORD error, const void* caller) {
638 if (!rt::startup_trace::enabled()) return;
639 static std::atomic<int> left{256};
640 if (left.fetch_sub(1, std::memory_order_relaxed) <= 0) return;
641 std::string utf8;
642 if (const int n = WideCharToMultiByte(CP_UTF8, 0, name, -1, nullptr, 0, nullptr, nullptr); n > 1) {
643 utf8.resize(static_cast<size_t>(n) - 1);
644 WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8.data(), n, nullptr, nullptr);
645 }
646 rt::startup_trace::event(
647 "file_open_failed",
648 std::string("\"api\":\"") + api + "\",\"path\":" + rt::startup_trace::quote(utf8) +
649 ",\"error\":" + std::to_string(error) + ",\"caller\":" +
650 rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller))));
651}
652
632HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes, 653HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes,
633 DWORD disposition, DWORD flags, HANDLE template_file) { 654 DWORD disposition, DWORD flags, HANDLE template_file) {
634 if (!name) { 655 if (!name) {
@@ -637,10 +658,17 @@ HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_
637 } 658 }
638 const Target t = open_target(name, access, disposition); 659 const Target t = open_target(name, access, disposition);
639 if (t.error) { 660 if (t.error) {
661 note_open_failure("CreateFileW", name, t.error, _ReturnAddress());
640 SetLastError(t.error); 662 SetLastError(t.error);
641 return INVALID_HANDLE_VALUE; 663 return INVALID_HANDLE_VALUE;
642 } 664 }
643 return ::CreateFileW(t.path.c_str(), access, share, attributes, disposition, flags, template_file); 665 const HANDLE h = ::CreateFileW(t.path.c_str(), access, share, attributes, disposition, flags, template_file);
666 if (h == INVALID_HANDLE_VALUE) {
667 const DWORD error = GetLastError();
668 note_open_failure("CreateFileW", name, error, _ReturnAddress());
669 SetLastError(error);
670 }
671 return h;
644} 672}
645 673
646HANDLE WINAPI create_file_a(LPCSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes, 674HANDLE WINAPI create_file_a(LPCSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes,
@@ -661,10 +689,17 @@ HANDLE WINAPI create_file2(LPCWSTR name, DWORD access, DWORD share, DWORD dispos
661 } 689 }
662 const Target t = open_target(name, access, disposition); 690 const Target t = open_target(name, access, disposition);
663 if (t.error) { 691 if (t.error) {
692 note_open_failure("CreateFile2", name, t.error, _ReturnAddress());
664 SetLastError(t.error); 693 SetLastError(t.error);
665 return INVALID_HANDLE_VALUE; 694 return INVALID_HANDLE_VALUE;
666 } 695 }
667 return ::CreateFile2(t.path.c_str(), access, share, disposition, params); 696 const HANDLE h = ::CreateFile2(t.path.c_str(), access, share, disposition, params);
697 if (h == INVALID_HANDLE_VALUE) {
698 const DWORD error = GetLastError();
699 note_open_failure("CreateFile2", name, error, _ReturnAddress());
700 SetLastError(error);
701 }
702 return h;
668} 703}
669 704
670DWORD WINAPI get_file_attributes_w(LPCWSTR name) { 705DWORD WINAPI get_file_attributes_w(LPCWSTR name) {