mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
* refactor(process): compose supervision and provider-client cleanup failures through the shared owner * refactor(native): compose LLDB, native UI and signature cleanup failures through the shared owner (#10908) * refactor(native): compose LLDB, native UI and signature cleanup failures through the shared owner * refactor(server): compose close, reset and session-status cleanup failures through the shared owner (#10915) * refactor(dotnet): compose managed snapshot cleanup failures through the shared owner (#10917) * refactor(dotnet): compose managed snapshot cleanup failures through the shared owner * refactor(flutter): compose build cleanup failures through the shared owner (#10918) * refactor(flutter): compose build cleanup failures through the shared owner * refactor(apple): compose asset catalog and keyed archive cleanup failures through the shared owner (#10919) * refactor(ghidra): join session cleanup failures through the shared owner (#10920) * refactor(ghidra): join session cleanup failures through the shared owner * refactor(desktop): compose build-setup cleanup failures through the shared owner (#10921) * refactor(desktop): compose build-setup cleanup failures through the shared owner * refactor(cutter): compose bridge service cleanup failures through the shared owner (#10922) * refactor(cutter): compose bridge service cleanup failures through the shared owner * refactor(electron): compose fuse and file-scope cleanup failures through the shared owner (#10923) * refactor(electron): compose fuse and file-scope cleanup failures through the shared owner * refactor(browser): compose scenario, web-bundle and capture cleanup failures through the shared owner (#10924) * refactor(application): compose uninstall, session retirement and decompile-all cleanup failures through the shared owner (#10933) * refactor(application): compose uninstall, session retirement and decompile-all cleanup failures through the shared owner * refactor(ghidra): compose Java probe, NativeAOT snapshot and provider-client cleanup failures through the shared owner (#10927) * refactor(ghidra): compose Java probe, NativeAOT snapshot and provider-client cleanup failures through the shared owner * refactor(hopper): compose provider close cleanup failures through the shared owner (#10928) * refactor(hopper): compose provider close cleanup failures through the shared owner * refactor(javascript): compose analysis worker runtime and inspection failures through the shared owner (#10929) * refactor(javascript): compose analysis worker runtime and inspection failures through the shared owner * refactor(kuna): compose project-cache and target-command cleanup failures through the shared owner (#10930) * refactor(kuna): compose project-cache and target-command cleanup failures through the shared owner * refactor(native): compose macOS provider, pwndbg and pwntools cleanup failures through the shared owner (#10931) * refactor(native): compose macOS provider, pwndbg and pwntools cleanup failures through the shared owner * refactor(wasm): compose WABT artifact cleanup failures through the shared owner (#10932) * refactor(wasm): compose WABT artifact cleanup failures through the shared owner * refactor(application): compose analysis-view cancellation and retained-content cleanup failures through the shared owner (#10941) * refactor(application): compose analysis-view cancellation and retained-content cleanup failures through the shared owner * refactor(kuna): compose span-close cancellation cleanup through the shared owner (#10942) * refactor(decompile): bind the initialization failure with its cleanup through the shared owner (#10965) * refactor(decompile): bind the initialization failure with its cleanup through the shared owner * refactor(cli): bind environment serve listener and close failures through the shared owner (#10966) * refactor(cli): bind environment serve listener and close failures through the shared owner * refactor(kuna): bind project export and span-read failures with their cleanup through the shared owner (#10967) * refactor(kuna): bind project export and span-read failures with their cleanup through the shared owner * refactor(cutter): bind descriptor read failures with their cleanup through the shared owner (#10968) * refactor(cutter): bind descriptor read failures with their cleanup through the shared owner * refactor(ida): bind workspace file failures with their cleanup through the shared owner (#10969) * refactor(ida): bind workspace file failures with their cleanup through the shared owner * refactor(rizin): bind debug finalization failures with their cleanup through the shared owner (#10970) * refactor(rizin): bind debug finalization failures with their cleanup through the shared owner * refactor(application): compose close rejections with unknown completion through one owner (#10971) * refactor(application): compose close rejections with unknown completion through one owner * refactor(hopper): compose startup and launcher close rejections through the shared owner (#10972) * refactor(hopper): compose startup and launcher close rejections through the shared owner * refactor(desktop): compose task-file close rejections through the shared owner (#10973) * fix(hopper): confirm reported close failures when a later close of the client succeeds * fix(hopper): keep owned close failures reading their own owner on retry HopperClient bound every failed close part to its client retry, so a launcher aggregate that already follows its child failures stayed pending after the launcher released them. AnalysisCleanupRelease.bindUnowned now owns the rule that a retry claims only ownerless failures; BinarySession's local copy of it is removed. * fix(hopper): release each close part's earlier failures when that part succeeds One shared retry release made the session part's earlier failures wait for the launcher part too, so a later close whose session cleanup succeeded kept reporting the session's released resources. HopperClient keeps one release per part (session, launcher), and each part confirms its own failures when it succeeds.
384 lines
13 KiB
TypeScript
384 lines
13 KiB
TypeScript
import type { JavaScriptMemberMutationAllowance } from "../domain/javascript/javascriptSemanticWorkAllowance.js";
|
|
import type { WebPageInspection } from "../domain/browserObservationSchemas.js";
|
|
import {
|
|
DEFAULT_WEB_BUNDLE_HEAP_MB,
|
|
DEFAULT_WEB_BUNDLE_SCRIPT_TIMEOUT_MS,
|
|
DEFAULT_WEB_BUNDLE_TOTAL_TIMEOUT_MS,
|
|
webBundleTotalTimeoutRecovery,
|
|
type AnalyzeWebBundleInput,
|
|
type WebBundleAnalysis,
|
|
} from "../domain/webBundleAnalysis.js";
|
|
import { buildCapturedWebBundleAnalysis } from "../domain/webBundleAnalyzerResult.js";
|
|
import { isIncludedScript } from "../domain/webBundleAnalyzerState.js";
|
|
import {
|
|
unavailableWebScriptRecovery,
|
|
type WebBundleScriptAnalysis,
|
|
} from "../domain/webBundleScriptAnalysis.js";
|
|
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import {
|
|
AnalysisCapabilityUnavailableError,
|
|
AnalysisTimeoutError,
|
|
AnalysisResourceConstraintError,
|
|
AnalysisOutputError,
|
|
cancelledBy,
|
|
OwnerCancellation,
|
|
} from "../domain/analysisErrorCore.js";
|
|
import { analysisErrorWithCleanupDependencies } from "../domain/analysisErrorCleanup.js";
|
|
import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js";
|
|
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import { ProviderStartupDeadline } from "../process/ProviderDeadline.js";
|
|
import {
|
|
WebBundleAnalysisWorker,
|
|
WebBundleWorkerCleanup,
|
|
} from "./WebBundleAnalysisWorker.js";
|
|
|
|
export type WebBundleAnalysisWorkerPort = Pick<
|
|
WebBundleAnalysisWorker,
|
|
"analyze" | "close"
|
|
>;
|
|
|
|
/**
|
|
* One host's web-analyzer permit and failed-cleanup set. Independent
|
|
* admissions do not share a reservation.
|
|
*/
|
|
export class WebBundleAnalysisAdmission {
|
|
readonly workers = new WebBundleWorkerCleanup();
|
|
#active: IsolatedWebBundleAnalyzer | undefined;
|
|
readonly #pendingCleanup = new Set<IsolatedWebBundleAnalyzer>();
|
|
|
|
active(): IsolatedWebBundleAnalyzer | undefined {
|
|
return this.#active;
|
|
}
|
|
|
|
hold(analyzer: IsolatedWebBundleAnalyzer): void {
|
|
this.#active = analyzer;
|
|
}
|
|
|
|
release(analyzer: IsolatedWebBundleAnalyzer): void {
|
|
if (this.#active === analyzer) this.#active = undefined;
|
|
}
|
|
|
|
pending(): readonly IsolatedWebBundleAnalyzer[] {
|
|
return [...this.#pendingCleanup];
|
|
}
|
|
|
|
retain(analyzer: IsolatedWebBundleAnalyzer): void {
|
|
this.#pendingCleanup.add(analyzer);
|
|
}
|
|
|
|
forget(analyzer: IsolatedWebBundleAnalyzer): void {
|
|
this.#pendingCleanup.delete(analyzer);
|
|
}
|
|
}
|
|
|
|
/** Own one bundle's sequential analysis and finalization through provider close. */
|
|
export class IsolatedWebBundleAnalyzer {
|
|
readonly #admission: WebBundleAnalysisAdmission;
|
|
#closed = false;
|
|
#running: Promise<Result<WebBundleAnalysis, AnalysisError>> | undefined;
|
|
#controller: AbortController | undefined;
|
|
#worker: WebBundleAnalysisWorkerPort | undefined;
|
|
|
|
readonly createWorker: (heapMb: number) => WebBundleAnalysisWorkerPort;
|
|
|
|
constructor(
|
|
createWorker?: (heapMb: number) => WebBundleAnalysisWorkerPort,
|
|
admission: WebBundleAnalysisAdmission = new WebBundleAnalysisAdmission(),
|
|
) {
|
|
this.#admission = admission;
|
|
this.createWorker =
|
|
createWorker ??
|
|
((heapMb) =>
|
|
new WebBundleAnalysisWorker(heapMb, undefined, admission.workers));
|
|
}
|
|
|
|
/** Reserve capacity before capture, so concurrent captures cannot retain source inventories. */
|
|
async reserve(): Promise<Result<void, AnalysisError>> {
|
|
if (this.#closed)
|
|
return err(
|
|
cancelledBy("analyze_web_bundle", OwnerCancellation.ownerClosed),
|
|
);
|
|
if (this.#admission.active() !== undefined)
|
|
return err(
|
|
new AnalysisResourceConstraintError(
|
|
"analyze_web_bundle",
|
|
"capacity",
|
|
"Another web-bundle capture or analysis owns this admission's source and result reservation",
|
|
{ concurrent_web_bundle_analyses: 1 },
|
|
{ measuredExhaustion: true },
|
|
),
|
|
);
|
|
// Claim the slot before awaiting cleanup so another host cannot pass the check.
|
|
this.#admission.hold(this);
|
|
try {
|
|
for (const owner of this.#admission.pending())
|
|
await owner.#releaseWorker();
|
|
await this.#admission.workers.retry();
|
|
await this.#releaseWorker();
|
|
} catch (cause) {
|
|
this.#admission.release(this);
|
|
return err(
|
|
cause instanceof AnalysisError
|
|
? cause
|
|
: new ProviderCleanupError(
|
|
"web-bundle-static-analysis",
|
|
[],
|
|
{ reason: "Earlier web analyzer cleanup remains incomplete" },
|
|
{ operation: "analyze_web_bundle", cause },
|
|
),
|
|
);
|
|
}
|
|
if (this.#closed) {
|
|
this.#admission.release(this);
|
|
return err(
|
|
cancelledBy("analyze_web_bundle", OwnerCancellation.ownerClosed),
|
|
);
|
|
}
|
|
return ok(undefined);
|
|
}
|
|
|
|
releaseReservation(): void {
|
|
this.#admission.release(this);
|
|
}
|
|
|
|
analyze(
|
|
inspection: WebPageInspection,
|
|
sourceMaps: WebBundleAnalysis["observations"]["source_maps"],
|
|
input: AnalyzeWebBundleInput,
|
|
signal?: AbortSignal,
|
|
memberMutationAllowance?: JavaScriptMemberMutationAllowance,
|
|
): Promise<Result<WebBundleAnalysis, AnalysisError>> {
|
|
if (this.#closed)
|
|
return Promise.resolve(
|
|
err(
|
|
cancelledBy("analyze_web_bundle", OwnerCancellation.ownerClosed, {
|
|
partialObservation: inspection,
|
|
}),
|
|
),
|
|
);
|
|
if (this.#running !== undefined)
|
|
return Promise.resolve(
|
|
err(
|
|
new AnalysisResourceConstraintError(
|
|
"analyze_web_bundle",
|
|
"capacity",
|
|
"This web analyzer already owns an active analysis",
|
|
{ concurrent_web_bundle_analyses: 1 },
|
|
{ measuredExhaustion: true },
|
|
),
|
|
),
|
|
);
|
|
this.#controller = new AbortController();
|
|
const running = this.#analyze(
|
|
inspection,
|
|
sourceMaps,
|
|
input,
|
|
signal,
|
|
memberMutationAllowance,
|
|
).finally(() => {
|
|
this.#running = undefined;
|
|
this.#controller = undefined;
|
|
});
|
|
this.#running = running;
|
|
return running;
|
|
}
|
|
|
|
async #analyze(
|
|
inspection: WebPageInspection,
|
|
sourceMaps: WebBundleAnalysis["observations"]["source_maps"],
|
|
input: AnalyzeWebBundleInput,
|
|
signal?: AbortSignal,
|
|
memberMutationAllowance?: JavaScriptMemberMutationAllowance,
|
|
): Promise<Result<WebBundleAnalysis, AnalysisError>> {
|
|
const scripts = inspection.scripts.items.filter(isIncludedScript);
|
|
const results: WebBundleScriptAnalysis[] = [];
|
|
const signals = [
|
|
this.#controller!.signal,
|
|
...(signal === undefined ? [] : [signal]),
|
|
];
|
|
const caller = AbortSignal.any(signals);
|
|
const deadline = new ProviderStartupDeadline(
|
|
input.total_analysis_timeout_ms ?? DEFAULT_WEB_BUNDLE_TOTAL_TIMEOUT_MS,
|
|
caller,
|
|
);
|
|
let failure: AnalysisError | undefined;
|
|
let resultCapacityExhausted = false;
|
|
const appendUnstarted = (): void => {
|
|
for (const script of scripts.slice(results.length))
|
|
results.push({
|
|
status: "unavailable",
|
|
script_key: script.script_key,
|
|
reason: "not_started",
|
|
detail: caller.aborted
|
|
? "The operation was cancelled before this script was started."
|
|
: deadline.interruption === "timeout"
|
|
? `The total_analysis_timeout_ms deadline of ${String(input.total_analysis_timeout_ms ?? DEFAULT_WEB_BUNDLE_TOTAL_TIMEOUT_MS)} ms expired before this script was started (deadlineExpired). This is not the per-script analysis_timeout_ms. A per-script timeout above the total is rejected.`
|
|
: resultCapacityExhausted
|
|
? "The host could not reserve more web-analysis result capacity; this script was not started."
|
|
: "An earlier analyzer failure prevented this script from starting.",
|
|
});
|
|
};
|
|
try {
|
|
await this.#admission.workers.retry();
|
|
this.#worker = this.createWorker(
|
|
input.max_heap_mb ?? DEFAULT_WEB_BUNDLE_HEAP_MB,
|
|
);
|
|
for (const script of scripts) {
|
|
if (deadline.signal.aborted) break;
|
|
const analyzed = await this.#worker.analyze(
|
|
script,
|
|
input.analysis_timeout_ms ?? DEFAULT_WEB_BUNDLE_SCRIPT_TIMEOUT_MS,
|
|
deadline.signal,
|
|
input.semantic_precision,
|
|
memberMutationAllowance,
|
|
);
|
|
if (analyzed.ok) results.push(analyzed.value);
|
|
else {
|
|
const error = analyzed.error.error;
|
|
results.push(
|
|
analyzed.error.completed ?? {
|
|
status: "unavailable",
|
|
script_key: script.script_key,
|
|
reason: caller.aborted
|
|
? "cancelled"
|
|
: deadline.interruption === "timeout" ||
|
|
error instanceof AnalysisTimeoutError
|
|
? "deadline"
|
|
: error instanceof AnalysisResourceConstraintError
|
|
? "resource_constraint"
|
|
: "worker_failure",
|
|
...unavailableWebScriptRecovery(error),
|
|
},
|
|
);
|
|
if (error.cleanupIncomplete) {
|
|
failure = error;
|
|
break;
|
|
}
|
|
if (analyzed.error.continuation === "result_capacity_exhausted") {
|
|
resultCapacityExhausted = true;
|
|
failure = error;
|
|
break;
|
|
}
|
|
if (error instanceof AnalysisCapabilityUnavailableError) {
|
|
failure = error;
|
|
break;
|
|
}
|
|
if (caller.aborted || deadline.signal.aborted) break;
|
|
}
|
|
}
|
|
} catch (cause) {
|
|
failure =
|
|
cause instanceof AnalysisError
|
|
? cause
|
|
: new AnalysisOutputError(
|
|
"analyze_web_bundle",
|
|
"Web analyzer could not complete its owned work",
|
|
{ cause },
|
|
);
|
|
}
|
|
appendUnstarted();
|
|
let partial: WebBundleAnalysis | undefined;
|
|
try {
|
|
partial = buildCapturedWebBundleAnalysis(inspection, sourceMaps, results);
|
|
} catch (cause) {
|
|
failure = new AnalysisOutputError(
|
|
"analyze_web_bundle",
|
|
"Completed web script facts could not be assembled",
|
|
{ cause },
|
|
);
|
|
}
|
|
const hasPrimaryFailure =
|
|
failure !== undefined &&
|
|
(!failure.cleanupIncomplete || failure.cleanupPrimary !== undefined);
|
|
try {
|
|
await this.#releaseWorker();
|
|
} catch (cause) {
|
|
const cleanup =
|
|
cause instanceof AnalysisError
|
|
? cause
|
|
: new ProviderCleanupError(
|
|
"web-bundle-static-analysis",
|
|
[],
|
|
{ reason: "Web analyzer final cleanup remains incomplete" },
|
|
{ operation: "analyze_web_bundle", cause },
|
|
);
|
|
failure =
|
|
failure === undefined
|
|
? cleanup
|
|
: analysisErrorWithCleanupDependencies(
|
|
failure,
|
|
cleanup,
|
|
"analyze_web_bundle",
|
|
);
|
|
} finally {
|
|
deadline.dispose();
|
|
}
|
|
if (caller.aborted && deadline.interruption !== "timeout") {
|
|
const cancelled = cancelledBy("analyze_web_bundle", caller, {
|
|
partialObservation: partial ?? inspection,
|
|
});
|
|
failure = hasPrimaryFailure
|
|
? analysisErrorWithCancellation(failure, cancelled)
|
|
: failure?.cleanupIncomplete === true
|
|
? analysisErrorWithCleanupDependencies(
|
|
cancelled,
|
|
failure,
|
|
"analyze_web_bundle",
|
|
)
|
|
: cancelled;
|
|
} else if (!hasPrimaryFailure && deadline.interruption === "timeout") {
|
|
const totalTimeoutMs =
|
|
input.total_analysis_timeout_ms ?? DEFAULT_WEB_BUNDLE_TOTAL_TIMEOUT_MS;
|
|
const timeout = new AnalysisTimeoutError(
|
|
"analyze_web_bundle",
|
|
totalTimeoutMs,
|
|
{
|
|
cause: deadline.signal.reason,
|
|
partialObservation: partial ?? inspection,
|
|
...webBundleTotalTimeoutRecovery(totalTimeoutMs),
|
|
},
|
|
);
|
|
failure =
|
|
failure?.cleanupIncomplete === true
|
|
? analysisErrorWithCleanupDependencies(
|
|
timeout,
|
|
failure,
|
|
"analyze_web_bundle",
|
|
)
|
|
: timeout;
|
|
}
|
|
if (failure !== undefined) {
|
|
failure.retainPartialObservation(partial ?? inspection);
|
|
return err(failure);
|
|
}
|
|
if (partial === undefined)
|
|
return err(
|
|
new AnalysisOutputError(
|
|
"analyze_web_bundle",
|
|
"Web analysis produced no result",
|
|
),
|
|
);
|
|
return ok(partial);
|
|
}
|
|
|
|
async close(): Promise<void> {
|
|
this.#closed = true;
|
|
this.#controller?.abort(
|
|
new Error("Browser provider closed during bundle analysis"),
|
|
);
|
|
await this.#running;
|
|
await this.#releaseWorker();
|
|
}
|
|
async #releaseWorker(): Promise<void> {
|
|
try {
|
|
await this.#worker?.close();
|
|
this.#worker = undefined;
|
|
this.#admission.forget(this);
|
|
} catch (cause) {
|
|
this.#admission.retain(this);
|
|
throw cause;
|
|
}
|
|
}
|
|
}
|