diff --git a/src/artifacts/NativeDmgArtifactReader.ts b/src/artifacts/NativeDmgArtifactReader.ts index c983c77a4..a3f3fb120 100644 --- a/src/artifacts/NativeDmgArtifactReader.ts +++ b/src/artifacts/NativeDmgArtifactReader.ts @@ -625,7 +625,9 @@ const verifyFailureReason = ( processOutput(cause, "stderr"), ); switch (diagnostic) { + // hdiutil follows AppleLanguages, so en-GB and en-AU spell it "recognised". case "image not recognized": + case "image not recognised": return "format"; case "invalid checksum": case "image data corrupted": diff --git a/src/artifacts/apple/MachoImage.fixture.ts b/src/artifacts/apple/MachoImage.fixture.ts index d43249d38..2fa611d1f 100644 --- a/src/artifacts/apple/MachoImage.fixture.ts +++ b/src/artifacts/apple/MachoImage.fixture.ts @@ -21,6 +21,7 @@ export const CPU = { i386: { type: 7, subtype: 3 }, arm64: { type: 0x0100000c, subtype: 0 }, arm64e: { type: 0x0100000c, subtype: 0x80000002 }, + arm64eX1: { type: 0x0100000c, subtype: 0x8000000c }, x86_64: { type: 0x01000007, subtype: 3 }, } as const; diff --git a/src/artifacts/apple/MachoLoadCommandReader.test.ts b/src/artifacts/apple/MachoLoadCommandReader.test.ts index a7761ffe4..97bceb672 100644 --- a/src/artifacts/apple/MachoLoadCommandReader.test.ts +++ b/src/artifacts/apple/MachoLoadCommandReader.test.ts @@ -136,6 +136,98 @@ it("uses legacy simulator and device platform facts when resolving dylibs", asyn ); }); +it("selects matching arm64e subtype slices when tracing a mixed FAT image", async () => { + const root = await createTestTempDirectory("rea-macho-arm64e-subtypes-"); + const executablePath = "Contents/MacOS/App"; + const dylibPath = "Contents/MacOS/libVariant.dylib"; + const installName = "@executable_path/libVariant.dylib"; + const arm64eDependency = "@executable_path/libArm64e.dylib"; + const arm64eX1Dependency = "@executable_path/libArm64eX1.dylib"; + const executableSlice = (cpu: typeof CPU.arm64e | typeof CPU.arm64eX1) => + machoImage({ + cpu, + commands: [ + buildVersionCommand(1), + dylibCommand(LC.LOAD_DYLIB, installName), + ], + }); + const executable = fatImage([ + { + cpu: CPU.arm64eX1, + bytes: executableSlice(CPU.arm64eX1), + }, + { + cpu: CPU.arm64e, + bytes: executableSlice(CPU.arm64e), + }, + ]); + const variantSlice = ( + cpu: typeof CPU.arm64e | typeof CPU.arm64eX1, + dependencyName: string, + ) => + machoImage({ + cpu, + fileType: FILE_TYPE.dylib, + commands: [ + buildVersionCommand(1), + dylibCommand(LC.ID_DYLIB, installName), + dylibCommand(LC.LOAD_DYLIB, dependencyName), + ], + }); + const completeDylib = fatImage([ + { + cpu: CPU.arm64e, + bytes: variantSlice(CPU.arm64e, arm64eDependency), + }, + { + cpu: CPU.arm64eX1, + bytes: variantSlice(CPU.arm64eX1, arm64eX1Dependency), + }, + ]); + await mkdir(join(root, "Contents/MacOS"), { recursive: true }); + await writeFile(join(root, executablePath), executable); + await writeFile(join(root, dylibPath), completeDylib); + + const trace = () => + traceDylibResolution({ + rootPath: root, + targetPath: join(root, executablePath), + targetSha256: createHash("sha256").update(executable).digest("hex"), + enumerateRoots: false, + parameters: { roots: [executablePath] }, + }); + + const complete = await trace(); + expect( + complete.edges + .filter(({ loader }) => loader === dylibPath) + .map(({ install_name: dependencyName }) => dependencyName), + ).toEqual([arm64eX1Dependency, arm64eDependency]); + + const onlyArm64e = fatImage([ + { + cpu: CPU.arm64e, + bytes: variantSlice(CPU.arm64e, arm64eDependency), + }, + ]); + await writeFile(join(root, dylibPath), onlyArm64e); + const missingVariant = await trace(); + expect( + missingVariant.edges + .filter( + ({ loader, install_name: dependencyName }) => + loader === executablePath && dependencyName === installName, + ) + .map(({ candidates, resolution }) => [ + candidates[0]?.outcome, + resolution.status, + ]), + ).toEqual([ + ["architecture-missing", "unresolved"], + ["resolved", "resolved"], + ]); +}); + describe("Mach-O load command reader: decodes thin-image load commands", () => { it("decodes dylib loading commands of a thin image", async () => { const image = machoImage({ diff --git a/src/artifacts/apple/MachoLoadCommandReader.ts b/src/artifacts/apple/MachoLoadCommandReader.ts index 6656153d0..27f7ec283 100644 --- a/src/artifacts/apple/MachoLoadCommandReader.ts +++ b/src/artifacts/apple/MachoLoadCommandReader.ts @@ -3,6 +3,7 @@ import type { MachoImageFacts, MachoSlice, } from "../../domain/apple/dylibResolution.js"; +import { machoCpuSubtypeBase } from "../../domain/apple/machoCpuIdentity.js"; import { classifyFatContainerIdentity, readFatContainerHeader, @@ -482,13 +483,12 @@ const CPU_ARCH_ABI64 = 0x01000000; const CPU_ARCH_ABI64_32 = 0x02000000; const CPU_TYPE_X86 = 7; const CPU_TYPE_ARM = 12; -const CPU_SUBTYPE_MASK = 0xff000000; const CPU_SUBTYPE_ARM64E = 2; const CPU_SUBTYPE_ARM64E_X1 = 12; const CPU_SUBTYPE_X86_64_H = 8; const architectureName = (cpuType: number, cpuSubtype: number): string => { - const subtype = cpuSubtype & ~CPU_SUBTYPE_MASK; + const subtype = machoCpuSubtypeBase(cpuSubtype); if (cpuType === (CPU_TYPE_ARM | CPU_ARCH_ABI64)) // Both pointer-authentication subtypes belong to the arm64e family. return subtype === CPU_SUBTYPE_ARM64E || subtype === CPU_SUBTYPE_ARM64E_X1 diff --git a/src/domain/apple/dyldPaths.ts b/src/domain/apple/dyldPaths.ts index e853b9a8a..af2764d29 100644 --- a/src/domain/apple/dyldPaths.ts +++ b/src/domain/apple/dyldPaths.ts @@ -3,6 +3,7 @@ import type { DylibTreeView, MachoSlice, } from "./dylibResolution.js"; +import { machoCpuSubtypeBase } from "./machoCpuIdentity.js"; const MAX_SYMLINK_HOPS = 32; @@ -105,10 +106,19 @@ const COMPATIBLE_ARCHITECTURES: Readonly> = { export const compatibleSlice = ( slices: readonly MachoSlice[], - process: string, + process: Pick, ): MachoSlice | undefined => { - for (const architecture of COMPATIBLE_ARCHITECTURES[process] ?? [process]) { - const slice = slices.find((item) => item.architecture === architecture); + for (const architecture of COMPATIBLE_ARCHITECTURES[process.architecture] ?? [ + process.architecture, + ]) { + const slice = slices.find( + (item) => + item.architecture === architecture && + (process.architecture !== "arm64e" || + (item.cpu_type === process.cpu_type && + machoCpuSubtypeBase(item.cpu_subtype) === + machoCpuSubtypeBase(process.cpu_subtype))), + ); if (slice !== undefined) return slice; } return undefined; diff --git a/src/domain/apple/dylibResolution.ts b/src/domain/apple/dylibResolution.ts index 871cb7601..00077efa1 100644 --- a/src/domain/apple/dylibResolution.ts +++ b/src/domain/apple/dylibResolution.ts @@ -220,7 +220,10 @@ interface LoadedImage { interface ProcessContext { readonly view: DylibTreeView; readonly root: string; - readonly architecture: string; + readonly processIdentity: Pick< + MachoSlice, + "architecture" | "cpu_type" | "cpu_subtype" + >; readonly searchOverrides: readonly DyldSearchOverride[]; readonly environmentRoots: Set; readonly platforms: readonly number[]; @@ -304,7 +307,7 @@ const evaluateCandidate = async ( let outcome: Candidate["outcome"]; if (facts.status !== "parsed") outcome = facts.status; else { - const slice = compatibleSlice(facts.slices, context.architecture); + const slice = compatibleSlice(facts.slices, context.processIdentity); if (slice === undefined) outcome = "architecture-missing"; // Dependency commands require a dylib; generic dyld image loadability // also admits main executables and bundles for different operations. @@ -314,7 +317,7 @@ const evaluateCandidate = async ( context.platforms, slice.platforms, slice.file_type_code, - context.architecture, + context.processIdentity.architecture, ); outcome = platformResult === "compatible" @@ -470,11 +473,11 @@ const resolveDependency = async ( resolved.image === null ? undefined : context.images.get(resolved.image); const slice = image?.status === "parsed" - ? compatibleSlice(image.slices, context.architecture) + ? compatibleSlice(image.slices, context.processIdentity) : undefined; return { root: context.root, - architecture: context.architecture, + architecture: context.processIdentity.architecture, loader, via: [...chain], ...dependency, @@ -522,7 +525,7 @@ const traceProcess = async ( const facts = context.images.get(image); const slice = facts?.status === "parsed" - ? compatibleSlice(facts.slices, context.architecture) + ? compatibleSlice(facts.slices, context.processIdentity) : undefined; if (slice === undefined) continue; context.loaded.set(image, { @@ -593,7 +596,11 @@ export const traceDylibLoading = async ( { view, root, - architecture: slice.architecture, + processIdentity: { + architecture: slice.architecture, + cpu_type: slice.cpu_type, + cpu_subtype: slice.cpu_subtype, + }, searchOverrides, environmentRoots, platforms: slice.platforms, diff --git a/src/domain/apple/machoCpuIdentity.ts b/src/domain/apple/machoCpuIdentity.ts new file mode 100644 index 000000000..b46445340 --- /dev/null +++ b/src/domain/apple/machoCpuIdentity.ts @@ -0,0 +1,6 @@ +/** CPU subtype bits reserved for ABI/capability flags, not the base subtype. */ +const CPU_SUBTYPE_CAPABILITY_MASK = 0xff000000; + +/** Return the CPU subtype identity shared across capability-flag variations. */ +export const machoCpuSubtypeBase = (cpuSubtype: number): number => + cpuSubtype & ~CPU_SUBTYPE_CAPABILITY_MASK; diff --git a/tests/boundary/process/processCaptureLifecycle.test.ts b/tests/boundary/process/processCaptureLifecycle.test.ts index b64b13078..dcba0510f 100644 --- a/tests/boundary/process/processCaptureLifecycle.test.ts +++ b/tests/boundary/process/processCaptureLifecycle.test.ts @@ -1047,6 +1047,8 @@ itWithCaptureCapability( executable: process.execPath, arguments: [processFixture, "tree"], working_directory: dirname(processFixture), + // The post-capture ps lookup requires OS PIDs, not display identities. + normalization: { pids: false }, timeout_ms: 20_000, idle_timeout_ms: 20_000, }), @@ -1069,9 +1071,42 @@ itWithCaptureCapability( expect(JSON.stringify(capture.process_samples)).toContain( dirname(processFixture), ); - const { stdout } = await execFileAsync("ps", ["-axo", "command="]); - expect(stdout).not.toContain(`${processFixture} tree-child`); - expect(stdout).not.toContain(`${processFixture} tree-grandchild`); + // Observe only this run's sampled descendants; never buffer the host table. + const fixturePids = [ + ...new Set( + capture.process_samples + .filter( + ({ command }) => + command.includes(`${processFixture} tree-child`) || + command.includes(`${processFixture} tree-grandchild`), + ) + .map(({ pid }) => pid), + ), + ]; + let remainingCommands = ""; + try { + remainingCommands = ( + await execFileAsync( + "ps", + ["-p", fixturePids.join(","), "-o", "command="], + { maxBuffer: 64 * 1024 }, + ) + ).stdout; + } catch (cause: unknown) { + // ps exits 1 with no rows when all selected processes have exited. + if ( + !(cause instanceof Error) || + !("code" in cause) || + cause.code !== 1 || + !("stdout" in cause) || + cause.stdout !== "" + ) + throw cause; + } + expect(remainingCommands).not.toContain(`${processFixture} tree-child`); + expect(remainingCommands).not.toContain( + `${processFixture} tree-grandchild`, + ); }, 20_000, ); diff --git a/tests/boundary/providers/native/nativeDmgArtifactReader.test.ts b/tests/boundary/providers/native/nativeDmgArtifactReader.test.ts index b19fb7d8e..9e03727c1 100644 --- a/tests/boundary/providers/native/nativeDmgArtifactReader.test.ts +++ b/tests/boundary/providers/native/nativeDmgArtifactReader.test.ts @@ -619,23 +619,31 @@ describe("native DMG artifact reader", () => { expect(calls).toContainEqual(["detach", "/dev/disk-fixture"]); }); - it("rejects non-zero verification results", async () => { - await expect( - createAttachedReader("/tmp/image.dmg", process.env, undefined, { - run: () => - Promise.resolve({ - stdout: "verify output", - stderr: "hdiutil: verify failed - image not recognized", - exitCode: 1, - }), - }), - ).rejects.toMatchObject({ - reason: "format", - message: expect.stringMatching( - /"arguments":\["verify","\/tmp\/image\.dmg"\].*"exitCode":1.*"stderr":"hdiutil: verify failed - image not recognized"/u, - ), - }); - }); + // hdiutil localizes its diagnostics; en-GB and other English locales spell + // the refusal "recognised". + it.each(["recognized", "recognised"])( + "rejects non-zero verification results (image not %s)", + async (spelling) => { + await expect( + createAttachedReader("/tmp/image.dmg", process.env, undefined, { + run: () => + Promise.resolve({ + stdout: "verify output", + stderr: `hdiutil: verify failed - image not ${spelling}`, + exitCode: 1, + }), + }), + ).rejects.toMatchObject({ + reason: "format", + message: expect.stringMatching( + new RegExp( + `"arguments":\\["verify","/tmp/image\\.dmg"\\].*"exitCode":1.*"stderr":"hdiutil: verify failed - image not ${spelling}"`, + "u", + ), + ), + }); + }, + ); it("retains the root and reports unknown ownership when recovery discovery fails", async () => { const mount = createMountFixture();