Skip to content

Commit de5bfec

Browse files
Merge pull request #1172 from morluto/fix/macho-arm64e-variant-slices
fix(artifacts): match lipo arm64e variant names to their FAT slices
2 parents 37025cc + 0b32577 commit de5bfec

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎src/artifacts/MachOSliceArtifactReader.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ const lipoMatchesSlice = (
255255
reported.cpu_subtype_code !== null;
256256
return (
257257
(hasPhysicalIdentity || structurallyUnique) &&
258-
reported.name === observed.architecture &&
258+
lipoNameMatches(reported.name, observed.architecture) &&
259259
(reported.file_offset === null ||
260260
reported.file_offset === observed.slice_offset) &&
261261
(reported.size === null || reported.size === observed.slice_size) &&
@@ -269,6 +269,15 @@ const lipoMatchesSlice = (
269269
);
270270
};
271271

272+
/**
273+
* Newer lipo names an arm64e ABI variant `arm64e.<variant>` (for example
274+
* `arm64e.v1` for pointer-auth ABI version 1). The FAT table names that slice
275+
* `arm64e`; its physical identity is still compared field by field.
276+
*/
277+
const lipoNameMatches = (reported: string, observed: string): boolean =>
278+
reported === observed ||
279+
(observed === "arm64e" && /^arm64e\.[A-Za-z0-9_]+$/u.test(reported));
280+
272281
const parseSliceKeyInteger = (value: string | undefined): number | null => {
273282
if (value === undefined || !/^\d+$/u.test(value)) return null;
274283
const parsed = Number(value);

‎tests/boundary/providers/native/artifactProvider.machOSlices.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,72 @@ const lipoRunner = (stdout: string): NativeCommandRunner => ({
186186
}),
187187
),
188188
});
189+
190+
// Newer lipo names arm64e ABI variants `arm64e.<variant>`; the FAT table names
191+
// the same slice `arm64e`, and its physical identity must still match.
192+
describe("artifact Mach-O arm64e variant slices", () => {
193+
const ARM64E_V1 = { type: 0x0100000c, subtype: 0x81000002 } as const;
194+
const variantSlices = async (
195+
cpu: { readonly type: number; readonly subtype: number },
196+
lipo: (size: number) => string,
197+
) => {
198+
const root = await createTestTempDirectory("rea-slices-arm64e-");
199+
const binary = join(root, "fat");
200+
const x86 = machoImage({ cpu: CPU.x86_64 });
201+
const arm = machoImage({ cpu });
202+
await writeFile(
203+
binary,
204+
fatImage([
205+
{ cpu: CPU.x86_64, bytes: x86 },
206+
{ cpu, bytes: arm },
207+
]),
208+
);
209+
const reader = new MachOSliceArtifactReader(
210+
binary,
211+
lipoRunner(
212+
`architecture x86_64\n cputype CPU_TYPE_X86_64\n cpusubtype CPU_SUBTYPE_X86_64_ALL\n offset 4096\n size ${String(x86.length)}\n align 2^12 (4096)\n${lipo(arm.length)}`,
213+
),
214+
);
215+
const entries = [];
216+
for await (const entry of reader.entries()) entries.push(entry);
217+
return entries.map(({ path, byteOffset }) => [path, byteOffset]);
218+
};
219+
220+
it.each([
221+
[
222+
"arm64e.v1",
223+
" cputype CPU_TYPE_ARM64\n cpusubtype CPU_SUBTYPE_ARM64E\n capabilities PTR_AUTH_VERSION USERSPACE 1",
224+
],
225+
[
226+
"arm64e.x1",
227+
" cputype CPU_TYPE_ARM64\n cpusubtype unknown arm64 cpusubtype",
228+
],
229+
])("accepts lipo's %s name for the FAT arm64e slice", async (name, cpu) => {
230+
expect(
231+
await variantSlices(
232+
ARM64E_V1,
233+
(size) =>
234+
`architecture ${name}\n${cpu}\n offset 8192\n size ${String(size)}\n align 2^12 (4096)\n`,
235+
),
236+
).toEqual([
237+
["slices/x86_64", 4096],
238+
[`slices/${name}`, 8192],
239+
]);
240+
});
241+
242+
it.each([
243+
["a plain arm64 slice", CPU.arm64, "8192"],
244+
["a different offset", ARM64E_V1, "8193"],
245+
])(
246+
"still rejects an arm64e variant name for %s",
247+
async (_label, cpu, offset) => {
248+
await expect(
249+
variantSlices(
250+
cpu,
251+
(size) =>
252+
`architecture arm64e.v1\n cputype CPU_TYPE_ARM64\n cpusubtype unknown arm64 cpusubtype\n offset ${offset}\n size ${String(size)}\n align 2^12 (4096)\n`,
253+
),
254+
).rejects.toMatchObject({ reason: "integrity" });
255+
},
256+
);
257+
});

0 commit comments

Comments
 (0)