// Synthetic startup fixture: process-attach callbacks see the command line before CRT argv is configured. #include #include #include #include #include namespace { struct Snapshot { int argc; bool has_argv; bool has_wargv; char argv[8][256]; wchar_t wargv[8][256]; char line[2048]; wchar_t wline[2048]; }; Snapshot g_startup{}; bool g_captured = false; template bool copy(C (&to)[N], const C* from) { if (!from) return false; size_t i = 0; for (; i + 1 < N && from[i]; ++i) to[i] = from[i]; to[i] = 0; return from[i] == 0; } bool capture(Snapshot& s) { s.argc = *__p___argc(); char** args = *__p___argv(); wchar_t** wargs = *__p___wargv(); s.has_argv = args != nullptr; s.has_wargv = wargs != nullptr; if (s.argc < 0 || s.argc > 8) return false; for (int i = 0; i < s.argc; ++i) if ((args && !copy(s.argv[i], args[i])) || (wargs && !copy(s.wargv[i], wargs[i]))) return false; return (!args || !args[s.argc]) && (!wargs || !wargs[s.argc]) && copy(s.line, GetCommandLineA()) && copy(s.wline, GetCommandLineW()); } // Wine's ucrtbase (11.20 measured) builds both argv widths while it loads, so a process-attach callback already // sees the final argc, __argv and __wargv, and a narrow main still has __wargv. Microsoft's UCRT configures neither // before CRT startup and only the narrow one for main. Only under Wine is that accepted, so on Windows a loader that // configures argv early still fails. bool host_is_wine() { static const bool wine = GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "wine_get_version") != nullptr; return wine; } bool matches(const Snapshot& a, const Snapshot& b) { // Native UCRT has not configured either argv width at process attach. GetCommandLine is already complete. bool argv_ok = a.argc == 0 && !a.has_argv && !a.has_wargv; if (!argv_ok && host_is_wine()) { // Wine: argv may already be configured at attach, but then it must be the final one. argv_ok = a.argc == b.argc && a.has_argv == b.has_argv && a.has_wargv == b.has_wargv; for (int i = 0; argv_ok && i < a.argc; ++i) argv_ok = (!a.has_argv || std::strcmp(a.argv[i], b.argv[i]) == 0) && (!a.has_wargv || std::wcscmp(a.wargv[i], b.wargv[i]) == 0); } return argv_ok && std::strcmp(a.line, b.line) == 0 && std::wcscmp(a.wline, b.wline) == 0; } } // namespace #ifdef STARTUP_CMDLINE_DLL extern "C" __declspec(dllexport) bool __cdecl startup_cmdline_matches(const Snapshot* entry) { return g_captured && matches(g_startup, *entry); } BOOL WINAPI DllMain(HINSTANCE, DWORD reason, LPVOID) { if (reason == DLL_PROCESS_ATTACH) { g_captured = capture(g_startup); if (std::strstr(GetCommandLineA(), "fail-init")) return FALSE; } return TRUE; } #else extern "C" __declspec(dllimport) bool __cdecl startup_cmdline_matches(const Snapshot* entry); namespace { void NTAPI startup_callback(PVOID, DWORD reason, PVOID) { if (reason == DLL_PROCESS_ATTACH) g_captured = capture(g_startup); } } // namespace #pragma section(".CRT$XLB", read) extern "C" __declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK g_startup_callback = startup_callback; #pragma comment(linker, "/INCLUDE:_tls_used") #pragma comment(linker, "/INCLUDE:g_startup_callback") int main(int argc, char** argv) { Snapshot entry{}; bool ok = capture(entry) && g_captured && matches(g_startup, entry) && startup_cmdline_matches(&entry); ok = ok && argc == entry.argc && entry.has_argv && (!entry.has_wargv || host_is_wine()); for (int i = 0; ok && i < argc; ++i) ok = std::strcmp(argv[i], entry.argv[i]) == 0; std::printf("tiny_startup_cmdline: %s\n", ok ? "PASS" : "FAIL"); return ok ? 0 : 1; } #endif