// SPDX-License-Identifier: LGPL-2.1-or-later #include #include #include #include #include #include #include "../edit_hook.h" #define PROBE_MESSAGE (WM_APP + 10) #define PROBE_DEPTH 16 static WNDPROC original, outer_original; static uintptr_t deepest, hook_stack; static BOOL nested, destroy_on_set, destroy_on_get, protect_on_length; static unsigned calls, forwarded_sets; static unsigned text_queries; typedef struct EditStateProbe { WCHAR *text; DWORD length; BOOL busy, known, destroyed; BOOL history_key, history_replaced; WCHAR pending_high; DWORD pending_caret; } EditStateProbe; static LRESULT CALLBACK passthrough(HWND window, UINT message, WPARAM wp, LPARAM lp, UINT_PTR id, DWORD_PTR reference) { (void)id; (void)reference; if (message == WM_SETTEXT) ++forwarded_sets; return DefSubclassProc(window, message, wp, lp); } static LRESULT CALLBACK application_proc(HWND window, UINT message, WPARAM wp, LPARAM lp) { volatile char marker = 0; ++calls; if (message == WM_GETTEXT) ++text_queries; if (message == WM_GETTEXTLENGTH && protect_on_length) { protect_on_length = FALSE; SendMessageW(window, EM_SETPASSWORDCHAR, '*', 0); } if (message == WM_GETTEXT && destroy_on_get) { DestroyWindow(window); return 0; } if (message == PROBE_MESSAGE) { if (wp) return SendMessageA(window, PROBE_MESSAGE, wp - 1, 0); deepest = (uintptr_t)▮ return 123; } if (message == WM_SETTEXT && nested) { char text[64]; GetWindowTextA(window, text, sizeof(text)); SendMessageA(window, PROBE_MESSAGE, PROBE_DEPTH, 0); } if (message == WM_SETTEXT && destroy_on_set) { DestroyWindow(window); return FALSE; } return CallWindowProcA(original, window, message, wp, lp); } static LRESULT CALLBACK outer_proc(HWND window, UINT message, WPARAM wp, LPARAM lp) { return CallWindowProcA(outer_original, window, message, wp, lp); } static LRESULT CALLBACK observer(int code, WPARAM wp, LPARAM lp) { volatile char marker = 0; if (code >= 0 && ((CWPSTRUCT *)lp)->message == PROBE_MESSAGE) hook_stack = (uintptr_t)▮ return CallNextHookEx(NULL, code, wp, lp); } static uintptr_t probe(HWND window, BOOL busy) { volatile char marker = 0; nested = busy; if (busy) SetWindowTextA(window, "replacement"); else if (SendMessageA(window, PROBE_MESSAGE, PROBE_DEPTH, 0) != 123) ExitProcess(2); nested = FALSE; return (uintptr_t)&marker - deepest; } int main(int argc, char **argv) { if (argc != 2) return 2; HMODULE module = LoadLibraryA(argv[1]); HOOKPROC callback = module ? (HOOKPROC)GetProcAddress(module, "WineAccessMenuHook") : NULL; if (!callback) return 3; HWND window = CreateWindowA("Edit", "initial", WS_POPUP | ES_MULTILINE, 0, 0, 200, 100, NULL, NULL, GetModuleHandleW(NULL), NULL); if (!window || IsWindowUnicode(window)) return 4; original = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)application_proc); // Include comctl32's subclass dispatch in both measurements so the budget // measures our callback rather than Wine's architecture-specific thunks. if (!SetWindowSubclass(window, passthrough, 1, 0)) return 4; uintptr_t baseline = probe(window, FALSE); uintptr_t busy_baseline = probe(window, TRUE); RemoveWindowSubclass(window, passthrough, 1); // Keep another subclass below ours: snapshot queries must not consume its // place in the chain before the original text change is forwarded. if (!SetWindowSubclass(window, passthrough, 1, 0)) return 4; // Attach through the real DLL's focus path, after the application's ANSI // subclass. This also exercises Wine's A/W procedure conversion. CWPSTRUCT focus = {0, 0, WM_SETFOCUS, window}; callback(HC_ACTION, 0, (LPARAM)&focus); if (!GetPropW(window, EDIT_STATE_PROPERTY)) return 5; uintptr_t hooked = probe(window, FALSE); unsigned sets_before = forwarded_sets; uintptr_t busy_hooked = probe(window, TRUE); if (forwarded_sets != sets_before + 1) return 14; printf("edit stack: baseline=%llu hooked=%llu busy-baseline=%llu busy-hooked=%llu\n", (unsigned long long)baseline, (unsigned long long)hooked, (unsigned long long)busy_baseline, (unsigned long long)busy_hooked); // Allow comctl32 and A/W thunk overhead, but not a 4 KiB buffer frame on // every ignored or reentrant message. This compares the same call chain. BOOL stack_ok = hooked < baseline + PROBE_DEPTH * 2048 && busy_hooked < busy_baseline + PROBE_DEPTH * 2048 + 8192; outer_original = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)outer_proc); SetWindowTextA(window, "ANSI replacement"); SendMessageA(window, EM_SETSEL, 16, 16); SendMessageA(window, WM_CHAR, '!', 1); char text[64]; WCHAR wide[64]; GetWindowTextA(window, text, sizeof(text)); GetWindowTextW(window, wide, 64); if (strcmp(text, "ANSI replacement!") || lstrcmpW(wide, L"ANSI replacement!") || !calls) return 6; // MUSHclient uses ANSI Edit controls. With a real message hook installed, // Wine copies the UTF-16 WM_GETTEXT buffer onto its callback stack. // Keep this fixture's PE stack at 1 MiB and verify the hook kept a usable // full snapshot after a large ANSI text replacement. Calling the hook // directly for attachment above does not exercise this overflow. HHOOK text_hook = SetWindowsHookExW(WH_CALLWNDPROC, callback, module, GetCurrentThreadId()); if (!text_hook) return 15; const DWORD lengths[] = {516000, 600000, 1040000}; for (UINT sample = 0; sample < ARRAYSIZE(lengths); ++sample) { const DWORD largeLength = lengths[sample]; char *largeText = malloc((size_t)largeLength + 1); if (!largeText) return 11; memset(largeText, 'a', largeLength); for (DWORD i = 78; i + 1 < largeLength; i += 80) { largeText[i] = '\r'; largeText[i + 1] = '\n'; } largeText[largeLength] = 0; if (!SetWindowTextA(window, largeText)) { free(largeText); return 12; } // Programmatic transcript replacement is lazy. Navigation/focus still // obtains a complete snapshot through the stack-safe direct query. SendMessageW(window, RegisterWindowMessageW(EDIT_FOCUS_MESSAGE), 0, 0); EditStateProbe *state = (EditStateProbe *)GetPropW(window, EDIT_STATE_PROPERTY); if (!state || !state->text || state->length != largeLength || state->text[largeLength] != 0) { fprintf(stderr, "large ANSI snapshot invalid: state=%p text=%p length=%lu\n", state, state ? state->text : NULL, (unsigned long)(state ? state->length : 0)); return 13; } for (DWORD i = 0; i < largeLength; ++i) { if (state->text[i] != (unsigned char)largeText[i]) { fprintf(stderr, "large ANSI snapshot mismatch at %lu\n", (unsigned long)i); return 13; } } size_t bytes = offsetof(WineAccessEditText, text) + ((size_t)largeLength + 1) * sizeof(WCHAR); WineAccessEditText *request = calloc(1, bytes); if (!request) return 16; request->capacity = largeLength + 1; SendMessageW(window, RegisterWindowMessageW(EDIT_REVIEW_MESSAGE), 0, (LPARAM)request); if (request->status != 1 || request->copied != largeLength || request->text[largeLength]) return 17; for (DWORD i = 0; i < largeLength; ++i) if (request->text[i] != (unsigned char)largeText[i]) return 18; free(request); free(largeText); printf("large ANSI snapshot: %lu UTF-16 units\n", (unsigned long)state->length); } UnhookWindowsHookEx(text_hook); unsigned queries_before = text_queries; for (unsigned i = 0; i < 2000; ++i) { SetWindowTextA(window, "incoming transcript line"); SendMessageW(window, EM_SETSEL, (WPARAM)-1, (LPARAM)-1); SendMessageW(window, EM_REPLACESEL, FALSE, (LPARAM)L" another line"); } if (text_queries != queries_before) return 19; printf("background transcript burst: no full-text reads for 2000 updates\n"); SendMessageW(window, RegisterWindowMessageW(EDIT_FOCUS_MESSAGE), 0, 0); WineAccessEditText *private_query = calloc(1, offsetof(WineAccessEditText, text) + 64 * sizeof(WCHAR)); if (!private_query) return 22; private_query->capacity = 64; protect_on_length = TRUE; SendMessageW(window, RegisterWindowMessageW(EDIT_REVIEW_MESSAGE), 0, (LPARAM)private_query); EditStateProbe *protected_state = (EditStateProbe *)GetPropW(window, EDIT_STATE_PROPERTY); if (!protected_state || protected_state->text || protected_state->known || private_query->status) return 23; free(private_query); SendMessageW(window, EM_SETPASSWORDCHAR, 0, 0); // Destroy while the native review callback is busy. Its state must survive // until the application's query returns, then be freed exactly once. destroy_on_get = TRUE; WineAccessEditText *destroy_query = calloc(1, offsetof(WineAccessEditText, text) + 64 * sizeof(WCHAR)); if (!destroy_query) return 20; destroy_query->capacity = 64; SendMessageW(window, RegisterWindowMessageW(EDIT_REVIEW_MESSAGE), 0, (LPARAM)destroy_query); if (IsWindow(window) || destroy_query->status) return 21; free(destroy_query); destroy_on_get = FALSE; // Also retain the application's destruction-during-replacement check. window = CreateWindowA("Edit", "initial", WS_POPUP | ES_MULTILINE, 0, 0, 200, 100, NULL, NULL, GetModuleHandleW(NULL), NULL); original = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)application_proc); focus.hwnd = window; callback(HC_ACTION, 0, (LPARAM)&focus); destroy_on_set = TRUE; SetWindowTextA(window, "destroy"); if (IsWindow(window)) return 7; window = CreateWindowA("Static", "", WS_POPUP, 0, 0, 10, 10, NULL, NULL, GetModuleHandleW(NULL), NULL); HHOOK measure = SetWindowsHookExW(WH_CALLWNDPROC, observer, NULL, GetCurrentThreadId()); if (!window || !measure) return 8; SendMessageW(window, PROBE_MESSAGE, 0, 0); uintptr_t unhooked_stack = hook_stack; HHOOK hook = SetWindowsHookExW(WH_CALLWNDPROC, callback, module, GetCurrentThreadId()); if (!hook) return 9; SendMessageW(window, PROBE_MESSAGE, 0, 0); uintptr_t overhead = unhooked_stack - hook_stack; printf("menu forwarding stack overhead: %llu\n", (unsigned long long)overhead); UnhookWindowsHookEx(hook); UnhookWindowsHookEx(measure); DestroyWindow(window); if (!stack_ok || overhead >= 2048) return 10; puts("PASS: bounded forwarding stack, ANSI subclass chaining, nested queries and destruction"); return 0; }