Skip to content

Commit 468e2ab

Browse files
authored
Avoid calling resetPrototype on missing object (#18076)
`wasmSourceMapData` and `wasmOffestData` are used to communicate data to new pthreads and cannot/should not be used in general just because `instantatiateWasm` is overridden. Folks who want to override `instantatiateWasm` and still want asan (and the offset converter in general) to work would need to create the `WasmOffsetConverter` as part of `instantatiateWasm`. Fixes: #17472
1 parent c4ea568 commit 468e2ab

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/preamble.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -808,10 +808,12 @@ function instantiateSync(file, info) {
808808
}
809809
#endif
810810

811-
#if expectToReceiveOnModule('instantiateWasm') && (LOAD_SOURCE_MAP || USE_OFFSET_CONVERTER)
812-
// When using postMessage to send an object, it is processed by the structured clone algorithm.
813-
// The prototype, and hence methods, on that object is then lost. This function adds back the lost prototype.
814-
// This does not work with nested objects that has prototypes, but it suffices for WasmSourceMap and WasmOffsetConverter.
811+
#if PTHREADS && (LOAD_SOURCE_MAP || USE_OFFSET_CONVERTER)
812+
// When using postMessage to send an object, it is processed by the structured
813+
// clone algorithm. The prototype, and hence methods, on that object is then
814+
// lost. This function adds back the lost prototype. This does not work with
815+
// nested objects that has prototypes, but it suffices for WasmSourceMap and
816+
// WasmOffsetConverter.
815817
function resetPrototype(constructor, attrs) {
816818
var object = Object.create(constructor.prototype);
817819
return Object.assign(object, attrs);
@@ -1093,26 +1095,31 @@ function createWasm() {
10931095

10941096
#if expectToReceiveOnModule('instantiateWasm')
10951097
// User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback
1096-
// to manually instantiate the Wasm module themselves. This allows pages to run the instantiation parallel
1097-
// to any other async startup actions they are performing.
1098-
// Also pthreads and wasm workers initialize the wasm instance through this path.
1098+
// to manually instantiate the Wasm module themselves. This allows pages to
1099+
// run the instantiation parallel to any other async startup actions they are
1100+
// performing.
1101+
// Also pthreads and wasm workers initialize the wasm instance through this
1102+
// path.
10991103
if (Module['instantiateWasm']) {
1100-
#if USE_OFFSET_CONVERTER
1101-
#if ASSERTIONS && PTHREADS
1104+
1105+
#if USE_OFFSET_CONVERTER && PTHREADS
11021106
if (ENVIRONMENT_IS_PTHREAD) {
1107+
#if ASSERTIONS
11031108
assert(Module['wasmOffsetData'], 'wasmOffsetData not found on Module object');
1104-
}
11051109
#endif
1106-
wasmOffsetConverter = resetPrototype(WasmOffsetConverter, Module['wasmOffsetData']);
1110+
wasmOffsetConverter = resetPrototype(WasmOffsetConverter, Module['wasmOffsetData']);
1111+
}
11071112
#endif
1108-
#if LOAD_SOURCE_MAP
1109-
#if ASSERTIONS && PTHREADS
1113+
1114+
#if LOAD_SOURCE_MAP && PTHREADS
11101115
if (ENVIRONMENT_IS_PTHREAD) {
1116+
#if ASSERTIONS
11111117
assert(Module['wasmSourceMapData'], 'wasmSourceMapData not found on Module object');
1112-
}
11131118
#endif
1114-
wasmSourceMap = resetPrototype(WasmSourceMap, Module['wasmSourceMapData']);
1119+
wasmSourceMap = resetPrototype(WasmSourceMap, Module['wasmSourceMapData']);
1120+
}
11151121
#endif
1122+
11161123
try {
11171124
return Module['instantiateWasm'](info, receiveInstance);
11181125
} catch(e) {

test/manual_wasm_instantiate.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@
172172
wasm.then(function(wasmBinary) {
173173
console.log('wasm download finished, begin instantiating');
174174
var wasmInstantiate = WebAssembly.instantiate(new Uint8Array(wasmBinary), imports).then(function(output) {
175+
// When overriding instantiateWasm, in asan builds, we also need
176+
// to take care of creating the WasmOffsetConverter
177+
if (typeof WasmOffsetConverter != "undefined") {
178+
wasmOffsetConverter = new WasmOffsetConverter(wasmBinary, output.module);
179+
}
175180
console.log('wasm instantiation succeeded');
176181
Module.testWasmInstantiationSucceeded = 1;
177182
successCallback(output.instance);

0 commit comments

Comments
 (0)