#include #include #include #include #include #include #include extern "C" const char* const g_standinNames[]; extern "C" const int g_standinCount; static char s_logPath[MAX_PATH]; static void LogLine(const char* text) { fputs(text, stderr); fflush(stderr); FILE* file = nullptr; if (s_logPath[0] && fopen_s(&file, s_logPath, "a") == 0 && file) { fputs(text, file); fclose(file); } } static void ModuleOffset(void* address, char* out, size_t size) { HMODULE module = nullptr; char path[MAX_PATH] = "?"; if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, static_cast(address), &module) && module) { GetModuleFileNameA(module, path, sizeof(path)); const char* name = strrchr(path, '\\'); _snprintf_s(out, size, _TRUNCATE, "%s+0x%llX", name ? name + 1 : path, static_cast(reinterpret_cast(address) - reinterpret_cast(module))); } else { _snprintf_s(out, size, _TRUNCATE, "%p", address); } } extern "C" __declspec(noreturn) void StandinHit(int index, void* returnAddress) { char where[MAX_PATH + 32]; ModuleOffset(returnAddress, where, sizeof(where)); char line[4096]; _snprintf_s(line, sizeof(line), _TRUNCATE, "STANDIN %s called from %s\n", index >= 0 && index < g_standinCount ? g_standinNames[index] : "?", where); LogLine(line); if (GetEnvironmentVariableA("WOWCLIENT_STANDIN_MSGBOX", nullptr, 0)) { MessageBoxA(nullptr, line, "WowClient stand-in", MB_OK | MB_ICONERROR); } ExitProcess(0x5717); } static void LogStack(CONTEXT* context) { #if defined(_M_X64) HANDLE process = GetCurrentProcess(); SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES); SymInitialize(process, nullptr, TRUE); CONTEXT frame = *context; for (int depth = 0; depth < 24 && frame.Rip; depth++) { char where[MAX_PATH + 32]; ModuleOffset(reinterpret_cast(frame.Rip), where, sizeof(where)); char buffer[sizeof(SYMBOL_INFO) + 256] = {}; SYMBOL_INFO* symbol = reinterpret_cast(buffer); symbol->SizeOfStruct = sizeof(SYMBOL_INFO); symbol->MaxNameLen = 255; DWORD64 displacement = 0; IMAGEHLP_LINE64 source = {}; source.SizeOfStruct = sizeof(source); DWORD lineDisplacement = 0; char line[1024]; if (SymFromAddr(process, frame.Rip, &displacement, symbol)) { if (SymGetLineFromAddr64(process, frame.Rip, &lineDisplacement, &source)) { _snprintf_s(line, sizeof(line), _TRUNCATE, " #%02d %s %s+0x%llX (%s:%lu)\n", depth, where, symbol->Name, static_cast(displacement), source.FileName, source.LineNumber); } else { _snprintf_s(line, sizeof(line), _TRUNCATE, " #%02d %s %s+0x%llX\n", depth, where, symbol->Name, static_cast(displacement)); } } else { _snprintf_s(line, sizeof(line), _TRUNCATE, " #%02d %s\n", depth, where); } LogLine(line); DWORD64 imageBase = 0; PRUNTIME_FUNCTION function = RtlLookupFunctionEntry(frame.Rip, &imageBase, nullptr); if (!function) { frame.Rip = *reinterpret_cast(frame.Rsp); frame.Rsp += 8; continue; } void* handlerData = nullptr; DWORD64 establisher = 0; RtlVirtualUnwind(UNW_FLAG_NHANDLER, imageBase, frame.Rip, function, &frame, &handlerData, &establisher, nullptr); } #endif } static volatile LONG s_hitFlags[16384]; extern "C" double StandinReport(int index, void* returnAddress) { static int s_continue = -1; if (s_continue < 0) { s_continue = GetEnvironmentVariableA("WOWCLIENT_STANDIN_CONTINUE", nullptr, 0) ? 1 : 0; } if (!s_continue) { StandinHit(index, returnAddress); } if (index >= 0 && index < static_cast(sizeof(s_hitFlags) / sizeof(s_hitFlags[0])) && InterlockedExchange(&s_hitFlags[index], 1)) { return 0.0; } char where[MAX_PATH + 32]; ModuleOffset(returnAddress, where, sizeof(where)); char line[4096]; _snprintf_s(line, sizeof(line), _TRUNCATE, "STANDIN %s called from %s (continued)\n", index >= 0 && index < g_standinCount ? g_standinNames[index] : "?", where); LogLine(line); return 0.0; } static LONG CALLBACK FirstChanceHandler(EXCEPTION_POINTERS* info) { DWORD code = info->ExceptionRecord->ExceptionCode; if (code == EXCEPTION_ACCESS_VIOLATION || code == EXCEPTION_ILLEGAL_INSTRUCTION || code == EXCEPTION_INT_DIVIDE_BY_ZERO || code == EXCEPTION_STACK_OVERFLOW || code == EXCEPTION_PRIV_INSTRUCTION) { char where[MAX_PATH + 32]; ModuleOffset(info->ExceptionRecord->ExceptionAddress, where, sizeof(where)); char line[1024]; if (code == EXCEPTION_ACCESS_VIOLATION && info->ExceptionRecord->NumberParameters >= 2) { _snprintf_s(line, sizeof(line), _TRUNCATE, "CRASH 0x%08lX at %s (%s 0x%llX)\n", code, where, info->ExceptionRecord->ExceptionInformation[0] ? "write" : "read", static_cast(info->ExceptionRecord->ExceptionInformation[1])); } else { _snprintf_s(line, sizeof(line), _TRUNCATE, "CRASH 0x%08lX at %s\n", code, where); } LogLine(line); LogStack(info->ContextRecord); } return EXCEPTION_CONTINUE_SEARCH; } static int CrtReportHook(int type, char* message, int* returnValue) { char line[4096]; _snprintf_s(line, sizeof(line), _TRUNCATE, "CRTREPORT %s: %s%s", type == _CRT_ASSERT ? "assert" : type == _CRT_ERROR ? "error" : "warning", message ? message : "", message && strchr(message, '\n') ? "" : "\n"); LogLine(line); if (type != _CRT_WARN) { ExitProcess(0x5718); } *returnValue = 0; return TRUE; } static struct StandinRuntimeInit { StandinRuntimeInit() { GetModuleFileNameA(nullptr, s_logPath, sizeof(s_logPath)); char* slash = strrchr(s_logPath, '\\'); if (slash) { strcpy_s(slash + 1, sizeof(s_logPath) - (slash + 1 - s_logPath), "WowClient_standin.log"); } DeleteFileA(s_logPath); AddVectoredExceptionHandler(1, FirstChanceHandler); _CrtSetReportHook(CrtReportHook); _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); } } s_standinRuntimeInit;