react-native-update-cli 2.26.0 → 2.26.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/lib/bundle-runner.js +13 -9
- package/lib/utils/constants.js +8 -1
- package/lib/utils/hermes-base.d.ts +7 -4
- package/lib/utils/hermes-base.js +202 -56
- package/lib/utils/hermes-literals.d.ts +22 -9
- package/lib/utils/hermes-literals.js +90 -35
- package/lib/utils/hermes-raw.d.ts +40 -0
- package/lib/utils/hermes-raw.js +486 -0
- package/lib/utils/hermes-timeout.d.ts +2 -0
- package/lib/utils/hermes-timeout.js +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Lossless operand audit accompanying the human-readable Hermes comparison.
|
|
4
|
+
* Pretty disassembly truncates strings and BigInts, prints function names in
|
|
5
|
+
* place of IDs, and renders -0 as 0. Never use those spellings as identities.
|
|
6
|
+
*
|
|
7
|
+
* The raw dump supplies instruction/operand boundaries; referenced data and
|
|
8
|
+
* doubles come from the HBC itself. Bytecode layouts are intentionally bounded
|
|
9
|
+
* to the layouts already supported by hbcTransform. Unknown data fails closed.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.UnverifiableHermesBytecode = void 0;
|
|
13
|
+
exports.readHermesSemanticData = readHermesSemanticData;
|
|
14
|
+
exports.normalizeRawHermesFunction = normalizeRawHermesFunction;
|
|
15
|
+
exports.auditRawHermesBytecode = auditRawHermesBytecode;
|
|
16
|
+
const promises_1 = require("node:fs/promises");
|
|
17
|
+
const node_string_decoder_1 = require("node:string_decoder");
|
|
18
|
+
const child_process_1 = require("child_process");
|
|
19
|
+
const hbcTransform_1 = require("./hbcTransform");
|
|
20
|
+
const hermes_literals_1 = require("./hermes-literals");
|
|
21
|
+
class UnverifiableHermesBytecode extends Error {
|
|
22
|
+
}
|
|
23
|
+
exports.UnverifiableHermesBytecode = UnverifiableHermesBytecode;
|
|
24
|
+
/** Fail closed on missing references rather than comparing equal placeholders. */
|
|
25
|
+
function requireValue(value, what) {
|
|
26
|
+
if (value === null || value === undefined) {
|
|
27
|
+
throw new UnverifiableHermesBytecode(what);
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
/** Bound a reference to its owning section before creating a zero-copy view. */
|
|
32
|
+
function checkedSlice(data, start, length) {
|
|
33
|
+
if (!Number.isSafeInteger(start) ||
|
|
34
|
+
!Number.isSafeInteger(length) ||
|
|
35
|
+
start < 0 ||
|
|
36
|
+
length < 0 ||
|
|
37
|
+
start + length > data.length) {
|
|
38
|
+
throw new UnverifiableHermesBytecode('HBC reference outside its section');
|
|
39
|
+
}
|
|
40
|
+
return data.subarray(start, start + length);
|
|
41
|
+
}
|
|
42
|
+
/** Read exact strings (including UTF-16 code units) and function identities. */
|
|
43
|
+
async function readHermesSemanticData(file) {
|
|
44
|
+
const bytes = await (0, promises_1.readFile)(file);
|
|
45
|
+
const resolved = requireValue((0, hbcTransform_1.resolveHbcSections)(bytes, bytes.length), 'unsupported or unreadable HBC layout');
|
|
46
|
+
// A newly added diff-transform layout must not silently opt into semantic
|
|
47
|
+
// parsing: function header and operand schemas need an independent review.
|
|
48
|
+
if (!((resolved.version >= 87 && resolved.version <= 96) ||
|
|
49
|
+
resolved.version === 98)) {
|
|
50
|
+
throw new UnverifiableHermesBytecode('unsupported semantic HBC version');
|
|
51
|
+
}
|
|
52
|
+
const layout = requireValue((0, hbcTransform_1.pickLayout)(bytes), 'unknown HBC layout');
|
|
53
|
+
const section = (name) => {
|
|
54
|
+
const r = requireValue(resolved.sections.get(name), `missing ${name}`);
|
|
55
|
+
return checkedSlice(bytes, r.start, r.size);
|
|
56
|
+
};
|
|
57
|
+
const strings = new Map();
|
|
58
|
+
const small = section('smallStringTable');
|
|
59
|
+
const overflow = section('overflowStringTable');
|
|
60
|
+
const storage = section('stringStorage');
|
|
61
|
+
for (let at = 0; at < small.length; at += 4) {
|
|
62
|
+
const word = small.readUInt32LE(at);
|
|
63
|
+
const utf16 = (word & 1) !== 0;
|
|
64
|
+
let offset = (word >>> 1) & 0x7fffff;
|
|
65
|
+
let length = word >>> 24;
|
|
66
|
+
if (length === 255) {
|
|
67
|
+
const entry = checkedSlice(overflow, offset * 8, 8);
|
|
68
|
+
offset = entry.readUInt32LE(0);
|
|
69
|
+
length = entry.readUInt32LE(4);
|
|
70
|
+
}
|
|
71
|
+
const value = checkedSlice(storage, offset, length * (utf16 ? 2 : 1));
|
|
72
|
+
strings.set(at / 4, value.toString(utf16 ? 'utf16le' : 'latin1'));
|
|
73
|
+
}
|
|
74
|
+
const string = (id) => requireValue(strings.get(id), `unresolved string id ${id}`);
|
|
75
|
+
// Audited v98 function schema: hermes-compiler 250829098.0.16/.17
|
|
76
|
+
// (250829098 stable snapshot), NOT every static_h build reporting HBC 98.
|
|
77
|
+
// Large headers: 37 bytes, flags[36]; small cache bits: 6/1/1. Upstream
|
|
78
|
+
// 7193d4485beeb87cd7a3b6ca8b6b5d97a1a433c4 removed NumCacheNewObject
|
|
79
|
+
// without immediately bumping HBC: 36 bytes/flags[35], cache bits 7/1.
|
|
80
|
+
// hbcTransform's file-header variants do not distinguish that function
|
|
81
|
+
// schema change. A compiler upgrade needs independent large/small fixtures;
|
|
82
|
+
// do not infer either schema from numStringSwitchImms or HBC version alone.
|
|
83
|
+
const shaped = resolved.version === 98;
|
|
84
|
+
const entrySize = shaped ? 12 : 16;
|
|
85
|
+
const headers = section('functionHeaders');
|
|
86
|
+
const functions = [];
|
|
87
|
+
for (let at = 0; at < headers.length; at += entrySize) {
|
|
88
|
+
const word0 = headers.readUInt32LE(at);
|
|
89
|
+
const word1 = headers.readUInt32LE(at + 4);
|
|
90
|
+
let flags = headers[at + entrySize - 1];
|
|
91
|
+
let offset = word0 & 0x1ffffff;
|
|
92
|
+
let size;
|
|
93
|
+
let name;
|
|
94
|
+
let fields;
|
|
95
|
+
if (flags & 0x20) {
|
|
96
|
+
// SmallFuncHeader::getLargeHeaderOffset, classic and Static Hermes.
|
|
97
|
+
const largeOffset = shaped
|
|
98
|
+
? ((word1 >>> 14) & 0xff) * 0x1000000 + offset
|
|
99
|
+
: (headers.readUInt32LE(at + 8) & 0x1ffffff) * 0x10000 + offset;
|
|
100
|
+
const large = checkedSlice(bytes, largeOffset, shaped ? 37 : 31);
|
|
101
|
+
offset = large.readUInt32LE(0);
|
|
102
|
+
size = large.readUInt32LE(shaped ? 12 : 8);
|
|
103
|
+
name = large.readUInt32LE(shaped ? 16 : 12);
|
|
104
|
+
flags = large[shaped ? 36 : 30];
|
|
105
|
+
fields = shaped
|
|
106
|
+
? [4, 8, 20, 24, 28].map((p) => large.readUInt32LE(p))
|
|
107
|
+
: [4, 20, 24].map((p) => large.readUInt32LE(p));
|
|
108
|
+
fields.push(...large.subarray(shaped ? 32 : 28, shaped ? 36 : 30));
|
|
109
|
+
}
|
|
110
|
+
else if (shaped) {
|
|
111
|
+
size = word1 & 0x3fff;
|
|
112
|
+
name = (word1 >>> 14) & 0xff;
|
|
113
|
+
fields = [
|
|
114
|
+
(word0 >>> 25) & 31,
|
|
115
|
+
word0 >>> 30,
|
|
116
|
+
(word1 >>> 22) & 31,
|
|
117
|
+
word1 >>> 27,
|
|
118
|
+
headers[at + 8],
|
|
119
|
+
headers[at + 9],
|
|
120
|
+
headers[at + 10] & 63,
|
|
121
|
+
(headers[at + 10] >>> 6) & 1,
|
|
122
|
+
headers[at + 10] >>> 7,
|
|
123
|
+
];
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
size = word1 & 0x7fff;
|
|
127
|
+
name = word1 >>> 15;
|
|
128
|
+
fields = [
|
|
129
|
+
word0 >>> 25,
|
|
130
|
+
headers.readUInt32LE(at + 8) >>> 25,
|
|
131
|
+
headers[at + 12],
|
|
132
|
+
headers[at + 13],
|
|
133
|
+
headers[at + 14],
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
checkedSlice(bytes, offset, size);
|
|
137
|
+
functions.push({
|
|
138
|
+
offset,
|
|
139
|
+
size,
|
|
140
|
+
// Debug presence and compact/overflow encoding are not runtime semantics.
|
|
141
|
+
metadata: JSON.stringify([string(name), flags & ~0x30, fields]),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const pairedStorage = (tableName, storageName) => {
|
|
145
|
+
const table = section(tableName);
|
|
146
|
+
const data = section(storageName);
|
|
147
|
+
const entries = [];
|
|
148
|
+
for (let at = 0; at < table.length; at += 8) {
|
|
149
|
+
entries.push(checkedSlice(data, table.readUInt32LE(at), table.readUInt32LE(at + 4)));
|
|
150
|
+
}
|
|
151
|
+
return entries;
|
|
152
|
+
};
|
|
153
|
+
const counts = Object.fromEntries(layout.headerFields.map((name, i) => [
|
|
154
|
+
name,
|
|
155
|
+
bytes.readUInt32LE(32 + i * 4),
|
|
156
|
+
]));
|
|
157
|
+
const options = bytes[32 + layout.headerFields.length * 4];
|
|
158
|
+
const moduleTable = section('cjsModuleTable');
|
|
159
|
+
const modules = [];
|
|
160
|
+
for (let at = 0; at < moduleTable.length; at += 8) {
|
|
161
|
+
const id = moduleTable.readUInt32LE(at);
|
|
162
|
+
modules.push([
|
|
163
|
+
options & 2 ? id : string(id),
|
|
164
|
+
moduleTable.readUInt32LE(at + 4),
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
167
|
+
const sourceTable = section('functionSourceTable');
|
|
168
|
+
const sources = [];
|
|
169
|
+
for (let at = 0; at < sourceTable.length; at += 8) {
|
|
170
|
+
sources.push([
|
|
171
|
+
sourceTable.readUInt32LE(at),
|
|
172
|
+
string(sourceTable.readUInt32LE(at + 4)),
|
|
173
|
+
]);
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
bytes,
|
|
177
|
+
version: resolved.version,
|
|
178
|
+
strings,
|
|
179
|
+
functions,
|
|
180
|
+
bigints: pairedStorage('bigIntTable', 'bigIntStorage'),
|
|
181
|
+
regexps: pairedStorage('regExpTable', 'regExpStorage'),
|
|
182
|
+
metadata: JSON.stringify([
|
|
183
|
+
resolved.version,
|
|
184
|
+
counts.globalCodeIndex,
|
|
185
|
+
counts.segmentID,
|
|
186
|
+
options,
|
|
187
|
+
modules,
|
|
188
|
+
sources,
|
|
189
|
+
]),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
const OPERAND_BYTES = {
|
|
193
|
+
Reg8: 1,
|
|
194
|
+
Reg32: 4,
|
|
195
|
+
UInt8: 1,
|
|
196
|
+
UInt16: 2,
|
|
197
|
+
UInt32: 4,
|
|
198
|
+
Addr8: 1,
|
|
199
|
+
Addr32: 4,
|
|
200
|
+
Imm32: 4,
|
|
201
|
+
Double: 8,
|
|
202
|
+
};
|
|
203
|
+
// Zero-based string operand positions from BytecodeList.def, with the missing
|
|
204
|
+
// DefineOwnById annotation supplied explicitly. Keep classic and v98 variants.
|
|
205
|
+
const STRING_OPERANDS = {
|
|
206
|
+
DeclareGlobalVar: [0],
|
|
207
|
+
ThrowIfHasRestrictedGlobalProperty: [0],
|
|
208
|
+
GetById: [3],
|
|
209
|
+
GetByIdWithReceiver: [4],
|
|
210
|
+
TryGetById: [3],
|
|
211
|
+
PutById: [3],
|
|
212
|
+
TryPutById: [3],
|
|
213
|
+
PutNewOwnById: [2],
|
|
214
|
+
PutNewOwnNEById: [2],
|
|
215
|
+
PutByIdLoose: [3],
|
|
216
|
+
PutByIdStrict: [3],
|
|
217
|
+
TryPutByIdLoose: [3],
|
|
218
|
+
TryPutByIdStrict: [3],
|
|
219
|
+
DefineOwnById: [3],
|
|
220
|
+
DelById: [2],
|
|
221
|
+
LoadConstString: [1],
|
|
222
|
+
CreatePrivateName: [1],
|
|
223
|
+
CreateRegExp: [1, 2],
|
|
224
|
+
};
|
|
225
|
+
/** Remove encoding-width suffixes; operand values remain part of the audit. */
|
|
226
|
+
function foldWidth(opcode) {
|
|
227
|
+
return opcode.replace(/(?:LongIndex|Long|Short)$/, '');
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Read raw operand boundaries and verify integer values against the HBC bytes.
|
|
231
|
+
* Doubles use their exact bits, not the rounded number printed by hermesc.
|
|
232
|
+
*/
|
|
233
|
+
function parseRawInstruction(line, data, fn) {
|
|
234
|
+
const m = /^\[@ (\d+)\] ([A-Za-z][A-Za-z0-9]*)(.*)$/.exec(line);
|
|
235
|
+
if (!m)
|
|
236
|
+
throw new UnverifiableHermesBytecode(`unrecognized raw instruction: ${line.slice(0, 100)}`);
|
|
237
|
+
const offset = Number(m[1]);
|
|
238
|
+
let size = 1;
|
|
239
|
+
const operands = m[3].trim() === ''
|
|
240
|
+
? []
|
|
241
|
+
: m[3]
|
|
242
|
+
.trim()
|
|
243
|
+
.split(/,\s*/)
|
|
244
|
+
.map((text) => {
|
|
245
|
+
const operand = /^([^<>]+)<([A-Za-z0-9]+)>$/.exec(text);
|
|
246
|
+
if (!operand || !Object.hasOwn(OPERAND_BYTES, operand[2])) {
|
|
247
|
+
throw new UnverifiableHermesBytecode(`unknown raw operand: ${text}`);
|
|
248
|
+
}
|
|
249
|
+
const type = operand[2];
|
|
250
|
+
const width = OPERAND_BYTES[type];
|
|
251
|
+
const raw = checkedSlice(checkedSlice(data.bytes, fn.offset, fn.size), offset + size, width);
|
|
252
|
+
size += width;
|
|
253
|
+
// Raw dump doubles are also rounded! Compare the actual IEEE-754 bits.
|
|
254
|
+
if (type === 'Double')
|
|
255
|
+
return { type, value: 0, bits: raw.toString('hex') };
|
|
256
|
+
const value = Number(operand[1]);
|
|
257
|
+
const binaryValue = type === 'Addr8'
|
|
258
|
+
? raw.readInt8(0)
|
|
259
|
+
: type === 'Addr32' || type === 'Imm32'
|
|
260
|
+
? raw.readInt32LE(0)
|
|
261
|
+
: width === 1
|
|
262
|
+
? raw.readUInt8(0)
|
|
263
|
+
: width === 2
|
|
264
|
+
? raw.readUInt16LE(0)
|
|
265
|
+
: raw.readUInt32LE(0);
|
|
266
|
+
if (!Number.isSafeInteger(value) || value !== binaryValue) {
|
|
267
|
+
throw new UnverifiableHermesBytecode('raw operand does not match the HBC bytes');
|
|
268
|
+
}
|
|
269
|
+
return { type, value };
|
|
270
|
+
});
|
|
271
|
+
return { offset, opcode: m[2], operands, size };
|
|
272
|
+
}
|
|
273
|
+
/** Canonicalize one complete function; addresses become instruction ordinals. */
|
|
274
|
+
function normalizeRawHermesFunction(lines, data, functionIndex, buffers) {
|
|
275
|
+
const fn = requireValue(data.functions[functionIndex], 'function index outside HBC table');
|
|
276
|
+
const instructions = lines
|
|
277
|
+
.filter((line) => line.startsWith('[@ '))
|
|
278
|
+
.map((line) => parseRawInstruction(line, data, fn));
|
|
279
|
+
const targets = new Map();
|
|
280
|
+
let end = 0;
|
|
281
|
+
for (const [index, inst] of instructions.entries()) {
|
|
282
|
+
if (inst.offset !== end)
|
|
283
|
+
throw new UnverifiableHermesBytecode('incomplete or unordered raw instruction stream');
|
|
284
|
+
targets.set(inst.offset, index);
|
|
285
|
+
end += inst.size;
|
|
286
|
+
}
|
|
287
|
+
// Static Hermes retains legal zero-byte (dead) functions in the table.
|
|
288
|
+
// Their metadata is still audited, and the raw reader still requires every
|
|
289
|
+
// function header. Only a body shorter/longer than its declared size fails.
|
|
290
|
+
if (end !== fn.size) {
|
|
291
|
+
throw new UnverifiableHermesBytecode('raw dump ended before the function body');
|
|
292
|
+
}
|
|
293
|
+
const target = (offset) => requireValue(targets.get(offset), `branch target ${offset} is not an instruction`);
|
|
294
|
+
const string = (id) => requireValue(data.strings.get(id), `unresolved string id ${id}`);
|
|
295
|
+
const literal = new hermes_literals_1.LiteralResolver(buffers, data.strings);
|
|
296
|
+
const output = [fn.metadata];
|
|
297
|
+
for (const inst of instructions) {
|
|
298
|
+
const op = foldWidth(inst.opcode);
|
|
299
|
+
const values = inst.operands.map((o) => o.value);
|
|
300
|
+
const refs = STRING_OPERANDS[op] ?? [];
|
|
301
|
+
const operands = inst.operands.map((operand, i) => {
|
|
302
|
+
if (refs.includes(i))
|
|
303
|
+
return ['string', string(operand.value)];
|
|
304
|
+
if (operand.type.startsWith('Addr'))
|
|
305
|
+
return ['target', target(inst.offset + operand.value)];
|
|
306
|
+
if (operand.type === 'Double')
|
|
307
|
+
return ['double', operand.bits];
|
|
308
|
+
return [
|
|
309
|
+
operand.type.startsWith('Reg') ? 'register' : 'integer',
|
|
310
|
+
operand.value,
|
|
311
|
+
];
|
|
312
|
+
});
|
|
313
|
+
if (op === 'LoadConstBigInt') {
|
|
314
|
+
operands[1] = [
|
|
315
|
+
'bigint',
|
|
316
|
+
requireValue(data.bigints[values[1]], 'unresolved BigInt').toString('hex'),
|
|
317
|
+
];
|
|
318
|
+
}
|
|
319
|
+
else if (op === 'CreateRegExp') {
|
|
320
|
+
operands[3] = [
|
|
321
|
+
'regexp',
|
|
322
|
+
requireValue(data.regexps[values[3]], 'unresolved RegExp').toString('hex'),
|
|
323
|
+
];
|
|
324
|
+
}
|
|
325
|
+
else if (op === 'NewArrayWithBuffer') {
|
|
326
|
+
if (values.length !== 4)
|
|
327
|
+
throw new UnverifiableHermesBytecode('unknown array buffer operands');
|
|
328
|
+
operands[3] = requireValue(literal.array(values[3], values[2]), 'undecodable array literal');
|
|
329
|
+
}
|
|
330
|
+
else if (op === 'NewObjectWithBuffer' ||
|
|
331
|
+
op === 'NewObjectWithBufferAndParent') {
|
|
332
|
+
if (buffers.layout === 'split') {
|
|
333
|
+
if (values.length !== 5 || op !== 'NewObjectWithBuffer')
|
|
334
|
+
throw new UnverifiableHermesBytecode('unknown split object operands');
|
|
335
|
+
operands[3] = requireValue(literal.objectKeys(values[3], values[2]), 'undecodable object keys');
|
|
336
|
+
operands[4] = requireValue(literal.objectValues(values[4], values[2]), 'undecodable object values');
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
const shapeAt = op === 'NewObjectWithBufferAndParent' ? 2 : 1;
|
|
340
|
+
if (values.length !== shapeAt + 2)
|
|
341
|
+
throw new UnverifiableHermesBytecode('unknown shaped object operands');
|
|
342
|
+
const shape = requireValue(literal.shape(values[shapeAt]), 'unknown object shape');
|
|
343
|
+
operands[shapeAt] = requireValue(literal.objectKeys(shape.keyOffset, shape.count), 'undecodable shape keys');
|
|
344
|
+
operands[shapeAt + 1] = requireValue(literal.objectValues(values[shapeAt + 1], shape.count), 'undecodable shape values');
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
else if (op === 'CacheNewObject') {
|
|
348
|
+
if (buffers.layout !== 'shaped' || values.length !== 4) {
|
|
349
|
+
throw new UnverifiableHermesBytecode('unknown cached object operands');
|
|
350
|
+
}
|
|
351
|
+
const shape = requireValue(literal.shape(values[2]), 'unknown cached object shape');
|
|
352
|
+
operands[2] = requireValue(literal.objectKeys(shape.keyOffset, shape.count), 'undecodable cached object keys');
|
|
353
|
+
}
|
|
354
|
+
else if (op === 'UIntSwitchImm' ||
|
|
355
|
+
op === 'SwitchImm' ||
|
|
356
|
+
op === 'StringSwitchImm') {
|
|
357
|
+
const strings = op === 'StringSwitchImm';
|
|
358
|
+
const tableAt = strings ? 2 : 1;
|
|
359
|
+
const count = strings ? values[4] : values[4] - values[3] + 1;
|
|
360
|
+
// The table offset is relative to the instruction, rounded up to 4-byte alignment.
|
|
361
|
+
const start = Math.ceil((fn.offset + inst.offset + values[tableAt]) / 4) * 4;
|
|
362
|
+
const table = checkedSlice(data.bytes, start, count * (strings ? 8 : 4));
|
|
363
|
+
const entries = [];
|
|
364
|
+
for (let i = 0; i < count; i++) {
|
|
365
|
+
const at = i * (strings ? 8 : 4);
|
|
366
|
+
entries.push([
|
|
367
|
+
strings ? string(table.readUInt32LE(at)) : values[3] + i,
|
|
368
|
+
target(inst.offset + table.readInt32LE(at + (strings ? 4 : 0))),
|
|
369
|
+
]);
|
|
370
|
+
}
|
|
371
|
+
operands[tableAt] = entries;
|
|
372
|
+
}
|
|
373
|
+
output.push(JSON.stringify([op, operands]));
|
|
374
|
+
}
|
|
375
|
+
return output;
|
|
376
|
+
}
|
|
377
|
+
/** Decode split UTF-8 sequences without buffering the whole raw dump. */
|
|
378
|
+
async function* linesOf(stream) {
|
|
379
|
+
const decoder = new node_string_decoder_1.StringDecoder('utf8');
|
|
380
|
+
let rest = '';
|
|
381
|
+
for await (const chunk of stream) {
|
|
382
|
+
rest += decoder.write(chunk);
|
|
383
|
+
let at = rest.indexOf('\n');
|
|
384
|
+
while (at >= 0) {
|
|
385
|
+
yield rest.slice(0, at).replace(/\r$/, '');
|
|
386
|
+
rest = rest.slice(at + 1);
|
|
387
|
+
at = rest.indexOf('\n');
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
rest += decoder.end();
|
|
391
|
+
if (rest)
|
|
392
|
+
yield rest;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Audit a pair of raw dumps while retaining one function per side plus HBC data.
|
|
396
|
+
* Cancellation/errors fail closed; both subprocesses are terminated and reaped.
|
|
397
|
+
*/
|
|
398
|
+
async function auditRawHermesBytecode(command, files, data, buffers, signal) {
|
|
399
|
+
if (data[0].metadata !== data[1].metadata)
|
|
400
|
+
return { status: 'different', detail: 'HBC runtime metadata differs' };
|
|
401
|
+
const children = files.map((file) => (0, child_process_1.spawn)(command, ['-b', '-dump-bytecode', '-pretty-disassemble=false', file], {
|
|
402
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
403
|
+
signal,
|
|
404
|
+
killSignal: 'SIGKILL',
|
|
405
|
+
}));
|
|
406
|
+
const exits = children.map((child) => new Promise((resolve) => {
|
|
407
|
+
let stderr = '';
|
|
408
|
+
let processError;
|
|
409
|
+
child.stderr?.on('data', (chunk) => {
|
|
410
|
+
stderr = (stderr + chunk.toString()).slice(-4096);
|
|
411
|
+
});
|
|
412
|
+
child.once('error', (error) => {
|
|
413
|
+
child.stdout?.destroy();
|
|
414
|
+
processError = error;
|
|
415
|
+
if (!child.pid)
|
|
416
|
+
resolve(error.message);
|
|
417
|
+
});
|
|
418
|
+
child.once('close', (code, reason) => resolve(processError
|
|
419
|
+
? processError.message
|
|
420
|
+
: code === 0
|
|
421
|
+
? null
|
|
422
|
+
: `${reason ?? `exit ${code}`}: ${stderr.trim().slice(-300)}`));
|
|
423
|
+
}));
|
|
424
|
+
const functions = async function* (side) {
|
|
425
|
+
let lines = null;
|
|
426
|
+
let index = 0;
|
|
427
|
+
for await (const line of linesOf(children[side].stdout)) {
|
|
428
|
+
if (/^(?:Function|NCFunction|Constructor)</.test(line)) {
|
|
429
|
+
if (lines)
|
|
430
|
+
yield normalizeRawHermesFunction(lines, data[side], index++, buffers[side]);
|
|
431
|
+
lines = [];
|
|
432
|
+
}
|
|
433
|
+
else if (/^(?:RegExp Bytecodes:|Debug |Textified callees table)/.test(line)) {
|
|
434
|
+
if (lines)
|
|
435
|
+
yield normalizeRawHermesFunction(lines, data[side], index++, buffers[side]);
|
|
436
|
+
lines = null;
|
|
437
|
+
}
|
|
438
|
+
else if (lines) {
|
|
439
|
+
lines.push(line);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
if (lines)
|
|
443
|
+
yield normalizeRawHermesFunction(lines, data[side], index++, buffers[side]);
|
|
444
|
+
const error = await exits[side];
|
|
445
|
+
if (error)
|
|
446
|
+
throw new UnverifiableHermesBytecode(`${side === 0 ? 'base' : 'plain'} raw dump: ${error}`);
|
|
447
|
+
if (index !== data[side].functions.length)
|
|
448
|
+
throw new UnverifiableHermesBytecode('raw dump function count does not match HBC header');
|
|
449
|
+
};
|
|
450
|
+
const a = functions(0);
|
|
451
|
+
const b = functions(1);
|
|
452
|
+
let index = 0;
|
|
453
|
+
try {
|
|
454
|
+
for (;;) {
|
|
455
|
+
const [left, right] = await Promise.all([a.next(), b.next()]);
|
|
456
|
+
if (left.done || right.done) {
|
|
457
|
+
return left.done && right.done
|
|
458
|
+
? { status: 'equivalent' }
|
|
459
|
+
: { status: 'different', detail: 'raw function counts differ' };
|
|
460
|
+
}
|
|
461
|
+
const length = Math.max(left.value.length, right.value.length);
|
|
462
|
+
for (let i = 0; i < length; i++) {
|
|
463
|
+
if (left.value[i] !== right.value[i]) {
|
|
464
|
+
return {
|
|
465
|
+
status: 'different',
|
|
466
|
+
detail: `function #${index}, raw instruction ${i}: ${left.value[i]?.slice(0, 150)} vs ${right.value[i]?.slice(0, 150)}`,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
index++;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
catch (error) {
|
|
474
|
+
return {
|
|
475
|
+
status: 'dump-failed',
|
|
476
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
finally {
|
|
480
|
+
for (const child of children) {
|
|
481
|
+
child.stdout?.destroy();
|
|
482
|
+
child.kill('SIGKILL');
|
|
483
|
+
}
|
|
484
|
+
await Promise.all(exits);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hermesTimeout = hermesTimeout;
|
|
4
|
+
/** Reject invalid/overflowing timer values instead of turning them into 1ms. */
|
|
5
|
+
function hermesTimeout(value, fallback) {
|
|
6
|
+
const parsed = Number(value);
|
|
7
|
+
return Number.isSafeInteger(parsed) && parsed > 0 && parsed <= 0x7fffffff
|
|
8
|
+
? parsed
|
|
9
|
+
: fallback;
|
|
10
|
+
}
|
package/package.json
CHANGED