From 273b3bdf9d6e736442ad584ad464df6e86ee928b Mon Sep 17 00:00:00 2001 From: Moriz82 <72836413+Moriz82@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:42:12 -0600 Subject: [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD) One squashed commit of a fork that was developed on a Linux/Proton machine and tested on two native Windows machines. On all of them the retail 1.0.2.1 package now launches, shows its launch movie, first-run pages and landing prompt, runs the story load to its end with the loading slideshow drawing its artwork, and enters the world phase. A first world frame is not verified yet. docs/linux_proton.md has the setup, a healthy boot's timeline, the machines and where it stops. Runtime: frames in the title's own window; DirectStorage pinned reads, page-list scatter/gather, BCPack and unshuffle with a locally generated predictor file; pooled, device-local and deferred image backing for streamed and placed textures; enhanced barriers into LAYOUT_UNDEFINED; a bindless table that degrades instead of refusing a submission; a descriptor hash finalizer (off until a library carries it); directly indexed heap offsets for compute dispatches and VS/PS/GS draws; title buffer windows outside a fragmented arena; opt-in device-local copies of streamed buffer views; write-fault retries under the tracker lock, shared trackers and write-watched title memory for native Windows; device-loss, compute-input and memory probes. Translator: loop bounds at structured exits, the eight FidelityFX programs that use a packed-dot form the disassembler skips, buffer bounds modes per view kind, a compaction option, the hash finalizer option. Tools and scripts: Linux boot scripts (Wine/Proton, no network), a Windows boot script, the paired compute translation script. No game data. AI-assisted (Claude Code and Codex agents), reviewed and booted by people on the machines named in docs/linux_proton.md. Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 --- runtime/src/thread_stacks.cpp | 237 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 runtime/src/thread_stacks.cpp (limited to 'runtime/src/thread_stacks.cpp') diff --git a/runtime/src/thread_stacks.cpp b/runtime/src/thread_stacks.cpp new file mode 100644 index 000000000..67188d908 --- /dev/null +++ b/runtime/src/thread_stacks.cpp @@ -0,0 +1,237 @@ +#include "thread_stacks.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../shims/shims.h" +#include "../shims/startup_trace.h" + +namespace rt { +namespace { + +using Clock = std::chrono::steady_clock; +using GetDescription = HRESULT(WINAPI*)(HANDLE, PWSTR*); +using QueryThread = LONG(NTAPI*)(HANDLE, int, void*, ULONG, ULONG*); + +struct CopiedStack { + DWORD64 copy = 0; // Where the bytes are now. + DWORD64 original = 0; // Where they were in the thread: [original, original + size). + size_t size = 0; +}; + +// Suspended section: system calls and a copy only. +bool copy_context_and_stack(HANDLE thread, CONTEXT& context, std::byte* buffer, CopiedStack& stack) { + if (SuspendThread(thread) == DWORD(-1)) return false; + context.ContextFlags = CONTEXT_FULL; + MEMORY_BASIC_INFORMATION region{}; + bool copied = false; + if (GetThreadContext(thread, &context) && + VirtualQuery(reinterpret_cast(context.Rsp), ®ion, sizeof(region)) && + region.State == MEM_COMMIT && !(region.Protect & (PAGE_GUARD | PAGE_NOACCESS))) { + // From RSP (rounded down to keep the 16-byte alignment the copy buffer also has) to the top of the committed + // run of stack pages, at most kMaxStackBytes. + stack.original = context.Rsp & ~DWORD64(15); + const DWORD64 top = reinterpret_cast(region.BaseAddress) + region.RegionSize; + stack.size = static_cast(std::min(StackSampler::kMaxStackBytes, top - stack.original)); + SIZE_T read = 0; + copied = ReadProcessMemory(GetCurrentProcess(), reinterpret_cast(stack.original), buffer, stack.size, + &read) && read == stack.size; + } + ResumeThread(thread); + stack.copy = reinterpret_cast(buffer); + return copied; +} + +// A frame's code can be unloaded after its thread resumes and a damaged stack can hand the unwinder a bad pointer: +// both are access violations here, and end the walk. +bool unwind_frame(CONTEXT& context) { + __try { + DWORD64 image_base = 0; + if (const auto* entry = RtlLookupFunctionEntry(context.Rip, &image_base, nullptr)) { + void* handler_data = nullptr; + DWORD64 establisher = 0; + RtlVirtualUnwind(UNW_FLAG_NHANDLER, image_base, context.Rip, const_cast(entry), + &context, &handler_data, &establisher, nullptr); + } else { + // No function entry: a leaf (or a thunk without unwind data) that has not moved RSP. + context.Rip = *reinterpret_cast(context.Rsp); + context.Rsp += sizeof(DWORD64); + } + } __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION || GetExceptionCode() == EXCEPTION_IN_PAGE_ERROR + ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) { + return false; + } + return true; +} + +size_t walk_copy(CONTEXT& context, const CopiedStack& stack, std::uint64_t* frames) { + // An address in the copied range now names the same word in the copy. + const auto rebase = [&](DWORD64& address) { + if (address >= stack.original && address - stack.original < stack.size) address += stack.copy - stack.original; + }; + const auto in_copy = [&](DWORD64 address) { + return address >= stack.copy && address - stack.copy + sizeof(DWORD64) <= stack.size; + }; + size_t count = 0; + rebase(context.Rsp); + while (context.Rip && count < StackSampler::kMaxFrames) { + frames[count++] = context.Rip; + if (!in_copy(context.Rsp)) break; + const DWORD64 rsp = context.Rsp; + // Any nonvolatile register can be a frame pointer, including one just restored from the copy's bytes. + for (DWORD64* reg : {&context.Rbx, &context.Rbp, &context.Rsi, &context.Rdi, + &context.R12, &context.R13, &context.R14, &context.R15}) + rebase(*reg); + if (!unwind_frame(context)) break; + rebase(context.Rsp); // A machine frame restores RSP from the copy as well. + if (context.Rsp <= rsp) break; + } + return count; +} + +std::string thread_label(HANDLE thread, GetDescription describe, QueryThread query) { + if (describe) { + PWSTR description = nullptr; + if (SUCCEEDED(describe(thread, &description)) && description) { + std::string name = startup_trace::detail::utf8_from_wide(description, static_cast(wcslen(description))); + LocalFree(description); + if (!name.empty()) return name; + } + } + constexpr int kWin32StartAddress = 9; // ThreadQuerySetWin32StartAddress + DWORD64 start = 0; + if (query && query(thread, kWin32StartAddress, &start, sizeof(start), nullptr) >= 0 && start) + return "start " + shims::module_offset(start); + return "start unknown"; +} + +struct ThreadTrace { + DWORD tid = 0; + std::string label; + std::array frames{}; + size_t count = 0; +}; + +void print_traces(unsigned at, const std::vector& traces) { + std::string out = "[rt] stacks t=" + std::to_string(at) + ": " + std::to_string(traces.size()) + " threads\n"; + for (auto it = traces.begin(); it != traces.end(); ++it) { + out += "[rt] stacks t=" + std::to_string(at) + " thread " + std::to_string(it->tid) + " (" + it->label + "):\n"; + const auto same = std::find_if(traces.begin(), it, [&](const ThreadTrace& earlier) { + return it->count && it->count == earlier.count && + std::equal(it->frames.begin(), it->frames.begin() + it->count, earlier.frames.begin()); + }); + if (same != it) { + out += " same as thread " + std::to_string(same->tid) + "\n"; + } else if (!it->count) { + out += " stack unavailable\n"; + } else { + for (size_t i = 0; i < it->count; ++i) + out += " #" + std::to_string(i) + " " + shims::module_offset(it->frames[i]) + "\n"; + } + } + std::fputs(out.c_str(), stderr); + std::fflush(stderr); +} + +void snapshot_threads(unsigned at, StackSampler& sampler, GetDescription describe, QueryThread query) { + const HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + if (snapshot == INVALID_HANDLE_VALUE) { + std::fprintf(stderr, "[rt] stacks t=%u: thread list failed (error %lu)\n", at, GetLastError()); + return; + } + std::vector traces; + traces.reserve(512); + const DWORD pid = GetCurrentProcessId(); + const DWORD self = GetCurrentThreadId(); + THREADENTRY32 entry{}; + entry.dwSize = sizeof(entry); + for (BOOL ok = Thread32First(snapshot, &entry); ok; ok = Thread32Next(snapshot, &entry)) { + entry.dwSize = sizeof(entry); + if (entry.th32OwnerProcessID != pid || entry.th32ThreadID == self) continue; + const HANDLE thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, + FALSE, entry.th32ThreadID); + if (!thread) continue; + if (GetProcessIdOfThread(thread) == pid) { // The listed thread may have exited and its ID been reused. + ThreadTrace& trace = traces.emplace_back(); + trace.tid = entry.th32ThreadID; + trace.label = thread_label(thread, describe, query); // Before sampling: it allocates. + trace.count = sampler.sample(thread, trace.frames.data()); + } + CloseHandle(thread); + } + CloseHandle(snapshot); + print_traces(at, traces); +} + +} // namespace + +StackSampler::StackSampler() : stack_(std::make_unique_for_overwrite(kMaxStackBytes)) {} + +std::size_t StackSampler::sample(void* thread, std::uint64_t* frames) { + CONTEXT context{}; + CopiedStack stack; + if (!copy_context_and_stack(static_cast(thread), context, stack_.get(), stack)) { + if (!context.Rip) return 0; + frames[0] = context.Rip; // The context without its stack: the innermost frame only. + return 1; + } + return walk_copy(context, stack, frames); +} + +bool parse_stack_times(std::string_view text, std::vector& seconds) { + seconds.clear(); + for (;;) { + const auto comma = text.find(','); + const auto token = text.substr(0, comma); + unsigned value = 0; + const auto [end, error] = std::from_chars(token.data(), token.data() + token.size(), value); + if (token.empty() || error != std::errc{} || end != token.data() + token.size()) { + seconds.clear(); + return false; + } + seconds.push_back(value); + if (comma == std::string_view::npos) break; + text.remove_prefix(comma + 1); + } + std::sort(seconds.begin(), seconds.end()); + seconds.erase(std::unique(seconds.begin(), seconds.end()), seconds.end()); + return true; +} + +std::jthread start_thread_stacks() { + const auto start = Clock::now(); + char spec[1024]; + const DWORD length = GetEnvironmentVariableA("LEONIDA_STACKS", spec, sizeof(spec)); + if (!length) return {}; + std::vector times; + if (length >= sizeof(spec) || !parse_stack_times(std::string_view(spec, length), times)) { + std::fprintf(stderr, "[rt] LEONIDA_STACKS: expected comma-separated whole seconds\n"); + return {}; + } + return std::jthread([start, times = std::move(times)](std::stop_token stop) { + const auto describe = reinterpret_cast(reinterpret_cast( + GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetThreadDescription"))); + const auto query = reinterpret_cast(reinterpret_cast( + GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtQueryInformationThread"))); + StackSampler sampler; + std::condition_variable_any wake; + std::mutex mutex; + std::unique_lock lock(mutex); + for (const unsigned at : times) { + wake.wait_until(lock, stop, start + std::chrono::seconds(at), [] { return false; }); + if (stop.stop_requested()) break; + snapshot_threads(at, sampler, describe, query); + } + }); +} + +} // namespace rt -- cgit v1.2.3