forked from vercel/pkg-fetch
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
verify.ts
123 lines (111 loc) · 3.32 KB
/
verify.ts
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
import { spawnSync } from 'child_process';
import { plusx } from './utils';
const script = `
var vm = require('vm');
var assert = require('assert');
var text = '(function () { return 42; })';
var cd, fn, result;
var modules = process.versions.modules | 0;
var v8 = process.versions.v8.split('.').slice(0, 2).join('.');
var s1 = new vm.Script(text, { filename: 's1', produceCachedData: true, sourceless: true });
assert(s1.cachedDataProduced);
cd = s1.cachedData;
var kCpuFeaturesOffset, cpuFeatures;
if (modules === 14) {
} else
if (modules === 46 || modules === 48 || modules === 51) {
kCpuFeaturesOffset = 0x0c;
} else
if (modules === 57) {
if (v8 === '6.2') {
kCpuFeaturesOffset = 0x0c;
} else
if (v8 === '5.8') {
kCpuFeaturesOffset = 0x0c;
} else {
kCpuFeaturesOffset = 0x10;
}
} else
if (modules === 59) {
kCpuFeaturesOffset = 0x0c;
} else
if (modules === 64) {
kCpuFeaturesOffset = 0x0c;
} else
if (modules === 72) {
// no cpu features anymore
} else
if (modules === 79) {
// no cpu features anymore
} else
if (modules === 83) {
// no cpu features anymore
} else
if (modules === 93) {
// no cpu features anymore
} else
if (modules === 108) {
// no cpu features anymore
} else
if (modules === 111) {
// no cpu features anymore
} else
if (modules === 115) {
// no cpu features anymore
} else {
assert(false, modules);
}
if (modules >= 46 && // no cpu_features field in 0.12
process.arch !== 'arm' && // non-zero features even in sourceless mode in arm
modules < 72) { // no cpu_features field in 12+
cpuFeatures = cd.readUInt32LE(kCpuFeaturesOffset);
assert(cpuFeatures === 0, 'CPU_FEATURES must be zero');
}
var s2 = new vm.Script(undefined, { filename: 's2', cachedData: cd, sourceless: true });
fn = s2.runInThisContext();
result = fn();
assert.equal(result, 42);
if (modules === 14) {
} else
if (modules === 46 || modules === 48 ||
modules === 51 || modules === 57 || modules === 59 || modules === 64) {
var paddedPayloadOffset = 0x48; // see SerializedCodeData::Payload()
var index = paddedPayloadOffset + 10;
cd[index] ^= 0xf0;
var s3 = new vm.Script(undefined, { filename: 's3', cachedData: cd, sourceless: true });
assert(s3.cachedDataRejected, 's3.cachedDataRejected must be true');
} else
if (modules === 72) {
} else
if (modules === 79) {
} else
if (modules === 83) {
} else
if (modules === 93) {
} else
if (modules === 108) {
} else
if (modules === 111) {
} else
if (modules === 115) {
} else {
assert(false, modules);
}
var s4 = new vm.Script(text, { filename: 's4', produceCachedData: true });
assert(s4.cachedDataProduced, 's4.cachedDataProduced must be true');
cd = s4.cachedData;
if (modules >= 46 && // no cpu_features field in 0.12
process.arch !== 'arm' && // zero features even in non-sourceless mode in arm
modules < 72) { // no cpu_features field in 12+
cpuFeatures = cd.readUInt32LE(kCpuFeaturesOffset);
assert(cpuFeatures !== 0, 'CPU_FEATURES must be non-zero');
}
console.log('ok');
`;
export async function verify(local: string) {
await plusx(local);
spawnSync(local, ['-e', script], {
env: { PKG_EXECPATH: 'PKG_INVOKE_NODEJS' },
stdio: 'inherit',
});
}