fix(android): support owned JADX stdio on Windows (#1276)

Support owned JADX stdio on Windows x64, preserve protocol input errors through cancellation, and verify native stdin and Android CLI/MCP lifecycle on the real Windows host.

Closes #1262
This commit is contained in:
Ysopwnable
2026-10-09 20:34:42 +08:00
committed by GitHub
parent b71a591a60
commit 636b245f1b
21 changed files with 954 additions and 49 deletions
+237
View File
@@ -0,0 +1,237 @@
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { setTimeout as delay } from "node:timers/promises";
/** Exercise owned Windows stdin through its production Writable and native boundary. */
export async function verifyWindowsProtocolInput(packageRoot, workspace) {
const { WindowsOwnedProcess } = await import(
pathToFileURL(join(packageRoot, "dist/windows/WindowsOwnedProcess.js"))
);
const { requireWindowsNativeAuthority } = await import(
pathToFileURL(join(packageRoot, "dist/windows/WindowsNativeLoader.js"))
);
const native = requireWindowsNativeAuthority();
assert.equal(native.inspection.protocolStdin, true);
const launch = (script) => acquire(WindowsOwnedProcess, workspace, script);
const echo = await verifyEcho(launch);
const cancellation = await verifyBlockedCancellation(launch);
await verifyNativePendingWrite(native, workspace);
const { JadxMcpTransport } = await import(
pathToFileURL(join(packageRoot, "dist/android/JadxMcpTransport.js"))
);
await verifyTransportPendingWrite(JadxMcpTransport, workspace);
return {
echo,
cancellation,
native_pending_write_closed: true,
forged_input_handle_rejected: true,
malformed_input_rejected: true,
provider_transport_pending_write_cancelled: true,
};
}
function acquire(Process, workspace, script) {
const child = new Process(
process.execPath,
["-e", script],
workspace,
process.env,
false,
true,
);
let stdout = "";
let stderr = "";
child.stdout.on("data", (bytes) => {
stdout += bytes.toString();
});
child.stderr.on("data", (bytes) => {
stderr += bytes.toString();
});
const inputErrors = [];
child.stdin.on("error", (cause) => inputErrors.push(cause.message));
const closed = new Promise((resolve, reject) => {
child.once("error", reject);
child.once("close", (code) => resolve({ code, stdout, stderr }));
});
return { child, closed, inputErrors, stdout: () => stdout };
}
async function verifyEcho(launch) {
const acquired = launch(`
const {createHash} = require('node:crypto');
const hash = createHash('sha256'); let count = 0;
process.stdin.on('data', bytes => { hash.update(bytes); count += bytes.length; });
process.stdin.pause(); setTimeout(() => process.stdin.resume(), 250);
process.stdin.on('end', () => console.log(JSON.stringify({count,sha256:hash.digest('hex')})));
`);
const bytes = Buffer.from(
"caller-selected UTF8: 路径 Ω; binary:\0\n".repeat(60000),
);
const expected = {
count: bytes.length,
sha256: createHash("sha256").update(bytes).digest("hex"),
};
let ticks = 0;
const timer = setInterval(() => ticks++, 20);
try {
let writable;
await new Promise((resolve, reject) => {
writable = acquired.child.stdin.write(bytes, (cause) =>
cause ? reject(cause) : resolve(),
);
// The native worker owns its copy before returning to the caller.
bytes.fill(0x5a);
});
assert.equal(
writable,
false,
"Large protocol input did not expose backpressure",
);
assert.ok(ticks > 0, "Input write blocked the JS event loop");
acquired.child.stdin.end();
const result = await deadline(acquired.closed);
assert.equal(result.code, 0, result.stderr);
assert.deepEqual(JSON.parse(result.stdout), expected);
assert.deepEqual(acquired.inputErrors, []);
assert.equal((await acquired.child.cleanup()).cleaned, true);
return {
bytes: expected.count,
sha256: expected.sha256,
backpressure: true,
event_loop_responsive: true,
eof_observed: true,
};
} finally {
clearInterval(timer);
await acquired.child.cleanup();
}
}
async function verifyBlockedCancellation(launch) {
const acquired = launch(`
const {spawn} = require('node:child_process');
const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], {stdio:'inherit'});
console.log('owned descendant ' + child.pid);
setInterval(() => {}, 1000);
`);
try {
for (
let attempt = 0;
attempt < 100 && !acquired.stdout().includes("owned descendant");
attempt++
)
await delay(25);
assert.match(acquired.stdout(), /owned descendant \d+/u);
const write = new Promise((resolve) =>
acquired.child.stdin.write(Buffer.alloc(2 * 1024 * 1024), (cause) =>
resolve(cause),
),
);
await delay(100);
const cleanup = await acquired.child.cleanup();
assert.equal(cleanup.cleaned, true, cleanup.reason);
assert.ok(
await deadline(write),
"Closing the owned job reported a successful blocked write",
);
await deadline(acquired.closed);
return { blocked_write_cancelled: true, descendant_job_settled: true };
} finally {
await acquired.child.cleanup();
}
}
async function verifyNativePendingWrite(native, workspace) {
const quote = (value) =>
`"${value.replace(/(\\*)"/gu, '$1$1\\"').replace(/(\\+)$/u, "$1$1")}"`;
const child = native.call("process_spawn", [
process.execPath,
[process.execPath, "-e", "setInterval(() => {}, 1000)"]
.map(quote)
.join(" "),
workspace,
Object.entries(process.env)
.filter(([, value]) => value !== undefined)
.map(([key, value]) => `${key}=${value}`),
true,
]);
let rejected;
try {
assert.throws(
() => native.call("process_stdin_write", [child.handle, "not a Buffer"]),
/must be a Buffer/u,
);
const pending = native.call("process_stdin_write", [
child.handle,
Buffer.alloc(2 * 1024 * 1024),
]);
rejected = pending.then(
() => false,
(cause) => cause,
);
assert.throws(
() =>
native.call("process_stdin_write", [
child.handle,
Buffer.from("second"),
]),
/pending write/u,
);
assert.throws(
() => native.call("process_stdin_write", [{}, Buffer.from("forged")]),
/does not belong to REA/u,
);
} finally {
// The worker retains its handle/buffer after the JS resource closes.
native.call("process_close", [child.handle]);
}
const failure = await deadline(rejected);
assert.ok(failure instanceof Error);
assert.match(failure.message, /Write owned process input failed/u);
}
async function verifyTransportPendingWrite(Transport, workspace) {
const transport = new Transport({
command: process.execPath,
arguments: ["-e", "setInterval(() => {}, 1000)"],
cwd: workspace,
});
transport.onerror = () => {};
try {
await transport.start();
const pending = transport
.send({
jsonrpc: "2.0",
id: "blocked",
method: "probe",
params: { payload: "x".repeat(2 * 1024 * 1024) },
})
.then(
() => false,
(cause) => cause,
);
await delay(100);
await transport.close();
assert.ok(
await deadline(pending),
"Transport cancellation accepted a blocked write",
);
} finally {
await transport.close();
}
}
async function deadline(promise) {
let timer;
return Promise.race([
promise,
new Promise((_, reject) => {
timer = setTimeout(
() => reject(new Error("Owned input fixture exceeded its deadline")),
10000,
);
}),
]).finally(() => clearTimeout(timer));
}
+5
View File
@@ -17,6 +17,7 @@ import {
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { verifyWindowsProtocolInput } from "./lib/windows-protocol-input.mjs";
if (process.platform !== "win32")
throw new Error(
@@ -411,6 +412,10 @@ setInterval(()=>{},1000);\n`,
await delay();
assert.equal(live(ownerClose.pid), false);
report.processes.killOnOwnerClose = true;
report.processes.protocolInput = await verifyWindowsProtocolInput(
packageRoot,
workspace,
);
const exited = launch([
"--max-old-space-size=128",
"-e",
+20 -7
View File
@@ -13,6 +13,8 @@ import fixture from "../../fixtures/android/apidemos.json" with { type: "json" }
import { verifyAndroidCliCancellation } from "./cli-cancellation.mjs";
import { verifyAndroidMetadata } from "./metadata.mjs";
import { verifyAndroidFailureRecovery } from "./failure-recovery.mjs";
import { resolveJadxConfiguration } from "../../../dist/android/JadxConfiguration.js";
import { verifyWindowsAndroidLifecycle } from "./windows-lifecycle.mjs";
const repository = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
const apk = process.env.REA_ANDROID_TEST_APK;
@@ -33,10 +35,10 @@ assert.equal(
JADX_RELEASE.sha256,
"Real Android lane requires the audited headless JADX release JAR",
);
const java =
process.env.JAVA_HOME === undefined
? "java"
: resolve(process.env.JAVA_HOME, "bin/java");
const { java, jvmArguments } = await resolveJadxConfiguration(
process.env,
"inspect_android_package",
);
const execute = promisify(execFile);
await execute(java, ["-version"], { timeout: 10_000 });
const entrypoint =
@@ -49,6 +51,7 @@ await verifyAndroidMetadata({
apk: path,
repository,
execute,
jvmArguments,
bridge: resolve(dirname(entrypoint), "../bridge/android/ReaJadxBridge.java"),
});
const tasks = [
@@ -200,15 +203,22 @@ assert.ok(
reference.dex_offset === null && reference.source_line === null,
),
);
await verifyAndroidCliCancellation({
const lifecycleOptions = {
entrypoint,
environment,
path,
repository,
execute,
});
};
const windowsLifecycle =
process.platform === "win32"
? await verifyWindowsAndroidLifecycle(lifecycleOptions)
: undefined;
if (windowsLifecycle === undefined)
await verifyAndroidCliCancellation(lifecycleOptions);
await verifyAndroidFailureRecovery({
java,
jvmArguments,
entrypoint,
environment,
path,
@@ -230,7 +240,10 @@ const report = {
cli_mcp_parity: true,
original_apk_unchanged: true,
apk_executed: false,
cli_sigterm_cleanup: true,
cli_sigterm_cleanup: process.platform !== "win32",
...(windowsLifecycle === undefined
? {}
: { windows_lifecycle: windowsLifecycle }),
metadata_without_code_generation: true,
stable_overload_selection: true,
};
+24 -6
View File
@@ -16,6 +16,7 @@ import { TOOL_CONTRACTS } from "../../../dist/contracts/toolContracts.js";
/** Real unusable Java runtimes and selectors must lead to applicable recovery. */
export async function verifyAndroidFailureRecovery({
java,
jvmArguments = [],
entrypoint,
environment,
path,
@@ -24,6 +25,8 @@ export async function verifyAndroidFailureRecovery({
fixture,
}) {
const root = await mkdtemp(join(tmpdir(), "rea-android-recovery-"));
const javaName = process.platform === "win32" ? "java.exe" : "java";
const jlinkName = process.platform === "win32" ? "jlink.exe" : "jlink";
try {
const withoutJar = { ...environment };
delete withoutJar.REA_JADX_MCP_JAR;
@@ -36,10 +39,10 @@ export async function verifyAndroidFailureRecovery({
});
let jlink;
for (const candidate of isAbsolute(java)
? [join(dirname(java), "jlink")]
? [join(dirname(java), jlinkName)]
: (environment.PATH ?? "")
.split(delimiter)
.map((directory) => join(directory, "jlink"))) {
.map((directory) => join(directory, jlinkName))) {
try {
await access(candidate);
jlink = candidate;
@@ -53,10 +56,22 @@ export async function verifyAndroidFailureRecovery({
"Android failure-recovery verification requires the full JDK's jlink via JAVA_HOME or PATH.",
);
const jre = join(root, "jre");
await execute(jlink, ["--add-modules", "java.base", "--output", jre], {
timeout: 60_000,
await execute(
jlink,
[
...jvmArguments.map((argument) => `-J${argument}`),
"--add-modules",
"java.base",
"--output",
jre,
],
{
timeout: 60_000,
},
);
await execute(join(jre, "bin", javaName), ["-version"], {
timeout: 10_000,
});
await execute(join(jre, "bin/java"), ["-version"], { timeout: 10_000 });
const empty = join(root, "empty-path");
await mkdir(empty);
if (process.platform === "linux") {
@@ -74,6 +89,8 @@ export async function verifyAndroidFailureRecovery({
}
const withoutJavaHome = { ...environment };
delete withoutJavaHome.JAVA_HOME;
for (const key of Object.keys(withoutJavaHome))
if (key.toLowerCase() === "path") delete withoutJavaHome[key];
const invalidJar = join(root, "invalid.jar");
await writeFile(invalidJar, "not a Java archive");
const environments = [
@@ -85,7 +102,8 @@ export async function verifyAndroidFailureRecovery({
{
label: "missing java",
env: { ...withoutJavaHome, PATH: empty },
observation: "ENOENT",
observation:
process.platform === "win32" ? "Cannot find java.exe" : "ENOENT",
},
{
label: "invalid engine archive with a working JDK",
+8 -1
View File
@@ -11,11 +11,16 @@ export const verifyAndroidMetadata = async ({
bridge,
repository,
execute,
jvmArguments = [],
}) => {
const javac =
process.env.JAVA_HOME === undefined
? "javac"
: resolve(process.env.JAVA_HOME, "bin/javac");
: resolve(
process.env.JAVA_HOME,
"bin",
process.platform === "win32" ? "javac.exe" : "javac",
);
try {
await execute(javac, ["-version"], { timeout: 10_000 });
} catch (cause) {
@@ -29,6 +34,7 @@ export const verifyAndroidMetadata = async ({
await execute(
javac,
[
...jvmArguments.map((argument) => `-J${argument}`),
"-classpath",
jar,
"-d",
@@ -44,6 +50,7 @@ export const verifyAndroidMetadata = async ({
const result = await execute(
java,
[
...jvmArguments,
"-classpath",
`${classes}${delimiter}${jar}`,
"ReaJadxMetadataRegression",
@@ -0,0 +1,301 @@
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import {
mkdtemp,
readdir,
realpath,
rmdir,
symlink,
unlink,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { resolveJadxConfiguration } from "../../../dist/android/JadxConfiguration.js";
/** Verify real Windows cancellation, disconnect and abrupt CLI owner exit. */
export async function verifyWindowsAndroidLifecycle(options) {
assert.equal(process.platform, "win32");
await verifyJavaSelection(options.environment);
const cancellation = await verifyMcp(options, "cancel");
await verifyMcp(options, "disconnect");
await verifyCliOwnerExit(options);
return {
mcp_cancelled_engine_exited: true,
mcp_cancelled_workspace_removed: true,
mcp_ping_and_next_call: true,
mcp_disconnect_engine_exited: true,
cli_abrupt_owner_exit_engine_exited: true,
// A forced Windows process exit cannot execute JavaScript workspace cleanup.
cli_abrupt_owner_exit_workspace_cleanup: false,
observed_resources: cancellation,
java_home_and_quoted_case_insensitive_path: true,
};
}
async function verifyJavaSelection(environment) {
const selected = await resolveJadxConfiguration(
environment,
"inspect_android_package",
);
const root = await mkdtemp(join(tmpdir(), "rea-windows-java-selection-"));
const alias = join(root, "JDK 路径 with spaces");
const pathEnvironment = Object.fromEntries(
Object.entries(environment).filter(
([key]) => !["path", "java_home"].includes(key.toLowerCase()),
),
);
try {
// The owned junction borrows the selected JDK directory without copying it.
await symlink(dirname(selected.java), alias, "junction");
pathEnvironment.Path = `"${alias}"`;
const onPath = await resolveJadxConfiguration(
pathEnvironment,
"inspect_android_package",
);
assert.equal(onPath.java, await realpath(selected.java));
console.log(
"PASS Windows JAVA_HOME and quoted Unicode/space Path selection",
);
} finally {
await unlink(alias).catch((cause) => {
if (cause.code !== "ENOENT") throw cause;
});
await rmdir(root);
}
}
async function verifyMcp({ entrypoint, environment, path, repository }, mode) {
const baseline = await runtimeRoots();
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
cwd: repository,
env: environment,
stderr: "pipe",
});
let stderr = "";
transport.stderr?.on("data", (chunk) => {
stderr = (stderr + chunk.toString()).slice(-65536);
});
const client = new Client({
name: `rea-windows-android-${mode}`,
version: "1",
});
let observer;
try {
await client.connect(transport);
assert.ok(transport.pid !== null);
observer = observeJava(transport.pid);
const controller = new AbortController();
const pending = client
.callTool(
{ name: "inspect_android_package", arguments: { path } },
{ timeout: 150000, signal: controller.signal },
)
.then(
(response) => ({ response }),
(error) => ({ error }),
);
const resources = await observer.acquired;
if (mode === "cancel") controller.abort();
else await client.close();
const outcome = await deadline(pending, 15000);
assert.ok("error" in outcome, "Cancelled request published a success");
await observer.verifyExit();
await waitFor(
async () => {
const remaining = await runtimeRoots();
return remaining.every((root) => baseline.includes(root));
},
15000,
"Owned Android workspace remained after cleanup",
);
if (mode === "cancel") {
await waitFor(
() => stderr.includes('"status":"error"'),
15000,
`Server did not finish the cancelled request: ${stderr}`,
);
assert.ok(
!stderr.includes('"status":"ok"'),
"Cancelled operation was logged successful",
);
await client.ping();
const next = await client.callTool(
{ name: "inspect_android_package", arguments: { path } },
{ timeout: 150000 },
);
assert.notEqual(next.isError, true, JSON.stringify(next));
assert.ok(next.structuredContent?.evidence_id);
}
console.log(
`PASS Windows MCP ${mode}: real Java exit and workspace cleanup`,
);
return resources;
} finally {
await client.close();
await transport.close();
await observer?.close();
}
}
async function verifyCliOwnerExit({
entrypoint,
environment,
path,
repository,
}) {
const child = spawn(
process.execPath,
[entrypoint, "inspect-android-package", path, "--format", "json"],
{ cwd: repository, env: environment, stdio: ["ignore", "pipe", "pipe"] },
);
let output = "";
child.stdout.on("data", (chunk) => {
output = (output + chunk.toString()).slice(-65536);
});
child.stderr.resume();
const exited = new Promise((resolve, reject) => {
child.once("error", reject);
child.once("close", resolve);
});
assert.ok(child.pid !== undefined);
const observer = observeJava(child.pid);
try {
await observer.acquired;
// Node's Windows SIGTERM terminates the retained child handle abruptly.
// The Java job must settle through kill-on-owner-close without JS cleanup.
assert.equal(child.kill(), true);
await deadline(exited, 15000);
await observer.verifyExit();
assert.equal(output, "", "Aborted CLI published successful Evidence");
console.log("PASS Windows CLI abrupt owner exit: real Java job cleanup");
} finally {
if (child.exitCode === null && child.signalCode === null) child.kill();
await deadline(exited, 15000);
await observer.close();
}
}
function observeJava(ownerPid) {
assert.ok(Number.isSafeInteger(ownerPid) && ownerPid > 0);
// Retain actual process handles and creation times. The process table only
// discovers the child; no PID or executable path grants termination authority.
const source = `
$ErrorActionPreference='Stop'
$owner=Get-Process -Id ${ownerPid}
$null=$owner.Handle; $ownerStart=$owner.StartTime
$java=$null; $deadline=[DateTime]::UtcNow.AddSeconds(45)
try {
while ([DateTime]::UtcNow -lt $deadline) {
if ($owner.HasExited) { throw 'REA owner exited before Java acquisition' }
$rows=Get-CimInstance Win32_Process -Filter 'ParentProcessId = ${ownerPid}'
foreach ($row in $rows) {
if ($row.Name -eq 'java.exe' -and $row.CommandLine -like '*ReaJadxBridge.java*') {
$candidate=Get-Process -Id $row.ProcessId
$null=$candidate.Handle
$sameCreation=$candidate.StartTime.ToUniversalTime().ToString('yyyyMMddHHmmssffffff') -eq $row.CreationDate.ToUniversalTime().ToString('yyyyMMddHHmmssffffff')
if ($sameCreation -and $candidate.StartTime -ge $ownerStart -and !$candidate.HasExited) {
$java=$candidate
$resourceArgs=@([regex]::Matches($row.CommandLine,'-Xmx[0-9]+m|-XX:ActiveProcessorCount=[0-9]+') | ForEach-Object {$_.Value})
@{affinity=[int64]$java.ProcessorAffinity; priority=[string]$java.PriorityClass; jvm_arguments=$resourceArgs} | ConvertTo-Json -Compress
break
}
$candidate.Dispose()
}
}
if ($null -ne $java) { break }
Start-Sleep -Milliseconds 100
}
if ($null -eq $java) { throw 'No real owned Java engine was acquired' }
$null=[Console]::ReadLine()
if (!$java.WaitForExit(15000)) { throw 'Retained Java process did not exit' }
@{exited=$java.HasExited} | ConvertTo-Json -Compress
} finally { if ($null -ne $java) {$java.Dispose()}; $owner.Dispose() }
`;
const observer = spawn(
join(
process.env.SystemRoot ?? "C:\\Windows",
"System32",
"WindowsPowerShell",
"v1.0",
"powershell.exe",
),
[
"-NoProfile",
"-NonInteractive",
"-EncodedCommand",
Buffer.from(source, "utf16le").toString("base64"),
],
{ stdio: ["pipe", "pipe", "pipe"], windowsHide: true },
);
let stdout = "";
let stderr = "";
observer.stdout.on("data", (chunk) => {
stdout = (stdout + chunk.toString()).slice(-65536);
});
observer.stderr.on("data", (chunk) => {
stderr = (stderr + chunk.toString()).slice(-65536);
});
const exited = new Promise((resolve, reject) => {
observer.once("error", reject);
observer.once("close", resolve);
});
const acquired = waitFor(
() => stdout.includes("\n") || observer.exitCode !== null,
50000,
"Java process observer did not acquire a handle",
).then(() => {
assert.ok(stdout.includes("\n"), stderr);
return JSON.parse(stdout.split(/\r?\n/u)[0]);
});
return {
acquired,
async verifyExit() {
observer.stdin.end("verify\n");
assert.equal(await deadline(exited, 20000), 0, stderr);
assert.equal(
JSON.parse(stdout.trim().split(/\r?\n/u).at(-1)).exited,
true,
);
},
async close() {
if (observer.exitCode === null && observer.signalCode === null) {
observer.stdin.end();
observer.kill();
}
await deadline(exited, 20000);
},
};
}
async function runtimeRoots() {
return (await readdir(tmpdir())).filter((name) =>
name.startsWith("rea-android-"),
);
}
async function waitFor(predicate, milliseconds, message) {
const until = Date.now() + milliseconds;
while (Date.now() < until) {
if (await predicate()) return;
await delay(50);
}
throw new Error(message);
}
async function deadline(promise, milliseconds) {
let timer;
return Promise.race([
promise,
new Promise((_, reject) => {
timer = setTimeout(
() => reject(new Error("Windows lifecycle deadline exceeded")),
milliseconds,
);
}),
]).finally(() => clearTimeout(timer));
}