From 3ff258697c274b7a08afc880a6b9128f9ebc54a3 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Fri, 3 Mar 2023 20:13:23 +0100 Subject: [PATCH 01/12] EmccAssertions and EmccEnvironment MSBuild props --- .../sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj | 2 ++ src/mono/wasm/build/WasmApp.Native.targets | 3 +++ src/mono/wasm/build/WasmApp.targets | 2 ++ src/mono/wasm/wasm.proj | 3 +-- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj b/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj index 11eac88f6dca6..67b26e1a86076 100644 --- a/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj +++ b/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj @@ -4,6 +4,8 @@ true true true + true + web -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index ce4c063a5806a..91cd1057334ba 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -418,7 +418,10 @@ + <_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" /> + <_EmccLDSFlags Condition="'$(EmccEnvironment)' != ''" Include="-s ENVIRONMENT=$(EmccEnvironment)" /> + <_EmccLDSFlags Condition="'$(EmccAssertions)' == 'true'" Include="-s ASSERTIONS=1" /> <_WasmNativeFileForLinking Include="%(_BitcodeFile.ObjectFile)" /> <_WasmNativeFileForLinking Include="%(_WasmSourceFileToCompile.ObjectFile)" /> diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index aa38682595789..40a760f114e54 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -49,6 +49,8 @@ - $(EmccFlags) - Emcc flags used for both compiling native files, and linking - $(EmccExtraLDFlags) - Extra emcc flags for linking - $(EmccExtraCFlags) - Extra emcc flags for compiling native files + - $(EmccAssertions) - Corresponds to `ASSERTIONS` arg for emcc. Default false. + - $(EmccEnvironment) - Corresponds to `ENVIRONMENT` arg for emcc. Default is `web,webview,worker,node,shell`. - $(EmccInitialHeapSize) - Initial heap size specified with `emcc`. Default value: 16777216 or size of the DLLs, whichever is larger. Corresponds to `INITIAL_MEMORY` arg for emcc. (previously named EmccTotalMemory, which is still kept as an alias) diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index 3f4d2d2e2e03e..abcda250683c6 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -196,7 +196,6 @@ - @@ -330,7 +329,7 @@ -g -Os -s -DDEBUG=1 -DENABLE_AOT_PROFILER=1 -DENABLE_BROWSER_PROFILER=1 -Oz -DENABLE_BROWSER_PROFILER=1 - $(CMakeConfigurationEmccFlags) + $(CMakeConfigurationEmccFlags) -s ASSERTIONS=1 -O2 $(CMakeConfigurationLinkFlags) -s EXPORT_ES6=1 From 428388d4c44b05dbc7e1bd6a5f03bb4773e8815c Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Fri, 3 Mar 2023 21:30:35 +0100 Subject: [PATCH 02/12] fix --- src/mono/wasm/wasm.proj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index abcda250683c6..4cd8fdcdc823d 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -197,12 +197,12 @@ - + From 7f792201b283836f0b0d7cba4366760408de37bd Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Fri, 3 Mar 2023 23:17:23 +0100 Subject: [PATCH 03/12] symbols as separate asset type --- .../sample/wasm/DefaultBrowserSample.targets | 1 + src/mono/wasm/build/WasmApp.targets | 1 - src/mono/wasm/runtime/assets.ts | 31 +++++++++++++++++ src/mono/wasm/runtime/dotnet.d.ts | 3 +- src/mono/wasm/runtime/logging.ts | 34 ++----------------- src/mono/wasm/runtime/startup.ts | 2 -- src/mono/wasm/runtime/types.ts | 2 +- src/mono/wasm/runtime/types/emscripten.ts | 1 - src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 8 +++++ 9 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/mono/sample/wasm/DefaultBrowserSample.targets b/src/mono/sample/wasm/DefaultBrowserSample.targets index ce72d3a8a960f..6654fa36eea78 100644 --- a/src/mono/sample/wasm/DefaultBrowserSample.targets +++ b/src/mono/sample/wasm/DefaultBrowserSample.targets @@ -3,6 +3,7 @@ true true embedded + true -1 true diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 40a760f114e54..18c0700cd9991 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -335,7 +335,6 @@ Condition="'$(WasmEmitSymbolMap)' == 'true' and '$(_HasDotnetJsSymbols)' != 'true' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js.symbols')" /> - diff --git a/src/mono/wasm/runtime/assets.ts b/src/mono/wasm/runtime/assets.ts index 30b43d61c8412..7b709763e8147 100644 --- a/src/mono/wasm/runtime/assets.ts +++ b/src/mono/wasm/runtime/assets.ts @@ -4,6 +4,7 @@ import cwraps from "./cwraps"; import { mono_wasm_load_icu_data } from "./icu"; import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports"; +import { wasm_func_map } from "./logging"; import { mono_wasm_load_bytes_into_heap } from "./memory"; import { MONO } from "./net6-legacy/imports"; import { endMeasure, MeasuredBlock, startMeasure } from "./profiler"; @@ -37,6 +38,7 @@ const skipBufferByAssetTypes: { [k: string]: boolean } = { "dotnetwasm": true, + "symbols": true, }; // these assets are instantiated differently than the main flow @@ -45,6 +47,7 @@ const skipInstantiateByAssetTypes: { } = { "js-module-threads": true, "dotnetwasm": true, + "symbols": true, }; export function get_preferred_icu_asset(): string | null { @@ -129,6 +132,12 @@ export async function mono_download_assets(): Promise { // this is after onRuntimeInitialized _instantiate_asset(asset, url, data); } + if (asset.behavior === "symbols") { + await instantiate_symbols_asset(asset); + asset.pendingDownloadInternal = null as any; // GC + asset.pendingDownload = null as any; // GC + asset.buffer = null as any; // GC + } } else { const headersOnly = skipBufferByAssetTypes[asset.behavior]; if (!headersOnly) { @@ -391,6 +400,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) { switch (asset.behavior) { case "dotnetwasm": case "js-module-threads": + case "symbols": // do nothing break; case "resource": @@ -493,6 +503,27 @@ export async function instantiate_wasm_asset( successCallback(compiledInstance, compiledModule); } +export async function instantiate_symbols_asset( + pendingAsset: AssetEntryInternal, +): Promise { + try { + const response = await pendingAsset.pendingDownloadInternal!.response; + const data = await response.text(); + data.split(/[\r\n]/).forEach((line: string) => { + const parts: string[] = line.split(/:/); + if (parts.length < 2) + return; + + parts[1] = parts.splice(1).join(":"); + wasm_func_map.set(Number(parts[0]), parts[1]); + }); + if (runtimeHelpers.diagnosticTracing) console.debug(`MONO_WASM: Loaded ${wasm_func_map.size} symbols`); + + } catch (error: any) { + console.log(`MONO_WASM: Error loading symbol file ${pendingAsset.name}: ${JSON.stringify(error)}`); + } +} + // used from Blazor export function mono_wasm_load_data_archive(data: Uint8Array, prefix: string): boolean { if (data.length < 8) diff --git a/src/mono/wasm/runtime/dotnet.d.ts b/src/mono/wasm/runtime/dotnet.d.ts index d6e84c710f1c4..a3e4b0bb2eb0a 100644 --- a/src/mono/wasm/runtime/dotnet.d.ts +++ b/src/mono/wasm/runtime/dotnet.d.ts @@ -57,7 +57,6 @@ declare interface EmscriptenModule { UTF8ArrayToString(u8Array: Uint8Array, idx?: number, maxBytesToRead?: number): string; FS_createPath(parent: string, path: string, canRead?: boolean, canWrite?: boolean): string; FS_createDataFile(parent: string, name: string, data: TypedArray, canRead: boolean, canWrite: boolean, canOwn?: boolean): string; - FS_readFile(filename: string, opts: any): any; removeRunDependency(id: string): void; addRunDependency(id: string): void; addFunction(fn: Function, signature: string): number; @@ -176,7 +175,7 @@ interface AssetEntry extends ResourceRequest { */ pendingDownload?: LoadingResource; } -type AssetBehaviours = "resource" | "assembly" | "pdb" | "heap" | "icu" | "vfs" | "dotnetwasm" | "js-module-threads"; +type AssetBehaviours = "resource" | "assembly" | "pdb" | "heap" | "icu" | "vfs" | "dotnetwasm" | "js-module-threads" | "symbols"; type GlobalizationMode = "icu" | // load ICU globalization data from any runtime assets with behavior "icu". "invariant" | // operate in invariant globalization mode. "auto"; diff --git a/src/mono/wasm/runtime/logging.ts b/src/mono/wasm/runtime/logging.ts index fb8cb01f52975..eddd079a02cb1 100644 --- a/src/mono/wasm/runtime/logging.ts +++ b/src/mono/wasm/runtime/logging.ts @@ -1,11 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -import BuildConfiguration from "consts:configuration"; -import { INTERNAL, Module, runtimeHelpers } from "./imports"; +import { INTERNAL, Module } from "./imports"; import { CharPtr, VoidPtr } from "./types/emscripten"; -const wasm_func_map = new Map(); +export const wasm_func_map = new Map(); const regexes: any[] = []; // V8 @@ -182,32 +181,3 @@ export function setup_proxy_console(id: string, console: Console, origin: string for (const m of ["log", ...methods]) anyConsole[m] = proxyConsoleMethod(`console.${m}`, send, true); } - -export function readSymbolMapFile(filename: string): void { - if (runtimeHelpers.mono_wasm_symbols_are_ready) return; - runtimeHelpers.mono_wasm_symbols_are_ready = true; - try { - const res = Module.FS_readFile(filename, { flags: "r", encoding: "utf8" }); - res.split(/[\r\n]/).forEach((line: string) => { - const parts: string[] = line.split(/:/); - if (parts.length < 2) - return; - - parts[1] = parts.splice(1).join(":"); - wasm_func_map.set(Number(parts[0]), parts[1]); - }); - if (BuildConfiguration === "Debug") { - console.debug(`MONO_WASM: Loaded ${wasm_func_map.size} symbols`); - } - } catch (error: any) { - if (error.errno == 44) {// NOENT - if (BuildConfiguration === "Debug") { - console.debug(`MONO_WASM: Could not find symbols file ${filename}. Ignoring.`); - } - } - else { - console.log(`MONO_WASM: Error loading symbol file ${filename}: ${JSON.stringify(error)}`); - } - return; - } -} diff --git a/src/mono/wasm/runtime/startup.ts b/src/mono/wasm/runtime/startup.ts index 8f5f30a4b1f2e..f92ca56c3b86d 100644 --- a/src/mono/wasm/runtime/startup.ts +++ b/src/mono/wasm/runtime/startup.ts @@ -24,7 +24,6 @@ import { cwraps_binding_api, cwraps_mono_api } from "./net6-legacy/exports-legac import { CharPtr, InstantiateWasmCallBack, InstantiateWasmSuccessCallback } from "./types/emscripten"; import { instantiate_wasm_asset, mono_download_assets, resolve_asset_path, start_asset_download, wait_for_all_assets } from "./assets"; import { BINDING, MONO } from "./net6-legacy/imports"; -import { readSymbolMapFile } from "./logging"; import { mono_wasm_init_diagnostics } from "./diagnostics"; import { preAllocatePThreadWorkerPool, instantiateWasmPThreadWorkerPool } from "./pthreads/browser"; import { export_linker } from "./exports-linker"; @@ -349,7 +348,6 @@ async function mono_wasm_before_user_runtime_initialized(): Promise { if (!runtimeHelpers.mono_wasm_load_runtime_done) mono_wasm_load_runtime("unused", config.debugLevel); if (!runtimeHelpers.mono_wasm_runtime_is_ready) mono_wasm_runtime_ready(); - if (!runtimeHelpers.mono_wasm_symbols_are_ready) readSymbolMapFile("dotnet.js.symbols"); setTimeout(() => { // when there are free CPU cycles diff --git a/src/mono/wasm/runtime/types.ts b/src/mono/wasm/runtime/types.ts index 5896e9736f924..8caacd7818f5d 100644 --- a/src/mono/wasm/runtime/types.ts +++ b/src/mono/wasm/runtime/types.ts @@ -201,6 +201,7 @@ export type AssetBehaviours = | "vfs" // load asset into the virtual filesystem (for fopen, File.Open, etc) | "dotnetwasm" // the binary of the dotnet runtime | "js-module-threads" // the javascript module for threads + | "symbols" // the javascript module for threads export type RuntimeHelpers = { runtime_interop_module: MonoAssembly; @@ -212,7 +213,6 @@ export type RuntimeHelpers = { mono_wasm_load_runtime_done: boolean; mono_wasm_runtime_is_ready: boolean; mono_wasm_bindings_is_ready: boolean; - mono_wasm_symbols_are_ready: boolean; loaded_files: string[]; maxParallelDownloads: number; diff --git a/src/mono/wasm/runtime/types/emscripten.ts b/src/mono/wasm/runtime/types/emscripten.ts index 3cc8933fd809c..11a40c91dd82c 100644 --- a/src/mono/wasm/runtime/types/emscripten.ts +++ b/src/mono/wasm/runtime/types/emscripten.ts @@ -49,7 +49,6 @@ export declare interface EmscriptenModule { UTF8ArrayToString(u8Array: Uint8Array, idx?: number, maxBytesToRead?: number): string; FS_createPath(parent: string, path: string, canRead?: boolean, canWrite?: boolean): string; FS_createDataFile(parent: string, name: string, data: TypedArray, canRead: boolean, canWrite: boolean, canOwn?: boolean): string; - FS_readFile(filename: string, opts: any): any; removeRunDependency(id: string): void; addRunDependency(id: string): void; addFunction(fn: Function, signature: string): number; diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index b7441396754fe..7a376cec60947 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -117,6 +117,11 @@ public IcuData(string name, string hash) : base(name, hash, "icu") {} public bool LoadRemote { get; set; } } + private sealed class SymbolsData : AssetEntry + { + public SymbolsData(string name, string hash) : base(name, hash, "symbols") {} + } + protected override bool ValidateArguments() { if (!base.ValidateArguments()) @@ -199,6 +204,9 @@ protected override bool ExecuteInternal() { config.Assets.Add(new ThreadsWorkerEntry (name, Utils.ComputeIntegrity(item.ItemSpec))); } + else if(name == "dotnet.js.symbols"){ + config.Assets.Add(new SymbolsData (name, Utils.ComputeIntegrity(item.ItemSpec))); + } } string packageJsonPath = Path.Combine(AppDir, "package.json"); From 028d598d65b97e9363d486df5c8ee3e9d4afd92e Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Fri, 3 Mar 2023 23:22:27 +0100 Subject: [PATCH 04/12] fix --- src/mono/sample/wasm/DefaultBrowserSample.targets | 1 - .../wasm/browser-advanced/Wasm.Advanced.Sample.csproj | 1 + src/mono/wasm/runtime/startup.ts | 6 +++--- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mono/sample/wasm/DefaultBrowserSample.targets b/src/mono/sample/wasm/DefaultBrowserSample.targets index 6654fa36eea78..ce72d3a8a960f 100644 --- a/src/mono/sample/wasm/DefaultBrowserSample.targets +++ b/src/mono/sample/wasm/DefaultBrowserSample.targets @@ -3,7 +3,6 @@ true true embedded - true -1 true diff --git a/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj b/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj index 67b26e1a86076..6f8d08534a19f 100644 --- a/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj +++ b/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj @@ -4,6 +4,7 @@ true true true + true true web diff --git a/src/mono/wasm/runtime/startup.ts b/src/mono/wasm/runtime/startup.ts index 3b444407c4e6c..a99cd1bf0a620 100644 --- a/src/mono/wasm/runtime/startup.ts +++ b/src/mono/wasm/runtime/startup.ts @@ -305,8 +305,8 @@ function mono_wasm_pre_init_essential(isWorker: boolean): void { init_c_exports(); cwraps_internal(INTERNAL); if (WasmEnableLegacyJsInterop) { - cwraps_mono_api(MONO); - cwraps_binding_api(BINDING); + cwraps_mono_api(MONO); + cwraps_binding_api(BINDING); } // removeRunDependency triggers the dependenciesFulfilled callback (runCaller) in // emscripten - on a worker since we don't have any other dependencies that causes run() to get @@ -559,7 +559,7 @@ export function bindings_init(): void { const mark = startMeasure(); init_managed_exports(); if (WasmEnableLegacyJsInterop) { - init_legacy_exports(); + init_legacy_exports(); } initialize_marshalers_to_js(); initialize_marshalers_to_cs(); From 9b7b5d68345a3d4e83e9cb40abddd8d745eeec5b Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Mon, 6 Mar 2023 19:31:05 +0100 Subject: [PATCH 05/12] feedback --- src/mono/wasm/build/WasmApp.Native.targets | 2 +- src/mono/wasm/runtime/types.ts | 2 +- src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 040073833a124..ef751755f8f98 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -421,7 +421,7 @@ <_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" /> - <_EmccLDSFlags Condition="'$(EmccEnvironment)' != ''" Include="-s ENVIRONMENT=$(EmccEnvironment)" /> + <_EmccLDSFlags Condition="'$(EmccEnvironment)' != ''" Include="-s ENVIRONMENT="$(EmccEnvironment)"" /> <_EmccLDSFlags Condition="'$(EmccAssertions)' == 'true'" Include="-s ASSERTIONS=1" /> <_WasmNativeFileForLinking Include="%(_BitcodeFile.ObjectFile)" /> diff --git a/src/mono/wasm/runtime/types.ts b/src/mono/wasm/runtime/types.ts index 5e88f6b34072e..03140c66bf99f 100644 --- a/src/mono/wasm/runtime/types.ts +++ b/src/mono/wasm/runtime/types.ts @@ -201,7 +201,7 @@ export type AssetBehaviours = | "vfs" // load asset into the virtual filesystem (for fopen, File.Open, etc) | "dotnetwasm" // the binary of the dotnet runtime | "js-module-threads" // the javascript module for threads - | "symbols" // the javascript module for threads + | "symbols" // the symbols for the wasm native code export type RuntimeHelpers = { runtime_interop_module: MonoAssembly; diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index 7a376cec60947..d3805880fc75b 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -204,7 +204,8 @@ protected override bool ExecuteInternal() { config.Assets.Add(new ThreadsWorkerEntry (name, Utils.ComputeIntegrity(item.ItemSpec))); } - else if(name == "dotnet.js.symbols"){ + else if(name == "dotnet.js.symbols") + { config.Assets.Add(new SymbolsData (name, Utils.ComputeIntegrity(item.ItemSpec))); } } From d359adb70f4b681258756b8d043a2e4b12f5375f Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Mon, 6 Mar 2023 20:44:09 +0100 Subject: [PATCH 06/12] feedback --- src/mono/wasm/wasm.proj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index e89496b51ecb5..81eccd347bdd4 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -198,12 +198,12 @@ + - From dd7561f92e15ebac4edd69fa356791cc183472a5 Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Tue, 7 Mar 2023 12:12:12 +0100 Subject: [PATCH 07/12] Update src/tasks/WasmAppBuilder/WasmAppBuilder.cs Co-authored-by: Ankit Jain --- src/tasks/WasmAppBuilder/WasmAppBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs index d3805880fc75b..0bf66e11f41b7 100644 --- a/src/tasks/WasmAppBuilder/WasmAppBuilder.cs +++ b/src/tasks/WasmAppBuilder/WasmAppBuilder.cs @@ -206,7 +206,7 @@ protected override bool ExecuteInternal() } else if(name == "dotnet.js.symbols") { - config.Assets.Add(new SymbolsData (name, Utils.ComputeIntegrity(item.ItemSpec))); + config.Assets.Add(new SymbolsData(name, Utils.ComputeIntegrity(item.ItemSpec))); } } From 9d442a83027c92213c97af1084074e21370d8f8f Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 14 Mar 2023 10:24:42 +0100 Subject: [PATCH 08/12] feedback --- src/mono/wasm/build/WasmApp.Native.targets | 5 +---- src/mono/wasm/build/WasmApp.targets | 2 +- src/mono/wasm/runtime/startup.ts | 1 - 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index f912a8f417282..b0f5c6929032b 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -170,7 +170,6 @@ <_WasmDevel Condition="'$(_WasmDevel)' == '' and '$(WasmBuildNative)' == 'true' and '$(Configuration)' == 'Debug'">true - <_EmccAssertionLevelDefault Condition="'$(_EmccAssertionLevelDefault)' == ''">0 <_EmccOptimizationFlagDefault Condition="'$(_WasmDevel)' == 'true'">-O0 <_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == '' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">-O1 <_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == ''">-Oz @@ -209,7 +208,6 @@ <_EmccCFlags Include="$(EmccCompileOptimizationFlag)" /> - <_EmccCFlags Include="-s ASSERTIONS=$(_EmccAssertionLevelDefault)" Condition="'$(_WasmDevel)' == 'true'" /> <_EmccCFlags Include="@(_EmccCommonFlags)" /> <_EmccCFlags Include="-DDISABLE_PERFTRACING_LISTEN_PORTS=1" /> @@ -227,7 +225,6 @@ <_EmccLDFlags Include="$(EmccLinkOptimizationFlag)" /> - <_EmccLDFlags Include="-s ASSERTIONS=$(_EmccAssertionLevelDefault)" Condition="'$(_WasmDevel)' == 'true'" /> <_EmccLDFlags Include="@(_EmccCommonFlags)" /> <_DriverCDependencies Include="$(_WasmPInvokeHPath);$(_WasmICallTablePath)" /> @@ -426,7 +423,7 @@ <_EmccLDSFlags Include="-s STACK_SIZE=$(EmccStackSize)" /> <_EmccLDSFlags Include="-s WASM_BIGINT=1" /> <_EmccLDSFlags Condition="'$(EmccEnvironment)' != ''" Include="-s ENVIRONMENT="$(EmccEnvironment)"" /> - <_EmccLDSFlags Condition="'$(EmccAssertions)' == 'true'" Include="-s ASSERTIONS=1" /> + <_EmccLDSFlags Condition="'$(EmccEnableAssertions)' == 'true'" Include="-s ASSERTIONS=1" /> <_WasmNativeFileForLinking Include="%(_BitcodeFile.ObjectFile)" /> <_WasmNativeFileForLinking Include="%(_WasmSourceFileToCompile.ObjectFile)" /> diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 0956e5b7a2b02..5772ad5fb47df 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -49,7 +49,7 @@ - $(EmccFlags) - Emcc flags used for both compiling native files, and linking - $(EmccExtraLDFlags) - Extra emcc flags for linking - $(EmccExtraCFlags) - Extra emcc flags for compiling native files - - $(EmccAssertions) - Corresponds to `ASSERTIONS` arg for emcc. Default false. + - $(EmccEnableAssertions) - Corresponds to `ASSERTIONS` arg for emcc. Default false. - $(EmccEnvironment) - Corresponds to `ENVIRONMENT` arg for emcc. Default is `web,webview,worker,node,shell`. - $(EmccInitialHeapSize) - Initial heap size specified with `emcc`. Default value: 16777216 or size of the DLLs, whichever is larger. Corresponds to `-s INITIAL_MEMORY=...` emcc arg. diff --git a/src/mono/wasm/runtime/startup.ts b/src/mono/wasm/runtime/startup.ts index dd0069ffce76b..591e9d7f48f5a 100644 --- a/src/mono/wasm/runtime/startup.ts +++ b/src/mono/wasm/runtime/startup.ts @@ -240,7 +240,6 @@ async function onRuntimeInitializedAsync(userOnRuntimeInitialized: () => void) { bindings_init(); if (!runtimeHelpers.mono_wasm_runtime_is_ready) mono_wasm_runtime_ready(); - if (!runtimeHelpers.mono_wasm_symbols_are_ready) readSymbolMapFile("dotnet.js.symbols"); setTimeout(() => { // when there are free CPU cycles From 4c2d6b8ed7c13eae0a329dd5d27a3d73db227191 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 14 Mar 2023 10:33:44 +0100 Subject: [PATCH 09/12] fix --- src/mono/wasm/runtime/dotnet.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mono/wasm/runtime/dotnet.d.ts b/src/mono/wasm/runtime/dotnet.d.ts index cb1285ee31e0f..96ef437674776 100644 --- a/src/mono/wasm/runtime/dotnet.d.ts +++ b/src/mono/wasm/runtime/dotnet.d.ts @@ -38,6 +38,7 @@ declare interface EmscriptenModule { HEAP8: Int8Array; HEAP16: Int16Array; HEAP32: Int32Array; + HEAP64: BigInt64Array; HEAPU8: Uint8Array; HEAPU16: Uint16Array; HEAPU32: Uint32Array; From 6706d3f04142d4a635c1266c62e4219336f4ea1e Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 14 Mar 2023 13:58:26 +0100 Subject: [PATCH 10/12] EmccExportedLibraryFunction --- .../sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj | 2 +- src/mono/wasi/build/WasiApp.Native.targets | 2 -- src/mono/wasm/build/WasmApp.Native.targets | 2 ++ src/mono/wasm/build/WasmApp.targets | 1 + src/mono/wasm/runtime/jiterpreter-jit-call.ts | 5 +++-- src/mono/wasm/wasm.proj | 3 ++- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj b/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj index 6f8d08534a19f..9379e718d57cd 100644 --- a/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj +++ b/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj @@ -5,7 +5,7 @@ true true true - true + true web -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js diff --git a/src/mono/wasi/build/WasiApp.Native.targets b/src/mono/wasi/build/WasiApp.Native.targets index 95cd07c61d04f..03a62b781b739 100644 --- a/src/mono/wasi/build/WasiApp.Native.targets +++ b/src/mono/wasi/build/WasiApp.Native.targets @@ -291,8 +291,6 @@ - - diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index b0f5c6929032b..922d39776ea41 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -413,6 +413,7 @@ <_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a <_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-js.a <_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-wasm.a + <_EmccExportedLibraryFunction>"[@(EmccExportedLibraryFunction -> '%27%(Identity)%27', ',')]" <_EmccExportedRuntimeMethods>"[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]" <_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',') @@ -449,6 +450,7 @@ <_EmccLinkStepArgs Include="-o "$(_WasmIntermediateOutputPath)dotnet.js"" /> <_WasmLinkDependencies Include="$(_EmccLinkRsp)" /> + <_EmccLinkStepArgs Include="-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$(_EmccExportedLibraryFunction)" Condition="'@(_EmccExportedLibraryFunction)' != ''" /> <_EmccLinkStepArgs Include="-s EXPORTED_RUNTIME_METHODS=$(_EmccExportedRuntimeMethods)" /> <_EmccLinkStepArgs Include="-s EXPORTED_FUNCTIONS=$(_EmccExportedFunctions)" /> diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index fc987eb1a096e..796a22378de0a 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -92,6 +92,7 @@ - @(EmccExportedRuntimeMethod) - Extra method for emcc flag EXPORTED_RUNTIME_METHODS + - @(EmccExportedLibraryFunction) - Extra method for emcc flag DEFAULT_LIBRARY_FUNCS_TO_INCLUDE - @(EmccExportedFunction) - Extra function for emcc flag EXPORTED_FUNCTIONS --> diff --git a/src/mono/wasm/runtime/jiterpreter-jit-call.ts b/src/mono/wasm/runtime/jiterpreter-jit-call.ts index e84fc620da4ff..8cc2d23d45c74 100644 --- a/src/mono/wasm/runtime/jiterpreter-jit-call.ts +++ b/src/mono/wasm/runtime/jiterpreter-jit-call.ts @@ -156,6 +156,9 @@ class TrampolineInfo { } } +// this is cached replacements for Module.getWasmTableEntry(); +// we could add and +// if we need to export the original function getWasmTableEntry (index: number) { let result = fnCache[index]; if (!result) { @@ -172,8 +175,6 @@ function getWasmTableEntry (index: number) { export function mono_interp_invoke_wasm_jit_call_trampoline ( thunkIndex: number, ret_sp: number, sp: number, ftndesc: number, thrown: NativePointer ) { - // FIXME: It's impossible to get emscripten to export this for some reason - // const thunk = Module.getWasmTableEntry(thunkIndex); const thunk = getWasmTableEntry(thunkIndex); try { thunk(ret_sp, sp, ftndesc, thrown); diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index c903d0412c225..9755e405afed4 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -198,7 +198,6 @@ - @@ -271,6 +270,7 @@ + <_EmccExportedLibraryFunction>"[@(EmccExportedLibraryFunction -> '%27%(Identity)%27', ',')]" <_EmccExportedRuntimeMethods>"[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]" <_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',') 16777216 @@ -289,6 +289,7 @@ <_EmccLinkFlags Include="-s ALLOW_TABLE_GROWTH=1" /> <_EmccLinkFlags Include="-s NO_EXIT_RUNTIME=1" /> <_EmccLinkFlags Include="-s FORCE_FILESYSTEM=1" /> + <_EmccLinkFlags Condition="'$(_EmccExportedLibraryFunction)' != ''" Include="-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$(_EmccExportedLibraryFunction)" /> <_EmccLinkFlags Include="-s EXPORTED_RUNTIME_METHODS=$(_EmccExportedRuntimeMethods)" /> <_EmccLinkFlags Include="-s EXPORTED_FUNCTIONS=$(_EmccExportedFunctions)" /> <_EmccLinkFlags Include="--source-map-base http://example.com" /> From a06095284d75477db66d16a3ed42a2f57a77aeac Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 14 Mar 2023 15:20:08 +0100 Subject: [PATCH 11/12] feedback --- src/mono/wasm/build/WasmApp.Native.targets | 2 +- src/mono/wasm/runtime/assets.ts | 34 +++++++++------------- src/mono/wasm/runtime/logging.ts | 15 +++++++++- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 922d39776ea41..7aa3cb71b29ca 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -450,7 +450,7 @@ <_EmccLinkStepArgs Include="-o "$(_WasmIntermediateOutputPath)dotnet.js"" /> <_WasmLinkDependencies Include="$(_EmccLinkRsp)" /> - <_EmccLinkStepArgs Include="-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$(_EmccExportedLibraryFunction)" Condition="'@(_EmccExportedLibraryFunction)' != ''" /> + <_EmccLinkStepArgs Include="-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$(_EmccExportedLibraryFunction)" Condition="'$(_EmccExportedLibraryFunction)' != ''" /> <_EmccLinkStepArgs Include="-s EXPORTED_RUNTIME_METHODS=$(_EmccExportedRuntimeMethods)" /> <_EmccLinkStepArgs Include="-s EXPORTED_FUNCTIONS=$(_EmccExportedFunctions)" /> diff --git a/src/mono/wasm/runtime/assets.ts b/src/mono/wasm/runtime/assets.ts index a921f3d03f3ec..3d77e14fccb8c 100644 --- a/src/mono/wasm/runtime/assets.ts +++ b/src/mono/wasm/runtime/assets.ts @@ -4,7 +4,7 @@ import cwraps from "./cwraps"; import { mono_wasm_load_icu_data } from "./icu"; import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports"; -import { wasm_func_map } from "./logging"; +import { parseSymbolMapFile } from "./logging"; import { mono_wasm_load_bytes_into_heap } from "./memory"; import { endMeasure, MeasuredBlock, startMeasure } from "./profiler"; import { createPromiseController, PromiseAndController } from "./promise-controller"; @@ -122,9 +122,7 @@ export async function mono_download_assets(): Promise { countAndStartDownload(asset); } else { // Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download. - asset.pendingDownloadInternal = null as any; // GC - asset.pendingDownload = null as any; // GC - asset.buffer = null as any; // GC + cleanupAsset(asset); // tell the debugger it is loaded if (asset.behavior == "resource" || asset.behavior == "assembly" || asset.behavior == "pdb") { const url = resolve_path(asset, ""); @@ -147,9 +145,7 @@ export async function mono_download_assets(): Promise { const url = asset.pendingDownloadInternal!.url; mono_assert(asset.buffer && typeof asset.buffer === "object", "asset buffer must be array or buffer like"); const data = new Uint8Array(asset.buffer!); - asset.pendingDownloadInternal = null as any; // GC - asset.pendingDownload = null as any; // GC - asset.buffer = null as any; // GC + cleanupAsset(asset); // wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped await memorySnapshotSkippedOrDone.promise; @@ -158,9 +154,7 @@ export async function mono_download_assets(): Promise { } if (asset.behavior === "symbols") { await instantiate_symbols_asset(asset); - asset.pendingDownloadInternal = null as any; // GC - asset.pendingDownload = null as any; // GC - asset.buffer = null as any; // GC + cleanupAsset(asset); } } else { const headersOnly = skipBufferByAssetTypes[asset.behavior]; @@ -531,17 +525,8 @@ export async function instantiate_symbols_asset( ): Promise { try { const response = await pendingAsset.pendingDownloadInternal!.response; - const data = await response.text(); - data.split(/[\r\n]/).forEach((line: string) => { - const parts: string[] = line.split(/:/); - if (parts.length < 2) - return; - - parts[1] = parts.splice(1).join(":"); - wasm_func_map.set(Number(parts[0]), parts[1]); - }); - if (runtimeHelpers.diagnosticTracing) console.debug(`MONO_WASM: Loaded ${wasm_func_map.size} symbols`); - + const text = await response.text(); + parseSymbolMapFile(text); } catch (error: any) { console.log(`MONO_WASM: Error loading symbol file ${pendingAsset.name}: ${JSON.stringify(error)}`); } @@ -611,3 +596,10 @@ export async function wait_for_all_assets() { export function mono_wasm_get_loaded_files(): string[] { return runtimeHelpers.loadedFiles; } + +export function cleanupAsset(asset: AssetEntryInternal) { + // give GC change to collect resources + asset.pendingDownloadInternal = null as any; // GC + asset.pendingDownload = null as any; // GC + asset.buffer = null as any; // GC +} \ No newline at end of file diff --git a/src/mono/wasm/runtime/logging.ts b/src/mono/wasm/runtime/logging.ts index eddd079a02cb1..a4420b01f57f5 100644 --- a/src/mono/wasm/runtime/logging.ts +++ b/src/mono/wasm/runtime/logging.ts @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -import { INTERNAL, Module } from "./imports"; +import { INTERNAL, Module, runtimeHelpers } from "./imports"; import { CharPtr, VoidPtr } from "./types/emscripten"; export const wasm_func_map = new Map(); @@ -181,3 +181,16 @@ export function setup_proxy_console(id: string, console: Console, origin: string for (const m of ["log", ...methods]) anyConsole[m] = proxyConsoleMethod(`console.${m}`, send, true); } + +export function parseSymbolMapFile(text: string) { + text.split(/[\r\n]/).forEach((line: string) => { + const parts: string[] = line.split(/:/); + if (parts.length < 2) + return; + + parts[1] = parts.splice(1).join(":"); + wasm_func_map.set(Number(parts[0]), parts[1]); + }); + + if (runtimeHelpers.diagnosticTracing) console.debug(`MONO_WASM: Loaded ${wasm_func_map.size} symbols`); +} \ No newline at end of file From ef636bb8b1ffdf4e5abfe119ad9945d054a08c6e Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Wed, 15 Mar 2023 09:07:38 +0100 Subject: [PATCH 12/12] Update src/mono/wasm/runtime/assets.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marek FiĊĦera --- src/mono/wasm/runtime/assets.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/runtime/assets.ts b/src/mono/wasm/runtime/assets.ts index 3d77e14fccb8c..b829f03bd32d9 100644 --- a/src/mono/wasm/runtime/assets.ts +++ b/src/mono/wasm/runtime/assets.ts @@ -598,7 +598,7 @@ export function mono_wasm_get_loaded_files(): string[] { } export function cleanupAsset(asset: AssetEntryInternal) { - // give GC change to collect resources + // give GC chance to collect resources asset.pendingDownloadInternal = null as any; // GC asset.pendingDownload = null as any; // GC asset.buffer = null as any; // GC