From 1e602c8f08c7fa38c5c0dbf4fb379b82cf8bbc77 Mon Sep 17 00:00:00 2001 From: SahinurDEV <68515168+SahinurDEV@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:09:26 +0600 Subject: [PATCH 1/4] fix(native): retain Objective-C classes whose adopted-protocol list exceeds its count limit Fixes #8945 --- src/native/AppleDispatchMetadata.test.ts | 81 ++++++++++++++++++++++++ src/native/AppleObjcDispatchFacets.ts | 22 ++++++- 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/src/native/AppleDispatchMetadata.test.ts b/src/native/AppleDispatchMetadata.test.ts index 9ac17f938c9..c1e1cd6006d 100644 --- a/src/native/AppleDispatchMetadata.test.ts +++ b/src/native/AppleDispatchMetadata.test.ts @@ -2478,3 +2478,84 @@ it.each([false, true])( ).toBe(unreadParent ? "partial" : "complete"); }, ); + +describe("Objective-C adopted protocol-list count limit", () => { + // Root class and metaclass that adopt the fixture protocol through the + // class_ro_t base protocol list at 0x700. + const rootClass = (protocolCount: bigint) => { + const bytes = fixture(); + bytes.writeUInt32LE(2, 12); + bytes.writeUInt32LE(5, 88); + bytes.writeUInt32LE(2, 0x300); + bytes.writeUInt32LE(3, 0x380); + bytes.writeBigUInt64LE(protocolCount, 0x700); + return bytes; + }; + const classCoverage = ( + result: ReturnType, + ) => + result.coverage.find( + ({ facet }) => facet === "objc_class_method_ivar_metadata", + ); + + it("retains the class and metaclass rows when the list exceeds its count limit", () => { + const result = decodeAppleDispatchMetadata( + rootClass(20001n), + 100, + provenance, + ); + expect( + result.objc_classes.map(({ name, is_meta_class, protocols, decode }) => ({ + name, + is_meta_class, + protocols, + decode, + })), + ).toEqual([ + { + name: "Fixture", + is_meta_class: false, + protocols: [], + decode: { status: "partial", reason: "protocol_list_unreadable" }, + }, + // The metaclass has no adopted-protocol list of its own. + { + name: "Fixture", + is_meta_class: true, + protocols: [], + decode: { status: "decoded", reason: null }, + }, + ]); + expect(result.objc_classes[0]?.properties).toHaveLength(3); + expect(result.objc_ivars).toHaveLength(1); + expect(result.objc_dispatch_implementations).toMatchObject([ + { class_name: "Fixture", selector: "performAction:" }, + ]); + expect(classCoverage(result)).toMatchObject({ + status: "partial", + // The class row is retained as partial, so it is examined but not + // counted as fully decoded; the metaclass is fully decoded. + examined: 2, + decoded: 1, + reason: expect.stringContaining( + "Protocol list of Fixture: Protocol list exceeds 20000 entries", + ), + }); + }); + + it.each([0n, 1n])( + "keeps complete class coverage for an in-limit count of %s", + (count) => { + const bytes = rootClass(count); + const result = decodeAppleDispatchMetadata(bytes, 100, provenance); + const reason = classCoverage(result)?.reason ?? ""; + expect(reason).not.toContain("Protocol list of Fixture"); + expect(result.objc_classes).toHaveLength(2); + for (const row of result.objc_classes) + expect(row.decode?.reason ?? "").not.toContain( + "protocol_list_unreadable", + ); + expect(classCoverage(result)?.status).toBe("complete"); + }, + ); +}); diff --git a/src/native/AppleObjcDispatchFacets.ts b/src/native/AppleObjcDispatchFacets.ts index fd46e5eab8f..58464569421 100644 --- a/src/native/AppleObjcDispatchFacets.ts +++ b/src/native/AppleObjcDispatchFacets.ts @@ -277,6 +277,22 @@ export const decodeObjcDispatchFacets = (input: { } } } + // An unreadable adopted-protocol list is a class-owned sublist failure: + // the caller retains the independently decoded class (and its metaclass) + // and reports the list as partial instead of discarding both rows. + const readAdoptedProtocols = ( + owner: string, + list: () => string[], + ): { protocols: string[]; unreadable: boolean } => { + try { + return { protocols: list(), unreadable: false }; + } catch (cause: unknown) { + failures.push( + `Protocol list of ${owner}: ${cause instanceof Error ? cause.message : String(cause)}`, + ); + return { protocols: [], unreadable: true }; + } + }; const visited = new Set(); const readClass = (address: bigint, meta = false) => { if (address === 0n || visited.has(hex(address)) || !budget.admit()) return; @@ -361,12 +377,16 @@ export const decodeObjcDispatchFacets = (input: { location: hex(read.pointer(ro + 64n)), onTruncated, }); - const protocols = protocolReader.list(read.pointer(ro + 40n), onTruncated); + const { protocols, unreadable: protocolListUnreadable } = + readAdoptedProtocols(name, () => + protocolReader.list(read.pointer(ro + 40n), onTruncated), + ); const classDecodeReasons = [ ...(superclass.name === null && !root ? ["superclass_binding_unresolved"] : []), ...(classListTruncated ? ["max_records_reached"] : []), + ...(protocolListUnreadable ? ["protocol_list_unreadable"] : []), ]; result.objc_classes.push({ name, From 87ca638dd0b269f68c15730623aafae3b54ae033 Mon Sep 17 00:00:00 2001 From: SahinurDEV <68515168+SahinurDEV@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:13:28 +0600 Subject: [PATCH 2/4] refactor(native): return a tagged Result for adopted-protocol list reads readAdoptedProtocols now returns Result and readClass records the failure reason and marks the class row partial. --- src/native/AppleObjcDispatchFacets.ts | 28 ++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/native/AppleObjcDispatchFacets.ts b/src/native/AppleObjcDispatchFacets.ts index 58464569421..85b546ef487 100644 --- a/src/native/AppleObjcDispatchFacets.ts +++ b/src/native/AppleObjcDispatchFacets.ts @@ -281,16 +281,18 @@ export const decodeObjcDispatchFacets = (input: { // the caller retains the independently decoded class (and its metaclass) // and reports the list as partial instead of discarding both rows. const readAdoptedProtocols = ( - owner: string, list: () => string[], - ): { protocols: string[]; unreadable: boolean } => { + ): Result< + string[], + { readonly _tag: "ProtocolListUnreadable"; readonly reason: string } + > => { try { - return { protocols: list(), unreadable: false }; + return ok(list()); } catch (cause: unknown) { - failures.push( - `Protocol list of ${owner}: ${cause instanceof Error ? cause.message : String(cause)}`, - ); - return { protocols: [], unreadable: true }; + return err({ + _tag: "ProtocolListUnreadable", + reason: cause instanceof Error ? cause.message : String(cause), + }); } }; const visited = new Set(); @@ -377,16 +379,20 @@ export const decodeObjcDispatchFacets = (input: { location: hex(read.pointer(ro + 64n)), onTruncated, }); - const { protocols, unreadable: protocolListUnreadable } = - readAdoptedProtocols(name, () => - protocolReader.list(read.pointer(ro + 40n), onTruncated), + const adoptedProtocols = readAdoptedProtocols(() => + protocolReader.list(read.pointer(ro + 40n), onTruncated), + ); + if (!adoptedProtocols.ok) + failures.push( + `Protocol list of ${name}: ${adoptedProtocols.error.reason}`, ); + const protocols = adoptedProtocols.ok ? adoptedProtocols.value : []; const classDecodeReasons = [ ...(superclass.name === null && !root ? ["superclass_binding_unresolved"] : []), ...(classListTruncated ? ["max_records_reached"] : []), - ...(protocolListUnreadable ? ["protocol_list_unreadable"] : []), + ...(adoptedProtocols.ok ? [] : ["protocol_list_unreadable"]), ]; result.objc_classes.push({ name, From 0e5d3c2f6043f50e87b4b9e7f95798e7717a9a0c Mon Sep 17 00:00:00 2001 From: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:22:05 +0000 Subject: [PATCH 3/4] test(native): verify protocol-list recovery over CLI and MCP Exercise the count guard through the real Darwin entrypoints selected by the Apple artifact CI lane, retaining target identity and partial facts. Share the existing controlled byte fixture with provider-boundary tests. Assisted-by: Reimu (gensokyo-skills:reimu-incident-triage) --- .../nativeDispatchPropertyAttributes.test.ts | 67 ++++++++++++++++++- .../nativeDispatchFormatFailures.test.ts | 10 +-- tests/fixtures/objcRecordBudgetMacho.ts | 8 +++ 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts b/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts index a254028d66c..b233b6561b5 100644 --- a/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts +++ b/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts @@ -1,4 +1,5 @@ -import { writeFile } from "node:fs/promises"; +import { readFile, writeFile } from "node:fs/promises"; +import { createHash } from "node:crypto"; import { join } from "node:path"; import { describe, expect } from "vitest"; @@ -6,6 +7,70 @@ import { describe, expect } from "vitest"; import { machoWithPropertyAttributes } from "../../fixtures/objcPropertyMacho.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; +import { objcAdoptedProtocolCountMacho } from "../../fixtures/objcRecordBudgetMacho.js"; +import { parseEvidence } from "../../../src/domain/evidence.js"; +import { nativeDispatchMetadataResultSchema } from "../../../src/domain/native/objcSwiftMetadata.js"; + +describe.skipIf(process.platform !== "darwin")( + "native adopted-protocol list recovery", + () => { + cliTest.for([1n, 20001n])( + "preserves class facts and partial-list semantics over CLI and stdio MCP for count %s", + async (count, { cli }) => { + const root = await createTestTempDirectory("rea-protocol-list-"); + const path = join(root, "fixture.macho"); + const bytes = objcAdoptedProtocolCountMacho(count); + await writeFile(path, bytes); + const sha256 = createHash("sha256").update(bytes).digest("hex"); + const cliReply = await cli.run({ + arguments: ["inspect-native-dispatch-metadata", path, "--json"], + environment: { REA_LOG_LEVEL: "silent" }, + }); + expect(cliReply.exitCode, cliReply.stderr).toBe(0); + const cliResult = nativeDispatchMetadataResultSchema.parse( + parseEvidence(cliReply.json).normalized_result, + ); + const call = await cli.sharedMcpCall!(); + const opened = await call("open_binary", { path }); + expect(opened.isError, JSON.stringify(opened)).not.toBe(true); + try { + const reply = await call("inspect_native_dispatch_metadata", {}); + expect(reply.isError, JSON.stringify(reply)).not.toBe(true); + const mcpResult = nativeDispatchMetadataResultSchema.parse( + parseEvidence(reply.structuredContent).normalized_result, + ); + for (const result of [cliResult, mcpResult]) { + expect(result.target_sha256).toBe(sha256); + expect(result.input_admission?.retained_bytes).toBe(bytes.length); + expect(result.result.objc_classes).toMatchObject([ + { + name: "FixtureClass", + methods: [{ selector: "classMethod" }], + protocols: count === 1n ? ["FixtureProtocol"] : [], + decode: + count === 1n + ? { status: "decoded", reason: null } + : { status: "partial", reason: "protocol_list_unreadable" }, + }, + ]); + expect( + result.result.coverage.find( + ({ facet }) => facet === "objc_class_method_ivar_metadata", + )?.status, + ).toBe(count === 1n ? "complete" : "partial"); + } + expect(mcpResult.result.objc_classes).toEqual( + cliResult.result.objc_classes, + ); + } finally { + const closed = await call("close_binary", {}); + expect(closed.isError).not.toBe(true); + } + expect((await readFile(path)).equals(bytes)).toBe(true); + }, + ); + }, +); describe.skipIf(process.platform !== "darwin")( "native dispatch property attributes", diff --git a/tests/boundary/providers/native/nativeDispatchFormatFailures.test.ts b/tests/boundary/providers/native/nativeDispatchFormatFailures.test.ts index ab2a388c489..9f3300fd8f3 100644 --- a/tests/boundary/providers/native/nativeDispatchFormatFailures.test.ts +++ b/tests/boundary/providers/native/nativeDispatchFormatFailures.test.ts @@ -10,7 +10,10 @@ import { NativeFixtureRunner, nativeMachoTargetForFile, } from "../../../fixtures/nativeCommands.js"; -import { objcRecordBudgetMacho } from "../../../fixtures/objcRecordBudgetMacho.js"; +import { + objcAdoptedProtocolCountMacho, + objcRecordBudgetMacho, +} from "../../../fixtures/objcRecordBudgetMacho.js"; import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; const sectionCases = [ @@ -107,10 +110,7 @@ it("continues decoding the well-formed eight-byte class-list control", async () it.each([1n, 20001n])( "retains class facts and source identity through provider delivery for protocol count %s", async (count) => { - const bytes = objcRecordBudgetMacho("class"); - // Attach the class to the fixture's existing protocol pointer at 0x6c0. - bytes.writeBigUInt64LE(0x1000006b8n, 0x328); - bytes.writeBigUInt64LE(count, 0x6b8); + const bytes = objcAdoptedProtocolCountMacho(count); const { result } = await inspect(bytes); expect(result.ok).toBe(true); if (!result.ok) throw result.error; diff --git a/tests/fixtures/objcRecordBudgetMacho.ts b/tests/fixtures/objcRecordBudgetMacho.ts index 747fac20f26..892a16266b4 100644 --- a/tests/fixtures/objcRecordBudgetMacho.ts +++ b/tests/fixtures/objcRecordBudgetMacho.ts @@ -76,3 +76,11 @@ export const objcRecordBudgetMacho = ( bytes.write("protocolMethod\0", 0x9a0); return bytes; }; + +/** A class adopts the fixture protocol through a separately counted list. */ +export const objcAdoptedProtocolCountMacho = (count: bigint): Buffer => { + const bytes = objcRecordBudgetMacho("class"); + bytes.writeBigUInt64LE(base + 0x6b8n, 0x328); + bytes.writeBigUInt64LE(count, 0x6b8); + return bytes; +}; From 5ed7a9e6c6f5e2b650352489e71dded8707514b9 Mon Sep 17 00:00:00 2001 From: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:38:37 +0000 Subject: [PATCH 4/4] fix(native): invalidate snapshots from the old dispatch reader Advance the source-owned reader commitment for recovered protocol-list results. Replace the fictional old-version control with the actual v2 binding so the regression catches unintended reuse of pre-fix snapshots. Assisted-by: Reimu (gensokyo-skills:reimu-incident-triage) --- docs/native-investigation.md | 3 ++- src/native/NativeMacOSProviderMetadata.ts | 4 ++-- .../filesystem/directAnalysisSnapshotReplay.test.ts | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/native-investigation.md b/docs/native-investigation.md index bbea1162126..9e92f074f69 100644 --- a/docs/native-investigation.md +++ b/docs/native-investigation.md @@ -932,7 +932,8 @@ complete observation. `protocol_list_unreadable`. Independently decoded metadata and any readable protocol-name prefix remain available; an empty partial list does not establish that its owner adopts no protocols. Existing list and record - limits remain in effect. + limits remain in effect. Reader v3 uses a new analysis profile; snapshots + bound to reader v2 are incompatible and must be regenerated for this recovery. The byte reader holds the whole selected file in memory, so it admits files up to 64 MiB / 67108864 bytes by default. Select a larger budget with `max_input_bytes` (CLI `--max-input-bytes`), at most 1 GiB / 1073741824 diff --git a/src/native/NativeMacOSProviderMetadata.ts b/src/native/NativeMacOSProviderMetadata.ts index 931d1ca5b67..db058e0b172 100644 --- a/src/native/NativeMacOSProviderMetadata.ts +++ b/src/native/NativeMacOSProviderMetadata.ts @@ -9,12 +9,12 @@ export const APPLE_DISPATCH_METADATA_ANALYSIS_PROFILE = createAnalysisProfile( { id: NATIVE_MACOS_PROVIDER_IDENTITY.id, name: NATIVE_MACOS_PROVIDER_IDENTITY.name, - version: "apple-metadata-reader-2", + version: "apple-metadata-reader-3", }, { operation: "inspect_native_dispatch_metadata", reader: "apple-dispatch-metadata", - revision: 2, + revision: 3, }, ); /** Operations whose target process can show UI, use the network and write files. */ diff --git a/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts b/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts index 2921a6a445e..748fe726621 100644 --- a/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts +++ b/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts @@ -1572,7 +1572,7 @@ const expectReaderBindingMismatch = ( expect(projectAnalysisError(error).details).toMatchObject({ binding_mismatch: { provider: { - snapshot: { version: "apple-metadata-reader-older" }, + snapshot: { version: "apple-metadata-reader-2" }, selected: { version: selectedVersion }, }, profile_digest: { snapshot: snapshotDigest }, @@ -1610,12 +1610,13 @@ const expectProfilelessWorkflowSnapshotRejections = async ( if (!opened.ok) throw opened.error; const readerIdentity = APPLE_DISPATCH_METADATA_ANALYSIS_PROFILE.provider; const wrongReader = structuredClone(snapshot); + // Reader v2 could discard owners when their adopted-protocol list was unreadable. const otherReaderProfile = createAnalysisProfile( - { ...readerIdentity, version: "apple-metadata-reader-older" }, + { ...readerIdentity, version: "apple-metadata-reader-2" }, { operation: "inspect_native_dispatch_metadata", reader: "apple-dispatch-metadata", - revision: 0, + revision: 2, }, ); wrongReader.binding = {