space-data-module-sdk 0.9.13 → 0.9.15
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 +32 -0
- package/package.json +1 -1
- package/src/compiler/compileCache.js +143 -43
- package/src/compiler/compileModule.js +70 -4
- package/src/compiler/guestLinkFiles.js +15 -2
- package/src/compiler/index.d.ts +79 -0
- package/src/compiler/index.js +7 -0
- package/src/compiler/pthreadArtifactGuard.js +3 -321
- package/src/compiler/simdArtifactGuard.js +137 -0
- package/src/compiler/wasmCodeScan.js +413 -0
- package/src/compiler/wasmTargetFeatures.js +63 -0
- package/src/index.d.ts +52 -0
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
// Instruction scanner for the code section of a wasm module.
|
|
2
|
+
//
|
|
3
|
+
// This is a real instruction decoder - NOT a byte scan - so bytes that happen to
|
|
4
|
+
// equal a prefix opcode inside i32.const / LEB128 / memarg immediates are never
|
|
5
|
+
// miscounted. It answers "which instruction classes does this module's code
|
|
6
|
+
// contain"; what to DO about the answer (thread-model policy, SIMD policy) lives
|
|
7
|
+
// in the guards that import it, so the decoder has one reason to change: the
|
|
8
|
+
// wasm instruction encoding.
|
|
9
|
+
//
|
|
10
|
+
// A function body is decoded to its closing `end`: block depth is tracked, and a
|
|
11
|
+
// body whose last instruction does not close the function block exactly at the
|
|
12
|
+
// body's final byte was mis-decoded, so it is reported as undecodable instead of
|
|
13
|
+
// contributing a count. A caller that needs a proof (the SIMD guard) therefore
|
|
14
|
+
// sees `undecodableFunctions`, never a silent miss.
|
|
15
|
+
|
|
16
|
+
import { decodeUnsignedLeb128 } from "../bundle/wasm.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Skip a LEB128-encoded integer (signedness is irrelevant for skipping — only
|
|
20
|
+
* the continuation bits matter). Bounds-checked.
|
|
21
|
+
*/
|
|
22
|
+
function skipLeb(bytes, cursor, end) {
|
|
23
|
+
let position = cursor;
|
|
24
|
+
while (position < end) {
|
|
25
|
+
const byte = bytes[position++];
|
|
26
|
+
if ((byte & 0x80) === 0) {
|
|
27
|
+
return position;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
throw new Error("LEB128 immediate ran past end of function body.");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Decode a LEB128 value AND return the next offset (bounds-checked). */
|
|
34
|
+
function readLeb(bytes, cursor, end) {
|
|
35
|
+
if (cursor >= end) {
|
|
36
|
+
throw new Error("LEB128 immediate ran past end of function body.");
|
|
37
|
+
}
|
|
38
|
+
const info = decodeUnsignedLeb128(bytes, cursor);
|
|
39
|
+
if (info.nextOffset > end) {
|
|
40
|
+
throw new Error("LEB128 immediate ran past end of function body.");
|
|
41
|
+
}
|
|
42
|
+
return info;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function skipBlockType(bytes, cursor, end) {
|
|
46
|
+
if (cursor >= end) {
|
|
47
|
+
throw new Error("Block type ran past end of function body.");
|
|
48
|
+
}
|
|
49
|
+
const byte = bytes[cursor];
|
|
50
|
+
// 0x40 (empty), single-byte value types, and single-byte type indices all
|
|
51
|
+
// consume exactly one byte; only a multi-byte (continuation-bit) type index
|
|
52
|
+
// needs a full LEB skip.
|
|
53
|
+
if ((byte & 0x80) !== 0) {
|
|
54
|
+
return skipLeb(bytes, cursor, end);
|
|
55
|
+
}
|
|
56
|
+
return cursor + 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function skipMemArg(bytes, cursor, end) {
|
|
60
|
+
const alignInfo = readLeb(bytes, cursor, end);
|
|
61
|
+
let position = alignInfo.nextOffset;
|
|
62
|
+
// Multi-memory / memory64: align bit 6 flags a following memory index.
|
|
63
|
+
if ((alignInfo.value & 0x40) !== 0) {
|
|
64
|
+
position = skipLeb(bytes, position, end);
|
|
65
|
+
}
|
|
66
|
+
return skipLeb(bytes, position, end);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function skipVec(bytes, cursor, end, bytesPerElement) {
|
|
70
|
+
const countInfo = readLeb(bytes, cursor, end);
|
|
71
|
+
let position = countInfo.nextOffset;
|
|
72
|
+
for (let i = 0; i < countInfo.value; i += 1) {
|
|
73
|
+
if (bytesPerElement === "leb") {
|
|
74
|
+
position = skipLeb(bytes, position, end);
|
|
75
|
+
} else {
|
|
76
|
+
position += bytesPerElement;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (position > end) {
|
|
80
|
+
throw new Error("Vector immediate ran past end of function body.");
|
|
81
|
+
}
|
|
82
|
+
return position;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// The fixed-width SIMD proposal occupies 0xFD sub-opcodes 0x00..0xFF. Every
|
|
86
|
+
// sub-opcode from 0x100 up belongs to a later proposal: relaxed-SIMD
|
|
87
|
+
// (0x100..0x113, results implementation-defined) and whatever follows it.
|
|
88
|
+
const FIXED_WIDTH_SIMD_LIMIT = 0x100;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Walk one function body to its closing `end` and count, by class, the
|
|
92
|
+
* instructions it holds. Throws when the body does not decode to exactly its
|
|
93
|
+
* own bytes.
|
|
94
|
+
*
|
|
95
|
+
* @returns {{ atomic: number, simd: number, relaxedSimd: number, exceptionHandling: number }}
|
|
96
|
+
* atomic 0xFE-prefixed threads instructions
|
|
97
|
+
* simd 0xFD-prefixed fixed-width SIMD instructions (sub-opcode < 0x100)
|
|
98
|
+
* relaxedSimd 0xFD-prefixed instructions at sub-opcode 0x100 and above
|
|
99
|
+
* exceptionHandling try / catch / throw / rethrow / delegate / try_table / throw_ref
|
|
100
|
+
*/
|
|
101
|
+
function scanFunctionBody(bytes, bodyStart, bodyEnd) {
|
|
102
|
+
let cursor = bodyStart;
|
|
103
|
+
// Local declarations: vec of (count, valtype).
|
|
104
|
+
const localVecCount = readLeb(bytes, cursor, bodyEnd);
|
|
105
|
+
cursor = localVecCount.nextOffset;
|
|
106
|
+
for (let i = 0; i < localVecCount.value; i += 1) {
|
|
107
|
+
cursor = skipLeb(bytes, cursor, bodyEnd); // count
|
|
108
|
+
if (cursor >= bodyEnd) {
|
|
109
|
+
throw new Error("Local declaration ran past end of function body.");
|
|
110
|
+
}
|
|
111
|
+
// valtype: usually one byte; concrete heap types are a LEB.
|
|
112
|
+
if ((bytes[cursor] & 0x80) !== 0) {
|
|
113
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
114
|
+
} else {
|
|
115
|
+
cursor += 1;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const counts = { atomic: 0, simd: 0, relaxedSimd: 0, exceptionHandling: 0 };
|
|
120
|
+
// The function body is itself a block; its closing `end` brings depth to 0.
|
|
121
|
+
let depth = 1;
|
|
122
|
+
while (cursor < bodyEnd) {
|
|
123
|
+
if (depth === 0) {
|
|
124
|
+
throw new Error("Instructions follow the function's closing end.");
|
|
125
|
+
}
|
|
126
|
+
const opcode = bytes[cursor++];
|
|
127
|
+
switch (opcode) {
|
|
128
|
+
// Control / parametric with no immediates.
|
|
129
|
+
case 0x00: // unreachable
|
|
130
|
+
case 0x01: // nop
|
|
131
|
+
case 0x05: // else (stays inside the open if block)
|
|
132
|
+
case 0x0f: // return
|
|
133
|
+
case 0x1a: // drop
|
|
134
|
+
case 0x1b: // select
|
|
135
|
+
break;
|
|
136
|
+
case 0x0b: // end
|
|
137
|
+
depth -= 1;
|
|
138
|
+
break;
|
|
139
|
+
case 0x02: // block
|
|
140
|
+
case 0x03: // loop
|
|
141
|
+
case 0x04: // if
|
|
142
|
+
cursor = skipBlockType(bytes, cursor, bodyEnd);
|
|
143
|
+
depth += 1;
|
|
144
|
+
break;
|
|
145
|
+
case 0x06: // try (legacy exception handling)
|
|
146
|
+
counts.exceptionHandling += 1;
|
|
147
|
+
cursor = skipBlockType(bytes, cursor, bodyEnd);
|
|
148
|
+
depth += 1;
|
|
149
|
+
break;
|
|
150
|
+
case 0x07: // catch tagidx
|
|
151
|
+
case 0x08: // throw tagidx
|
|
152
|
+
case 0x09: // rethrow labelidx
|
|
153
|
+
counts.exceptionHandling += 1;
|
|
154
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
155
|
+
break;
|
|
156
|
+
case 0x0a: // throw_ref
|
|
157
|
+
case 0x19: // catch_all
|
|
158
|
+
counts.exceptionHandling += 1;
|
|
159
|
+
break;
|
|
160
|
+
case 0x18: // delegate labelidx: closes the try block like an end
|
|
161
|
+
counts.exceptionHandling += 1;
|
|
162
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
163
|
+
depth -= 1;
|
|
164
|
+
break;
|
|
165
|
+
case 0x1f: { // try_table blocktype vec(catch clause)
|
|
166
|
+
counts.exceptionHandling += 1;
|
|
167
|
+
cursor = skipBlockType(bytes, cursor, bodyEnd);
|
|
168
|
+
const clauses = readLeb(bytes, cursor, bodyEnd);
|
|
169
|
+
cursor = clauses.nextOffset;
|
|
170
|
+
for (let i = 0; i < clauses.value; i += 1) {
|
|
171
|
+
if (cursor >= bodyEnd) {
|
|
172
|
+
throw new Error("Catch clause ran past end of function body.");
|
|
173
|
+
}
|
|
174
|
+
const kind = bytes[cursor++];
|
|
175
|
+
if (kind === 0x00 || kind === 0x01) {
|
|
176
|
+
cursor = skipLeb(bytes, cursor, bodyEnd); // tagidx
|
|
177
|
+
} else if (kind !== 0x02 && kind !== 0x03) {
|
|
178
|
+
throw new Error(`Unknown catch clause kind 0x${kind.toString(16)}.`);
|
|
179
|
+
}
|
|
180
|
+
cursor = skipLeb(bytes, cursor, bodyEnd); // labelidx
|
|
181
|
+
}
|
|
182
|
+
depth += 1;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case 0x0c: // br
|
|
186
|
+
case 0x0d: // br_if
|
|
187
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
188
|
+
break;
|
|
189
|
+
case 0x0e: {
|
|
190
|
+
// br_table: vec<labelidx> + default labelidx
|
|
191
|
+
cursor = skipVec(bytes, cursor, bodyEnd, "leb");
|
|
192
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
case 0x10: // call
|
|
196
|
+
case 0x12: // return_call
|
|
197
|
+
case 0x14: // call_ref typeidx
|
|
198
|
+
case 0x15: // return_call_ref typeidx
|
|
199
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
200
|
+
break;
|
|
201
|
+
case 0x11: // call_indirect
|
|
202
|
+
case 0x13: // return_call_indirect
|
|
203
|
+
cursor = skipLeb(bytes, cursor, bodyEnd); // typeidx
|
|
204
|
+
cursor = skipLeb(bytes, cursor, bodyEnd); // tableidx
|
|
205
|
+
break;
|
|
206
|
+
case 0x1c: // select t*
|
|
207
|
+
cursor = skipVec(bytes, cursor, bodyEnd, 1);
|
|
208
|
+
break;
|
|
209
|
+
case 0x20: // local.get
|
|
210
|
+
case 0x21: // local.set
|
|
211
|
+
case 0x22: // local.tee
|
|
212
|
+
case 0x23: // global.get
|
|
213
|
+
case 0x24: // global.set
|
|
214
|
+
case 0x25: // table.get
|
|
215
|
+
case 0x26: // table.set
|
|
216
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
217
|
+
break;
|
|
218
|
+
case 0x3f: // memory.size
|
|
219
|
+
case 0x40: // memory.grow
|
|
220
|
+
cursor = skipLeb(bytes, cursor, bodyEnd); // memidx (reserved)
|
|
221
|
+
break;
|
|
222
|
+
case 0x41: // i32.const
|
|
223
|
+
case 0x42: // i64.const
|
|
224
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
225
|
+
break;
|
|
226
|
+
case 0x43: // f32.const
|
|
227
|
+
cursor += 4;
|
|
228
|
+
break;
|
|
229
|
+
case 0x44: // f64.const
|
|
230
|
+
cursor += 8;
|
|
231
|
+
break;
|
|
232
|
+
case 0xd0: // ref.null
|
|
233
|
+
if (cursor < bodyEnd && (bytes[cursor] & 0x80) !== 0) {
|
|
234
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
235
|
+
} else {
|
|
236
|
+
cursor += 1;
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
case 0xd1: // ref.is_null
|
|
240
|
+
case 0xd3: // ref.eq
|
|
241
|
+
case 0xd4: // ref.as_non_null
|
|
242
|
+
break;
|
|
243
|
+
case 0xd2: // ref.func
|
|
244
|
+
case 0xd5: // br_on_null labelidx
|
|
245
|
+
case 0xd6: // br_on_non_null labelidx
|
|
246
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
247
|
+
break;
|
|
248
|
+
case 0xfc: {
|
|
249
|
+
// Bulk-memory / saturating-truncation / table ops.
|
|
250
|
+
const sub = readLeb(bytes, cursor, bodyEnd);
|
|
251
|
+
cursor = sub.nextOffset;
|
|
252
|
+
switch (sub.value) {
|
|
253
|
+
case 0: case 1: case 2: case 3:
|
|
254
|
+
case 4: case 5: case 6: case 7:
|
|
255
|
+
break; // trunc_sat: no immediates
|
|
256
|
+
case 8: // memory.init dataidx memidx
|
|
257
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
258
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
259
|
+
break;
|
|
260
|
+
case 9: // data.drop
|
|
261
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
262
|
+
break;
|
|
263
|
+
case 10: // memory.copy memidx memidx
|
|
264
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
265
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
266
|
+
break;
|
|
267
|
+
case 11: // memory.fill memidx
|
|
268
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
269
|
+
break;
|
|
270
|
+
case 12: // table.init elemidx tableidx
|
|
271
|
+
case 14: // table.copy tableidx tableidx
|
|
272
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
273
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
274
|
+
break;
|
|
275
|
+
case 13: // elem.drop
|
|
276
|
+
case 15: // table.grow
|
|
277
|
+
case 16: // table.size
|
|
278
|
+
case 17: // table.fill
|
|
279
|
+
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
280
|
+
break;
|
|
281
|
+
case 19: // i64.add128
|
|
282
|
+
case 20: // i64.sub128
|
|
283
|
+
case 21: // i64.mul_wide_s
|
|
284
|
+
case 22: // i64.mul_wide_u
|
|
285
|
+
break; // wide arithmetic: no immediates
|
|
286
|
+
default:
|
|
287
|
+
// An unknown sub-opcode's immediates cannot be skipped: the body is
|
|
288
|
+
// undecodable rather than guessed at (as an unknown opcode is, below).
|
|
289
|
+
throw new Error(`Unsupported 0xFC sub-opcode ${sub.value}.`);
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
case 0xfd: {
|
|
294
|
+
// SIMD.
|
|
295
|
+
const sub = readLeb(bytes, cursor, bodyEnd);
|
|
296
|
+
cursor = sub.nextOffset;
|
|
297
|
+
const op = sub.value;
|
|
298
|
+
if (op >= FIXED_WIDTH_SIMD_LIMIT) {
|
|
299
|
+
counts.relaxedSimd += 1;
|
|
300
|
+
} else {
|
|
301
|
+
counts.simd += 1;
|
|
302
|
+
}
|
|
303
|
+
if (op <= 0x0b) {
|
|
304
|
+
cursor = skipMemArg(bytes, cursor, bodyEnd); // v128 load/store
|
|
305
|
+
} else if (op === 0x0c || op === 0x0d) {
|
|
306
|
+
cursor += 16; // v128.const / i8x16.shuffle
|
|
307
|
+
} else if (op >= 0x15 && op <= 0x22) {
|
|
308
|
+
cursor += 1; // extract/replace lane
|
|
309
|
+
} else if (op >= 0x54 && op <= 0x5b) {
|
|
310
|
+
cursor = skipMemArg(bytes, cursor, bodyEnd);
|
|
311
|
+
cursor += 1; // load/store lane
|
|
312
|
+
} else if (op === 0x5c || op === 0x5d) {
|
|
313
|
+
cursor = skipMemArg(bytes, cursor, bodyEnd); // load32/64_zero
|
|
314
|
+
}
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
case 0xfe: {
|
|
318
|
+
// Atomics / threads.
|
|
319
|
+
const sub = readLeb(bytes, cursor, bodyEnd);
|
|
320
|
+
cursor = sub.nextOffset;
|
|
321
|
+
counts.atomic += 1;
|
|
322
|
+
if (sub.value === 0x03) {
|
|
323
|
+
cursor += 1; // atomic.fence: one reserved byte, no memarg
|
|
324
|
+
} else {
|
|
325
|
+
cursor = skipMemArg(bytes, cursor, bodyEnd);
|
|
326
|
+
}
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
default:
|
|
330
|
+
if (opcode >= 0x28 && opcode <= 0x3e) {
|
|
331
|
+
// Memory load/store family (i32.load .. i64.store32): each carries a
|
|
332
|
+
// memarg (align + offset). Skipping this is what prevents 0xFE bytes
|
|
333
|
+
// inside memory-offset immediates from being miscounted as atomics.
|
|
334
|
+
cursor = skipMemArg(bytes, cursor, bodyEnd);
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
if (opcode >= 0x45 && opcode <= 0xc4) {
|
|
338
|
+
// MVP numeric / comparison / conversion opcodes and the sign-extension
|
|
339
|
+
// operators take no immediates.
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
// Reserved opcodes and prefixes this decoder does not know (the GC
|
|
343
|
+
// prefix 0xFB): their immediates cannot be skipped, so the body is
|
|
344
|
+
// undecodable rather than guessed at.
|
|
345
|
+
throw new Error(`Unsupported opcode 0x${opcode.toString(16)}.`);
|
|
346
|
+
}
|
|
347
|
+
if (cursor > bodyEnd) {
|
|
348
|
+
throw new Error("Instruction immediate ran past end of function body.");
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (depth !== 0) {
|
|
352
|
+
throw new Error("Function body does not close its block at its final byte.");
|
|
353
|
+
}
|
|
354
|
+
return counts;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Scan the code section of a parsed wasm module (parseWasmModuleSections).
|
|
359
|
+
*
|
|
360
|
+
* @returns {{
|
|
361
|
+
* atomic: number, simd: number, relaxedSimd: number, exceptionHandling: number,
|
|
362
|
+
* undecodableFunctions: number,
|
|
363
|
+
* }}
|
|
364
|
+
* Totals over every function that decodes; `undecodableFunctions` counts the
|
|
365
|
+
* bodies that did not (their instructions contribute nothing).
|
|
366
|
+
*/
|
|
367
|
+
export function scanWasmCode(parsed) {
|
|
368
|
+
const totals = {
|
|
369
|
+
atomic: 0,
|
|
370
|
+
simd: 0,
|
|
371
|
+
relaxedSimd: 0,
|
|
372
|
+
exceptionHandling: 0,
|
|
373
|
+
undecodableFunctions: 0,
|
|
374
|
+
};
|
|
375
|
+
const codeSection = parsed.sections.find((section) => section.id === 10);
|
|
376
|
+
if (!codeSection) {
|
|
377
|
+
return totals;
|
|
378
|
+
}
|
|
379
|
+
const bytes = parsed.bytes;
|
|
380
|
+
try {
|
|
381
|
+
let cursor = codeSection.payloadStart;
|
|
382
|
+
const funcCount = readLeb(bytes, cursor, codeSection.payloadEnd);
|
|
383
|
+
cursor = funcCount.nextOffset;
|
|
384
|
+
for (let i = 0; i < funcCount.value; i += 1) {
|
|
385
|
+
if (cursor >= codeSection.payloadEnd) {
|
|
386
|
+
totals.undecodableFunctions += funcCount.value - i;
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
const sizeInfo = readLeb(bytes, cursor, codeSection.payloadEnd);
|
|
390
|
+
const bodyStart = sizeInfo.nextOffset;
|
|
391
|
+
const bodyEnd = bodyStart + sizeInfo.value;
|
|
392
|
+
if (bodyEnd > codeSection.payloadEnd) {
|
|
393
|
+
totals.undecodableFunctions += funcCount.value - i;
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
try {
|
|
397
|
+
const counts = scanFunctionBody(bytes, bodyStart, bodyEnd);
|
|
398
|
+
totals.atomic += counts.atomic;
|
|
399
|
+
totals.simd += counts.simd;
|
|
400
|
+
totals.relaxedSimd += counts.relaxedSimd;
|
|
401
|
+
totals.exceptionHandling += counts.exceptionHandling;
|
|
402
|
+
} catch {
|
|
403
|
+
// A body that does not decode is counted, not trusted and not fatal.
|
|
404
|
+
totals.undecodableFunctions += 1;
|
|
405
|
+
}
|
|
406
|
+
cursor = bodyEnd;
|
|
407
|
+
}
|
|
408
|
+
} catch {
|
|
409
|
+
// Truncated / malformed code section: report what decoded.
|
|
410
|
+
totals.undecodableFunctions += 1;
|
|
411
|
+
}
|
|
412
|
+
return totals;
|
|
413
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// The `target_features` custom section of a wasm module (tool-conventions
|
|
2
|
+
// "Feature section"): the features its objects were built with, as the linker
|
|
3
|
+
// merged them. It is the artifact's own statement of what a runtime must
|
|
4
|
+
// support, independent of any instruction the code section happens to contain.
|
|
5
|
+
|
|
6
|
+
import { decodeUnsignedLeb128, listWasmCustomSections } from "../bundle/wasm.js";
|
|
7
|
+
|
|
8
|
+
const textDecoder = new TextDecoder();
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Every entry of the target_features section.
|
|
12
|
+
*
|
|
13
|
+
* @param {Uint8Array} wasmBytes
|
|
14
|
+
* @returns {{ prefix: string, name: string }[]|null} `prefix` is "+" (used),
|
|
15
|
+
* "=" (used and required of every object) or "-" (disallowed); null when the
|
|
16
|
+
* module has no target_features section (or it cannot be read at all).
|
|
17
|
+
*/
|
|
18
|
+
export function readTargetFeatureEntries(wasmBytes) {
|
|
19
|
+
let customSections;
|
|
20
|
+
try {
|
|
21
|
+
customSections = listWasmCustomSections(wasmBytes);
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const featureSection = customSections.find(
|
|
26
|
+
(section) => section.name === "target_features",
|
|
27
|
+
);
|
|
28
|
+
if (!featureSection) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const bytes = featureSection.dataBytes;
|
|
32
|
+
const entries = [];
|
|
33
|
+
try {
|
|
34
|
+
let cursor = 0;
|
|
35
|
+
const countInfo = decodeUnsignedLeb128(bytes, cursor);
|
|
36
|
+
cursor = countInfo.nextOffset;
|
|
37
|
+
for (let i = 0; i < countInfo.value; i += 1) {
|
|
38
|
+
const prefix = String.fromCharCode(bytes[cursor]);
|
|
39
|
+
cursor += 1;
|
|
40
|
+
const nameLen = decodeUnsignedLeb128(bytes, cursor);
|
|
41
|
+
cursor = nameLen.nextOffset;
|
|
42
|
+
entries.push({
|
|
43
|
+
prefix,
|
|
44
|
+
name: textDecoder.decode(bytes.subarray(cursor, cursor + nameLen.value)),
|
|
45
|
+
});
|
|
46
|
+
cursor += nameLen.value;
|
|
47
|
+
}
|
|
48
|
+
} catch {
|
|
49
|
+
return entries.length > 0 ? entries : null;
|
|
50
|
+
}
|
|
51
|
+
return entries;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The names in the target_features section, whatever their prefix.
|
|
56
|
+
*
|
|
57
|
+
* @param {Uint8Array} wasmBytes
|
|
58
|
+
* @returns {string[]|null} null when there is no readable section.
|
|
59
|
+
*/
|
|
60
|
+
export function readDeclaredFeatures(wasmBytes) {
|
|
61
|
+
const entries = readTargetFeatureEntries(wasmBytes);
|
|
62
|
+
return entries === null ? null : entries.map((entry) => entry.name);
|
|
63
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -993,12 +993,48 @@ export interface CompilationResult {
|
|
|
993
993
|
tempDir: string | null;
|
|
994
994
|
wasmBytes: Uint8Array;
|
|
995
995
|
guestLink: GuestLinkArtifact | null;
|
|
996
|
+
/**
|
|
997
|
+
* How the emitted artifact uses WebAssembly SIMD, as the artifact guard read
|
|
998
|
+
* it. Always present: `requested: false, used: false` for a module that did
|
|
999
|
+
* not opt in with `simd128: true` and carries no SIMD.
|
|
1000
|
+
*/
|
|
1001
|
+
simd128: Simd128Report;
|
|
1002
|
+
/**
|
|
1003
|
+
* Build warnings (not manifest warnings): today, the SIMD an artifact carries without
|
|
1004
|
+
* the build having passed `simd128: true`, with what to do about it. Empty otherwise.
|
|
1005
|
+
*/
|
|
1006
|
+
warnings: string[];
|
|
996
1007
|
/** Which compile steps the content cache answered. */
|
|
997
1008
|
compileCache?: CompileCacheReport;
|
|
998
1009
|
manifestWarnings: string[];
|
|
999
1010
|
report: ComplianceReport;
|
|
1000
1011
|
}
|
|
1001
1012
|
|
|
1013
|
+
export interface Simd128Report {
|
|
1014
|
+
/** The module opted in with `simd128: true` (compiled with -msimd128). */
|
|
1015
|
+
requested: boolean;
|
|
1016
|
+
/**
|
|
1017
|
+
* The artifact carries SIMD but the build did not pass `simd128: true` (a module built by its
|
|
1018
|
+
* own clang driver with -msimd128, or a per-function target("simd128") attribute). It is
|
|
1019
|
+
* recorded as if declared (`guestLink.simd128`), a warning is added to `CompilationResult.warnings`
|
|
1020
|
+
* and emitted as the process warning SDM_SIMD128_UNDECLARED, and the artifact is not refused
|
|
1021
|
+
* yet: undeclared SIMD will become an error in a later release.
|
|
1022
|
+
*/
|
|
1023
|
+
undeclared: boolean;
|
|
1024
|
+
/** `simd128` is listed as used in the artifact's target_features section; null when it has none. */
|
|
1025
|
+
declared: boolean | null;
|
|
1026
|
+
/** `relaxed-simd` is listed in target_features; always false or null in an artifact that shipped. */
|
|
1027
|
+
declaredRelaxed: boolean | null;
|
|
1028
|
+
/** The code section holds fixed-width SIMD instructions. */
|
|
1029
|
+
used: boolean;
|
|
1030
|
+
/** Fixed-width SIMD (0xFD) instructions in the code section. */
|
|
1031
|
+
instructionCount: number;
|
|
1032
|
+
/** Relaxed-SIMD instructions; always 0 in an artifact that shipped (the guard refuses them). */
|
|
1033
|
+
relaxedInstructionCount: number;
|
|
1034
|
+
/** Function bodies the scanner could not decode; their instructions are not counted. */
|
|
1035
|
+
undecodableFunctions: number;
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1002
1038
|
export interface GuestLinkArtifact {
|
|
1003
1039
|
format: "wasm-object";
|
|
1004
1040
|
language: string;
|
|
@@ -1006,6 +1042,8 @@ export interface GuestLinkArtifact {
|
|
|
1006
1042
|
methodSymbols: Record<string, string>;
|
|
1007
1043
|
threadModel: ModuleThreadModelName;
|
|
1008
1044
|
capabilities: string[];
|
|
1045
|
+
/** Present (true) only for a module that uses WebAssembly SIMD, declared with `simd128: true` or not. */
|
|
1046
|
+
simd128?: true;
|
|
1009
1047
|
objectBytes: Uint8Array;
|
|
1010
1048
|
/** Present when the compile linked `libraries`. */
|
|
1011
1049
|
libraries?: GuestLinkLibrary[];
|
|
@@ -1076,6 +1114,20 @@ export function compileModuleFromSource(options: {
|
|
|
1076
1114
|
* on failure. A failed compile always removes it.
|
|
1077
1115
|
*/
|
|
1078
1116
|
keepTempDir?: boolean;
|
|
1117
|
+
/**
|
|
1118
|
+
* Opt in to WebAssembly SIMD: the module's own code and its `libraries` are
|
|
1119
|
+
* compiled with -msimd128 (so `__wasm_simd128__` is defined and the compiler may
|
|
1120
|
+
* vectorize), and the artifact guard records the use in `CompilationResult.simd128`
|
|
1121
|
+
* and the guest-link metadata. Default `false`: a module that does not opt in is
|
|
1122
|
+
* compiled with exactly the flags it always was. An artifact that carries SIMD
|
|
1123
|
+
* anyway (the module's own clang driver passes -msimd128, or a per-function
|
|
1124
|
+
* target("simd128") attribute) is recorded as if declared and warned about, not
|
|
1125
|
+
* refused yet; undeclared SIMD will become an error in a later release.
|
|
1126
|
+
* Relaxed-SIMD is always refused: its results are implementation-defined. Needs
|
|
1127
|
+
* `threadModel` "wasi-sequential" or "emscripten-pthreads" (the clang
|
|
1128
|
+
* wasm32-wasip1-threads toolchain).
|
|
1129
|
+
*/
|
|
1130
|
+
simd128?: boolean;
|
|
1079
1131
|
importedMemory?: boolean;
|
|
1080
1132
|
sharedMemory?: boolean;
|
|
1081
1133
|
initialMemoryBytes?: number;
|