Skip to content

Commit f198db6

Browse files
committed
Implement a FNV1-a style checksum to the binary decoded Wasm data, and display a help notice about needing to serve the HTML/JS file with UTF-8 encoding. This helps developers identify if their own shell files might not have the necessary encoding.
1 parent 04fda5c commit f198db6

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

src/postamble_minimal.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ function initRuntime(wasmExports) {
151151

152152
#if SINGLE_FILE && SINGLE_FILE_BINARY_ENCODE && !WASM2JS
153153
Module['wasm'] = binaryDecode("<<< WASM_BINARY_DATA >>>");
154+
#if ASSERTIONS
155+
assert(Module['wasm'].reduce((x,y) => ((x^y)*65599) >>> 0, 2166136261) == "<<< WASM_BINARY_DATA_CHECKSUM >>>", "The checksum of the binary decoded WebAssembly Module code does not match the original data. Double check that this .html/.js file is interpreted with UTF-8 encoding, e.g. by setting <meta charset='utf-8'> inside <head> of the HTML file, or by passing 'Content-Type: application/javascript; charset=utf-8' HTTP header.");
156+
#endif
154157
#elif SINGLE_FILE && WASM == 1 && !WASM2JS
155158
Module['wasm'] = base64Decode('<<< WASM_BINARY_DATA >>>');
156159
#endif

src/preamble.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,10 @@ function getWasmImports() {
876876
return exports;
877877
#else
878878
wasmBinaryFile ??= findWasmBinary();
879+
#if SINGLE_FILE && SINGLE_FILE_BINARY_ENCODE && !WASM2JS && ASSERTIONS
880+
assert(wasmBinaryFile.reduce((x,y) => ((x^y)*65599) >>> 0, 2166136261) == "<<< WASM_BINARY_DATA_CHECKSUM >>>", "The checksum of the binary decoded WebAssembly Module code does not match the original data. Double check that this .html/.js file is interpreted with UTF-8 encoding, e.g. by setting <meta charset='utf-8'> inside <head> of the HTML file, or by passing 'Content-Type: application/javascript; charset=utf-8' HTTP header.");
881+
#endif
882+
879883
#if WASM_ASYNC_COMPILATION
880884
#if RUNTIME_DEBUG
881885
dbg('asynchronously preparing wasm');

tools/link.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,12 @@ def phase_binaryen(target, options, wasm_target):
24902490

24912491
if settings.SINGLE_FILE_BINARY_ENCODE:
24922492
js = do_replace(js, '"<<< WASM_BINARY_DATA >>>"', binary_encode(wasm_target))
2493+
if settings.ASSERTIONS:
2494+
def checksum(a):
2495+
h = 2166136261
2496+
for b in a: h = (h ^ b) * 65599 & 0xffffffff
2497+
return h
2498+
js = do_replace(js, '"<<< WASM_BINARY_DATA_CHECKSUM >>>"', str(checksum(utils.read_binary(wasm_target))))
24932499
else:
24942500
js = do_replace(js, '<<< WASM_BINARY_DATA >>>', base64_encode(wasm_target))
24952501
delete_file(wasm_target)

0 commit comments

Comments
 (0)