[Emulator] Removed windows rdrand ntdll patch

This commit is contained in:
Gliniak
2025-11-03 20:10:36 +01:00
parent b800011265
commit 3252d256ca

View File

@@ -19,69 +19,13 @@
// Autogenerated by `xb premake`.
#include "build/version.h"
#include <Psapi.h>
DEFINE_bool(enable_console, false, "Open a console window with the main window",
"Logging");
static uintptr_t g_xenia_exe_base = 0;
static size_t g_xenia_exe_size = 0;
#if XE_ARCH_AMD64 == 1
DEFINE_bool(enable_rdrand_ntdll_patch, true,
"Hot-patches ntdll at the start of the process to not use rdrand "
"as part of the RNG for heap randomization. Can reduce CPU usage "
"significantly, but is untested on all Windows versions.",
"Win32");
// begin ntdll hack
#include <psapi.h>
static bool g_didfailtowrite = false;
static void write_process_memory(HANDLE process, uintptr_t offset,
unsigned size, const unsigned char* bvals) {
if (!WriteProcessMemory(process, (void*)offset, bvals, size, nullptr)) {
if (!g_didfailtowrite) {
MessageBoxA(nullptr, "Failed to write to process!", "Failed", MB_OK);
g_didfailtowrite = true;
}
}
}
static constexpr unsigned char pattern_cmp_processorfeature_28_[] = {
0x80, 0x3C, 0x25, 0x90,
0x02, 0xFE, 0x7F, 0x00}; // cmp byte ptr ds:7FFE0290h, 0
static constexpr unsigned char pattern_replacement[] = {
0x48, 0x39, 0xe4, // cmp rsp, rsp = always Z
0x0F, 0x1F, 0x44, 0x00, 0x00 // 5byte nop
};
static void patch_ntdll_instance(HANDLE process, uintptr_t ntdll_base) {
MODULEINFO modinfo;
GetModuleInformation(process, (HMODULE)ntdll_base, &modinfo,
sizeof(MODULEINFO));
std::vector<uintptr_t> possible_places{};
unsigned char* strt = (unsigned char*)modinfo.lpBaseOfDll;
for (unsigned i = 0; i < modinfo.SizeOfImage; ++i) {
for (unsigned j = 0; j < sizeof(pattern_cmp_processorfeature_28_); ++j) {
if (strt[i + j] != pattern_cmp_processorfeature_28_[j]) {
goto miss;
}
}
possible_places.push_back((uintptr_t)(&strt[i]));
miss:;
}
for (auto&& place : possible_places) {
write_process_memory(process, place, sizeof(pattern_replacement),
pattern_replacement);
}
}
static void do_ntdll_hack_this_process() {
patch_ntdll_instance(GetCurrentProcess(),
(uintptr_t)GetModuleHandleA("ntdll.dll"));
}
#endif
static HMODULE probe_for_module(void* addr) {
// get 65k aligned addr downwards to probe for MZ
@@ -107,8 +51,6 @@ static HMODULE probe_for_module(void* addr) {
}
}
// end ntdll hack
static constexpr auto XENIA_ERROR_LANGUAGE =
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
@@ -376,11 +318,6 @@ int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE hinstance_prev,
xe::InitializeWin32App(app->GetName());
if (app->OnInitialize()) {
#if XE_ARCH_AMD64 == 1
if (cvars::enable_rdrand_ntdll_patch) {
do_ntdll_hack_this_process();
}
#endif
// TODO(Triang3l): Rework this, need to initialize the console properly,
// disable has_console_attached_ by default in windowed apps, and attach
// only if needed.