Files
reverse-engineering-anything/src/domain/native/nativeSegments.ts
T
morluto f88d8964f9 fix(hopper): validate every bridge reply against one contract table (#10662)
* fix(hopper): validate inventory replies against their contracts at the provider edge

Hopper validated only the response envelope. list_names replies outside
the inventory contract were returned unchanged (projectHopperNameInventory
passed them through), and the regex search re-parsed its inventory with
its own policy. A per-method reply contract table built from the shared
native inventory schemas now validates list_names, list_procedures,
list_strings, search_procedures and search_strings once; a reply outside
the contract is an AnalysisOutputError. The remaining 31 operations are a
counted pending list that may only shrink.

* fix(hopper): add reply contracts for scalar, boolean and address-list operations

Twenty-two more Hopper operations are validated against reply contracts
built from the shared native schemas. The pending list shrinks from 31
to 9 operations.

* refactor(hopper): validate every bridge reply through one contract table

The reply table now covers every bridge method REA reads, including the
private caller-reconciliation methods. HopperFileOffset receives a
validated observation and HopperCallerReconciliation reads typed replies
instead of keeping its own schemas and re-parsing the native result. The
segment list schema moves from contracts into its domain owner.

* fix(hopper): apply reply contracts on the ready path and test every entry path

executeIfReady validated only list_procedures, so a malformed
list_documents reply could reach the completion path. Every contracted
operation is now validated there, and provider-level tests cover the
direct and ready paths for each inventory and document operation.
2026-11-14 18:59:39 -05:00

114 lines
3.7 KiB
TypeScript

import { z } from "zod";
import { ok, type Result } from "../result.js";
import { AnalysisOutputError } from "../analysisErrorCore.js";
import type { JsonValue } from "../jsonValue.js";
import { invalidNativeOutput } from "./nativeProviderOutput.js";
import { addressDistance } from "./addressCoordinate.js";
export interface SegmentSummary {
readonly name: string;
readonly start: string;
readonly end: string;
readonly readable: boolean | null;
readonly writable: boolean | null;
readonly executable: boolean | null;
readonly image_base?: string;
readonly address_space?: string;
}
const availableMemoryPermissions = z.object({
available: z.literal(true),
source: z.literal("ghidra-memory-block"),
});
/** One reported memory region with its permission availability and provenance. */
const nativeMemoryRegionSchema = z.object({
name: z.string(),
start: z.string(),
end: z.string(),
readable: z.boolean().nullable(),
writable: z.boolean().nullable(),
executable: z.boolean().nullable(),
permissions: z.union([
z.object({ available: z.literal(false), reason: z.string() }),
availableMemoryPermissions,
]),
provenance: z.enum(["hopper-public-python-api", "ghidra-memory-block"]),
address_space: z.string().optional(),
image_base: z.string().optional(),
initialized: z.boolean().optional(),
overlay: z.boolean().optional(),
});
/** Provider segment list with nested sections, as delivered by list_segments. */
export const nativeSegmentListSchema = z.array(
nativeMemoryRegionSchema.extend({
sections: z.array(nativeMemoryRegionSchema),
}),
);
const segmentSchema = z.object({
name: z.string(),
start: z.string(),
end: z.string(),
readable: z.boolean().nullable(),
writable: z.boolean().nullable(),
executable: z.boolean().nullable(),
image_base: z.string().min(1).exactOptional(),
address_space: z.string().min(1).exactOptional(),
});
/** Preserve reported address context in the provider segment array. */
export const parseSegments = (
value: JsonValue,
operation = "list_segments",
): Result<readonly SegmentSummary[], AnalysisOutputError> => {
const parsed = z.array(segmentSchema).safeParse(value);
return parsed.success
? ok(parsed.data)
: invalidNativeOutput(operation, "segment list", parsed.error);
};
/** Return a reported overview base only when every parsed segment agrees. */
export const parseSegmentImageBase = (
segments: readonly SegmentSummary[],
): string | undefined => {
const base = segments[0]?.image_base;
return base !== undefined &&
segments.every((segment) => segment.image_base === base)
? base
: undefined;
};
/** Project the same overview from complete lists or provider-owned aggregates. */
export const projectBinaryOverview = (input: {
readonly document: string;
readonly procedure_count: number;
readonly string_count: number;
readonly segments?: readonly SegmentSummary[];
}): JsonValue => {
const { document, procedure_count, string_count, segments } = input;
const imageBase =
segments === undefined ? undefined : parseSegmentImageBase(segments);
return {
document,
...(imageBase === undefined ? {} : { image_base: imageBase }),
...(segments === undefined
? {}
: {
segments: segments.map(
({ name, start, end, image_base, address_space }) => ({
name,
start,
end,
length: addressDistance(start, end),
...(image_base === undefined ? {} : { image_base }),
...(address_space === undefined ? {} : { address_space }),
}),
),
segment_count: segments.length,
}),
procedure_count,
string_count,
};
};