Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions runtime/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) {
}

// A run that ends in an exception nothing handles (a fault in the title or one of its DLLs, the int3 after a device
// loss) would otherwise keep none of the pipelines it compiled, and the next boot builds them all again. The device is
// loss) would otherwise keep none of the pipelines it compiled, and the next boot builds them all again: this prints
// the fault and the threads' call chains (rt::print_fault_stacks) and saves them. The device is
// left alone apart from that write; the exception then goes on as before (the previous filter, or Windows' own
// handling), so the exit code is the exception's. Not for a stack overflow: no stack is left to write with.
static LPTOP_LEVEL_EXCEPTION_FILTER previous_fatal_filter = nullptr;
Expand All @@ -364,9 +365,15 @@ static LONG WINAPI save_caches_on_fatal_exception(EXCEPTION_POINTERS* info) {
// Several title threads can fault at once (measured: eight within a second). The first writes; the others
// wait here for it, so none of them reaches the title's filter, which may end the process, mid-write.
static std::once_flag once;
std::call_once(once, [code] {
std::call_once(once, [info, code] {
std::fprintf(stderr, "[rt] unhandled exception 0x%08lx: writing the session pipeline cache before the "
"process ends\n", static_cast<unsigned long>(code));
// Before the waits below, while the faulting thread's stack is still the one we are running on. The
// fatal report takes no lock, no heap and no other thread: the fault may be the heap's, or the loader
// lookup's (a thread that faults while it holds ranges_mutex_ would wait on itself), so an address here
// is named only from the ranges published at map time, and the other threads are left alone.
rt::print_fault_stacks(info ? info->ExceptionRecord : nullptr, info ? info->ContextRecord : nullptr,
"fatal", true);
rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
std::fflush(stderr);
Expand Down
7 changes: 7 additions & 0 deletions runtime/src/pe_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "clzero_patch.h"
#include "cpu_emulation.h"
#include "isa_jit_patch.h"
#include "thread_stacks.h"

namespace rt {

Expand Down Expand Up @@ -1433,6 +1434,7 @@ Loader::~Loader() {
shims::remove_xmem_allocation_hooks(this);
shims::remove_bundled_loader(this);
shims::remove_module_lookup(this);
clear_fault_ranges(this); // before the unmapping below: a fault after this names these addresses as numbers
for (auto* m : order_) {
// TLS state must go away before the image and its TLS block are unmapped.
if (m->tls_index != kNoTlsIndex && m->tls_slot < kTlsSlotCount) {
Expand Down Expand Up @@ -1773,6 +1775,9 @@ Module& Loader::map_image(const std::filesystem::path& path, const std::string&
// lookup first.
std::unique_lock lock(ranges_mutex_);
ranges_.push_back({reinterpret_cast<uint64_t>(m->base), m->size, m->name});
// A second copy for the fatal report, which may not take ranges_mutex_ (a thread that faults in the load
// path below holds it) nor the heap. See rt::publish_fault_range.
publish_fault_range(reinterpret_cast<uint64_t>(m->base), m->size, m->name.c_str(), this);
}

std::memcpy(m->base, file.data(), oh.SizeOfHeaders);
Expand Down Expand Up @@ -2354,6 +2359,8 @@ void Loader::protect_sections(Module& m) {
std::unique_lock lock(ranges_mutex_);
ranges_.push_back({reinterpret_cast<uint64_t>(isa.stubs),
static_cast<uint32_t>(isa.stub_bytes), m.name + "[isa-stubs]"});
publish_fault_range(reinterpret_cast<uint64_t>(isa.stubs), static_cast<uint32_t>(isa.stub_bytes),
(m.name + "[isa-stubs]").c_str(), this);
}
if (isa.inplace_sites || isa.bmi_stubs || isa.avx2_stubs || isa.fma3_stubs) {
std::fprintf(stderr,
Expand Down
Loading