1
+// TEMPORARY one-time workaround for a Node --build-sea bug on Linux arm64.
2
+// It breaks kernels with 16 KiB or 64 KiB pages. Revert when Node ships a fix.
3
+// Node issue: https://github.com/nodejs/node/issues/67048
4
+// Workaround PR: https://github.com/pingdotgg/t3code/pull/25825
5
+import * as NodeModule from "node:module";
6
+import * as Effect from "effect/Effect";
7
+import * as FileSystem from "effect/FileSystem";
8
+import * as Path from "effect/Path";
9
+import * as Schema from "effect/Schema";
10
+import { ChildProcess, ChildProcessSpawner } from "effect/process";
11
+
12
+class LinuxArm64SeaError extends Schema.TaggedError<LinuxArm64SeaError>()("LinuxArm64SeaError", {
13
+ message: Schema.String,
14
+}) {}
15
+
16
+const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
17
+
18
+const run = Effect.fn("LinuxArm64Sea.run")(function* (args: Array<string>, verbose: boolean) {
19
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
20
+ const child = yield* spawner.spawn(
21
+ ChildProcess.make(process.execPath, args, {
22
+ stdout: verbose ? "inherit" : "ignore",
23
+ stderr: "inherit",
24
+ }),
25
+ );
26
+ const exitCode = yield* child.exitCode;
27
+ if (exitCode !== 0) {
28
+ return yield* new LinuxArm64SeaError({ message: `${args[0]} exited with code ${exitCode}.` });
29
+ }
30
+});
31
+
32
+// Builds the SEA blob with this Node and injects it into a copy of this Node with postject.
33
+export const injectLinuxArm64Sea = Effect.fn("LinuxArm64Sea.inject")(function* (input: {
34
+ bundlePath: string;
35
+ outputPath: string;
36
+ nodeVersion: string;
37
+ verbose: boolean;
38
+}) {
39
+ // The copied execPath is the shipped runtime; vp pack only checks its own Node.
40
+ if (process.versions.node !== input.nodeVersion) {
41
+ return yield* new LinuxArm64SeaError({
42
+ message: `Linux ARM64 executables must be built with Node ${input.nodeVersion}; running ${process.versions.node}.`,
43
+ });
44
+ }
45
+ const fs = yield* FileSystem.FileSystem;
46
+ const path = yield* Path.Path;
47
+ const temp = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sea-" });
48
+ const blobPath = path.join(temp, "sea.blob");
49
+ const configPath = path.join(temp, "sea.json");
50
+ yield* fs.writeFileString(
51
+ configPath,
52
+ yield* encodeJson({
53
+ main: input.bundlePath,
54
+ output: blobPath,
55
+ mainFormat: "module",
56
+ disableExperimentalSEAWarning: true,
57
+ // Node SEA useCodeCache breaks import(), which the server needs.
58
+ useCodeCache: false,
59
+ }),
60
+ );
61
+ yield* run(["--experimental-sea-config", configPath], input.verbose);
62
+ yield* fs.copyFile(process.execPath, input.outputPath);
63
+ yield* fs.chmod(input.outputPath, 0o755);
64
+ const require = NodeModule.createRequire(import.meta.url);
65
+ yield* run(
66
+ [
67
+ require.resolve("postject/dist/cli.js"),
68
+ input.outputPath,
69
+ "NODE_SEA_BLOB",
70
+ blobPath,
71
+ "--sentinel-fuse",
72
+ "NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2",
73
+ ],
74
+ input.verbose,
75
+ );
76
+}, Effect.scoped);
77
+
78
+// Kernels map LOAD segments in whole pages. Checking at 64 KiB covers 4, 16, and 64 KiB kernels.
79
+export function assertLinuxArm64PageCompatibility(bytes: Uint8Array) {
80
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
81
+ if (
82
+ bytes.length < 64 ||
83
+ view.getUint32(0, false) !== 0x7f454c46 ||
84
+ bytes[4] !== 2 ||
85
+ bytes[5] !== 1 ||
86
+ view.getUint16(18, true) !== 183
87
+ ) {
88
+ throw new Error("Expected a little-endian ELF64 ARM64 executable.");
89
+ }
90
+ const tableOffset = Number(view.getBigUint64(32, true));
91
+ const entrySize = view.getUint16(54, true);
92
+ const count = view.getUint16(56, true);
93
+ if (
94
+ !Number.isSafeInteger(tableOffset) ||
95
+ entrySize < 56 ||
96
+ count === 0 ||
97
+ tableOffset + entrySize * count > bytes.length
98
+ ) {
99
+ throw new Error("Invalid ELF program header table.");
100
+ }
101
+ const pageSize = 0x10000n;
102
+ const ranges: Array<{ start: bigint; end: bigint }> = [];
103
+ for (let index = 0; index < count; index++) {
104
+ const header = tableOffset + index * entrySize;
105
+ if (view.getUint32(header, true) !== 1) continue;
106
+ const offset = view.getBigUint64(header + 8, true);
107
+ const address = view.getBigUint64(header + 16, true);
108
+ const size = view.getBigUint64(header + 40, true);
109
+ if (offset % pageSize !== address % pageSize) {
110
+ throw new Error(`ARM64 LOAD segment ${index} is incompatible with 64 KiB pages.`);
111
+ }
112
+ if (size === 0n) continue;
113
+ const start = address / pageSize;
114
+ const end = (address + size + pageSize - 1n) / pageSize;
115
+ if (ranges.some((range) => start < range.end && range.start < end)) {
116
+ throw new Error(`ARM64 LOAD segments overlap on a 64 KiB page (segment ${index}).`);
117
+ }
118
+ ranges.push({ start, end });
119
+ }
120
+ if (ranges.length === 0) throw new Error("ELF executable has no LOAD segments.");
121
+}