Skip to content

Commit 36db19c

Browse files
committed
fix(providers)!: preserve unknown segment extents and initialized IDA readiness
BREAKING CHANGE: binary_overview segment length is null when Hopper coordinates do not establish a finite same-address-space extent. Consumers must preserve this unknown instead of treating it as zero or a saturated value.
1 parent 5fa7a52 commit 36db19c

6 files changed

Lines changed: 79 additions & 14 deletions

File tree

‎src/contracts/toolOutputSchemaGroups.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ export const enhancedOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
367367
name: z.string(),
368368
start: z.string(),
369369
end: z.string(),
370-
length: z.number().min(0),
370+
length: z.number().min(0).nullable(),
371371
image_base: z.string().exactOptional(),
372372
address_space: z.string().exactOptional(),
373373
}),

‎src/domain/hopperValues.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
22

33
import {
44
functionBodySchema,
5+
addressDistance,
56
procedureIdentitySchema,
67
parseDocuments,
78
parseListCount,
@@ -12,6 +13,15 @@ import {
1213
} from "./hopperValues.js";
1314

1415
describe("Hopper boundary values", () => {
16+
it("distinguishes measured zero, malformed, incomparable, reversed, and oversized extents", () => {
17+
expect(addressDistance("not-an-address", "0x2000")).toBeNull();
18+
expect(addressDistance("ram:0x1000", "rom:0x2000")).toBeNull();
19+
expect(addressDistance("0x2000", "0x1000")).toBeNull();
20+
expect(addressDistance("0x1000", "0x1000")).toBe(0);
21+
expect(addressDistance("0x1000", "0x2000")).toBe(4096);
22+
expect(addressDistance("0x0", "0x20000000000000")).toBeNull();
23+
});
24+
1525
it("parses Hopper's address-keyed name and string maps", () => {
1626
expect(parseNames({ "0x1000": "_main" })).toEqual({
1727
ok: true,

‎src/domain/hopperValues.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@ export interface SegmentSummary {
2525
}
2626

2727
/** Return a bounded byte distance between canonical addresses in one space. */
28-
export const addressDistance = (start: string, end: string): number => {
28+
export const addressDistance = (start: string, end: string): number | null => {
2929
const left = addressCoordinate(start);
3030
const right = addressCoordinate(end);
31-
if (left === null || right === null || left.space !== right.space) return 0;
31+
if (left === null || right === null || left.space !== right.space)
32+
return null;
3233
const distance = right.offset - left.offset;
33-
if (distance <= 0n) return 0;
34-
return Number(
35-
distance > BigInt(Number.MAX_SAFE_INTEGER)
36-
? BigInt(Number.MAX_SAFE_INTEGER)
37-
: distance,
38-
);
34+
if (distance < 0n || distance > BigInt(Number.MAX_SAFE_INTEGER)) return null;
35+
return Number(distance);
3936
};
4037

4138
const procedureMapSchema = z.record(z.string(), z.string());

‎src/ida/IdaSessionClient.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,28 @@ describe("headless IDA producer pagination", () => {
153153
});
154154
});
155155

156-
describe("attached IDA observation semantics", () => {
156+
describe("attached IDA readiness and queued cancellation", () => {
157+
it("allows ready-only work after an attached provider has initialized", async () => {
158+
const { producer, client } = await fixture("attached");
159+
await expect(client.executeIfReady("list_procedures", {})).resolves.toBe(
160+
undefined,
161+
);
162+
163+
const startup = await client.execute("list_procedures", {});
164+
if (!startup.ok) throw startup.error;
165+
const result = await client.executeIfReady("list_procedures", {});
166+
if (result === undefined)
167+
throw new Error("expected ready attached IDA dispatch");
168+
if (!result.ok) throw result.error;
169+
expect(result.value.result).toEqual([
170+
expect.objectContaining({ address: "0x1000", value: "main" }),
171+
]);
172+
expect(
173+
producer.calls.filter(({ name }) => name === "idb_open"),
174+
).toHaveLength(0);
175+
await client.close();
176+
});
177+
157178
it("returns cold and pending startup calls immediately, then cancels queued work before dispatch", async () => {
158179
const { producer, client } = await fixture("headless");
159180
await expect(client.executeIfReady("list_procedures", {})).resolves.toBe(
@@ -233,7 +254,9 @@ describe("attached IDA observation semantics", () => {
233254
);
234255
await client.close();
235256
});
257+
});
236258

259+
describe("attached IDA input identity and validation", () => {
237260
it("accepts a normalized spelling of the bound document path", async () => {
238261
const { target, producer, client } = await fixture();
239262
const equivalent =

‎src/ida/IdaSessionClient.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export class IdaSessionClient implements AnalysisClient {
162162
#databaseCloseCompleted = false;
163163
#openAttempted = false;
164164
#openCompleted = false;
165+
#ready = false;
165166
#blockingSessions: readonly UpstreamSession[] | undefined;
166167
#openRejected = false;
167168
#callGraphAdvertised = false;
@@ -197,11 +198,10 @@ export class IdaSessionClient implements AnalysisClient {
197198
parameters: Readonly<Record<string, JsonValue>>,
198199
options?: ExecutionOptions,
199200
) {
200-
if (!this.#openCompleted || this.#closed || this.#closing)
201+
if (!this.#ready || this.#closed || this.#closing)
201202
return Promise.resolve(undefined);
202203
const call = this.#tail.then(() => {
203-
if (!this.#openCompleted || this.#closed || this.#closing)
204-
return undefined;
204+
if (!this.#ready || this.#closed || this.#closing) return undefined;
205205
if (wasCancelled(options))
206206
return err(new AnalysisCancelledError(operation));
207207
return this.#execute(operation, parameters, options, true);
@@ -601,6 +601,7 @@ export class IdaSessionClient implements AnalysisClient {
601601
);
602602
this.#database = opened.session.session_id;
603603
}
604+
this.#ready = true;
604605
}
605606

606607
async #nativeMetadata(): Promise<JsonValue> {

‎tests/boundary/mcp/enhancedTools.04-uses-complete-inventories-inline.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ const completeOverviewPort: AnalysisOperationPort = {
135135
},
136136
};
137137

138-
describe("enhanced MCP tools", () => {
138+
describe("enhanced MCP binary overview", () => {
139139
it("returns the complete overview inline with exhaustive totals", async () => {
140140
const client = await connect(completeOverviewPort);
141141
const result = jsonResult(
@@ -210,7 +210,9 @@ describe("enhanced MCP tools", () => {
210210
string_count: 0,
211211
});
212212
});
213+
});
213214

215+
describe("enhanced MCP inventory validation", () => {
214216
it("rejects paginated provider output", async () => {
215217
const client = await connect({
216218
execute: () =>
@@ -262,6 +264,38 @@ describe("enhanced MCP tools", () => {
262264
text: JSON.stringify(parseMcpToolError(result)),
263265
});
264266
});
267+
268+
it("preserves unknown length for malformed segment coordinates", async () => {
269+
const client = await connect({
270+
execute: (name) => {
271+
if (name === "list_segments")
272+
return Promise.resolve(
273+
ok([
274+
{
275+
name: "__TEXT",
276+
start: "unparsed-start",
277+
end: "0x2000",
278+
readable: null,
279+
writable: null,
280+
executable: null,
281+
},
282+
]),
283+
);
284+
if (name === "current_document") return Promise.resolve(ok("fixture"));
285+
if (name === "list_procedures" || name === "list_strings")
286+
return Promise.resolve(ok([]));
287+
throw new Error(`Unexpected analysis operation: ${name}`);
288+
},
289+
});
290+
expect(
291+
jsonResult(
292+
await client.callTool({ name: "binary_overview", arguments: {} }),
293+
),
294+
).toMatchObject({
295+
segments: [{ name: "__TEXT", start: "unparsed-start", length: null }],
296+
segment_count: 1,
297+
});
298+
});
265299
});
266300

267301
const overviewPort = (

0 commit comments

Comments
 (0)