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
|
@@ -7,9 +7,10 @@
|
|
|
7
7
|
|
|
8
8
|
import {
|
|
9
9
|
decodeUnsignedLeb128,
|
|
10
|
-
listWasmCustomSections,
|
|
11
10
|
parseWasmModuleSections,
|
|
12
11
|
} from "../bundle/wasm.js";
|
|
12
|
+
import { scanWasmCode } from "./wasmCodeScan.js";
|
|
13
|
+
import { readDeclaredFeatures } from "./wasmTargetFeatures.js";
|
|
13
14
|
|
|
14
15
|
// Mirrors ModuleThreadModel.EMSCRIPTEN_PTHREADS in compileModule.js. Kept as a
|
|
15
16
|
// local literal to avoid an import cycle (compileModule imports this module).
|
|
@@ -139,291 +140,6 @@ export function assertPthreadFlagsPresent(args, context = {}) {
|
|
|
139
140
|
}
|
|
140
141
|
}
|
|
141
142
|
|
|
142
|
-
const VALTYPE_BYTES = new Set([0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x70, 0x6f]);
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Skip a LEB128-encoded integer (signedness is irrelevant for skipping — only
|
|
146
|
-
* the continuation bits matter). Bounds-checked.
|
|
147
|
-
*/
|
|
148
|
-
function skipLeb(bytes, cursor, end) {
|
|
149
|
-
let position = cursor;
|
|
150
|
-
while (position < end) {
|
|
151
|
-
const byte = bytes[position++];
|
|
152
|
-
if ((byte & 0x80) === 0) {
|
|
153
|
-
return position;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
throw new Error("LEB128 immediate ran past end of function body.");
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** Decode a LEB128 value AND return the next offset (bounds-checked). */
|
|
160
|
-
function readLeb(bytes, cursor, end) {
|
|
161
|
-
if (cursor >= end) {
|
|
162
|
-
throw new Error("LEB128 immediate ran past end of function body.");
|
|
163
|
-
}
|
|
164
|
-
const info = decodeUnsignedLeb128(bytes, cursor);
|
|
165
|
-
if (info.nextOffset > end) {
|
|
166
|
-
throw new Error("LEB128 immediate ran past end of function body.");
|
|
167
|
-
}
|
|
168
|
-
return info;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function skipBlockType(bytes, cursor, end) {
|
|
172
|
-
if (cursor >= end) {
|
|
173
|
-
throw new Error("Block type ran past end of function body.");
|
|
174
|
-
}
|
|
175
|
-
const byte = bytes[cursor];
|
|
176
|
-
// 0x40 (empty), single-byte value types, and single-byte type indices all
|
|
177
|
-
// consume exactly one byte; only a multi-byte (continuation-bit) type index
|
|
178
|
-
// needs a full LEB skip.
|
|
179
|
-
if ((byte & 0x80) !== 0) {
|
|
180
|
-
return skipLeb(bytes, cursor, end);
|
|
181
|
-
}
|
|
182
|
-
return cursor + 1;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function skipMemArg(bytes, cursor, end) {
|
|
186
|
-
const alignInfo = readLeb(bytes, cursor, end);
|
|
187
|
-
let position = alignInfo.nextOffset;
|
|
188
|
-
// Multi-memory / memory64: align bit 6 flags a following memory index.
|
|
189
|
-
if ((alignInfo.value & 0x40) !== 0) {
|
|
190
|
-
position = skipLeb(bytes, position, end);
|
|
191
|
-
}
|
|
192
|
-
return skipLeb(bytes, position, end);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function skipVec(bytes, cursor, end, bytesPerElement) {
|
|
196
|
-
const countInfo = readLeb(bytes, cursor, end);
|
|
197
|
-
let position = countInfo.nextOffset;
|
|
198
|
-
for (let i = 0; i < countInfo.value; i += 1) {
|
|
199
|
-
if (bytesPerElement === "leb") {
|
|
200
|
-
position = skipLeb(bytes, position, end);
|
|
201
|
-
} else {
|
|
202
|
-
position += bytesPerElement;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
if (position > end) {
|
|
206
|
-
throw new Error("Vector immediate ran past end of function body.");
|
|
207
|
-
}
|
|
208
|
-
return position;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Walk one function body and count genuine 0xFE-prefixed atomic instructions.
|
|
213
|
-
* This is a real instruction decoder — NOT a byte scan — so `0xFE` bytes that
|
|
214
|
-
* appear inside i32.const / LEB128 / memarg immediates are never miscounted.
|
|
215
|
-
*/
|
|
216
|
-
function countAtomicsInFunctionBody(bytes, bodyStart, bodyEnd) {
|
|
217
|
-
let cursor = bodyStart;
|
|
218
|
-
// Local declarations: vec of (count, valtype).
|
|
219
|
-
const localVecCount = readLeb(bytes, cursor, bodyEnd);
|
|
220
|
-
cursor = localVecCount.nextOffset;
|
|
221
|
-
for (let i = 0; i < localVecCount.value; i += 1) {
|
|
222
|
-
cursor = skipLeb(bytes, cursor, bodyEnd); // count
|
|
223
|
-
if (cursor >= bodyEnd) {
|
|
224
|
-
throw new Error("Local declaration ran past end of function body.");
|
|
225
|
-
}
|
|
226
|
-
// valtype: usually one byte; concrete heap types are a LEB.
|
|
227
|
-
if ((bytes[cursor] & 0x80) !== 0) {
|
|
228
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
229
|
-
} else {
|
|
230
|
-
cursor += 1;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
let atomics = 0;
|
|
235
|
-
while (cursor < bodyEnd) {
|
|
236
|
-
const opcode = bytes[cursor++];
|
|
237
|
-
switch (opcode) {
|
|
238
|
-
// Control / parametric with no immediates.
|
|
239
|
-
case 0x00: // unreachable
|
|
240
|
-
case 0x01: // nop
|
|
241
|
-
case 0x05: // else
|
|
242
|
-
case 0x0b: // end
|
|
243
|
-
case 0x0f: // return
|
|
244
|
-
case 0x1a: // drop
|
|
245
|
-
case 0x1b: // select
|
|
246
|
-
break;
|
|
247
|
-
case 0x02: // block
|
|
248
|
-
case 0x03: // loop
|
|
249
|
-
case 0x04: // if
|
|
250
|
-
cursor = skipBlockType(bytes, cursor, bodyEnd);
|
|
251
|
-
break;
|
|
252
|
-
case 0x0c: // br
|
|
253
|
-
case 0x0d: // br_if
|
|
254
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
255
|
-
break;
|
|
256
|
-
case 0x0e: {
|
|
257
|
-
// br_table: vec<labelidx> + default labelidx
|
|
258
|
-
cursor = skipVec(bytes, cursor, bodyEnd, "leb");
|
|
259
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
260
|
-
break;
|
|
261
|
-
}
|
|
262
|
-
case 0x10: // call
|
|
263
|
-
case 0x12: // return_call
|
|
264
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
265
|
-
break;
|
|
266
|
-
case 0x11: // call_indirect
|
|
267
|
-
case 0x13: // return_call_indirect
|
|
268
|
-
cursor = skipLeb(bytes, cursor, bodyEnd); // typeidx
|
|
269
|
-
cursor = skipLeb(bytes, cursor, bodyEnd); // tableidx
|
|
270
|
-
break;
|
|
271
|
-
case 0x1c: // select t*
|
|
272
|
-
cursor = skipVec(bytes, cursor, bodyEnd, 1);
|
|
273
|
-
break;
|
|
274
|
-
case 0x20: // local.get
|
|
275
|
-
case 0x21: // local.set
|
|
276
|
-
case 0x22: // local.tee
|
|
277
|
-
case 0x23: // global.get
|
|
278
|
-
case 0x24: // global.set
|
|
279
|
-
case 0x25: // table.get
|
|
280
|
-
case 0x26: // table.set
|
|
281
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
282
|
-
break;
|
|
283
|
-
case 0x3f: // memory.size
|
|
284
|
-
case 0x40: // memory.grow
|
|
285
|
-
cursor = skipLeb(bytes, cursor, bodyEnd); // memidx (reserved)
|
|
286
|
-
break;
|
|
287
|
-
case 0x41: // i32.const
|
|
288
|
-
case 0x42: // i64.const
|
|
289
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
290
|
-
break;
|
|
291
|
-
case 0x43: // f32.const
|
|
292
|
-
cursor += 4;
|
|
293
|
-
break;
|
|
294
|
-
case 0x44: // f64.const
|
|
295
|
-
cursor += 8;
|
|
296
|
-
break;
|
|
297
|
-
case 0xd0: // ref.null
|
|
298
|
-
if (cursor < bodyEnd && (bytes[cursor] & 0x80) !== 0) {
|
|
299
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
300
|
-
} else {
|
|
301
|
-
cursor += 1;
|
|
302
|
-
}
|
|
303
|
-
break;
|
|
304
|
-
case 0xd1: // ref.is_null
|
|
305
|
-
break;
|
|
306
|
-
case 0xd2: // ref.func
|
|
307
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
308
|
-
break;
|
|
309
|
-
case 0xfc: {
|
|
310
|
-
// Bulk-memory / saturating-truncation / table ops.
|
|
311
|
-
const sub = readLeb(bytes, cursor, bodyEnd);
|
|
312
|
-
cursor = sub.nextOffset;
|
|
313
|
-
switch (sub.value) {
|
|
314
|
-
case 0: case 1: case 2: case 3:
|
|
315
|
-
case 4: case 5: case 6: case 7:
|
|
316
|
-
break; // trunc_sat: no immediates
|
|
317
|
-
case 8: // memory.init dataidx memidx
|
|
318
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
319
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
320
|
-
break;
|
|
321
|
-
case 9: // data.drop
|
|
322
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
323
|
-
break;
|
|
324
|
-
case 10: // memory.copy memidx memidx
|
|
325
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
326
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
327
|
-
break;
|
|
328
|
-
case 11: // memory.fill memidx
|
|
329
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
330
|
-
break;
|
|
331
|
-
case 12: // table.init elemidx tableidx
|
|
332
|
-
case 14: // table.copy tableidx tableidx
|
|
333
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
334
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
335
|
-
break;
|
|
336
|
-
case 13: // elem.drop
|
|
337
|
-
case 15: // table.grow
|
|
338
|
-
case 16: // table.size
|
|
339
|
-
case 17: // table.fill
|
|
340
|
-
cursor = skipLeb(bytes, cursor, bodyEnd);
|
|
341
|
-
break;
|
|
342
|
-
default:
|
|
343
|
-
break;
|
|
344
|
-
}
|
|
345
|
-
break;
|
|
346
|
-
}
|
|
347
|
-
case 0xfd: {
|
|
348
|
-
// SIMD (not emitted by our pthreads builds, but decode defensively).
|
|
349
|
-
const sub = readLeb(bytes, cursor, bodyEnd);
|
|
350
|
-
cursor = sub.nextOffset;
|
|
351
|
-
const op = sub.value;
|
|
352
|
-
if (op <= 0x0b) {
|
|
353
|
-
cursor = skipMemArg(bytes, cursor, bodyEnd); // v128 load/store
|
|
354
|
-
} else if (op === 0x0c || op === 0x0d) {
|
|
355
|
-
cursor += 16; // v128.const / i8x16.shuffle
|
|
356
|
-
} else if (op >= 0x15 && op <= 0x22) {
|
|
357
|
-
cursor += 1; // extract/replace lane
|
|
358
|
-
} else if (op >= 0x54 && op <= 0x5b) {
|
|
359
|
-
cursor = skipMemArg(bytes, cursor, bodyEnd);
|
|
360
|
-
cursor += 1; // load/store lane
|
|
361
|
-
} else if (op === 0x5c || op === 0x5d) {
|
|
362
|
-
cursor = skipMemArg(bytes, cursor, bodyEnd); // load32/64_zero
|
|
363
|
-
}
|
|
364
|
-
break;
|
|
365
|
-
}
|
|
366
|
-
case 0xfe: {
|
|
367
|
-
// Atomics / threads.
|
|
368
|
-
const sub = readLeb(bytes, cursor, bodyEnd);
|
|
369
|
-
cursor = sub.nextOffset;
|
|
370
|
-
atomics += 1;
|
|
371
|
-
if (sub.value === 0x03) {
|
|
372
|
-
cursor += 1; // atomic.fence: one reserved byte, no memarg
|
|
373
|
-
} else {
|
|
374
|
-
cursor = skipMemArg(bytes, cursor, bodyEnd);
|
|
375
|
-
}
|
|
376
|
-
break;
|
|
377
|
-
}
|
|
378
|
-
default:
|
|
379
|
-
if (opcode >= 0x28 && opcode <= 0x3e) {
|
|
380
|
-
// Memory load/store family (i32.load .. i64.store32): each carries a
|
|
381
|
-
// memarg (align + offset). Skipping this is what prevents 0xFE bytes
|
|
382
|
-
// inside memory-offset immediates from being miscounted as atomics.
|
|
383
|
-
cursor = skipMemArg(bytes, cursor, bodyEnd);
|
|
384
|
-
break;
|
|
385
|
-
}
|
|
386
|
-
// All remaining MVP numeric/comparison/conversion opcodes
|
|
387
|
-
// (0x45..0xc4, sign-extension, etc.) take no immediates.
|
|
388
|
-
break;
|
|
389
|
-
}
|
|
390
|
-
if (cursor > bodyEnd) {
|
|
391
|
-
throw new Error("Instruction immediate ran past end of function body.");
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
return atomics;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
function countAtomicInstructions(parsed) {
|
|
398
|
-
const codeSection = parsed.sections.find((section) => section.id === 10);
|
|
399
|
-
if (!codeSection) {
|
|
400
|
-
return 0;
|
|
401
|
-
}
|
|
402
|
-
const bytes = parsed.bytes;
|
|
403
|
-
let total = 0;
|
|
404
|
-
try {
|
|
405
|
-
let cursor = codeSection.payloadStart;
|
|
406
|
-
const funcCount = readLeb(bytes, cursor, codeSection.payloadEnd);
|
|
407
|
-
cursor = funcCount.nextOffset;
|
|
408
|
-
for (let i = 0; i < funcCount.value && cursor < codeSection.payloadEnd; i += 1) {
|
|
409
|
-
const sizeInfo = readLeb(bytes, cursor, codeSection.payloadEnd);
|
|
410
|
-
const bodyStart = sizeInfo.nextOffset;
|
|
411
|
-
const bodyEnd = bodyStart + sizeInfo.value;
|
|
412
|
-
if (bodyEnd > codeSection.payloadEnd) {
|
|
413
|
-
break;
|
|
414
|
-
}
|
|
415
|
-
try {
|
|
416
|
-
total += countAtomicsInFunctionBody(bytes, bodyStart, bodyEnd);
|
|
417
|
-
} catch {
|
|
418
|
-
// Best-effort per function: skip an undecodable body rather than crash.
|
|
419
|
-
}
|
|
420
|
-
cursor = bodyEnd;
|
|
421
|
-
}
|
|
422
|
-
} catch {
|
|
423
|
-
// Truncated / malformed code section: return best-effort count.
|
|
424
|
-
}
|
|
425
|
-
return total;
|
|
426
|
-
}
|
|
427
143
|
|
|
428
144
|
function readLimits(bytes, cursor, end) {
|
|
429
145
|
const flags = bytes[cursor];
|
|
@@ -598,40 +314,6 @@ function isEmscriptenThreadHook(entry) {
|
|
|
598
314
|
);
|
|
599
315
|
}
|
|
600
316
|
|
|
601
|
-
function readDeclaredFeatures(wasmBytes) {
|
|
602
|
-
let customSections;
|
|
603
|
-
try {
|
|
604
|
-
customSections = listWasmCustomSections(wasmBytes);
|
|
605
|
-
} catch {
|
|
606
|
-
return null;
|
|
607
|
-
}
|
|
608
|
-
const featureSection = customSections.find(
|
|
609
|
-
(section) => section.name === "target_features",
|
|
610
|
-
);
|
|
611
|
-
if (!featureSection) {
|
|
612
|
-
return null;
|
|
613
|
-
}
|
|
614
|
-
const bytes = featureSection.dataBytes;
|
|
615
|
-
const features = [];
|
|
616
|
-
try {
|
|
617
|
-
let cursor = 0;
|
|
618
|
-
const countInfo = decodeUnsignedLeb128(bytes, cursor);
|
|
619
|
-
cursor = countInfo.nextOffset;
|
|
620
|
-
for (let i = 0; i < countInfo.value; i += 1) {
|
|
621
|
-
cursor += 1; // prefix ('+', '-', '=')
|
|
622
|
-
const nameLen = decodeUnsignedLeb128(bytes, cursor);
|
|
623
|
-
cursor = nameLen.nextOffset;
|
|
624
|
-
features.push(
|
|
625
|
-
textDecoder.decode(bytes.subarray(cursor, cursor + nameLen.value)),
|
|
626
|
-
);
|
|
627
|
-
cursor += nameLen.value;
|
|
628
|
-
}
|
|
629
|
-
} catch {
|
|
630
|
-
return features.length > 0 ? features : null;
|
|
631
|
-
}
|
|
632
|
-
return features;
|
|
633
|
-
}
|
|
634
|
-
|
|
635
317
|
/**
|
|
636
318
|
* Parse a compiled wasm artifact and report its threading features, including
|
|
637
319
|
* the isomorphic wasi-threads host contract.
|
|
@@ -658,7 +340,7 @@ export function analyzeWasmThreadFeatures(wasmBytes) {
|
|
|
658
340
|
...collectDeclaredMemories(parsed),
|
|
659
341
|
];
|
|
660
342
|
const sharedMemory = memories.find((memory) => memory.shared) ?? null;
|
|
661
|
-
const atomicInstructionCount =
|
|
343
|
+
const atomicInstructionCount = scanWasmCode(parsed).atomic;
|
|
662
344
|
const declaredFeatures = readDeclaredFeatures(parsed.bytes);
|
|
663
345
|
const usesAtomics =
|
|
664
346
|
atomicInstructionCount > 0 ||
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// WebAssembly SIMD guardrail: the opt-in flag and the emitted-artifact validator.
|
|
2
|
+
//
|
|
3
|
+
// SIMD is OPT-IN per module: `compileModuleFromSource({ simd128: true })` compiles
|
|
4
|
+
// the module's own code (and its `libraries`) with -msimd128. The guard then holds
|
|
5
|
+
// the emitted artifact to that declaration:
|
|
6
|
+
//
|
|
7
|
+
// * a module that DID opt in must declare `simd128` in target_features (the flag
|
|
8
|
+
// reached the objects);
|
|
9
|
+
// * a module that did NOT opt in but whose artifact carries SIMD anyway (a
|
|
10
|
+
// per-function `__attribute__((target("simd128")))`, or a module built by its own
|
|
11
|
+
// clang driver with -msimd128) is NOT refused yet: the SIMD is recorded as if the
|
|
12
|
+
// module had declared it, and a warning tells the module to pass
|
|
13
|
+
// `simd128: true`. Undeclared SIMD will become an error in a later release;
|
|
14
|
+
// * relaxed-SIMD is refused either way. Its instructions are implementation-defined
|
|
15
|
+
// (whether an FMA fuses, which lane a select takes), so the same module would
|
|
16
|
+
// return different bytes in the browser and under WasmEdge. Fixed-width simd128
|
|
17
|
+
// is deterministic and is the only SIMD the SDK admits;
|
|
18
|
+
// * an artifact with a function body the scanner cannot decode is refused: SIMD
|
|
19
|
+
// inside that body would go uncounted, so its use cannot be ruled out.
|
|
20
|
+
//
|
|
21
|
+
// Both runtime lanes execute fixed-width SIMD: every current browser engine and
|
|
22
|
+
// WasmEdge 0.16.4 (interpreter and AOT). The analysis is recorded on the compile
|
|
23
|
+
// result and in the guest-link metadata so a build record says what the artifact
|
|
24
|
+
// needs.
|
|
25
|
+
|
|
26
|
+
import { parseWasmModuleSections } from "../bundle/wasm.js";
|
|
27
|
+
import { scanWasmCode } from "./wasmCodeScan.js";
|
|
28
|
+
import { readTargetFeatureEntries } from "./wasmTargetFeatures.js";
|
|
29
|
+
|
|
30
|
+
/** The clang flag the `simd128` compile option turns on for a module's own code. */
|
|
31
|
+
export const SIMD128_COMPILE_FLAG = "-msimd128";
|
|
32
|
+
|
|
33
|
+
/** The process warning code for an artifact that carries SIMD its build did not declare. */
|
|
34
|
+
export const SIMD128_UNDECLARED_WARNING_CODE = "SDM_SIMD128_UNDECLARED";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Parse a compiled wasm artifact and report how it uses SIMD.
|
|
38
|
+
*
|
|
39
|
+
* @param {Uint8Array|ArrayBuffer|ArrayBufferView} wasmBytes
|
|
40
|
+
* @returns {{
|
|
41
|
+
* declared: boolean|null,
|
|
42
|
+
* declaredRelaxed: boolean|null,
|
|
43
|
+
* used: boolean,
|
|
44
|
+
* instructionCount: number,
|
|
45
|
+
* relaxedInstructionCount: number,
|
|
46
|
+
* undecodableFunctions: number,
|
|
47
|
+
* }}
|
|
48
|
+
* declared / declaredRelaxed whether target_features lists simd128 /
|
|
49
|
+
* relaxed-simd as used; null when the module has no target_features section.
|
|
50
|
+
* instructionCount fixed-width SIMD instructions in the code section.
|
|
51
|
+
* relaxedInstructionCount SIMD instructions beyond the fixed-width set
|
|
52
|
+
* (relaxed-simd and later proposals).
|
|
53
|
+
* undecodableFunctions function bodies the scanner could not decode (their
|
|
54
|
+
* instructions are not counted).
|
|
55
|
+
*/
|
|
56
|
+
export function analyzeWasmSimdFeatures(wasmBytes) {
|
|
57
|
+
const parsed = parseWasmModuleSections(wasmBytes);
|
|
58
|
+
const code = scanWasmCode(parsed);
|
|
59
|
+
const entries = readTargetFeatureEntries(parsed.bytes);
|
|
60
|
+
const declares = (name) =>
|
|
61
|
+
entries === null
|
|
62
|
+
? null
|
|
63
|
+
: entries.some((entry) => entry.name === name && entry.prefix !== "-");
|
|
64
|
+
return {
|
|
65
|
+
declared: declares("simd128"),
|
|
66
|
+
declaredRelaxed: declares("relaxed-simd"),
|
|
67
|
+
used: code.simd > 0,
|
|
68
|
+
instructionCount: code.simd,
|
|
69
|
+
relaxedInstructionCount: code.relaxedSimd,
|
|
70
|
+
undecodableFunctions: code.undecodableFunctions,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Hold a compiled wasm artifact's SIMD use to the build's declaration (see the file
|
|
76
|
+
* header). Returns the analysis plus `requested` (the module opted in) and
|
|
77
|
+
* `undeclared` (SIMD without the opt-in; `warning` then says what to do) on success;
|
|
78
|
+
* throws a clear, actionable error otherwise.
|
|
79
|
+
*
|
|
80
|
+
* @param {Uint8Array|ArrayBuffer|ArrayBufferView} wasmBytes
|
|
81
|
+
* @param {{ optedIn?: boolean, source?: string }} [options]
|
|
82
|
+
*/
|
|
83
|
+
export function assertSimdArtifact(wasmBytes, options = {}) {
|
|
84
|
+
const source = options.source ? String(options.source) : "emitted wasm";
|
|
85
|
+
const optedIn = options.optedIn === true;
|
|
86
|
+
let analysis;
|
|
87
|
+
try {
|
|
88
|
+
analysis = analyzeWasmSimdFeatures(wasmBytes);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`simd128 artifact validation failed for ${source}: could not parse the ` +
|
|
92
|
+
`emitted wasm (${error instanceof Error ? error.message : String(error)}).`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const failures = [];
|
|
97
|
+
if (analysis.relaxedInstructionCount > 0 || analysis.declaredRelaxed === true) {
|
|
98
|
+
failures.push(
|
|
99
|
+
"it uses relaxed-SIMD " +
|
|
100
|
+
`(${analysis.relaxedInstructionCount} instruction(s); target_features ` +
|
|
101
|
+
`${analysis.declaredRelaxed ? "declares" : "does not declare"} relaxed-simd) - ` +
|
|
102
|
+
"relaxed-SIMD results are implementation-defined, so the same module would return " +
|
|
103
|
+
"different bytes in the browser and under WasmEdge; only fixed-width simd128 is admitted",
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (analysis.undecodableFunctions > 0) {
|
|
107
|
+
failures.push(
|
|
108
|
+
`${analysis.undecodableFunctions} function body(ies) could not be decoded, so SIMD ` +
|
|
109
|
+
"inside them would go uncounted and the artifact's SIMD use cannot be ruled out",
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
if (optedIn && analysis.declared === false) {
|
|
113
|
+
failures.push(
|
|
114
|
+
`simd128 was requested (${SIMD128_COMPILE_FLAG}) but target_features does not ` +
|
|
115
|
+
"declare simd128 - the flag did not reach the module's objects",
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
if (failures.length > 0) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`simd128 artifact validation REJECTED ${source}: ${failures.join("; ")}. ` +
|
|
121
|
+
"This build must not ship.",
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const undeclared = !optedIn && (analysis.instructionCount > 0 || analysis.declared === true);
|
|
126
|
+
const result = { requested: optedIn, undeclared, ...analysis };
|
|
127
|
+
if (undeclared) {
|
|
128
|
+
result.warning =
|
|
129
|
+
`${source} uses WebAssembly SIMD (${analysis.instructionCount} v128 instruction(s); ` +
|
|
130
|
+
`target_features ${analysis.declared ? "declares" : "does not declare"} simd128) but the build did ` +
|
|
131
|
+
"not declare it. Pass compileModuleFromSource({ simd128: true }) so the module declares that it " +
|
|
132
|
+
"needs SIMD (a module built by its own clang driver with -msimd128, or a per-function " +
|
|
133
|
+
'target("simd128") attribute, is the usual cause). The SDK records the use as if declared ' +
|
|
134
|
+
"and does not refuse the artifact yet; undeclared SIMD will become an error in a later release.";
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|