Avoid callback stack overflow when snapshotting large Edit controls
Reading a large ANSI Edit control through GetWindowTextW sends a WM_GETTEXT buffer through WH_CALLWNDPROC. Wine copies that UTF-16 buffer onto the application's callback stack, exhausting a 1 MiB stack at about 516,000 code units and potentially leaving an exception status in the cached snapshot length. Query WM_GETTEXTLENGTH and WM_GETTEXT through the current window procedure with CallWindowProcW instead. This avoids the message-hook buffer copy while preserving ANSI/Unicode conversion and the application's subclass chain. Reenter the busy accessibility subclass rather than calling DefSubclassProc directly, which can consume callbacks needed to forward the original edit message. Reject negative or oversized copy results and discard the unusable buffer before its length can be used for indexing. Extend the native hook-safety fixture for both 32-bit and 64-bit DLLs: - Reserve a 1 MiB PE stack and install a real WH_CALLWNDPROC hook. - Replace ANSI text with 516,000, 600,000 and 1,040,000 characters and verify the complete UTF-16 snapshot, its length and its terminator. - Use short lines to avoid excessive paragraph-layout time. - Keep a lower subclass installed and verify that the original text replacement still reaches it exactly once during the reentrant probe. Document the reproduction, why direct hook invocation alone misses it, and the remaining distinction between fixture coverage and live MUSHclient acceptance. Validation: - Synchronized deedra with its upstream and origin/master; both were already up to date. - cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=winelib-toolchain.cmake - cmake --build build --parallel: passed. - git diff --check: passed. - xvfb-run -a ctest --test-dir build --output-on-failure: interrupted at the user's request to skip tests. Foreground-focus, msaa-states, review-capture64 and review-capture32 passed before interruption; hook-safety had started but has no completed result from this run. - Further integration tests were skipped at the user's request. Live MUSHclient behavior, audible speech and physical braille remain unverified by this commit session. Human review and submission remain the contributor's responsibility under the README policy.
This commit is contained in:
1 parent
dbf3cebe88
commit
e962fd0a78
4 files changed
+88
-5
No files matched your search
+1
-1
@@ -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}")
|
||||
|
||||
+22
-3
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <commctrl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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;
|
||||
|
||||
Reference in new issue
Block a user