Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(native-ui): handle incomplete accessibility child collections (#944)

morlutoac3bae3cb97c
diff --git a/bridge/native/NativeUIChildren.swift b/bridge/native/NativeUIChildren.swift
new file mode 100644
index 000000000..8e2afcf9a
--- /dev/null
+++ b/bridge/native/NativeUIChildren.swift
@@ -0,0 +1,11 @@
+struct ChildBatch<Element> {
+ let values: [Element]
+ let complete: Bool
+}
+
+func captureChildValues<Element>(requestedCount: Int, copy: () -> [Element]?) -> ChildBatch<Element> {
+ guard requestedCount > 0 else { return ChildBatch(values: [], complete: true) }
+ let returned = copy() ?? []
+ let values = Array(returned.prefix(requestedCount))
+ return ChildBatch(values: values, complete: values.count == requestedCount)
+}
diff --git a/bridge/native/ReaNativeUI.swift b/bridge/native/ReaNativeUI.swift
index d274a5f56..05a24ccf5 100644
--- a/bridge/native/ReaNativeUI.swift
+++ b/bridge/native/ReaNativeUI.swift
@@ -26,11 +26,12 @@ func childCount(_ element: AXUIElement) -> Int {
var count = 0
return AXUIElementGetAttributeValueCount(element, kAXChildrenAttribute as CFString, &count) == .success ? count : 0
}
-func children(_ element: AXUIElement, limit: Int) -> [AXUIElement] {
- var value: CFArray?
- let count = min(limit, childCount(element))
- if count == 0 { return [] }
- return AXUIElementCopyAttributeValues(element, kAXChildrenAttribute as CFString, 0, count, &value) == .success ? value as? [AXUIElement] ?? [] : []
+func children(_ element: AXUIElement, count: Int) -> ChildBatch<AXUIElement> {
+ captureChildValues(requestedCount: count) {
+ var value: CFArray?
+ guard AXUIElementCopyAttributeValues(element, kAXChildrenAttribute as CFString, 0, count, &value) == .success else { return nil }
+ return value as? [AXUIElement]
+ }
}
func text(_ element: AXUIElement, _ key: String) -> Any { (attribute(element, key) as? String) ?? NSNull() as Any }
func windowBounds(_ element: AXUIElement) -> CGRect? {
@@ -115,9 +116,12 @@ func observe(_ request: Request) async throws -> [String: Any] {
if path.count >= 32 { if totalChildren > 0 { truncated = true }; continue }
let available = max(0, request.max_nodes - nodes.count - pending.count)
let count = min(available, totalChildren)
- let items = children(element, limit: count)
+ let batch = children(element, count: count)
+ if !batch.complete { truncated = true }
if totalChildren > count { truncated = true }
- for index in (0..<count).reversed() { pending.append((items[index], path + [index])) }
+ for (index, item) in batch.values.enumerated().reversed() {
+ pending.append((item, path + [index]))
+ }
}
}
var screenshot: Any = NSNull()
diff --git a/package.json b/package.json
index 86de894b2..ad1e9cab9 100644
--- a/package.json
+++ b/package.json
@@ -42,6 +42,7 @@
"dist",
"install.sh",
"bridge/hopper_bridge.py",
+ "bridge/native/NativeUIChildren.swift",
"bridge/native/ReaNativeUI.swift",
"bridge/ghidra/ReaGhidraBridge.java",
"bridge/ghidra/ReaGhidraExtensions.java",
diff --git a/scripts/verify-native-ui.mjs b/scripts/verify-native-ui.mjs
index 9040f384a..f76af57d1 100644
--- a/scripts/verify-native-ui.mjs
+++ b/scripts/verify-native-ui.mjs
@@ -15,6 +15,23 @@ const root = await mkdtemp(join(tmpdir(), "rea-ui-fixture-"));
let child;
let fixturePid;
try {
+ const childRetrievalTest = join(root, "child-retrieval-test");
+ await promisify(execFile)("/usr/bin/xcrun", [
+ "swiftc",
+ fileURLToPath(
+ new URL("../bridge/native/NativeUIChildren.swift", import.meta.url),
+ ),
+ fileURLToPath(
+ new URL(
+ "../tests/conformance/native/native-ui-children/main.swift",
+ import.meta.url,
+ ),
+ ),
+ "-o",
+ childRetrievalTest,
+ ]);
+ await promisify(execFile)(childRetrievalTest, []);
+
const contents = join(root, "Fixture.app", "Contents");
await mkdir(join(contents, "MacOS"), { recursive: true });
await writeFile(
@@ -82,7 +99,6 @@ try {
"capture_native_ui_scenario",
{
...scope,
- restore: "leave-as-is",
screenshot: false,
steps: [
{ kind: "click", path: button.path },
@@ -111,8 +127,6 @@ try {
String(scope.pid),
"--window-id",
String(scope.window_id),
- "--restore",
- "leave-as-is",
"--steps",
JSON.stringify([
{ kind: "click", path: button.path },
diff --git a/src/native/NativeUiHelperRuntime.ts b/src/native/NativeUiHelperRuntime.ts
index 475704538..e4571ec91 100644
--- a/src/native/NativeUiHelperRuntime.ts
+++ b/src/native/NativeUiHelperRuntime.ts
@@ -1,4 +1,4 @@
-import { mkdtemp, rm } from "node:fs/promises";
+import { mkdtemp, rm, symlink } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
@@ -14,6 +14,13 @@ export const createNativeUiHelperRuntime = () => {
if (executable === undefined) {
root = await mkdtemp(join(tmpdir(), "rea-native-ui-"));
const output = join(root, "observer");
+ const main = join(root, "main.swift");
+ await symlink(
+ fileURLToPath(
+ new URL("../../bridge/native/ReaNativeUI.swift", import.meta.url),
+ ),
+ main,
+ );
await execFileOutput(
"/usr/bin/xcrun",
[
@@ -21,8 +28,12 @@ export const createNativeUiHelperRuntime = () => {
"-module-cache-path",
join(root, "modules"),
fileURLToPath(
- new URL("../../bridge/native/ReaNativeUI.swift", import.meta.url),
+ new URL(
+ "../../bridge/native/NativeUIChildren.swift",
+ import.meta.url,
+ ),
),
+ main,
"-o",
output,
],
diff --git a/tests/conformance/native/native-ui-children/main.swift b/tests/conformance/native/native-ui-children/main.swift
new file mode 100644
index 000000000..d76181905
--- /dev/null
+++ b/tests/conformance/native/native-ui-children/main.swift
@@ -0,0 +1,18 @@
+let failed: ChildBatch<Int> = captureChildValues(requestedCount: 2) { nil }
+precondition(failed.values.isEmpty, "AX retrieval failure must return no values")
+precondition(!failed.complete, "AX retrieval failure must mark traversal incomplete")
+
+let short = captureChildValues(requestedCount: 3) { [10, 20] }
+precondition(short.values == [10, 20], "Short retrieval must preserve returned children")
+precondition(!short.complete, "Short retrieval must mark traversal incomplete")
+var visited: [Int] = []
+for (index, value) in short.values.enumerated() {
+ visited.append(index * 10 + value)
+}
+precondition(visited == [10, 30], "Traversal must use actual returned indices")
+
+let complete = captureChildValues(requestedCount: 2) { [10, 20, 30] }
+precondition(complete.values == [10, 20], "Retrieval must respect the requested node budget")
+precondition(complete.complete, "A full bounded result must remain complete")
+
+print("Native UI child retrieval seam passed")