Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

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

Kaoru0822fd3282a87e40
diff --git a/src/native/NativeMachoInspection.ts b/src/native/NativeMachoInspection.ts
index 4593069d3..5077f2417 100644
--- a/src/native/NativeMachoInspection.ts
+++ b/src/native/NativeMachoInspection.ts
@@ -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;
};
diff --git a/tests/boundary/providers/native/nativeSelectedArchitecture.test.ts b/tests/boundary/providers/native/nativeSelectedArchitecture.test.ts
index a57b5ba8a..fb9de3c47 100644
--- a/tests/boundary/providers/native/nativeSelectedArchitecture.test.ts
+++ b/tests/boundary/providers/native/nativeSelectedArchitecture.test.ts
@@ -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);
+ },
+);