fix(native): select concrete arm64e Mach-O slices

This commit is contained in:
Kaoru0822
2026-10-06 01:25:11 -07:00
parent d565ecdc25
commit fd3282a87e
2 changed files with 124 additions and 16 deletions
+53 -16
View File
@@ -32,9 +32,12 @@ interface NativeMachoInspectionContext {
) => NativeCommandInvocation;
}
const REQUIRED_COMMANDS = [
const DISCOVERY_COMMANDS = [
["file", ["-b"]],
["lipo", ["-detailed_info"]],
] as const;
const REQUIRED_SLICE_COMMANDS = [
["otool", ["-h", "-l"]],
["nm", ["-gjU"]],
["dyld_info", ["-imports"]],
@@ -51,12 +54,27 @@ export const inspectNativeMacho = async (
context: NativeMachoInspectionContext,
): Promise<Result<NativeMachoObservation, AnalysisError>> => {
const captures: NativeCommandCapture[] = [];
for (const [tool, prefix] of REQUIRED_COMMANDS) {
for (const [tool, prefix] of DISCOVERY_COMMANDS) {
const captured = await context.run(
tool,
[...prefix, context.target.path],
context.signal,
);
if (!captured.ok) return err(captured.error);
captures.push(captured.value);
}
const lipo = captures.find(({ tool }) => tool === "lipo");
if (lipo === undefined) throw new TypeError("Missing lipo capture");
const selectedArchitecture = selectArchitecture(
context.target,
parseLipoArchitectures(lipo.stdout).map(({ name }) => name),
);
for (const [tool, prefix] of REQUIRED_SLICE_COMMANDS) {
const captured = await context.run(
tool,
[
...prefix,
...selectedArchitectureArguments(tool, context.target),
...selectedArchitectureArguments(tool, selectedArchitecture),
context.target.path,
],
context.signal,
@@ -70,7 +88,7 @@ export const inspectNativeMacho = async (
tool,
[
...prefix,
...selectedArchitectureArguments(tool, context.target),
...selectedArchitectureArguments(tool, selectedArchitecture),
context.target.path,
],
context.signal,
@@ -262,18 +280,37 @@ export const architectureLocations = (
const selectedArchitectureArguments = (
tool: string,
target: BinaryTarget,
architecture: string | null,
): string[] => {
if (
target.kind !== "executable" ||
target.availableArchitectures.length < 2 ||
tool === "file" ||
tool === "lipo"
)
return [];
const architecture =
target.architecture === "x86" ? "i386" : target.architecture;
if (architecture === null) return [];
const baseArchitecture = architecture.split(".")[0] ?? architecture;
// dyld_info distinguishes arm64e ABI variants; the other Apple tools use
// the base CPU-family spelling for the same slice.
const toolArchitecture =
tool === "dyld_info" ? architecture : baseArchitecture;
return tool === "dwarfdump"
? [`--arch=${architecture}`]
: ["-arch", architecture];
? [`--arch=${toolArchitecture}`]
: ["-arch", toolArchitecture];
};
const selectArchitecture = (
target: BinaryTarget,
available: readonly string[],
): string | null => {
if (target.kind !== "executable" || target.availableArchitectures.length < 2)
return null;
const normalized =
target.architecture === "x86" ? "i386" : target.architecture;
if (available.includes(normalized)) return normalized;
// BinaryTarget intentionally normalizes ARM64 CPU subtypes. Recover the
// concrete arm64e slice name from lipo before invoking slice-aware tools.
if (normalized === "arm64") {
const arm64e = available.find((architecture) => architecture === "arm64e");
if (arm64e !== undefined) return arm64e;
const variant = available.find((architecture) =>
/^arm64e\.[A-Za-z0-9_]+$/u.test(architecture),
);
if (variant !== undefined) return variant;
}
return normalized;
};
@@ -1,5 +1,7 @@
import { expect, it } from "vitest";
import { inspectMachoSchema } from "../../../../src/domain/nativeInspection.js";
import { err } from "../../../../src/domain/result.js";
import { NativeCommandFailure } from "../../../../src/native/CommandRunner.js";
import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js";
import {
NativeFixtureRunner,
@@ -40,3 +42,72 @@ it("does not attribute the first universal slice's UUID and segments to the sele
"AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA",
);
});
it.each([
{
architectures: ["x86_64", "arm64e", "arm64e.x1"],
selected: "arm64e",
dyldArchitecture: "arm64e",
},
{
architectures: ["x86_64", "arm64e.v1"],
selected: "arm64e",
dyldArchitecture: "arm64e.v1",
},
])(
"selects the $dyldArchitecture slice of a universal arm64e target",
async ({ architectures, selected, dyldArchitecture }) => {
const calls: {
readonly tool: string;
readonly arguments_: readonly string[];
}[] = [];
const lipo = architectures
.map(
(architecture, index) =>
`architecture ${architecture}\n cputype 16777228\n cpusubtype 2\n offset ${index * 16384}\n size 8192\n align 2^14 (16384)`,
)
.join("\n");
const fixtures = new NativeFixtureRunner({
lipo,
dwarfdump: `UUID: 01234567-89AB-CDEF-0123-456789ABCDEF (${dyldArchitecture}) fixture\n`,
});
const client = new NativeMacOSProvider(
{
run(tool, arguments_) {
calls.push({ tool, arguments_ });
const expected = tool === "dyld_info" ? dyldArchitecture : selected;
if (
!["file", "lipo"].includes(tool) &&
!arguments_.some(
(argument, index) =>
argument === expected ||
argument === `--arch=${expected}` ||
(argument === "-arch" && arguments_[index + 1] === expected),
)
)
return Promise.resolve(
err(new NativeCommandFailure(tool, "nonzero-exit", 1)),
);
return fixtures.run(tool, arguments_);
},
},
"darwin",
).createClient(nativeMachoTarget("/owned/universal"));
const result = await client.execute("inspect_macho", {});
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.result).toMatchObject({
uuid: "01234567-89AB-CDEF-0123-456789ABCDEF",
architectures: {
items: architectures.map((name) => ({ name })),
},
});
expect(
calls
.filter(({ tool }) => tool === "dyld_info")
.every(({ arguments_ }) => arguments_.includes(dyldArchitecture)),
).toBe(true);
},
);