fix(ghidra): recover regex stack exhaustion without losing annotations

This commit is contained in:
morluto
2026-10-09 03:54:11 +08:00
parent 549effa056
commit bc4aa46d7c
9 changed files with 151 additions and 10 deletions
+22 -1
View File
@@ -1490,6 +1490,7 @@ public final class ReaGhidraBridge extends HeadlessScript {
: regexMatcher(expression, caseSensitive);
JsonArray items = new JsonArray();
for (InventoryItem item : inventory) {
monitor.checkCancelled();
if (!matcher.matches(item.value)) {
continue;
}
@@ -3098,9 +3099,29 @@ public final class ReaGhidraBridge extends HeadlessScript {
);
}
catch (PatternSyntaxException exception) {
// Java wraps compiler stack exhaustion in PatternSyntaxException
// without retaining its cause. This producer reason is not bad syntax.
if (exception.getDescription().equals("Stack overflow during pattern compilation"))
throw regexStackExhausted(expression, "compiling");
throw new RequestFailure("invalid_request", "Invalid regex pattern " + expression + ": " + exception.getDescription() + " at index " + exception.getIndex());
}
return value -> pattern.matcher(value).find();
catch (StackOverflowError exhausted) {
throw regexStackExhausted(expression, "compiling");
}
return value -> {
try {
return pattern.matcher(value).find();
}
catch (StackOverflowError exhausted) {
throw regexStackExhausted(expression, "matching");
}
};
}
private static RequestFailure regexStackExhausted(String expression, String stage) {
return new RequestFailure("regex_stack_exhausted",
"Ghidra's Java regex engine exhausted its stack while " + stage +
" pattern " + expression + ". Use literal mode or simplify the regex; the session remains usable.");
}
private static SessionDescriptor readDescriptor(Path path) throws IOException {
+8
View File
@@ -93,6 +93,14 @@ body are excluded. `procedure_references` returns internal and external edges;
The reference collector's semantics participate in the analysis profile, so
older snapshots cannot replay the former incomplete results under this profile.
Ghidra regex searches use Java Pattern semantics. If compiling or matching a
pattern exhausts the engine's stack, REA returns a resource constraint and
preserves the active database and annotations. Retry in literal mode or simplify
the regex; successful literal searches retain complete matching strings.
Cancelling an active request terminates the ephemeral database and discards its
annotations. REA retains the selected target and imports the original artifact
again on the next Ghidra query.
```bash
rea inspect-native-instruction <binary> <address> --provider ghidra
rea resolve-native-call-targets <binary> <call-site> --provider ghidra
+3
View File
@@ -282,6 +282,9 @@ profile; CLI and MCP must reject that binding with a mismatch reason and
recovery advice. The rejected open must preserve the active live session.
Exact external entries must resolve while retaining an empty body; unknown
external addresses remain unresolved and external annotations are rejected.
An adversarial regex over a full 12 KB literal must report stack exhaustion as
a resource constraint, preserve live annotations, and allow complete literal
searches afterward; CLI and MCP must agree on both results and recovery advice.
Malformed annotation readback, memory completeness, and inventory data remain separate
SDK/provider integration cases; success from a real
provider cannot establish rejection of a contradictory provider response.
+74
View File
@@ -414,6 +414,79 @@ export async function verifyGhidraBoundaries(
comment: changes.comment,
inline_comment: changes.inline_comment,
});
const regexPattern = "^(a|aa)*b$";
const regexFailure = await client.callTool(
{
name: "search_strings",
arguments: { pattern: regexPattern, mode: "regex" },
},
options,
);
assert.equal(regexFailure.isError, true);
const regexError = regexFailure.structuredContent.error;
assert.equal(regexError.code, "resource_constraint");
assert.equal(regexError.details.operation, "search_strings");
assert.equal(regexError.details.resource, "memory");
assert.match(
regexError.remediation.action,
/literal mode or simplify the regex/u,
);
assert.match(
regexError.details.reason,
/exhausted its stack.*Use literal mode/u,
);
rejectedCalls++;
const nestedPattern = "(".repeat(12000) + "a" + ")".repeat(12000);
const compileFailure = await client.callTool(
{
name: "search_procedures",
arguments: { pattern: nestedPattern, mode: "regex" },
},
options,
);
assert.equal(compileFailure.isError, true);
const compileError = compileFailure.structuredContent.error;
assert.equal(compileError.code, "resource_constraint");
assert.equal(compileError.details.operation, "search_procedures");
assert.match(compileError.details.reason, /stack while compiling pattern/u);
assert.equal(compileError.remediation.action, regexError.remediation.action);
rejectedCalls++;
assert.deepEqual(
await call("analyze_function", { procedure: address }),
updated.dossier,
"Regex stack exhaustion must preserve the live annotation database",
);
assert.equal(
(await call("binary_session")).capabilities.find(
(item) => item.operation === "search_strings",
).available,
true,
);
assert.ok(
(await call("search_procedures", { pattern: changes.name })).some(
(item) => item.address === address,
),
"Regex compiler exhaustion must preserve edited names and search availability",
);
const longLiteral = "a".repeat(3 * 16 ** 3) + "!";
const longString = strings.find((item) => item.value === longLiteral);
assert.ok(longString, "The provider must retain the full regression literal");
const literalMatches = await call("search_strings", { pattern: longLiteral });
assert.deepEqual(literalMatches, [
{ address: longString.address, value: longLiteral },
]);
assert.deepEqual(await cli("search", longLiteral), literalMatches);
await assert.rejects(
cli("search", regexPattern, ["--mode", "regex"]),
(error) => {
assert.equal(error.code, 1);
const rejected = JSON.parse(error.stdout);
assert.equal(rejected.code, regexError.code);
assert.equal(rejected.details.reason, regexError.details.reason);
assert.equal(rejected.remediation.action, regexError.remediation.action);
return true;
},
);
assert.deepEqual(updated.effects, {
scope: "session-analysis-database",
source_bytes_modified: false,
@@ -597,6 +670,7 @@ export async function verifyGhidraBoundaries(
annotation_native_text_validation: true,
lossless_unicode_transport: true,
exact_external_entry_resolution: true,
recoverable_regex_stack_exhaustion: true,
complete_function_body_references: true,
ambiguity_candidates_inline: true,
legacy_reference_snapshot_rejected: true,
+3 -1
View File
@@ -20,15 +20,17 @@ export class AnalysisUnsupportedTargetError extends AnalysisError {
/** An analysis or transport resource failure, distinct from malformed input or unsupported coverage. */
export class AnalysisResourceConstraintError extends AnalysisError {
readonly _tag = "AnalysisResourceConstraintError";
readonly remediationAction: string | undefined;
constructor(
readonly operation: string,
readonly resource: "memory" | "cpu" | "file-size" | "transport",
readonly reason: string,
readonly reportedLimits: Readonly<Record<string, JsonValue>> | null,
options?: AnalysisErrorOptions,
options?: AnalysisErrorOptions & { readonly remediationAction?: string },
) {
super(`Resource constraint during ${operation}: ${reason}`, options);
this.remediationAction = options?.remediationAction;
}
}
+10 -7
View File
@@ -41,13 +41,16 @@ export const analysisErrorRemediationAction = (
if (error instanceof AnalysisUnsupportedTargetError)
return "Select a target supported by this operation or choose an operation supporting the reported target format.";
if (error instanceof AnalysisResourceConstraintError)
return error.resource === "transport"
? "Use the reported Evidence reference with a focused analysis tool when retained in this session, or export the session through export_evidence_bundle to a caller-selected path. Complete CLI JSON output is also available. The connection remains usable."
: error.resource === "cpu"
? "Review the reported worker CPU limits and observed signal. Retry with sufficient CPU time or a smaller artifact; REA retains tighter inherited limits."
: error.resource === "file-size"
? "Review the reported worker file-size limits and write failure. Retry with a sufficient file-size allowance for the evidence reply; REA retains tighter inherited limits."
: "Review the reported worker memory limits and available host memory. Retry with sufficient memory or a smaller artifact; REA retains tighter inherited limits.";
return (
error.remediationAction ??
(error.resource === "transport"
? "Use the reported Evidence reference with a focused analysis tool when retained in this session, or export the session through export_evidence_bundle to a caller-selected path. Complete CLI JSON output is also available. The connection remains usable."
: error.resource === "cpu"
? "Review the reported worker CPU limits and observed signal. Retry with sufficient CPU time or a smaller artifact; REA retains tighter inherited limits."
: error.resource === "file-size"
? "Review the reported worker file-size limits and write failure. Retry with a sufficient file-size allowance for the evidence reply; REA retains tighter inherited limits."
: "Review the reported worker memory limits and available host memory. Retry with sufficient memory or a smaller artifact; REA retains tighter inherited limits.")
);
if (error instanceof HopperTimeoutError)
return error.providerState === "busy"
? "Check binary_session.analysis_activity, wait for the active Hopper request to finish, then retry."
+5 -1
View File
@@ -26,6 +26,7 @@ export const GHIDRA_OPERATIONS = Object.freeze([
export const healthLimitations = Object.freeze([
"The session serves operations only after Ghidra reports default auto-analysis complete; incomplete analysis does not expose partial results.",
"The imported Program and temporary project are ephemeral and deleted on close. Annotation operations edit session database metadata; original executable bytes are never written.",
"Cancelling an active Ghidra request terminates its ephemeral database and discards session annotations. The selected target remains open; the next provider query imports the original artifact again.",
]);
/** Additional limitations applied to the experimental Windows x64 P0 boundary. */
@@ -97,7 +98,10 @@ export const limitationsFor = (operation: string): readonly string[] => {
];
case "search_procedures":
case "search_strings":
return common;
return [
...common,
"Regex mode uses Java Pattern semantics. Matcher or compiler stack exhaustion returns a resource constraint without closing the session; literal mode avoids regex recursion and returns complete matching values.",
];
case "procedure_pseudo_code":
return [
...common,
+16
View File
@@ -13,6 +13,7 @@ import {
AnalysisCancelledError,
AnalysisCapabilityUnavailableError,
AnalysisInputError,
AnalysisResourceConstraintError,
AnalysisTimeoutError,
} from "../domain/analysisErrorCore.js";
import { type AnalysisError } from "../domain/analysisErrorBase.js";
@@ -335,6 +336,21 @@ const projectSessionError = (
);
if (failure.kind === "remote" && failure.remoteCode === "decompile_cancelled")
return new AnalysisCancelledError(operation);
if (
failure.kind === "remote" &&
failure.remoteCode === "regex_stack_exhausted"
)
return new AnalysisResourceConstraintError(
operation,
"memory",
failure.message,
null,
{
cause: failure,
remediationAction:
"Retry this search in literal mode or simplify the regex. The active analysis session and annotations remain available.",
},
);
if (
failure.kind === "remote" &&
["invalid_request", "not_found", "ambiguous"].includes(
+10
View File
@@ -2,6 +2,16 @@
volatile int rea_ghidra_inventory_global = 7;
// This literal reproduces stack exhaustion in Java's recursive regex matcher.
#define REA_REPEAT_4(value) value value value value
#define REA_REPEAT_16(value) REA_REPEAT_4(REA_REPEAT_4(value))
#define REA_REGEX_RUN REA_REPEAT_16(REA_REPEAT_16(REA_REPEAT_16("a")))
__attribute__((used)) const char *volatile rea_ghidra_regex_subject =
REA_REGEX_RUN REA_REGEX_RUN REA_REGEX_RUN "!";
#undef REA_REGEX_RUN
#undef REA_REPEAT_16
#undef REA_REPEAT_4
enum ReaFixtureState { REA_FIXTURE_IDLE = 0, REA_FIXTURE_READY = 3 };
union ReaFixturePayload { unsigned int word; unsigned char bytes[4]; };
struct ReaFixtureLayout {