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