The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off - Exercises the packaging filesystem and readelf. |
| 2 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | import * as NodeFSP from "node:fs/promises"; |
| 4 | import * as NodeOS from "node:os"; |
| 5 | import * as NodePath from "node:path"; |
| 6 | import { describe, expect, it } from "vite-plus/test"; |
| 7 | |
| 8 | import afterPack from "./linux-after-pack.ts"; |
| 9 | import { parseGlibcNeeds, scanGlibcNeeds } from "./linux-glibc.ts"; |
| 10 | |
| 11 | it.each([ |
| 12 | ["GLIBC_2.9", "2.9"], |
| 13 | ["GLIBC_2.34", "2.34"], |
| 14 | ["GLIBC_2.10", "2.10"], |
| 15 | ["GLIBC_2.2.5", "2.2.5"], |
| 16 | ["GLIBC_ABI_DT_RELR", "2.36"], |
| 17 | ])("reads required %s without counting newer definitions", (name, expected) => { |
| 18 | expect( |
| 19 | parseGlibcNeeds(` |
| 20 | Version definition section '.gnu.version_d' contains 1 entry: |
| 21 | 0x0010: Rev: 1 Flags: none Index: 2 Cnt: 1 Name: GLIBC_2.99 |
| 22 | Version needs section '.gnu.version_r' contains 1 entry: |
| 23 | 000000: Version: 1 File: libc.so.6 Cnt: 3 |
| 24 | 0x0010: Name: GLIBC_2.2 Flags: none Version: 3 |
| 25 | 0x0020: Name: ${name} Flags: none Version: 4 |
| 26 | 0x0030: Name: GLIBCXX_3.4.30 Flags: none Version: 5 |
| 27 | `), |
| 28 | ).toBe(expected); |
| 29 | }); |
| 30 | |
| 31 | it("ignores definitions, symbol tables, and unversioned/static binaries", () => { |
| 32 | expect(parseGlibcNeeds("No version information found in this file.")).toBeUndefined(); |
| 33 | expect( |
| 34 | parseGlibcNeeds(` |
| 35 | Version symbols section '.gnu.version' contains 1 entry: |
| 36 | 000: 2 (GLIBC_2.99) |
| 37 | Version definition section '.gnu.version_d' contains 1 entry: |
| 38 | 0x0010: Rev: 1 Flags: none Index: 2 Cnt: 1 Name: GLIBC_2.99 |
| 39 | `), |
| 40 | ).toBeUndefined(); |
| 41 | }); |
| 42 | |
| 43 | // Rust std links pidfd_spawnp@GLIBC_2.39 weakly; the loader only warns when a weak version is missing. |
| 44 | it("does not count weak requirements", () => { |
| 45 | expect( |
| 46 | parseGlibcNeeds(`Version needs section '.gnu.version_r' contains 1 entry: |
| 47 | 0x0010: Version: 1 File: libc.so.6 Cnt: 2 |
| 48 | 0x0150: Name: GLIBC_2.34 Flags: none Version: 2 |
| 49 | 0x0160: Name: GLIBC_2.39 Flags: WEAK Version: 10 |
| 50 | `), |
| 51 | ).toBe("2.34"); |
| 52 | }); |
| 53 | |
| 54 | it.each(["GLIBC_PRIVATE", "GLIBC_ABI_UNKNOWN"])("rejects unhandled requirement %s", (name) => { |
| 55 | expect(() => |
| 56 | parseGlibcNeeds(`Version needs section '.gnu.version_r' contains 1 entry: |
| 57 | 0x0010: Name: ${name} Flags: none Version: 3 |
| 58 | `), |
| 59 | ).toThrow(name); |
| 60 | }); |
| 61 | |
| 62 | // Minimal ELF64 with a linked string table and a version NEEDS section, for both release CPUs. |
| 63 | function elfFixture(machine: number, version: string): Buffer { |
| 64 | const strings = Buffer.from(`\0libc.so.6\0GLIBC_${version}\0`); |
| 65 | const names = Buffer.from("\0.shstrtab\0.dynstr\0.gnu.version_r\0"); |
| 66 | const stringsOffset = 64; |
| 67 | const namesOffset = stringsOffset + strings.length; |
| 68 | const needsOffset = namesOffset + names.length; |
| 69 | const sectionsOffset = needsOffset + 32; |
| 70 | const elf = Buffer.alloc(sectionsOffset + 4 * 64); |
| 71 | Buffer.from([0x7f, 0x45, 0x4c, 0x46, 2, 1, 1]).copy(elf); |
| 72 | elf.writeUInt16LE(3, 16); |
| 73 | elf.writeUInt16LE(machine, 18); |
| 74 | elf.writeUInt32LE(1, 20); |
| 75 | elf.writeBigUInt64LE(BigInt(sectionsOffset), 40); |
| 76 | elf.writeUInt16LE(64, 52); |
| 77 | elf.writeUInt16LE(64, 58); |
| 78 | elf.writeUInt16LE(4, 60); |
| 79 | elf.writeUInt16LE(1, 62); |
| 80 | strings.copy(elf, stringsOffset); |
| 81 | names.copy(elf, namesOffset); |
| 82 | elf.writeUInt16LE(1, needsOffset); |
| 83 | elf.writeUInt16LE(1, needsOffset + 2); |
| 84 | elf.writeUInt32LE(1, needsOffset + 4); |
| 85 | elf.writeUInt32LE(16, needsOffset + 8); |
| 86 | elf.writeUInt16LE(2, needsOffset + 16 + 6); |
| 87 | elf.writeUInt32LE(11, needsOffset + 16 + 8); |
| 88 | for (const [index, name, type, offset, size, link] of [ |
| 89 | [1, 1, 3, namesOffset, names.length, 0], |
| 90 | [2, 11, 3, stringsOffset, strings.length, 0], |
| 91 | [3, 19, 0x6ffffffe, needsOffset, 32, 2], |
| 92 | ] as const) { |
| 93 | const section = sectionsOffset + index * 64; |
| 94 | elf.writeUInt32LE(name, section); |
| 95 | elf.writeUInt32LE(type, section + 4); |
| 96 | elf.writeBigUInt64LE(BigInt(offset), section + 24); |
| 97 | elf.writeBigUInt64LE(BigInt(size), section + 32); |
| 98 | elf.writeUInt32LE(link, section + 40); |
| 99 | elf.writeBigUInt64LE(1n, section + 48); |
| 100 | } |
| 101 | elf.writeUInt32LE(1, sectionsOffset + 3 * 64 + 44); |
| 102 | return elf; |
| 103 | } |
| 104 | |
| 105 | describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("packaged ELF scan", () => { |
| 106 | it.each([62, 183])( |
| 107 | "checks executable, addon and extensionless ELF requirements for machine %s", |
| 108 | async (machine) => { |
| 109 | const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-glibc-")); |
| 110 | try { |
| 111 | const app = NodePath.join(root, "app"); |
| 112 | const native = NodePath.join(app, "resources/app.asar.unpacked/node_modules/addon"); |
| 113 | await NodeFSP.mkdir(native, { recursive: true }); |
| 114 | await NodeFSP.writeFile(NodePath.join(app, "t3code"), elfFixture(machine, "2.25")); |
| 115 | await NodeFSP.writeFile(NodePath.join(native, "pty.node"), elfFixture(machine, "2.34")); |
| 116 | await NodeFSP.writeFile(NodePath.join(app, "not-elf"), "GLIBC_2.99"); |
| 117 | await NodeFSP.writeFile(NodePath.join(root, "outside"), elfFixture(machine, "2.99")); |
| 118 | await NodeFSP.symlink("../outside", NodePath.join(app, "symlink")); |
| 119 | expect(await scanGlibcNeeds(app)).toEqual({ minimum: "2.34", elfCount: 2 }); |
| 120 | const deb = { name: "deb", options: { depends: ["libnss3"] } }; |
| 121 | for (let arch = 0; arch < 2; arch++) { |
| 122 | await afterPack({ appOutDir: app, electronPlatformName: "linux", targets: [deb] }); |
| 123 | } |
| 124 | expect(deb.options.depends).toEqual(["libnss3", "libc6 (>= 2.34)"]); |
| 125 | await NodeFSP.writeFile(NodePath.join(native, "helper"), elfFixture(machine, "2.40")); |
| 126 | await expect( |
| 127 | afterPack({ appOutDir: app, electronPlatformName: "linux", targets: [deb] }), |
| 128 | ).rejects.toThrow(/helper.*GLIBC_2\.40.*2\.39/s); |
| 129 | await NodeFSP.writeFile(NodePath.join(native, "helper"), Buffer.from("\x7fELFbroken")); |
| 130 | await expect(scanGlibcNeeds(app)).rejects.toThrow(/helper/); |
| 131 | } finally { |
| 132 | await NodeFSP.rm(root, { recursive: true, force: true }); |
| 133 | } |
| 134 | }, |
| 135 | ); |
| 136 | }); |