-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathsea.js
More file actions
249 lines (218 loc) Β· 8.4 KB
/
sea.js
File metadata and controls
249 lines (218 loc) Β· 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const { inspect } = require('util');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const { readFileSync, copyFileSync, statSync } = require('fs');
const {
spawnSyncAndExitWithoutError,
spawnSyncAndAssert,
} = require('../common/child_process');
function skipIfBuildSEAIsNotSupported() {
if (!process.config.variables.node_use_lief)
common.skip('Node.js was not built with LIEF support.');
skipIfSingleExecutableIsNotSupported();
}
function skipIfSingleExecutableIsNotSupported() {
if (!process.config.variables.single_executable_application)
common.skip('Single Executable Application support has been disabled.');
if (!['darwin', 'win32', 'linux'].includes(process.platform))
common.skip(`Unsupported platform ${process.platform}.`);
if (process.platform === 'linux' && process.config.variables.is_debug === 1)
common.skip('Running the resultant binary fails with `Couldn\'t read target executable"`.');
if (process.config.variables.node_shared)
common.skip('Running the resultant binary fails with ' +
'`/home/iojs/node-tmp/.tmp.2366/sea: error while loading shared libraries: ' +
'libnode.so.112: cannot open shared object file: No such file or directory`.');
if (process.config.variables.icu_gyp_path === 'tools/icu/icu-system.gyp')
common.skip('Running the resultant binary fails with ' +
'`/home/iojs/node-tmp/.tmp.2379/sea: error while loading shared libraries: ' +
'libicui18n.so.71: cannot open shared object file: No such file or directory`.');
if (!process.config.variables.node_use_openssl || process.config.variables.node_shared_openssl)
common.skip('Running the resultant binary fails with `Node.js is not compiled with OpenSSL crypto support`.');
if (process.config.variables.want_separate_host_toolset !== 0)
common.skip('Running the resultant binary fails with `Segmentation fault (core dumped)`.');
if (process.platform === 'linux') {
const osReleaseText = readFileSync('/etc/os-release', { encoding: 'utf-8' });
const isAlpine = /^NAME="Alpine Linux"/m.test(osReleaseText);
if (isAlpine) common.skip('Alpine Linux is not supported.');
if (process.arch === 's390x') {
common.skip('On s390x, postject fails with `memory access out of bounds`.');
}
}
if (process.config.variables.ubsan) {
common.skip('UndefinedBehavior Sanitizer is not supported');
}
try {
readFileSync(process.execPath);
} catch (e) {
if (e.code === 'ERR_FS_FILE_TOO_LARGE') {
common.skip('The Node.js binary is too large to be supported by postject');
}
}
tmpdir.refresh();
// The SEA tests involve making a copy of the executable and writing some fixtures
// to the tmpdir. To be safe, ensure that the disk space has at least a copy of the
// executable and some extra space for blobs and configs is available.
const stat = statSync(process.execPath);
const expectedSpace = stat.size + 10 * 1024 * 1024;
if (!tmpdir.hasEnoughSpace(expectedSpace)) {
common.skip(`Available disk space < ${Math.floor(expectedSpace / 1024 / 1024)} MB`);
}
}
function buildSEA(fixtureDir, options = {}) {
const {
workingDir = tmpdir.path,
configPath = 'sea-config.json',
verifyWorkflow = false,
failure,
} = options;
// Copy fixture files to working directory if they are different.
if (fixtureDir !== workingDir) {
fs.cpSync(fixtureDir, workingDir, { recursive: true });
}
// Parse the config to get the output file path, if on Windows, ensure it ends with .exe
const config = JSON.parse(fs.readFileSync(path.resolve(workingDir, configPath)));
assert.strictEqual(typeof config.output, 'string');
if (process.platform === 'win32') {
if (!config.output.endsWith('.exe')) {
config.output += '.exe';
}
if (config.executable && !config.executable.endsWith('.exe')) {
config.executable += '.exe';
}
fs.writeFileSync(path.resolve(workingDir, configPath), JSON.stringify(config, null, 2));
}
// Build the SEA.
const child = spawnSyncAndAssert(process.execPath, ['--build-sea', configPath], {
cwd: workingDir,
env: {
NODE_DEBUG_NATIVE: 'SEA',
...process.env,
},
}, failure === undefined ? {
status: 0,
signal: null,
} : {
stderr: failure,
status: 1,
});
if (failure !== undefined) {
// Log more information, otherwise it's hard to debug failures from CI.
console.log(child.stderr.toString());
return child;
}
const outputFile = path.resolve(workingDir, config.output);
assert(fs.existsSync(outputFile), `Expected SEA output file ${outputFile} to exist`);
signSEA(outputFile, verifyWorkflow);
return outputFile;
}
function generateSEA(fixtureDir, options = {}) {
const {
workingDir = tmpdir.path,
configPath = 'sea-config.json',
verifyWorkflow = false,
} = options;
// Copy fixture files to working directory if they are different.
if (fixtureDir !== workingDir) {
fs.cpSync(fixtureDir, workingDir, { recursive: true });
}
// Determine the output executable path.
const outputFile = path.resolve(workingDir, process.platform === 'win32' ? 'sea.exe' : 'sea');
try {
// Copy the executable.
copyFileSync(process.execPath, outputFile);
console.log(`Copied ${process.execPath} to ${outputFile}`);
} catch (e) {
const message = `Cannot copy ${process.execPath} to ${outputFile}: ${inspect(e)}`;
if (verifyWorkflow) {
throw new Error(message, { cause: e });
}
common.skip(message);
}
// Generate the blob using --experimental-sea-config.
spawnSyncAndExitWithoutError(
process.execPath,
['--experimental-sea-config', configPath],
{
cwd: workingDir,
env: {
NODE_DEBUG_NATIVE: 'SEA',
...process.env,
},
},
);
// Parse the config to get the output file path.
const config = JSON.parse(fs.readFileSync(path.resolve(workingDir, configPath)));
assert.strictEqual(typeof config.output, 'string');
const seaPrepBlob = path.resolve(workingDir, config.output);
assert(fs.existsSync(seaPrepBlob), `Expected SEA blob ${seaPrepBlob} to exist`);
// Use postject to inject the blob.
const postjectFile = fixtures.path('postject-copy', 'node_modules', 'postject', 'dist', 'cli.js');
try {
spawnSyncAndExitWithoutError(process.execPath, [
postjectFile,
outputFile,
'NODE_SEA_BLOB',
seaPrepBlob,
'--sentinel-fuse', 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
...process.platform === 'darwin' ? [ '--macho-segment-name', 'NODE_SEA' ] : [],
]);
} catch (e) {
const message = `Cannot inject ${seaPrepBlob} into ${outputFile}: ${inspect(e)}`;
if (verifyWorkflow) {
throw new Error(message, { cause: e });
}
common.skip(message);
}
console.log(`Injected ${seaPrepBlob} into ${outputFile}`);
signSEA(outputFile, verifyWorkflow);
return outputFile;
}
function signSEA(targetExecutable, verifyWorkflow = false) {
if (process.platform === 'darwin') {
try {
spawnSyncAndExitWithoutError('codesign', [ '--sign', '-', targetExecutable ]);
spawnSyncAndExitWithoutError('codesign', [ '--verify', targetExecutable ]);
} catch (e) {
const message = `Cannot sign ${targetExecutable}: ${inspect(e)}`;
if (verifyWorkflow) {
throw new Error(message, { cause: e });
}
common.skip(message);
}
console.log(`Signed ${targetExecutable}`);
} else if (process.platform === 'win32') {
try {
spawnSyncAndExitWithoutError('where', [ 'signtool' ]);
} catch (e) {
const message = `Cannot find signtool: ${inspect(e)}`;
if (verifyWorkflow) {
throw new Error(message, { cause: e });
}
common.skip(message);
}
let stderr;
try {
({ stderr } = spawnSyncAndExitWithoutError('signtool', [ 'sign', '/fd', 'SHA256', targetExecutable ]));
spawnSyncAndExitWithoutError('signtool', ['verify', '/pa', 'SHA256', targetExecutable]);
} catch (e) {
const message = `Cannot sign ${targetExecutable}: ${inspect(e)}\n${stderr}`;
if (verifyWorkflow) {
throw new Error(message, { cause: e });
}
common.skip(message);
}
console.log(`Signed ${targetExecutable}`);
}
}
module.exports = {
skipIfBuildSEAIsNotSupported,
skipIfSingleExecutableIsNotSupported,
generateSEA,
signSEA,
buildSEA,
};