* fix(native): reject required otool fields truncated before their value #1967 let requireUniqueField count a label at end of line so an empty dylib-use options row is present. That applied to every required field, so truncated output such as a bare vmaddr, vmsize, uuid or entryoff line passed the presence check while parseField dropped it, and the value silently became null. Let parseField decide which labeled line carries a value: the single matching line must parse as that field. The empty options row remains the only valueless field parseField admits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(native): admit the empty segname of Mach-O object files MH_OBJECT files put their sections in one segment whose name is empty, and otool -l prints it as a bare segname label. Requiring a value for every scalar field rejected valid object-file output as unreadable. Let parseField admit an empty segname like the empty dylib-use options row, and keep projecting an empty or absent segment name as "unknown", as main did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(native): admit all-NUL Mach-O section names An all-NUL section_64.sectname prints as a bare sectname label, like an empty segname. Admit it in parseField and keep projecting the empty name as "unknown", as main did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
276 lines
8.0 KiB
TypeScript
276 lines
8.0 KiB
TypeScript
import { expect, it } from "vitest";
|
|
|
|
import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js";
|
|
import { parseOtoolLoadCommands } from "../../../../src/native/parsers/otool.js";
|
|
import {
|
|
NativeFixtureRunner,
|
|
nativeFixture,
|
|
nativeMachoTarget,
|
|
withOtoolMachHeader,
|
|
} from "../../../fixtures/nativeCommands.js";
|
|
|
|
it("retains lazily loaded library dependencies", () => {
|
|
const load = parseOtoolLoadCommands(
|
|
withOtoolMachHeader(
|
|
[
|
|
"Load command 0",
|
|
" cmd LC_LAZY_LOAD_DYLIB",
|
|
" cmdsize 48",
|
|
" name /usr/lib/libLazy.dylib (offset 24)",
|
|
" time stamp 2 Thu Jan 1 00:00:02 1970",
|
|
" current version 1.0.0",
|
|
"compatibility version 1.0.0",
|
|
"",
|
|
].join("\n"),
|
|
),
|
|
);
|
|
expect(load.dependencies).toContainEqual({
|
|
path: "/usr/lib/libLazy.dylib",
|
|
kind: "LC_LAZY_LOAD_DYLIB",
|
|
current_version: "1.0.0",
|
|
compatibility_version: "1.0.0",
|
|
});
|
|
});
|
|
|
|
// Captured from the real Apple bundle CI fixture's `otool -h -l` output.
|
|
const delayedDylib = withOtoolMachHeader(`Load command 0
|
|
cmd LC_LOAD_DYLIB
|
|
cmdsize 56
|
|
name @rpath/libdelay.dylib (offset 28)
|
|
options delay-init
|
|
current version 0.0.0
|
|
compatibility version 1.0.0
|
|
`);
|
|
|
|
it("preserves dylib-use options through native Mach-O inspection", async () => {
|
|
const runner = new NativeFixtureRunner({ "otool:-h": delayedDylib });
|
|
const client = new NativeMacOSProvider({}, runner, "darwin").createClient(
|
|
nativeMachoTarget("/owned/fixture"),
|
|
);
|
|
const result = await client.execute("inspect_macho", {});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.result).toMatchObject({
|
|
load_commands: {
|
|
items: [{ kind: "LC_LOAD_DYLIB", fields: { options: "delay-init" } }],
|
|
},
|
|
dependencies: {
|
|
items: [
|
|
{
|
|
path: "@rpath/libdelay.dylib",
|
|
kind: "LC_LOAD_DYLIB",
|
|
current_version: "0.0.0",
|
|
compatibility_version: "1.0.0",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
});
|
|
|
|
it("retains empty dylib-use options and rejects missing or ambiguous encoding fields", () => {
|
|
expect(
|
|
parseOtoolLoadCommands(delayedDylib.replace("delay-init", "")).commands[0]
|
|
?.fields.options,
|
|
).toBe("");
|
|
const malformed: Array<readonly [string, RegExp]> = [
|
|
["", /timestamp or options/u],
|
|
[
|
|
" options delay-init\n options weak\n",
|
|
/options is missing or repeated/u,
|
|
],
|
|
[
|
|
" options delay-init\n time stamp 2 Thu Jan 1 00:00:02 1970\n",
|
|
/timestamp or options/u,
|
|
],
|
|
[
|
|
" options delay-init\n injected text\n",
|
|
/ambiguous multiline text/u,
|
|
],
|
|
];
|
|
for (const [replacement, reason] of malformed) {
|
|
expect(() =>
|
|
parseOtoolLoadCommands(
|
|
delayedDylib.replace(" options delay-init\n", replacement),
|
|
),
|
|
).toThrow(reason);
|
|
}
|
|
});
|
|
|
|
it.each(["vmaddr", "vmsize", "uuid", "entryoff"])(
|
|
"rejects a required %s label truncated before its value",
|
|
async (key) => {
|
|
const raw = await nativeFixture("otool-load.txt");
|
|
const truncated = raw.replace(
|
|
new RegExp(`^(\\s*${key})\\s+\\S.*$`, "mu"),
|
|
"$1",
|
|
);
|
|
expect(truncated).not.toBe(raw);
|
|
expect(() => parseOtoolLoadCommands(raw)).not.toThrow();
|
|
// Only the empty dylib-use options row may omit its value.
|
|
expect(() => parseOtoolLoadCommands(truncated)).toThrow(
|
|
`otool field ${key} has no value`,
|
|
);
|
|
},
|
|
);
|
|
|
|
it("retains the unnamed segment of a Mach-O object file", () => {
|
|
// Captured from `otool -l` on a clang `-c` object: MH_OBJECT sections live
|
|
// in one segment whose segname is empty, printed as a bare label.
|
|
const objectSegment = [
|
|
"Load command 0",
|
|
" cmd LC_SEGMENT_64",
|
|
" cmdsize 152",
|
|
" segname ",
|
|
" vmaddr 0x0000000000000000",
|
|
" vmsize 0x0000000000000018",
|
|
" fileoff 392",
|
|
" filesize 24",
|
|
" maxprot 0x00000007",
|
|
" initprot 0x00000007",
|
|
" nsects 1",
|
|
" flags 0x0",
|
|
"Section",
|
|
" sectname __text",
|
|
" segname __TEXT",
|
|
" addr 0x0000000000000000",
|
|
" size 0x0000000000000018",
|
|
" offset 392",
|
|
" align 2^2 (4)",
|
|
" reloff 0",
|
|
" nreloc 0",
|
|
" flags 0x80000400",
|
|
" reserved1 0",
|
|
" reserved2 0",
|
|
"",
|
|
].join("\n");
|
|
const load = parseOtoolLoadCommands(withOtoolMachHeader(objectSegment));
|
|
expect(load.segments).toMatchObject([
|
|
{ name: "unknown", sections: [{ segment: "__TEXT", name: "__text" }] },
|
|
]);
|
|
// An all-NUL section name is printed the same way and stays "unknown".
|
|
expect(
|
|
parseOtoolLoadCommands(
|
|
withOtoolMachHeader(
|
|
objectSegment.replace(" sectname __text", " sectname "),
|
|
),
|
|
).segments,
|
|
).toMatchObject([{ sections: [{ segment: "__TEXT", name: "unknown" }] }]);
|
|
expect(() =>
|
|
parseOtoolLoadCommands(
|
|
withOtoolMachHeader(
|
|
objectSegment.replace(" vmaddr 0x0000000000000000", " vmaddr"),
|
|
),
|
|
),
|
|
).toThrow("otool field vmaddr has no value");
|
|
});
|
|
|
|
it("preserves dependency install names that begin with the word stamp", () => {
|
|
const load = parseOtoolLoadCommands(
|
|
withOtoolMachHeader(
|
|
[
|
|
"Load command 0",
|
|
" cmd LC_LOAD_DYLIB",
|
|
" cmdsize 48",
|
|
" name stamp plugin.dylib (offset 24)",
|
|
" time stamp 2 Thu Jan 1 00:00:02 1970",
|
|
" current version 1.0.0",
|
|
"compatibility version 1.0.0",
|
|
"",
|
|
].join("\n"),
|
|
),
|
|
);
|
|
expect(load.dependencies).toContainEqual({
|
|
path: "stamp plugin.dylib",
|
|
kind: "LC_LOAD_DYLIB",
|
|
current_version: "1.0.0",
|
|
compatibility_version: "1.0.0",
|
|
});
|
|
expect(
|
|
load.commands.find((command) => command.kind === "LC_LOAD_DYLIB")?.fields[
|
|
"time stamp"
|
|
],
|
|
).toMatch(/^2 Thu Jan/);
|
|
});
|
|
|
|
it("normalizes legacy version-minimum commands into build metadata", () => {
|
|
const load = parseOtoolLoadCommands(
|
|
withOtoolMachHeader(
|
|
[
|
|
"Load command 0",
|
|
" cmd LC_VERSION_MIN_MACOSX",
|
|
" cmdsize 16",
|
|
" version 10.13",
|
|
" sdk 10.15",
|
|
"Load command 1",
|
|
" cmd LC_VERSION_MIN_IPHONEOS",
|
|
" cmdsize 16",
|
|
" version 12.0",
|
|
" sdk 12.1",
|
|
"Load command 2",
|
|
" cmd LC_SOURCE_VERSION",
|
|
" cmdsize 16",
|
|
" version 1.2.3",
|
|
"",
|
|
].join("\n"),
|
|
),
|
|
);
|
|
expect(load.builds).toEqual([
|
|
{
|
|
platform: "MACOSX",
|
|
minimum_os: "10.13",
|
|
sdk: "10.15",
|
|
tools: [],
|
|
},
|
|
{
|
|
platform: "IPHONEOS",
|
|
minimum_os: "12.0",
|
|
sdk: "12.1",
|
|
tools: [],
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("does not classify LC_LOAD_DYLINKER as a dylib dependency", async () => {
|
|
const load = parseOtoolLoadCommands(
|
|
await nativeFixture("native-macho-header/otool-header-load.txt"),
|
|
);
|
|
expect(load.commands.some(({ kind }) => kind === "LC_LOAD_DYLINKER")).toBe(
|
|
true,
|
|
);
|
|
expect(load.dependencies.some(({ path }) => path === "/usr/lib/dyld")).toBe(
|
|
false,
|
|
);
|
|
});
|
|
|
|
it("limits thread-state entrypoints instead of reporting none silently", async () => {
|
|
const runner = new NativeFixtureRunner({
|
|
"otool:-h": withOtoolMachHeader(
|
|
[
|
|
"Load command 0",
|
|
" cmd LC_UNIXTHREAD",
|
|
" cmdsize 16",
|
|
" flavor 1",
|
|
" count 1",
|
|
"",
|
|
].join("\n"),
|
|
),
|
|
});
|
|
const client = new NativeMacOSProvider({}, runner, "darwin").createClient(
|
|
nativeMachoTarget("/owned/legacy-fixture"),
|
|
);
|
|
const result = await client.execute("inspect_macho", {});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.result).toMatchObject({
|
|
entrypoints: {
|
|
items: [],
|
|
total: null,
|
|
exhaustive: false,
|
|
limitations: [
|
|
"Thread-state entrypoints (LC_UNIXTHREAD/LC_THREAD) are retained as raw load commands; file offsets are not derived.",
|
|
],
|
|
},
|
|
});
|
|
expect(result.value.limitations).toContain(
|
|
"Thread-state entrypoints (LC_UNIXTHREAD/LC_THREAD) are retained as raw load commands; file offsets are not derived.",
|
|
);
|
|
});
|