fix(android)!: preserve JVM settings and reuse metadata sessions (#908)

* perf(graph): avoid repeated traversal edge scans and copies

* fix(android)!: preserve JVM settings and reuse metadata sessions

Read class metadata without generating application source, retain immutable serialized APK sessions, and join owned cleanup on cancellation and shutdown.

BREAKING CHANGE: Android analysis now requires a full JDK 17 or newer with its compiler module for the packaged Java source bridge.
This commit is contained in:
morluto
2026-10-07 16:50:41 +08:00
committed by GitHub
parent f430c416c7
commit eab34a9d8c
36 changed files with 1165 additions and 138 deletions
+42 -17
View File
@@ -16,11 +16,12 @@ import { analyzeJadxRequest, type JadxToolPort } from "./JadxAnalysis.js";
import { JadxMcpTransport, type JadxLauncher } from "./JadxMcpTransport.js";
import {
jadxLoadSchema,
jadxRuntimeSchema,
parseJadxEnvelope,
parseJadxJson,
} from "./JadxProtocol.js";
import {
JADX_HEAP_LIMIT_MIB,
JADX_BRIDGE_IDENTITY,
JADX_RELEASE,
JADX_PROVIDER_IDENTITY,
JADX_LIMITATIONS,
@@ -30,7 +31,7 @@ import {
export class JadxSession {
readonly transport: JadxMcpTransport;
readonly #client = new Client({ name: "rea-android-adapter", version: "1" });
readonly #raw: JsonValue[] = [];
#loaded: ReturnType<typeof jadxLoadSchema.parse> | undefined;
constructor(
options: Omit<OwnedProviderProcessSpawnOptions, "runId" | "stdin">,
@@ -45,6 +46,7 @@ export class JadxSession {
readonly target: BinaryTarget;
readonly snapshot: string;
readonly jarHash: string;
readonly bridgeHash: string;
readonly signal: AbortSignal;
}): Promise<AnalysisExecution> {
const { request, target, snapshot, jarHash, signal } = context;
@@ -54,42 +56,64 @@ export class JadxSession {
signal.addEventListener("abort", abort, { once: true });
try {
signal.throwIfAborted();
await this.#client.connect(this.transport, { timeout: 30_000 });
this.transport.beginOperation();
const raw: JsonValue[] = [];
if (this.#loaded === undefined)
await this.#client.connect(this.transport, { timeout: 30_000 });
const server = this.#client.getServerVersion();
if (
server?.name !== "jadx-headless-mcp" ||
server.version !== JADX_RELEASE.version
server?.name !== JADX_BRIDGE_IDENTITY.name ||
server.version !== JADX_BRIDGE_IDENTITY.version
)
throw new AnalysisCapabilityUnavailableError(
"jadx",
request.operation,
`Expected jadx-headless-mcp ${JADX_RELEASE.version}; server reported ${server?.name ?? "unknown"} ${server?.version ?? "unknown"}.`,
`Expected ${JADX_BRIDGE_IDENTITY.name} ${JADX_BRIDGE_IDENTITY.version}; server reported ${server?.name ?? "unknown"} ${server?.version ?? "unknown"}.`,
);
const tools = this.#tools(request, target, signal);
const loaded = jadxLoadSchema.parse(
await tools.json("load_apk", {
path: snapshot,
threads: 1,
resources: "full",
}),
const tools = this.#tools(request, target, signal, raw);
const runtime = jadxRuntimeSchema.parse(
await tools.json("rea_jvm_status", {}),
);
if (runtime.engine_reported_version !== JADX_RELEASE.version)
throw new AnalysisCapabilityUnavailableError(
"jadx",
request.operation,
`Expected engine ${JADX_RELEASE.version}; JAR reported ${runtime.engine_reported_version}.`,
);
const loaded =
this.#loaded ??
jadxLoadSchema.parse(
await tools.json("load_apk", {
path: snapshot,
threads: 1,
resources: "full",
}),
);
if (loaded.apk_path !== snapshot)
throw new AnalysisOutputError(
request.operation,
"JADX loaded a different APK path than the admitted snapshot",
);
this.#loaded = loaded;
const engine = {
name: "jadx-headless-mcp",
version: JADX_RELEASE.version,
version: runtime.engine_reported_version,
artifact_sha256: jarHash,
source_revision:
jarHash === JADX_RELEASE.sha256 ? JADX_RELEASE.revision : null,
worker_count: 1,
heap_limit_mib: JADX_HEAP_LIMIT_MIB,
heap_limit_mib:
Math.floor(runtime.max_heap_bytes / (1024 * 1024)) || null,
} as const;
const result = await analyzeJadxRequest(tools, request, loaded, engine);
return createAnalysisExecution(result, JADX_PROVIDER_IDENTITY, {
rawResult: { server, jar_sha256: jarHash, calls: this.#raw },
rawResult: {
server,
bridge_sha256: context.bridgeHash,
jar_sha256: jarHash,
loaded,
calls: raw,
},
subject: target,
limitations: JADX_LIMITATIONS,
locations:
@@ -115,6 +139,7 @@ export class JadxSession {
request: AndroidRequest,
target: BinaryTarget,
signal: AbortSignal,
raw: JsonValue[],
): JadxToolPort {
const call = async (
name: string,
@@ -126,7 +151,7 @@ export class JadxSession {
{ timeout: 120_000, signal },
);
const envelope = parseJadxEnvelope(response, request.operation);
this.#raw.push({
raw.push({
operation: name,
input,
response: jsonValueSchema.parse(response),