fix(hopper): harden Linux demo dialog handling

This commit is contained in:
Namnarak
2026-10-07 20:14:40 +07:00
parent f5fbbc3fdf
commit 16777fe7b0
4 changed files with 238 additions and 7 deletions
+65 -7
View File
@@ -24,8 +24,10 @@ SUPPORTED_HOPPER_SHA256 = {
"1339f9e58377442b0c6fcb0dfc3cec20d593cc557408521fad9a00dbc6b8da13",
}
EXPECTED_SCREEN = (1280, 1024)
EXPECTED_DIALOG = (189, 370, 901, 284)
DEMO_CLICK = (305, 632)
DIALOG_TITLES = {"Registration", "Registration — Hopper Disassembler"}
HOPPER_WINDOW_TITLE = "Hopper Disassembler"
DEMO_CLICK_LEFT = 116
DEMO_CLICK_BOTTOM = 22
PRIVATE_DISPLAY_UNAVAILABLE = 70
X11_AUTHORIZATION_FAILED = 71
UNSUPPORTED_HOPPER_BUILD = 72
@@ -197,6 +199,47 @@ def windows(x11: ctypes.CDLL, display: int, root: int) -> list[tuple[int, int, i
return found
def window_title(x11: ctypes.CDLL, display: int, window: int) -> str | None:
title = ctypes.c_char_p()
if not x11.XFetchName(display, window, ctypes.byref(title)) or not title.value:
return None
try:
return title.value.decode("utf-8", "replace")
finally:
x11.XFree(ctypes.cast(title, ctypes.c_void_p))
def window_transient_for(
x11: ctypes.CDLL, display: int, window: int
) -> int | None:
parent = ctypes.c_ulong()
if not x11.XGetTransientForHint(display, window, ctypes.byref(parent)):
return None
return int(parent.value)
def find_demo_dialog(
x11: ctypes.CDLL,
display: int,
observed: list[tuple[int, int, int, int, int]],
) -> tuple[int, int, int, int, int] | None:
for candidate in observed:
window = candidate[0]
if window_title(x11, display, window) not in DIALOG_TITLES:
continue
parent = window_transient_for(x11, display, window)
if parent is None:
continue
if window_title(x11, display, parent) == HOPPER_WINDOW_TITLE:
return candidate
return None
def demo_click(dialog: tuple[int, int, int, int, int]) -> tuple[int, int]:
_window, x, y, _width, height = dialog
return x + DEMO_CLICK_LEFT, y + height - DEMO_CLICK_BOTTOM
def configure_x11(x11: ctypes.CDLL, xtst: ctypes.CDLL) -> None:
window_pointer = ctypes.POINTER(ctypes.c_ulong)
x11.XOpenDisplay.argtypes = [ctypes.c_char_p]
@@ -213,6 +256,18 @@ def configure_x11(x11: ctypes.CDLL, xtst: ctypes.CDLL) -> None:
ctypes.POINTER(XWindowAttributes),
]
x11.XGetWindowAttributes.restype = ctypes.c_int
x11.XFetchName.argtypes = [
ctypes.c_void_p,
ctypes.c_ulong,
ctypes.POINTER(ctypes.c_char_p),
]
x11.XFetchName.restype = ctypes.c_int
x11.XGetTransientForHint.argtypes = [
ctypes.c_void_p,
ctypes.c_ulong,
ctypes.POINTER(ctypes.c_ulong),
]
x11.XGetTransientForHint.restype = ctypes.c_int
x11.XQueryTree.argtypes = [
ctypes.c_void_p,
ctypes.c_ulong,
@@ -645,6 +700,7 @@ def run_launch(args: argparse.Namespace) -> int:
deadline = time.monotonic() + 10
observed: list[tuple[int, int, int, int, int]] = []
dialog: tuple[int, int, int, int, int] | None = None
while time.monotonic() < deadline:
if socket.exists():
x11.XCloseDisplay(display)
@@ -660,7 +716,8 @@ def run_launch(args: argparse.Namespace) -> int:
"owned Hopper process exited",
)
observed = windows(x11, display, root)
if any(window[1:] == EXPECTED_DIALOG for window in observed):
dialog = find_demo_dialog(x11, display, observed)
if dialog is not None:
break
time.sleep(0.1)
else:
@@ -676,8 +733,10 @@ def run_launch(args: argparse.Namespace) -> int:
"expected Hopper demo dialog not found",
)
assert dialog is not None
click_x, click_y = demo_click(dialog)
input_results = (
xtst.XTestFakeMotionEvent(display, -1, DEMO_CLICK[0], DEMO_CLICK[1], 0),
xtst.XTestFakeMotionEvent(display, -1, click_x, click_y, 0),
xtst.XTestFakeButtonEvent(display, 1, True, 0),
xtst.XTestFakeButtonEvent(display, 1, False, 0),
)
@@ -696,9 +755,8 @@ def run_launch(args: argparse.Namespace) -> int:
finally:
if display is not None:
x11.XCloseDisplay(display)
if child is not None and child.poll() is None:
child.terminate()
child.wait()
if child is not None:
stop_process(child)
stop_process(xvfb)
capture.close()
@@ -25,6 +25,12 @@ const helperPath = fileURLToPath(
const hangingHelperPath = fileURLToPath(
new URL("../../../fixtures/x11ProbeHang.py", import.meta.url),
);
const dialogDetectionFixturePath = fileURLToPath(
new URL("../../../fixtures/hopperDialogDetection.py", import.meta.url),
);
const cleanupTimeoutFixturePath = fileURLToPath(
new URL("../../../fixtures/hopperCleanupTimeout.py", import.meta.url),
);
type DiagnosticOverrides = Partial<
Omit<HopperStartupDiagnostic, "failure_code" | "status">
@@ -81,6 +87,46 @@ const failureExit = (code: HopperStartupFailureCode): number =>
? 79
: 70;
describe("Hopper demo dialog detection", () => {
it.skipIf(process.platform !== "linux")(
"uses the Hopper transient-parent relation instead of exact dialog geometry",
() => {
const result = spawnSync(
"/usr/bin/python3",
[dialogDetectionFixturePath, helperPath],
{ encoding: "utf8" },
);
expect(result.status).toBe(0);
expect(JSON.parse(result.stdout)).toEqual({
dialog: [10, 20, 100, 1200, 284],
click: [136, 362],
wrong_parent_rejected: true,
});
},
);
});
describe("Hopper process cleanup", () => {
it.skipIf(process.platform !== "linux")(
"kills a child that ignores SIGTERM after the bounded timeout",
() => {
const result = spawnSync(
"/usr/bin/python3",
[cleanupTimeoutFixturePath, helperPath],
{ encoding: "utf8", timeout: 5_000 },
);
expect(result.status).toBe(0);
const parsed = JSON.parse(result.stdout) as {
readonly returncode: number;
readonly elapsed_ms: number;
};
expect(parsed.returncode).toBe(-9);
expect(parsed.elapsed_ms).toBeGreaterThanOrEqual(1_800);
expect(parsed.elapsed_ms).toBeLessThan(4_000);
},
);
});
describe("Linux private display selection", () => {
it("prefers a successful direct Xvfb probe", async () => {
const calls: LinuxPrivateDisplayRunnableStrategy[] = [];
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
import importlib.util
import json
import subprocess
import sys
import time
helper_path = sys.argv[1]
spec = importlib.util.spec_from_file_location("hopper_demo_x11", helper_path)
if spec is None or spec.loader is None:
raise SystemExit("unable to load helper")
helper = importlib.util.module_from_spec(spec)
spec.loader.exec_module(helper)
child = subprocess.Popen(
[
sys.executable,
"-c",
(
"import signal,time;"
"signal.signal(signal.SIGTERM, signal.SIG_IGN);"
"print('ready', flush=True);"
"time.sleep(100)"
),
],
stdout=subprocess.PIPE,
text=True,
)
assert child.stdout is not None
if child.stdout.readline().strip() != "ready":
raise SystemExit("child did not become ready")
started = time.monotonic()
helper.stop_process(child)
elapsed = time.monotonic() - started
print(
json.dumps(
{
"returncode": child.returncode,
"elapsed_ms": round(elapsed * 1000),
},
separators=(",", ":"),
)
)
if child.returncode != -9:
raise SystemExit(f"expected SIGKILL fallback, got {child.returncode}")
if not 1800 <= elapsed * 1000 < 4000:
raise SystemExit(f"unexpected cleanup duration: {elapsed}")
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/env python3
import importlib.util
import json
import sys
helper_path = sys.argv[1]
spec = importlib.util.spec_from_file_location("hopper_demo_x11", helper_path)
if spec is None or spec.loader is None:
raise SystemExit("unable to load helper")
helper = importlib.util.module_from_spec(spec)
spec.loader.exec_module(helper)
class FakeX11:
def __init__(self, titles, transients):
self.titles = titles
self.transients = transients
def XFetchName(self, _display, window, target):
value = self.titles.get(window)
if value is None:
return 0
target._obj.value = value.encode("utf-8")
return 1
def XGetTransientForHint(self, _display, window, target):
value = self.transients.get(window)
if value is None:
return 0
target._obj.value = value
return 1
def XFree(self, _value):
return 1
x11 = FakeX11(
{
10: "Registration",
11: "Hopper Disassembler",
20: "Registration",
21: "Unrelated application",
30: "Other window",
},
{
10: 11,
20: 21,
},
)
observed = [
(20, 189, 370, 901, 284),
(30, 0, 0, 1280, 1024),
(10, 20, 100, 1200, 284),
]
dialog = helper.find_demo_dialog(x11, 0, observed)
if dialog is None:
raise SystemExit("expected Hopper registration dialog was not detected")
if dialog[0] != 10:
raise SystemExit(f"wrong dialog detected: {dialog!r}")
print(
json.dumps(
{
"dialog": dialog,
"click": helper.demo_click(dialog),
"wrong_parent_rejected": helper.find_demo_dialog(
x11,
0,
[observed[0], observed[1]],
)
is None,
},
separators=(",", ":"),
)
)