Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

linux-glibc.test.ts

125 lines5.3 KB

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