diff --git a/CMakeLists.txt b/CMakeLists.txt index 9169a71..3e8ee3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,7 +114,7 @@ if(BUILD_TESTING) endif() set(hook_fixture "${CMAKE_CURRENT_BINARY_DIR}/hook-safety-fixture${arch}.exe") add_custom_command(OUTPUT "${hook_fixture}" - COMMAND "${fixture_compiler}" -std=c11 -O2 -Wall -Wextra + COMMAND "${fixture_compiler}" -std=c11 -O2 -Wall -Wextra -Wl,--stack,1048576 -o "${hook_fixture}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/hook_safety_fixture.c" -luser32 -lcomctl32 DEPENDS tests/hook_safety_fixture.c edit_hook.h VERBATIM) add_custom_target(hook-safety-fixture${arch} ALL DEPENDS "${hook_fixture}") diff --git a/edit_hook.c b/edit_hook.c index 587576c..ae2470a 100644 --- a/edit_hook.c +++ b/edit_hook.c @@ -25,14 +25,33 @@ static void selection(HWND window, DWORD *start, DWORD *end) { SendMessageW(window, EM_GETSEL, (WPARAM)start, (LPARAM)end); } +static LRESULT query_text(HWND window, UINT message, WPARAM wp, LPARAM lp) { + // SendMessage/GetWindowText routes the entire WM_GETTEXT buffer through + // WH_CALLWNDPROC. Wine copies it onto the application's callback stack, + // overflowing a 1 MiB stack at roughly 516,000 UTF-16 units. + // Call the current procedure directly instead. Reentering our busy subclass + // preserves the whole chain; DefSubclassProc here would consume callbacks + // still needed when handle_edit forwards the original message. + WNDPROC procedure = (WNDPROC)GetWindowLongPtrW(window, GWLP_WNDPROC); + return procedure ? CallWindowProcW(procedure, window, message, wp, lp) : 0; +} + static void snapshot(HWND window, EditState *state) { free(state->text); state->text = NULL; - state->length = GetWindowTextLengthW(window); + state->length = query_text(window, WM_GETTEXTLENGTH, 0, 0); if (state->length <= MAX_SNAPSHOT_UNITS) { state->text = calloc((size_t)state->length + 1, sizeof(WCHAR)); - if (state->text) - state->length = GetWindowTextW(window, state->text, state->length + 1); + if (state->text) { + LRESULT copied = query_text(window, WM_GETTEXT, state->length + 1, (LPARAM)state->text); + if (copied >= 0 && (ULONGLONG)copied <= state->length) { + state->length = copied; + } else { + // A failed query must never become a length used to index this buffer. + free(state->text); + state->text = NULL; + } + } } } diff --git a/tests/README.md b/tests/README.md index cfdc078..c46b27c 100644 --- a/tests/README.md +++ b/tests/README.md @@ -132,6 +132,17 @@ subclasses installed before and after attachment, A/W text queries, typing and destruction during a text change. The scope fixture switches between UI threads and checks that only the foreground thread acquires new Edit subclasses. +The stack fixture reserves 1 MiB and installs a real `WH_CALLWNDPROC` hook +while replacing an ANSI editor's text with 516,000, 600,000 and 1,040,000 +characters. It verifies that the complete UTF-16 snapshot survives. Short +lines avoid spending the test timeout laying out a single huge paragraph. +Calling the hook function directly only attaches the subclass; it does not +reproduce Wine copying a large `WM_GETTEXT` buffer onto its callback stack. +Before the fix, both architectures failed with an exception status stored as +the snapshot length. Snapshot queries now call the current window procedure +directly, preserving the subclass chain without invoking the message hook. +A lower subclass also verifies that the original text change still reaches it. + The original 32-bit callbacks fail the stack budget; the original global 64-bit hooks fail the background-thread check. These reproduce specific defects, without proving they caused the live MUSHclient stack overflow. Hook DLLs keep diff --git a/tests/hook_safety_fixture.c b/tests/hook_safety_fixture.c index 831ddfc..4644364 100644 --- a/tests/hook_safety_fixture.c +++ b/tests/hook_safety_fixture.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "../edit_hook.h" @@ -11,11 +12,21 @@ static WNDPROC original, outer_original; static uintptr_t deepest, hook_stack; static BOOL nested, destroy_on_set; -static unsigned calls; +static unsigned calls, forwarded_sets; + +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); } @@ -74,13 +85,18 @@ int main(int argc, char **argv) { 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); @@ -99,6 +115,43 @@ int main(int argc, char **argv) { 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; } + 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; + } + } + free(largeText); + printf("large ANSI snapshot: %lu UTF-16 units\n", (unsigned long)state->length); + } + UnhookWindowsHookEx(text_hook); // Destroy while the accessibility callback is busy; it must defer freeing // its state until the application's callback has returned. destroy_on_set = TRUE;