From bcfb4072f2ff665e93477206e14ce4b0581f97b9 Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Mon, 5 Oct 2026 02:59:30 -0400 Subject: [PATCH] fix(native): preserve text-valued load command metadata (#520) * fix(native): preserve text-valued load command metadata Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> * fix(native): share numeric literal pattern between parsers --------- Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com> --- src/native/parsers/otool.ts | 11 +- .../native/nativeLoadText.golden.test.ts | 34 +++++ .../load-command-text-values/README.md | 17 +++ .../decimal-space.txt | 133 ++++++++++++++++++ .../load-command-text-values/decimal.txt | 133 ++++++++++++++++++ .../load-command-text-values/fixture.c | 1 + .../load-command-text-values/hex-space.txt | 133 ++++++++++++++++++ .../load-command-text-values/hex.txt | 133 ++++++++++++++++++ .../load-command-text-values/ordinary.txt | 133 ++++++++++++++++++ 9 files changed, 723 insertions(+), 5 deletions(-) create mode 100644 tests/boundary/providers/native/nativeLoadText.golden.test.ts create mode 100644 tests/fixtures/native-macos/load-command-text-values/README.md create mode 100644 tests/fixtures/native-macos/load-command-text-values/decimal-space.txt create mode 100644 tests/fixtures/native-macos/load-command-text-values/decimal.txt create mode 100644 tests/fixtures/native-macos/load-command-text-values/fixture.c create mode 100644 tests/fixtures/native-macos/load-command-text-values/hex-space.txt create mode 100644 tests/fixtures/native-macos/load-command-text-values/hex.txt create mode 100644 tests/fixtures/native-macos/load-command-text-values/ordinary.txt diff --git a/src/native/parsers/otool.ts b/src/native/parsers/otool.ts index 61a50f258..b4900fc4f 100644 --- a/src/native/parsers/otool.ts +++ b/src/native/parsers/otool.ts @@ -238,11 +238,12 @@ const permissions = (raw: string | null) => { return { read: null, write: null, execute: null, raw }; }; +/** A whole decimal or hexadecimal integer literal, with no surrounding text. */ +const NUMERIC_PATTERN = /^(?:0x[a-fA-F0-9]+|\d+)$/u; + const numeric = (value: string): number | null => { - const token = value.split(/\s+/u)[0]; - if (token === undefined || !/^(?:0x[a-fA-F0-9]+|\d+)$/u.test(token)) - return null; - const parsed = Number.parseInt(token, token.startsWith("0x") ? 16 : 10); + if (!NUMERIC_PATTERN.test(value)) return null; + const parsed = Number.parseInt(value, value.startsWith("0x") ? 16 : 10); return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : null; }; @@ -275,7 +276,7 @@ const hexField = ( name: string, ): string | null => { const value = stringField(fields, name); - if (value === null || !/^(?:0x[a-fA-F0-9]+|\d+)$/u.test(value)) return null; + if (value === null || !NUMERIC_PATTERN.test(value)) return null; return `0x${BigInt(value).toString(16)}`; }; diff --git a/tests/boundary/providers/native/nativeLoadText.golden.test.ts b/tests/boundary/providers/native/nativeLoadText.golden.test.ts new file mode 100644 index 000000000..a5ed5b023 --- /dev/null +++ b/tests/boundary/providers/native/nativeLoadText.golden.test.ts @@ -0,0 +1,34 @@ +import { expect, it } from "vitest"; +import { parseOtoolLoadCommands } from "../../../../src/native/parsers/otool.js"; +import { nativeFixture } from "../../../fixtures/nativeCommands.js"; +it.each([ + ["decimal-space", "123 library.dylib"], + ["hex-space", "0x123 library.dylib"], + ["decimal", "123"], + ["hex", "0x123"], + ["ordinary", "ordinary.dylib"], +])("preserves native install-name text from %s capture", async (slug, name) => { + const load = parseOtoolLoadCommands( + await nativeFixture(`load-command-text-values/${slug}.txt`), + ); + expect(load.dependencies).toContainEqual({ + path: name, + kind: "LC_ID_DYLIB", + current_version: "0.0.0", + compatibility_version: "0.0.0", + }); + expect( + load.commands.find((command) => command.kind === "LC_ID_DYLIB")?.fields + .name, + ).toBe(`${name} (offset 24)`); + const text = load.segments.find((segment) => segment.name === "__TEXT"); + expect(text).toMatchObject({ + file_offset: 0, + file_size: 16384, + initial_permissions: { read: true, write: false, execute: true }, + }); + expect( + load.commands.find((command) => command.kind === "LC_SEGMENT_64")?.fields + .cmdsize, + ).toBe(232); +}); diff --git a/tests/fixtures/native-macos/load-command-text-values/README.md b/tests/fixtures/native-macos/load-command-text-values/README.md new file mode 100644 index 000000000..07ff55a1b --- /dev/null +++ b/tests/fixtures/native-macos/load-command-text-values/README.md @@ -0,0 +1,17 @@ +# Native load-command text captures + +These are actual `xcrun otool -l` captures of dylibs built from `fixture.c` +with Apple clang. Only the owned output file path in each capture was replaced +with `/owned/fixture.dylib`. No fixture binary is committed or executed. + +For each install name `123 library.dylib`, `0x123 library.dylib`, `123`, +`0x123`, and `ordinary.dylib`: + +```sh +xcrun clang -dynamiclib fixture.c "-Wl,-install_name,$INSTALL_NAME" -o fixture.dylib +xcrun otool -l fixture.dylib +``` + +The install name is a text field, including when its first token resembles a +decimal or hexadecimal number. The captures also retain ordinary byte-range, +permission, and command-size numeric fields as controls. diff --git a/tests/fixtures/native-macos/load-command-text-values/decimal-space.txt b/tests/fixtures/native-macos/load-command-text-values/decimal-space.txt new file mode 100644 index 000000000..84fc187d8 --- /dev/null +++ b/tests/fixtures/native-macos/load-command-text-values/decimal-space.txt @@ -0,0 +1,133 @@ +/owned/fixture.dylib: +Load command 0 + cmd LC_SEGMENT_64 + cmdsize 232 + segname __TEXT + vmaddr 0x0000000000000000 + vmsize 0x0000000000004000 + fileoff 0 + filesize 16384 + maxprot 0x00000005 + initprot 0x00000005 + nsects 2 + flags 0x0 +Section + sectname __text + segname __TEXT + addr 0x00000000000002d8 + size 0x0000000000000008 + offset 728 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x80000400 + reserved1 0 + reserved2 0 +Section + sectname __unwind_info + segname __TEXT + addr 0x00000000000002e0 + size 0x0000000000000058 + offset 736 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x00000000 + reserved1 0 + reserved2 0 +Load command 1 + cmd LC_SEGMENT_64 + cmdsize 72 + segname __LINKEDIT + vmaddr 0x0000000000004000 + vmsize 0x0000000000004000 + fileoff 16384 + filesize 416 + maxprot 0x00000001 + initprot 0x00000001 + nsects 0 + flags 0x0 +Load command 2 + cmd LC_ID_DYLIB + cmdsize 48 + name 123 library.dylib (offset 24) + time stamp 1 Wed Dec 31 19:00:01 1969 + current version 0.0.0 +compatibility version 0.0.0 +Load command 3 + cmd LC_DYLD_CHAINED_FIXUPS + cmdsize 16 + dataoff 16384 + datasize 48 +Load command 4 + cmd LC_DYLD_EXPORTS_TRIE + cmdsize 16 + dataoff 16432 + datasize 32 +Load command 5 + cmd LC_SYMTAB + cmdsize 24 + symoff 16472 + nsyms 1 + stroff 16488 + strsize 16 +Load command 6 + cmd LC_DYSYMTAB + cmdsize 80 + ilocalsym 0 + nlocalsym 0 + iextdefsym 0 + nextdefsym 1 + iundefsym 1 + nundefsym 0 + tocoff 0 + ntoc 0 + modtaboff 0 + nmodtab 0 + extrefsymoff 0 + nextrefsyms 0 + indirectsymoff 0 + nindirectsyms 0 + extreloff 0 + nextrel 0 + locreloff 0 + nlocrel 0 +Load command 7 + cmd LC_UUID + cmdsize 24 + uuid 24B9FAB9-2497-3735-9F47-A229BEB1917C +Load command 8 + cmd LC_BUILD_VERSION + cmdsize 32 + platform 1 + minos 26.0 + sdk 26.4 + ntools 1 + tool 3 + version 1266.8 +Load command 9 + cmd LC_SOURCE_VERSION + cmdsize 16 + version 0.0 +Load command 10 + cmd LC_LOAD_DYLIB + cmdsize 56 + name /usr/lib/libSystem.B.dylib (offset 24) + time stamp 2 Wed Dec 31 19:00:02 1969 + current version 1356.0.0 +compatibility version 1.0.0 +Load command 11 + cmd LC_FUNCTION_STARTS + cmdsize 16 + dataoff 16464 + datasize 8 +Load command 12 + cmd LC_DATA_IN_CODE + cmdsize 16 + dataoff 16472 + datasize 0 +Load command 13 + cmd LC_CODE_SIGNATURE + cmdsize 16 + dataoff 16512 + datasize 288 diff --git a/tests/fixtures/native-macos/load-command-text-values/decimal.txt b/tests/fixtures/native-macos/load-command-text-values/decimal.txt new file mode 100644 index 000000000..51e0372e6 --- /dev/null +++ b/tests/fixtures/native-macos/load-command-text-values/decimal.txt @@ -0,0 +1,133 @@ +/owned/fixture.dylib: +Load command 0 + cmd LC_SEGMENT_64 + cmdsize 232 + segname __TEXT + vmaddr 0x0000000000000000 + vmsize 0x0000000000004000 + fileoff 0 + filesize 16384 + maxprot 0x00000005 + initprot 0x00000005 + nsects 2 + flags 0x0 +Section + sectname __text + segname __TEXT + addr 0x00000000000002c8 + size 0x0000000000000008 + offset 712 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x80000400 + reserved1 0 + reserved2 0 +Section + sectname __unwind_info + segname __TEXT + addr 0x00000000000002d0 + size 0x0000000000000058 + offset 720 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x00000000 + reserved1 0 + reserved2 0 +Load command 1 + cmd LC_SEGMENT_64 + cmdsize 72 + segname __LINKEDIT + vmaddr 0x0000000000004000 + vmsize 0x0000000000004000 + fileoff 16384 + filesize 400 + maxprot 0x00000001 + initprot 0x00000001 + nsects 0 + flags 0x0 +Load command 2 + cmd LC_ID_DYLIB + cmdsize 32 + name 123 (offset 24) + time stamp 1 Wed Dec 31 19:00:01 1969 + current version 0.0.0 +compatibility version 0.0.0 +Load command 3 + cmd LC_DYLD_CHAINED_FIXUPS + cmdsize 16 + dataoff 16384 + datasize 48 +Load command 4 + cmd LC_DYLD_EXPORTS_TRIE + cmdsize 16 + dataoff 16432 + datasize 32 +Load command 5 + cmd LC_SYMTAB + cmdsize 24 + symoff 16472 + nsyms 1 + stroff 16488 + strsize 16 +Load command 6 + cmd LC_DYSYMTAB + cmdsize 80 + ilocalsym 0 + nlocalsym 0 + iextdefsym 0 + nextdefsym 1 + iundefsym 1 + nundefsym 0 + tocoff 0 + ntoc 0 + modtaboff 0 + nmodtab 0 + extrefsymoff 0 + nextrefsyms 0 + indirectsymoff 0 + nindirectsyms 0 + extreloff 0 + nextrel 0 + locreloff 0 + nlocrel 0 +Load command 7 + cmd LC_UUID + cmdsize 24 + uuid 10FE37E1-45B3-3639-9EE8-6504F523CC76 +Load command 8 + cmd LC_BUILD_VERSION + cmdsize 32 + platform 1 + minos 26.0 + sdk 26.4 + ntools 1 + tool 3 + version 1266.8 +Load command 9 + cmd LC_SOURCE_VERSION + cmdsize 16 + version 0.0 +Load command 10 + cmd LC_LOAD_DYLIB + cmdsize 56 + name /usr/lib/libSystem.B.dylib (offset 24) + time stamp 2 Wed Dec 31 19:00:02 1969 + current version 1356.0.0 +compatibility version 1.0.0 +Load command 11 + cmd LC_FUNCTION_STARTS + cmdsize 16 + dataoff 16464 + datasize 8 +Load command 12 + cmd LC_DATA_IN_CODE + cmdsize 16 + dataoff 16472 + datasize 0 +Load command 13 + cmd LC_CODE_SIGNATURE + cmdsize 16 + dataoff 16512 + datasize 272 diff --git a/tests/fixtures/native-macos/load-command-text-values/fixture.c b/tests/fixtures/native-macos/load-command-text-values/fixture.c new file mode 100644 index 000000000..68ad7aad9 --- /dev/null +++ b/tests/fixtures/native-macos/load-command-text-values/fixture.c @@ -0,0 +1 @@ +int owned_value(void) { return 7; } diff --git a/tests/fixtures/native-macos/load-command-text-values/hex-space.txt b/tests/fixtures/native-macos/load-command-text-values/hex-space.txt new file mode 100644 index 000000000..310f93630 --- /dev/null +++ b/tests/fixtures/native-macos/load-command-text-values/hex-space.txt @@ -0,0 +1,133 @@ +/owned/fixture.dylib: +Load command 0 + cmd LC_SEGMENT_64 + cmdsize 232 + segname __TEXT + vmaddr 0x0000000000000000 + vmsize 0x0000000000004000 + fileoff 0 + filesize 16384 + maxprot 0x00000005 + initprot 0x00000005 + nsects 2 + flags 0x0 +Section + sectname __text + segname __TEXT + addr 0x00000000000002d8 + size 0x0000000000000008 + offset 728 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x80000400 + reserved1 0 + reserved2 0 +Section + sectname __unwind_info + segname __TEXT + addr 0x00000000000002e0 + size 0x0000000000000058 + offset 736 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x00000000 + reserved1 0 + reserved2 0 +Load command 1 + cmd LC_SEGMENT_64 + cmdsize 72 + segname __LINKEDIT + vmaddr 0x0000000000004000 + vmsize 0x0000000000004000 + fileoff 16384 + filesize 416 + maxprot 0x00000001 + initprot 0x00000001 + nsects 0 + flags 0x0 +Load command 2 + cmd LC_ID_DYLIB + cmdsize 48 + name 0x123 library.dylib (offset 24) + time stamp 1 Wed Dec 31 19:00:01 1969 + current version 0.0.0 +compatibility version 0.0.0 +Load command 3 + cmd LC_DYLD_CHAINED_FIXUPS + cmdsize 16 + dataoff 16384 + datasize 48 +Load command 4 + cmd LC_DYLD_EXPORTS_TRIE + cmdsize 16 + dataoff 16432 + datasize 32 +Load command 5 + cmd LC_SYMTAB + cmdsize 24 + symoff 16472 + nsyms 1 + stroff 16488 + strsize 16 +Load command 6 + cmd LC_DYSYMTAB + cmdsize 80 + ilocalsym 0 + nlocalsym 0 + iextdefsym 0 + nextdefsym 1 + iundefsym 1 + nundefsym 0 + tocoff 0 + ntoc 0 + modtaboff 0 + nmodtab 0 + extrefsymoff 0 + nextrefsyms 0 + indirectsymoff 0 + nindirectsyms 0 + extreloff 0 + nextrel 0 + locreloff 0 + nlocrel 0 +Load command 7 + cmd LC_UUID + cmdsize 24 + uuid 8228593E-23E8-3CE8-B904-388DCD86E04D +Load command 8 + cmd LC_BUILD_VERSION + cmdsize 32 + platform 1 + minos 26.0 + sdk 26.4 + ntools 1 + tool 3 + version 1266.8 +Load command 9 + cmd LC_SOURCE_VERSION + cmdsize 16 + version 0.0 +Load command 10 + cmd LC_LOAD_DYLIB + cmdsize 56 + name /usr/lib/libSystem.B.dylib (offset 24) + time stamp 2 Wed Dec 31 19:00:02 1969 + current version 1356.0.0 +compatibility version 1.0.0 +Load command 11 + cmd LC_FUNCTION_STARTS + cmdsize 16 + dataoff 16464 + datasize 8 +Load command 12 + cmd LC_DATA_IN_CODE + cmdsize 16 + dataoff 16472 + datasize 0 +Load command 13 + cmd LC_CODE_SIGNATURE + cmdsize 16 + dataoff 16512 + datasize 288 diff --git a/tests/fixtures/native-macos/load-command-text-values/hex.txt b/tests/fixtures/native-macos/load-command-text-values/hex.txt new file mode 100644 index 000000000..a79f56656 --- /dev/null +++ b/tests/fixtures/native-macos/load-command-text-values/hex.txt @@ -0,0 +1,133 @@ +/owned/fixture.dylib: +Load command 0 + cmd LC_SEGMENT_64 + cmdsize 232 + segname __TEXT + vmaddr 0x0000000000000000 + vmsize 0x0000000000004000 + fileoff 0 + filesize 16384 + maxprot 0x00000005 + initprot 0x00000005 + nsects 2 + flags 0x0 +Section + sectname __text + segname __TEXT + addr 0x00000000000002c8 + size 0x0000000000000008 + offset 712 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x80000400 + reserved1 0 + reserved2 0 +Section + sectname __unwind_info + segname __TEXT + addr 0x00000000000002d0 + size 0x0000000000000058 + offset 720 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x00000000 + reserved1 0 + reserved2 0 +Load command 1 + cmd LC_SEGMENT_64 + cmdsize 72 + segname __LINKEDIT + vmaddr 0x0000000000004000 + vmsize 0x0000000000004000 + fileoff 16384 + filesize 408 + maxprot 0x00000001 + initprot 0x00000001 + nsects 0 + flags 0x0 +Load command 2 + cmd LC_ID_DYLIB + cmdsize 32 + name 0x123 (offset 24) + time stamp 1 Wed Dec 31 19:00:01 1969 + current version 0.0.0 +compatibility version 0.0.0 +Load command 3 + cmd LC_DYLD_CHAINED_FIXUPS + cmdsize 16 + dataoff 16384 + datasize 48 +Load command 4 + cmd LC_DYLD_EXPORTS_TRIE + cmdsize 16 + dataoff 16432 + datasize 32 +Load command 5 + cmd LC_SYMTAB + cmdsize 24 + symoff 16472 + nsyms 1 + stroff 16488 + strsize 16 +Load command 6 + cmd LC_DYSYMTAB + cmdsize 80 + ilocalsym 0 + nlocalsym 0 + iextdefsym 0 + nextdefsym 1 + iundefsym 1 + nundefsym 0 + tocoff 0 + ntoc 0 + modtaboff 0 + nmodtab 0 + extrefsymoff 0 + nextrefsyms 0 + indirectsymoff 0 + nindirectsyms 0 + extreloff 0 + nextrel 0 + locreloff 0 + nlocrel 0 +Load command 7 + cmd LC_UUID + cmdsize 24 + uuid 0D299AA2-C6B2-3E5C-819C-1E673AD2E421 +Load command 8 + cmd LC_BUILD_VERSION + cmdsize 32 + platform 1 + minos 26.0 + sdk 26.4 + ntools 1 + tool 3 + version 1266.8 +Load command 9 + cmd LC_SOURCE_VERSION + cmdsize 16 + version 0.0 +Load command 10 + cmd LC_LOAD_DYLIB + cmdsize 56 + name /usr/lib/libSystem.B.dylib (offset 24) + time stamp 2 Wed Dec 31 19:00:02 1969 + current version 1356.0.0 +compatibility version 1.0.0 +Load command 11 + cmd LC_FUNCTION_STARTS + cmdsize 16 + dataoff 16464 + datasize 8 +Load command 12 + cmd LC_DATA_IN_CODE + cmdsize 16 + dataoff 16472 + datasize 0 +Load command 13 + cmd LC_CODE_SIGNATURE + cmdsize 16 + dataoff 16512 + datasize 280 diff --git a/tests/fixtures/native-macos/load-command-text-values/ordinary.txt b/tests/fixtures/native-macos/load-command-text-values/ordinary.txt new file mode 100644 index 000000000..ece037aff --- /dev/null +++ b/tests/fixtures/native-macos/load-command-text-values/ordinary.txt @@ -0,0 +1,133 @@ +/owned/fixture.dylib: +Load command 0 + cmd LC_SEGMENT_64 + cmdsize 232 + segname __TEXT + vmaddr 0x0000000000000000 + vmsize 0x0000000000004000 + fileoff 0 + filesize 16384 + maxprot 0x00000005 + initprot 0x00000005 + nsects 2 + flags 0x0 +Section + sectname __text + segname __TEXT + addr 0x00000000000002d0 + size 0x0000000000000008 + offset 720 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x80000400 + reserved1 0 + reserved2 0 +Section + sectname __unwind_info + segname __TEXT + addr 0x00000000000002d8 + size 0x0000000000000058 + offset 728 + align 2^2 (4) + reloff 0 + nreloc 0 + flags 0x00000000 + reserved1 0 + reserved2 0 +Load command 1 + cmd LC_SEGMENT_64 + cmdsize 72 + segname __LINKEDIT + vmaddr 0x0000000000004000 + vmsize 0x0000000000004000 + fileoff 16384 + filesize 416 + maxprot 0x00000001 + initprot 0x00000001 + nsects 0 + flags 0x0 +Load command 2 + cmd LC_ID_DYLIB + cmdsize 40 + name ordinary.dylib (offset 24) + time stamp 1 Wed Dec 31 19:00:01 1969 + current version 0.0.0 +compatibility version 0.0.0 +Load command 3 + cmd LC_DYLD_CHAINED_FIXUPS + cmdsize 16 + dataoff 16384 + datasize 48 +Load command 4 + cmd LC_DYLD_EXPORTS_TRIE + cmdsize 16 + dataoff 16432 + datasize 32 +Load command 5 + cmd LC_SYMTAB + cmdsize 24 + symoff 16472 + nsyms 1 + stroff 16488 + strsize 16 +Load command 6 + cmd LC_DYSYMTAB + cmdsize 80 + ilocalsym 0 + nlocalsym 0 + iextdefsym 0 + nextdefsym 1 + iundefsym 1 + nundefsym 0 + tocoff 0 + ntoc 0 + modtaboff 0 + nmodtab 0 + extrefsymoff 0 + nextrefsyms 0 + indirectsymoff 0 + nindirectsyms 0 + extreloff 0 + nextrel 0 + locreloff 0 + nlocrel 0 +Load command 7 + cmd LC_UUID + cmdsize 24 + uuid 4DF8197A-F92A-399C-B0B9-F51891FF0759 +Load command 8 + cmd LC_BUILD_VERSION + cmdsize 32 + platform 1 + minos 26.0 + sdk 26.4 + ntools 1 + tool 3 + version 1266.8 +Load command 9 + cmd LC_SOURCE_VERSION + cmdsize 16 + version 0.0 +Load command 10 + cmd LC_LOAD_DYLIB + cmdsize 56 + name /usr/lib/libSystem.B.dylib (offset 24) + time stamp 2 Wed Dec 31 19:00:02 1969 + current version 1356.0.0 +compatibility version 1.0.0 +Load command 11 + cmd LC_FUNCTION_STARTS + cmdsize 16 + dataoff 16464 + datasize 8 +Load command 12 + cmd LC_DATA_IN_CODE + cmdsize 16 + dataoff 16472 + datasize 0 +Load command 13 + cmd LC_CODE_SIGNATURE + cmdsize 16 + dataoff 16512 + datasize 288