Files
rea/tests/boundary/providers/native/nativeDispatchFormatFailures.test.ts
T
Lewis WangandClaude Opus 5.5 94c1c6aec0 fix(native): decode Objective-C categories that Swift lists in __objc_catlist2 (#14025)
* fix(native): decode Objective-C categories that Swift lists in __objc_catlist2

Swift lists categories on a class it reaches through a Swift class stub
in __objc_catlist2, and the Objective-C runtime loads that list like
__objc_catlist. inspect_native_dispatch_metadata read only __objc_catlist,
so these categories, their methods and protocols were missing while
objc_properties_categories coverage reported complete.

Read both lists. A stub's first word is a plain value from 1 to 15, as
the runtime's isStubClass checks, and it holds no class_t, so a category
whose class reference is a stub keeps a null class and coverage names
the reference instead of reading unrelated bytes as a class name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(native): version the dispatch reader profile for __objc_catlist2 categories

Reading __objc_catlist2 changes inspect_native_dispatch_metadata results,
but the reader still declared apple-metadata-reader-4, so a snapshot saved
before this change matched the current profile and replayed its empty,
complete category result. Declare reader version and revision 5 so those
snapshots are refused with a binding mismatch and the image is read again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 10:25:48 -05:00

146 lines
4.7 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { join } from "node:path";
import { expect, it } from "vitest";
import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js";
import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js";
import { nativeDispatchMetadataResultSchema } from "../../../../src/domain/native/objcSwiftMetadata.js";
import {
NativeFixtureRunner,
nativeMachoTargetForFile,
} from "../../../fixtures/nativeCommands.js";
import {
objcAdoptedProtocolCountMacho,
objcRecordBudgetMacho,
} from "../../../fixtures/objcRecordBudgetMacho.js";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
const sectionCases = [
{
section: "__objc_classlist",
size: 12,
reason: "Misaligned Objective-C class-list size",
},
{
section: "__objc_protolist",
size: 12,
reason: "Misaligned Objective-C protocol list",
},
{
section: "__objc_catlist",
size: 12,
reason: "Misaligned Objective-C category list",
},
{
section: "__objc_catlist2",
size: 12,
reason: "Misaligned Objective-C category list",
},
{
section: "__swift5_types",
size: 6,
reason: "Misaligned Swift type section",
},
{
section: "__swift5_proto",
size: 6,
reason: "Misaligned Swift conformance section",
},
];
const inspect = async (bytes: Buffer) => {
const root = await createTestTempDirectory("rea-dispatch-format-");
const path = join(root, "fixture.macho");
await writeFile(path, bytes);
const client = new NativeMacOSProvider(
{},
new NativeFixtureRunner(),
"darwin",
).createClient(await nativeMachoTargetForFile(path));
try {
return {
path,
result: await client.execute("inspect_native_dispatch_metadata", {}),
};
} finally {
expect((await client.close()).ok).toBe(true);
}
};
it.each(sectionCases)(
"reports malformed $section bytes as a target refusal with the producing reason",
async ({ section, size, reason }) => {
const bytes = objcRecordBudgetMacho("class");
bytes.fill(0, 104, 120);
bytes.write(section, 104);
bytes.writeBigUInt64LE(BigInt(size), 144);
const { path, result } = await inspect(bytes);
if (result.ok) throw new Error("Expected malformed target refusal");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "unsupported_target",
category: "unsupported_target",
details: { operation: "inspect_native_dispatch_metadata", path, reason },
});
},
);
it("keeps an unsupported big-endian CPU declaration distinct from provider failure", async () => {
const bytes = Buffer.alloc(32);
bytes.writeUInt32BE(0xfeedfacf);
bytes.writeUInt32BE(0x0100000c, 4);
bytes.writeUInt32BE(2, 12);
const { path, result } = await inspect(bytes);
if (result.ok) throw new Error("Expected unsupported target refusal");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "unsupported_target",
category: "unsupported_target",
details: { path, reason: "Unsupported big-endian Mach-O CPU type" },
});
});
it("continues decoding the well-formed eight-byte class-list control", async () => {
const { result } = await inspect(objcRecordBudgetMacho("class"));
if (!result.ok) throw result.error;
expect(result.value.result).toMatchObject({
result: {
objc_classes: [expect.objectContaining({ name: "FixtureClass" })],
},
});
});
it.each([1n, 20001n])(
"retains class facts and source identity through provider delivery for protocol count %s",
async (count) => {
const bytes = objcAdoptedProtocolCountMacho(count);
const { result } = await inspect(bytes);
if (!result.ok) throw result.error;
const report = nativeDispatchMetadataResultSchema.parse(
result.value.result,
);
expect(report.target_sha256).toBe(
createHash("sha256").update(bytes).digest("hex"),
);
expect(report.input_admission?.retained_bytes).toBe(bytes.length);
expect(report.result.objc_classes).toMatchObject([
{
name: "FixtureClass",
methods: [{ selector: "classMethod", method_type: "instance" }],
protocols: count === 1n ? ["FixtureProtocol"] : [],
decode:
count === 1n
? { status: "decoded", reason: null }
: { status: "partial", reason: "protocol_list_unreadable" },
},
]);
expect(report.result.objc_dispatch_implementations).toMatchObject([
{ class_name: "FixtureClass", selector: "classMethod" },
]);
expect(
report.result.coverage.find(
({ facet }) => facet === "objc_class_method_ivar_metadata",
)?.status,
).toBe(count === 1n ? "complete" : "partial");
},
);