refactor(native): reuse Mach-O signature recognition for initializers (#12478)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 19:08:28 +00:00
1 parent 7df462cbfa
commit f759193fa4
1 file changed
+2 -13
@@ -18,7 +18,6 @@ import {
ArtifactResourceScope,
} from "../../resources/index.js";
import {
BinaryReader,
BinaryFormatFailure,
BinaryResourceFailure,
binaryResourceCeiling,
@@ -42,6 +41,7 @@ import {
ELF_POINTER_RELOCATION_FORMAT,
ElfPointerRelocationFailure,
} from "../../domain/native/elfPointerRelocations.js";
import { hasMachoMagic } from "../../domain/apple/machoContainer.js";
import { requireSelectedNativeImage } from "../../domain/apple/machoSliceSelection.js";
import { readElfInitializers } from "./ElfInitializers.js";
import { readMachoInitializers } from "./MachoInitializers.js";
@@ -49,11 +49,6 @@ import { errorMessage } from "../../kernel/errorMessage.js";
const OPERATION = "inspect_native_initializers";
const MACHO_MAGICS = new Set([
0xfeedface, 0xcefaedfe, 0xfeedfacf, 0xcffaedfe, 0xcafebabe, 0xbebafeca,
0xcafebabf, 0xbfbafeca,
]);
const isElf = (bytes: Buffer): boolean =>
bytes.length >= 4 &&
bytes[0] === 0x7f &&
@@ -61,12 +56,6 @@ const isElf = (bytes: Buffer): boolean =>
bytes[2] === 0x4c &&
bytes[3] === 0x46;
const isMacho = (bytes: Buffer): boolean =>
bytes.length >= 4 &&
MACHO_MAGICS.has(
new BinaryReader(bytes, false, "native-init-format-sniff").u32(0),
);
/** Portable ELF and Mach-O initializer reader over one stable regular file. */
export class NativeInitializersProvider implements NativeInitializersPort {
readonly identity = NATIVE_INITIALIZERS_PROVIDER;
@@ -157,7 +146,7 @@ const inspectSelected = async (
);
return readElfInitializers(selected.bytes, page, signal);
}
if (!isMacho(container) && !isMacho(selected.bytes))
if (!hasMachoMagic(container) && !hasMachoMagic(selected.bytes))
throw new BinaryFormatFailure(
"malformed",
"Selected file is not an ELF or Mach-O image",