diff options
| author | omardev29 <154835571+omardev29@users.noreply.github.com> | 2026-11-22 06:04:28 +0100 |
|---|---|---|
| committer | omardev29 <154835571+omardev29@users.noreply.github.com> | 2026-11-22 12:29:52 +0100 |
| commit | 05a37ae72de8271321b29257d523ec917b0d11d0 (patch) | |
| tree | e34d4c1072675ff1bf8959495f03b88d7f567bc6 /runtime/src/pe_loader.cpp | |
| parent | f3d302dded431836590ac8e2a9f3bea148bc2a07 (diff) | |
Loader: also redirect RtlPcToFileHeader in the module a host import resolves into
GetProcAddress follows forwarders, so a host import can land in another DLL
than the one named in the import table. The throw calls the RtlPcToFileHeader
import of the module that implements _CxxThrowException, so that module is
redirected too. On Windows it is vcruntime140.dll itself (no change).
Under Wine, without this, run_tiny_game_eh faults inside a Wine DLL after the
throw, reading 0xE080, an offset inside tiny_game's image size (consistent with
a base of 0). With it, the fork's CI run of this branch prints "PASS a C++
exception is thrown and caught inside the image" under Wine. Which Wine DLL
implements _CxxThrowException was not checked.
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 8865e6ddc..533a0264f 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -320,6 +320,16 @@ void redirect_pc_to_file_header(HMODULE host) { | |||
| 320 | } | 320 | } |
| 321 | } | 321 | } |
| 322 | 322 | ||
| 323 | // The module a host import actually resolved into. GetProcAddress follows forwarders, so this can be another DLL | ||
| 324 | // than the one imported (on Windows vcruntime140.dll implements _CxxThrowException itself); that module's | ||
| 325 | // RtlPcToFileHeader import is the one the throw calls. | ||
| 326 | void redirect_pc_to_file_header_at(const void* resolved) { | ||
| 327 | HMODULE owner = nullptr; | ||
| 328 | if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | ||
| 329 | static_cast<LPCWSTR>(resolved), &owner) && owner) | ||
| 330 | redirect_pc_to_file_header(owner); | ||
| 331 | } | ||
| 332 | |||
| 323 | } // namespace | 333 | } // namespace |
| 324 | 334 | ||
| 325 | namespace { | 335 | namespace { |
| @@ -710,6 +720,7 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d | |||
| 710 | if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h); | 720 | if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h); |
| 711 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str()); | 721 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str()); |
| 712 | if (!p) return nullptr; | 722 | if (!p) return nullptr; |
| 723 | if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p)); | ||
| 713 | binding = Binding::Host; | 724 | binding = Binding::Host; |
| 714 | return reinterpret_cast<void*>(p); | 725 | return reinterpret_cast<void*>(p); |
| 715 | } | 726 | } |
| @@ -743,6 +754,7 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name | |||
| 743 | if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it | 754 | if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it |
| 744 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); | 755 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); |
| 745 | if (p) { | 756 | if (p) { |
| 757 | if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p)); | ||
| 746 | m.imports.push_back({dll, sym, Binding::Host}); | 758 | m.imports.push_back({dll, sym, Binding::Host}); |
| 747 | return reinterpret_cast<void*>(p); | 759 | return reinterpret_cast<void*>(p); |
| 748 | } | 760 | } |