From 4e581dd350dcf6ef03ba84c62ee8501dd55449a5 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:33:31 -0500 Subject: [PATCH] perf(ghidra/kuna): fuse checkpoint publish stream-hash+fsync and pre-filter Kuna regex searches (#13468) * perf(ghidra/kuna): fuse checkpoint publish stream-hash+fsync and pre-filter Kuna regex searches (#13465) * fix(kuna): defer regex candidate allocation until admission --- src/ghidra/GhidraCheckpoint.ts | 68 ++++++++++++------------------- src/ghidra/GhidraFileArtifacts.ts | 7 +++- src/kuna/KunaSessionClient.ts | 26 +++++++++++- src/kuna/KunaStringQueries.ts | 22 +++++++++- 4 files changed, 76 insertions(+), 47 deletions(-) diff --git a/src/ghidra/GhidraCheckpoint.ts b/src/ghidra/GhidraCheckpoint.ts index e8aba4fbd5..6a27ac4f54 100644 --- a/src/ghidra/GhidraCheckpoint.ts +++ b/src/ghidra/GhidraCheckpoint.ts @@ -1,16 +1,7 @@ import { appendCallerPath } from "../filesystem/callerPath.js"; import { classifyProviderFailure } from "../providers/classifyProviderFailure.js"; import { createHash, randomUUID } from "node:crypto"; -import { constants } from "node:fs"; -import { - copyFile, - lstat, - mkdir, - open, - realpath, - rename, - rm, -} from "node:fs/promises"; +import { lstat, mkdir, open, realpath, rename, rm } from "node:fs/promises"; import { join } from "node:path"; import { z } from "zod"; import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js"; @@ -32,6 +23,7 @@ import { } from "../filesystem/RegularFile.js"; import { acquireOwnedLease, type OwnedLease } from "../process/OwnedLease.js"; import { + copyGhidraRegularFile, ghidraFileDialect, readGhidraProfileArtifact, } from "./GhidraFileArtifacts.js"; @@ -320,45 +312,35 @@ export class GhidraCheckpoint { let failed = false; try { assertArtifactReadActive(signal); - await copyFile(path, destination, constants.COPYFILE_EXCL); - const sha256 = await hashGhidraPackedProgram( - destination, - this.resources, + const hash = createHash("sha256"); + await copyGhidraRegularFile(path, destination, this.resources, { signal, - ); + hash, + sync: true, + }); + const sha256 = hash.digest("hex"); const source: CheckpointSource = { file, sha256, kind: previous === null ? "initial-analysis" : "retained", }; - for (const [output, bytes] of [ - [destination, undefined], - [ - temporary, - JSON.stringify({ - version: 2, - target_sha256: this.selection.target_sha256, - base_profile_digest: this.selection.base_profile_digest, - source, - }), - ], - ] as const) { - const handle = await open( - output, - bytes === undefined ? "r+" : "wx", - 0o600, - ); - await withArtifactFileHandle( - output, - handle, - this.resources, - signal, - async (handle) => { - if (bytes !== undefined) await handle.writeFile(bytes); - await handle.sync(); - }, - ); - } + const metadata = JSON.stringify({ + version: 2, + target_sha256: this.selection.target_sha256, + base_profile_digest: this.selection.base_profile_digest, + source, + }); + const handle = await open(temporary, "wx", 0o600); + await withArtifactFileHandle( + temporary, + handle, + this.resources, + signal, + async (handle) => { + await handle.writeFile(metadata); + await handle.sync(); + }, + ); assertArtifactReadActive(signal); await rename( temporary, diff --git a/src/ghidra/GhidraFileArtifacts.ts b/src/ghidra/GhidraFileArtifacts.ts index 8dd4f9c757..8c8dc5a881 100644 --- a/src/ghidra/GhidraFileArtifacts.ts +++ b/src/ghidra/GhidraFileArtifacts.ts @@ -74,10 +74,11 @@ export const copyGhidraRegularFile = async ( readonly signal?: AbortSignal | undefined; readonly buffer?: Buffer | undefined; readonly hash?: Hash | undefined; + readonly sync?: boolean | undefined; readonly onOpenedSource?: ((initialSize: number) => void) | undefined; } = {}, ): Promise => { - const { signal, hash, onOpenedSource } = options; + const { signal, hash, sync, onOpenedSource } = options; const scratch = options.buffer ?? Buffer.allocUnsafe(STREAM_COPY_BUFFER_BYTES); await withArtifactRegularFile( @@ -130,6 +131,10 @@ export const copyGhidraRegularFile = async ( assertArtifactReadActive(signal); if (!sameRegularFileState(initial, await srcHandle.stat())) throw new RegularFileChangedError(sourcePath); + if (sync) { + assertArtifactReadActive(signal); + await dest.sync(); + } }, ); }, diff --git a/src/kuna/KunaSessionClient.ts b/src/kuna/KunaSessionClient.ts index d8bc3c280c..beb0fbc58e 100644 --- a/src/kuna/KunaSessionClient.ts +++ b/src/kuna/KunaSessionClient.ts @@ -43,6 +43,7 @@ import { matchedProcedureAliases, type AnalysisSearchQuery, } from "../domain/analysisSearch.js"; +import { extractRequiredRegexLiteral } from "../domain/regexLiteralCandidate.js"; import type { BinaryTarget } from "../domain/binaryTargetTypes.js"; import { createEvidence, @@ -1248,12 +1249,35 @@ export class KunaSessionClient implements AnalysisClient { if (query.mode === "regex") { // Each distinct spelling is matched once, however many entries share it. // byName already holds every unique primary name and alias in inventory order. + const requiredLiteral = extractRequiredRegexLiteral( + query.pattern, + query.case_sensitive, + ); + const prefilter = + requiredLiteral === undefined + ? undefined + : analysisSearchPredicate({ + pattern: requiredLiteral, + mode: "literal", + case_sensitive: query.case_sensitive, + }); let names: readonly string[] = []; const matched = await this.#queryWork.match({ operation, pattern: query.pattern, caseSensitive: query.case_sensitive, - values: () => (names = [...index.value.byName.keys()]), + values: () => { + if (prefilter === undefined) { + names = [...index.value.byName.keys()]; + } else { + const filtered: string[] = []; + for (const name of index.value.byName.keys()) { + if (prefilter(name)) filtered.push(name); + } + names = filtered; + } + return names; + }, signal: options.signal, ...regex, }); diff --git a/src/kuna/KunaStringQueries.ts b/src/kuna/KunaStringQueries.ts index 3b84655e8f..53db4788b5 100644 --- a/src/kuna/KunaStringQueries.ts +++ b/src/kuna/KunaStringQueries.ts @@ -10,6 +10,7 @@ import { analysisSearchPredicate, type AnalysisSearchQuery, } from "../domain/analysisSearch.js"; +import { extractRequiredRegexLiteral } from "../domain/regexLiteralCandidate.js"; import type { JsonValue } from "../domain/jsonValue.js"; import { err, ok, type Result } from "../domain/result.js"; import { normalizeKunaAddress } from "./KunaAddress.js"; @@ -73,16 +74,33 @@ export class KunaStringQueries { items = kunaStringsAtAddress(inventory.value, address); if (query !== undefined) { if (query.mode === "regex") { + const requiredLiteral = extractRequiredRegexLiteral( + query.pattern, + query.case_sensitive, + ); + const prefilter = + requiredLiteral === undefined + ? undefined + : analysisSearchPredicate({ + pattern: requiredLiteral, + mode: "literal", + case_sensitive: query.case_sensitive, + }); + let candidates = available; const matched = await this.work.match({ operation, pattern: query.pattern, caseSensitive: query.case_sensitive, - values: () => available.map((item) => item.value), + values: () => { + if (prefilter !== undefined) + candidates = available.filter((item) => prefilter(item.value)); + return candidates.map((item) => item.value); + }, signal, ...regex, }); if (!matched.ok) return matched; - items = matched.value.map((index) => available[index]!); + items = matched.value.map((index) => candidates[index]!); } else { const predicate = analysisSearchPredicate(query); items = available.filter((item) => predicate(item.value));