diff options
| author | Zack <zmasi21@gmail.com> | 2026-11-28 17:30:56 -0800 |
|---|---|---|
| committer | Zack <zmasi21@gmail.com> | 2026-11-28 17:30:56 -0800 |
| commit | 9125412a5e0879fb16b6290cd2e39a1d70c07165 (patch) | |
| tree | c3f69343cda4e430ed184eac70fb5f5d3f8f9efc /runtime/src/pe_loader.cpp | |
| parent | f450d2943c0806e3fd601d7e6d503beb045ccf8c (diff) | |
| parent | 53b77df14f76b27ff679406a081cbc226fef26d4 (diff) | |
Merge remote-tracking branch 'origin/main' into docs/d3d12-port-nv-docs-8gb-and-winmem
# Conflicts:
# d3d12/README.md
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index d44160505..303469b06 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -20,8 +20,10 @@ | |||
| 20 | #include "../shims/shims.h" | 20 | #include "../shims/shims.h" |
| 21 | #include "../shims/startup_trace.h" | 21 | #include "../shims/startup_trace.h" |
| 22 | #include "base_reservation.h" | 22 | #include "base_reservation.h" |
| 23 | #include "bmi_emulation.h" | ||
| 23 | #include "clzero_patch.h" | 24 | #include "clzero_patch.h" |
| 24 | #include "cpu_emulation.h" | 25 | #include "cpu_emulation.h" |
| 26 | #include "isa_jit_patch.h" | ||
| 25 | 27 | ||
| 26 | namespace rt { | 28 | namespace rt { |
| 27 | 29 | ||
| @@ -2337,6 +2339,47 @@ void Loader::protect_sections(Module& m) { | |||
| 2337 | "[rt] loader: %s: %zu CLZERO loops run from stubs at %p (%zu bytes), not as a #UD per line\n", | 2339 | "[rt] loader: %s: %zu CLZERO loops run from stubs at %p (%zu bytes), not as a #UD per line\n", |
| 2338 | m.name.c_str(), patched.sites, static_cast<void*>(patched.stubs), patched.stub_bytes); | 2340 | m.name.c_str(), patched.sites, static_cast<void*>(patched.stubs), patched.stub_bytes); |
| 2339 | } | 2341 | } |
| 2342 | { | ||
| 2343 | const auto isa = rt::isa_jit_patch::patch_image(m.base, m.size); | ||
| 2344 | if (isa.stubs && isa.stub_bytes) { | ||
| 2345 | std::unique_lock lock(ranges_mutex_); | ||
| 2346 | ranges_.push_back({reinterpret_cast<uint64_t>(isa.stubs), | ||
| 2347 | static_cast<uint32_t>(isa.stub_bytes), m.name + "[isa-stubs]"}); | ||
| 2348 | } | ||
| 2349 | if (isa.inplace_sites || isa.bmi_stubs || isa.avx2_stubs || isa.fma3_stubs) { | ||
| 2350 | std::fprintf(stderr, | ||
| 2351 | "[rt] loader: %s: native AVX1/x64 acceleration: %zu in-place, %zu BMI, %zu AVX2 (%zu fused 4B), " | ||
| 2352 | "%zu FMA3 stubs at %p (%zu KiB)\n", | ||
| 2353 | m.name.c_str(), isa.inplace_sites, isa.bmi_stubs, isa.avx2_stubs, isa.avx2_fused_4b, | ||
| 2354 | isa.fma3_stubs, static_cast<void*>(isa.stubs), (isa.stub_bytes + 1023u) / 1024u); | ||
| 2355 | } | ||
| 2356 | } | ||
| 2357 | static const auto bmi_host = rt::bmi_emulation::detect_host(); | ||
| 2358 | if ((!bmi_host.bmi1 || !bmi_host.lzcnt) && rt::bmi_emulation::lzcnt_patch_enabled()) { | ||
| 2359 | std::vector<std::pair<uint32_t, uint32_t>> exec_ranges; | ||
| 2360 | const IMAGE_SECTION_HEADER* cs = IMAGE_FIRST_SECTION(nt); | ||
| 2361 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++cs) { | ||
| 2362 | if (!(cs->Characteristics & IMAGE_SCN_MEM_EXECUTE) || cs->VirtualAddress >= m.size) continue; | ||
| 2363 | const uint64_t size = std::min<uint64_t>(std::min(cs->Misc.VirtualSize, cs->SizeOfRawData), | ||
| 2364 | m.size - cs->VirtualAddress); | ||
| 2365 | if (size) exec_ranges.push_back({cs->VirtualAddress, static_cast<uint32_t>(size)}); | ||
| 2366 | } | ||
| 2367 | const auto exc = directory(nt, IMAGE_DIRECTORY_ENTRY_EXCEPTION); | ||
| 2368 | const RUNTIME_FUNCTION* pdata = nullptr; | ||
| 2369 | uint32_t pdata_count = 0; | ||
| 2370 | if (exc.VirtualAddress && exc.Size >= sizeof(RUNTIME_FUNCTION) && exc.VirtualAddress < m.size) { | ||
| 2371 | const uint64_t max_bytes = std::min<uint64_t>(exc.Size, m.size - exc.VirtualAddress); | ||
| 2372 | pdata_count = static_cast<uint32_t>(max_bytes / sizeof(RUNTIME_FUNCTION)); | ||
| 2373 | pdata = at<RUNTIME_FUNCTION>(m.base, exc.VirtualAddress); | ||
| 2374 | } | ||
| 2375 | const auto patched = rt::bmi_emulation::patch_image( | ||
| 2376 | m.base, m.size, exec_ranges, pdata, pdata_count, !bmi_host.bmi1, !bmi_host.lzcnt); | ||
| 2377 | if (patched.tzcnt_sites || patched.lzcnt_sites) | ||
| 2378 | std::fprintf(stderr, | ||
| 2379 | "[rt] loader: %s: patched %zu TZCNT and %zu LZCNT sites (%zu stubs at %p, %zu #UD)\n", | ||
| 2380 | m.name.c_str(), patched.tzcnt_sites, patched.lzcnt_sites, patched.stub_sites, | ||
| 2381 | static_cast<void*>(patched.stubs), patched.ud2_sites); | ||
| 2382 | } | ||
| 2340 | VirtualProtect(m.base, nt->OptionalHeader.SizeOfHeaders, PAGE_READONLY, &old); | 2383 | VirtualProtect(m.base, nt->OptionalHeader.SizeOfHeaders, PAGE_READONLY, &old); |
| 2341 | auto* sec = IMAGE_FIRST_SECTION(nt); | 2384 | auto* sec = IMAGE_FIRST_SECTION(nt); |
| 2342 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { | 2385 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { |