diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index fc73f37f5..f87531063 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -19,6 +19,7 @@ | |||
| 19 | #include "../shims/shims.h" | 19 | #include "../shims/shims.h" |
| 20 | #include "../shims/startup_trace.h" | 20 | #include "../shims/startup_trace.h" |
| 21 | #include "base_reservation.h" | 21 | #include "base_reservation.h" |
| 22 | #include "bmi_emulation.h" | ||
| 22 | #include "clzero_patch.h" | 23 | #include "clzero_patch.h" |
| 23 | #include "cpu_emulation.h" | 24 | #include "cpu_emulation.h" |
| 24 | 25 | ||
| @@ -2215,6 +2216,32 @@ void Loader::protect_sections(Module& m) { | |||
| 2215 | "[rt] loader: %s: %zu CLZERO loops run from stubs at %p (%zu bytes), not as a #UD per line\n", | 2216 | "[rt] loader: %s: %zu CLZERO loops run from stubs at %p (%zu bytes), not as a #UD per line\n", |
| 2216 | m.name.c_str(), patched.sites, static_cast<void*>(patched.stubs), patched.stub_bytes); | 2217 | m.name.c_str(), patched.sites, static_cast<void*>(patched.stubs), patched.stub_bytes); |
| 2217 | } | 2218 | } |
| 2219 | static const auto bmi_host = rt::bmi_emulation::detect_host(); | ||
| 2220 | if ((!bmi_host.bmi1 || !bmi_host.lzcnt) && rt::bmi_emulation::lzcnt_patch_enabled()) { | ||
| 2221 | std::vector<std::pair<uint32_t, uint32_t>> exec_ranges; | ||
| 2222 | const IMAGE_SECTION_HEADER* cs = IMAGE_FIRST_SECTION(nt); | ||
| 2223 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++cs) { | ||
| 2224 | if (!(cs->Characteristics & IMAGE_SCN_MEM_EXECUTE) || cs->VirtualAddress >= m.size) continue; | ||
| 2225 | const uint64_t size = std::min<uint64_t>(std::min(cs->Misc.VirtualSize, cs->SizeOfRawData), | ||
| 2226 | m.size - cs->VirtualAddress); | ||
| 2227 | if (size) exec_ranges.push_back({cs->VirtualAddress, static_cast<uint32_t>(size)}); | ||
| 2228 | } | ||
| 2229 | const auto exc = directory(nt, IMAGE_DIRECTORY_ENTRY_EXCEPTION); | ||
| 2230 | const RUNTIME_FUNCTION* pdata = nullptr; | ||
| 2231 | uint32_t pdata_count = 0; | ||
| 2232 | if (exc.VirtualAddress && exc.Size >= sizeof(RUNTIME_FUNCTION) && exc.VirtualAddress < m.size) { | ||
| 2233 | const uint64_t max_bytes = std::min<uint64_t>(exc.Size, m.size - exc.VirtualAddress); | ||
| 2234 | pdata_count = static_cast<uint32_t>(max_bytes / sizeof(RUNTIME_FUNCTION)); | ||
| 2235 | pdata = at<RUNTIME_FUNCTION>(m.base, exc.VirtualAddress); | ||
| 2236 | } | ||
| 2237 | const auto patched = rt::bmi_emulation::patch_image( | ||
| 2238 | m.base, m.size, exec_ranges, pdata, pdata_count, !bmi_host.bmi1, !bmi_host.lzcnt); | ||
| 2239 | if (patched.tzcnt_sites || patched.lzcnt_sites) | ||
| 2240 | std::fprintf(stderr, | ||
| 2241 | "[rt] loader: %s: patched %zu TZCNT and %zu LZCNT sites (%zu stubs at %p, %zu #UD)\n", | ||
| 2242 | m.name.c_str(), patched.tzcnt_sites, patched.lzcnt_sites, patched.stub_sites, | ||
| 2243 | static_cast<void*>(patched.stubs), patched.ud2_sites); | ||
| 2244 | } | ||
| 2218 | VirtualProtect(m.base, nt->OptionalHeader.SizeOfHeaders, PAGE_READONLY, &old); | 2245 | VirtualProtect(m.base, nt->OptionalHeader.SizeOfHeaders, PAGE_READONLY, &old); |
| 2219 | auto* sec = IMAGE_FIRST_SECTION(nt); | 2246 | auto* sec = IMAGE_FIRST_SECTION(nt); |
| 2220 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { | 2247 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { |