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
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 11:33:31 -05:00
1 parent 35db86b87e
commit 4e581dd350
4 files changed
+76 -47

No files matched your search

+25 -43
View File
@@ -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,
+6 -1
View File
@@ -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<void> => {
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();
}
},
);
},
+25 -1
View File
@@ -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,
});
+20 -2
View File
@@ -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));