Skip to content

Commit

Permalink
[release/8.0-rc2] [browser] Fix SIMD+EH check (#92439)
Browse files Browse the repository at this point in the history
* Move simd+eh check after emcripten provided build time values

* Call cwraps.mono_wasm_abort from runtimeHelpers.abort when cwraps are ready (onRuntimeInitializedAsync)

* Assign early mono_wasm_exit and abort even earlier

* Fire feature check before awaiting wasm download, but await it after.

* Whitespaces

---------

Co-authored-by: Marek Fišera <mara@neptuo.com>
  • Loading branch information
github-actions[bot] and maraf authored Sep 22, 2023
1 parent 79bd007 commit b1dbbb9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/mono/wasm/runtime/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
beforeOnRuntimeInitialized: createPromiseController<void>(),
afterOnRuntimeInitialized: createPromiseController<void>(),
afterPostRun: createPromiseController<void>(),
mono_wasm_exit: () => {
throw new Error("Mono shutdown");
},
abort: (reason: any) => {
throw reason;
}
});

Object.assign(globalObjects.module.config!, {}) as any;
Expand Down
38 changes: 22 additions & 16 deletions src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ import { assertNoProxies } from "./gc-handles";
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

Expand Down Expand Up @@ -240,6 +232,15 @@ async function onRuntimeInitializedAsync(userOnRuntimeInitialized: () => void) {
// wait for previous stage
await runtimeHelpers.afterPreRun.promise;
mono_log_debug("onRuntimeInitialized");

runtimeHelpers.mono_wasm_exit = cwraps.mono_wasm_exit;
runtimeHelpers.abort = (reason: any) => {
if (!loaderHelpers.is_exited()) {
cwraps.mono_wasm_abort();
}
throw reason;
};

const mark = startMeasure();
// signal this stage, this will allow pending assets to allocate memory
runtimeHelpers.beforeOnRuntimeInitialized.promise_control.resolve();
Expand Down Expand Up @@ -361,13 +362,6 @@ function mono_wasm_pre_init_essential(isWorker: boolean): void {
}

init_c_exports();
runtimeHelpers.mono_wasm_exit = cwraps.mono_wasm_exit;
runtimeHelpers.abort = (reason: any) => {
if (!loaderHelpers.is_exited()) {
cwraps.mono_wasm_abort();
}
throw reason;
};
cwraps_internal(INTERNAL);
if (WasmEnableLegacyJsInterop && !linkerDisableLegacyJsInterop) {
cwraps_mono_api(MONO);
Expand All @@ -386,7 +380,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");


if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
}
Expand Down Expand Up @@ -466,8 +459,12 @@ async function instantiate_wasm_module(
await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

const wasmFeaturePromise = ensureUsedWasmFeatures();

replace_linker_placeholders(imports);
const assetToLoad = await loaderHelpers.wasmDownloadPromise.promise;

await wasmFeaturePromise;
await instantiate_wasm_asset(assetToLoad, imports, successCallback);
assetToLoad.pendingDownloadInternal = null as any; // GC
assetToLoad.pendingDownload = null as any; // GC
Expand Down Expand Up @@ -499,6 +496,15 @@ async function instantiate_wasm_module(
Module.removeRunDependency("instantiate_wasm_module");
}

async function ensureUsedWasmFeatures() {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
}

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshotSize) {
Expand Down

0 comments on commit b1dbbb9

Please sign in to comment.