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:
deedra committed 2026-10-23 09:24:36 -07:00
1 parent dbf3cebe88
commit e962fd0a78
4 files changed
+88 -5

No files matched your search

+54 -1
View File
@@ -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;