diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 03:14:21 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 03:14:21 -0500 |
| commit | eda43133f8deac7d4a8b422a6bd74e923dd4ebc5 (patch) | |
| tree | bc3aad56e2b601a8ce9f1b73a89340f35918ec7a /runtime/src/pe_loader.cpp | |
| parent | 2437ae42a4bfaba74b91eb3a9520965367531020 (diff) | |
| parent | cc7651422956d1f362bdf32f900e543b357056eb (diff) | |
Merge pull request #1172 from farhat
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index cd1a38bcc..15a62d9b4 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -166,6 +166,26 @@ Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) | |||
| 166 | } | 166 | } |
| 167 | 167 | ||
| 168 | Loader::~Loader() { | 168 | Loader::~Loader() { |
| 169 | // Importers must release their resources before their dependencies. Keep every mapping, unwind table and TLS | ||
| 170 | // block alive throughout the notifications: a detach handler can still call an imported function or free an | ||
| 171 | // FLS slot whose destructor lives in another mapped image. | ||
| 172 | for (auto it = init_order_.rbegin(); it != init_order_.rend(); ++it) { | ||
| 173 | Module& m = **it; | ||
| 174 | if (!m.initialised) continue; // a map-only load never opts in to running image code | ||
| 175 | m.initialised = false; | ||
| 176 | try { | ||
| 177 | run_tls_callbacks(m, DLL_PROCESS_DETACH); | ||
| 178 | } catch (const std::exception& e) { | ||
| 179 | // A module may have changed its callback table since attach. A destructor cannot propagate the validation | ||
| 180 | // error; report it and still let DllMain release resources before the image is freed. | ||
| 181 | std::fprintf(stderr, "[rt] TLS process detach failed for %s: %s\n", m.name.c_str(), e.what()); | ||
| 182 | } | ||
| 183 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | ||
| 184 | if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { | ||
| 185 | auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva); | ||
| 186 | entry(m.base, DLL_PROCESS_DETACH, nullptr); // explicit unload; the return value is ignored | ||
| 187 | } | ||
| 188 | } | ||
| 169 | shims::remove_module_lookup(this); | 189 | shims::remove_module_lookup(this); |
| 170 | for (auto* m : order_) { | 190 | for (auto* m : order_) { |
| 171 | // TLS state must go away before the image and its TLS block are unmapped. | 191 | // TLS state must go away before the image and its TLS block are unmapped. |