Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser] new EmccEnableAssertions and EmccEnvironment MSBuild props #82954

Merged
merged 16 commits into from
Mar 15, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<EnableAggressiveTrimming>true</EnableAggressiveTrimming>
<PublishTrimmed>true</PublishTrimmed>
<WasmEnableWebcil>true</WasmEnableWebcil>
<WasmEmitSymbolMap>true</WasmEmitSymbolMap>
<EmccEnableAssertions>true</EmccEnableAssertions>
<EmccEnvironment>web</EmccEnvironment>
<!-- add OpenGL emulation -->
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
Expand Down
2 changes: 0 additions & 2 deletions src/mono/wasi/build/WasiApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@
<!--<_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a</_WasmEHLib>-->
<!--<_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-js.a</_WasmEHLibToExclude>-->
<!--<_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-wasm.a</_WasmEHLibToExclude>-->
<!--<_EmccExportedRuntimeMethods>&quot;[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedRuntimeMethods>-->
<!--<_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',')</_EmccExportedFunctions>-->
<!--</PropertyGroup>-->
<!--<ItemGroup>-->
<!--[> order matters <]-->
Expand Down
8 changes: 5 additions & 3 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@

<_WasmDevel Condition="'$(_WasmDevel)' == '' and '$(WasmBuildNative)' == 'true' and '$(Configuration)' == 'Debug'">true</_WasmDevel>

<_EmccAssertionLevelDefault Condition="'$(_EmccAssertionLevelDefault)' == ''">0</_EmccAssertionLevelDefault>
<_EmccOptimizationFlagDefault Condition="'$(_WasmDevel)' == 'true'">-O0</_EmccOptimizationFlagDefault>
<_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == '' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">-O1</_EmccOptimizationFlagDefault>
<_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == ''">-Oz</_EmccOptimizationFlagDefault>
Expand Down Expand Up @@ -209,7 +208,6 @@

<!-- Adding optimization flag at the top, so it gets precedence -->
<_EmccCFlags Include="$(EmccCompileOptimizationFlag)" />
<_EmccCFlags Include="-s ASSERTIONS=$(_EmccAssertionLevelDefault)" Condition="'$(_WasmDevel)' == 'true'" />
<_EmccCFlags Include="@(_EmccCommonFlags)" />

<_EmccCFlags Include="-DDISABLE_PERFTRACING_LISTEN_PORTS=1" />
Expand All @@ -227,7 +225,6 @@

<!-- Adding optimization flag at the top, so it gets precedence -->
<_EmccLDFlags Include="$(EmccLinkOptimizationFlag)" />
<_EmccLDFlags Include="-s ASSERTIONS=$(_EmccAssertionLevelDefault)" Condition="'$(_WasmDevel)' == 'true'" />
<_EmccLDFlags Include="@(_EmccCommonFlags)" />

<_DriverCDependencies Include="$(_WasmPInvokeHPath);$(_WasmICallTablePath)" />
Expand Down Expand Up @@ -416,14 +413,18 @@
<_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a</_WasmEHLib>
<_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-js.a</_WasmEHLibToExclude>
<_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-wasm.a</_WasmEHLibToExclude>
<_EmccExportedLibraryFunction>&quot;[@(EmccExportedLibraryFunction -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedLibraryFunction>
<_EmccExportedRuntimeMethods>&quot;[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedRuntimeMethods>
<_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',')</_EmccExportedFunctions>
</PropertyGroup>
<ItemGroup>
<!-- order matters -->
<!-- some flags will be duplicated on the commandline and in the .rsp file. The last wins. -->
<_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" />
<_EmccLDSFlags Include="-s STACK_SIZE=$(EmccStackSize)" />
<_EmccLDSFlags Include="-s WASM_BIGINT=1" />
<_EmccLDSFlags Condition="'$(EmccEnvironment)' != ''" Include="-s ENVIRONMENT=&quot;$(EmccEnvironment)&quot;" />
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
<_EmccLDSFlags Condition="'$(EmccEnableAssertions)' == 'true'" Include="-s ASSERTIONS=1" />

<_WasmNativeFileForLinking Include="%(_BitcodeFile.ObjectFile)" />
<_WasmNativeFileForLinking Include="%(_WasmSourceFileToCompile.ObjectFile)" />
Expand All @@ -449,6 +450,7 @@
<_EmccLinkStepArgs Include="-o &quot;$(_WasmIntermediateOutputPath)dotnet.js&quot;" />
<_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)" />

Expand Down
4 changes: 3 additions & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -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
- $(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.
(previously named EmccTotalMemory, which is still kept as an alias)
Expand Down Expand Up @@ -90,6 +92,7 @@
<WasmExtraConfig Include="string_val" Value="&quot;abc&quot;" />
<WasmExtraConfig Include="string_with_json" Value="&quot;{ &quot;abc&quot;: 4 }&quot;" />
- @(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
-->

Expand Down Expand Up @@ -341,7 +344,6 @@
Condition="'$(WasmEmitSymbolMap)' == 'true' and
'$(_HasDotnetJsSymbols)' != 'true' and
Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js.symbols')" />
<WasmFilesToIncludeInFileSystem Include="@(WasmNativeAsset)" Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js.symbols'" />
</ItemGroup>

<ItemGroup Condition="'$(InvariantGlobalization)' != 'true'">
Expand Down
35 changes: 29 additions & 6 deletions src/mono/wasm/runtime/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { parseSymbolMapFile } from "./logging";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
Expand Down Expand Up @@ -36,6 +37,7 @@ const skipBufferByAssetTypes: {
[k: string]: boolean
} = {
"dotnetwasm": true,
"symbols": true,
};

const containedInSnapshotByAssetTypes: {
Expand All @@ -57,6 +59,7 @@ const skipInstantiateByAssetTypes: {
} = {
"js-module-threads": true,
"dotnetwasm": true,
"symbols": true,
};

export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
Expand Down Expand Up @@ -119,9 +122,7 @@ export async function mono_download_assets(): Promise<void> {
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, "");
Expand All @@ -144,15 +145,17 @@ export async function mono_download_assets(): Promise<void> {
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;
await beforeOnRuntimeInitialized.promise;
_instantiate_asset(asset, url, data);
}
if (asset.behavior === "symbols") {
await instantiate_symbols_asset(asset);
cleanupAsset(asset);
}
} else {
const headersOnly = skipBufferByAssetTypes[asset.behavior];
if (!headersOnly) {
Expand Down Expand Up @@ -415,6 +418,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":
Expand Down Expand Up @@ -516,6 +520,18 @@ export async function instantiate_wasm_asset(
successCallback(compiledInstance, compiledModule);
}

export async function instantiate_symbols_asset(
pendingAsset: AssetEntryInternal,
): Promise<void> {
try {
const response = await pendingAsset.pendingDownloadInternal!.response;
const text = await response.text();
parseSymbolMapFile(text);
} 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)
Expand Down Expand Up @@ -580,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 chance to collect resources
asset.pendingDownloadInternal = null as any; // GC
asset.pendingDownload = null as any; // GC
asset.buffer = null as any; // GC
}
3 changes: 1 addition & 2 deletions src/mono/wasm/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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;
addFunction(fn: Function, signature: string): number;
stackSave(): VoidPtr;
stackRestore(stack: VoidPtr): void;
Expand Down Expand Up @@ -178,7 +177,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";
Expand Down
5 changes: 3 additions & 2 deletions src/mono/wasm/runtime/jiterpreter-jit-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class TrampolineInfo {
}
}

// this is cached replacements for Module.getWasmTableEntry();
// we could add <EmccExportedLibraryFunction Include="$getWasmTableEntry" /> and <EmccExportedRuntimeMethod Include="getWasmTableEntry" />
// if we need to export the original
function getWasmTableEntry (index: number) {
let result = fnCache[index];
if (!result) {
Expand All @@ -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 = <Function>Module.getWasmTableEntry(thunkIndex);
const thunk = <Function>getWasmTableEntry(thunkIndex);
try {
thunk(ret_sp, sp, ftndesc, thrown);
Expand Down
43 changes: 13 additions & 30 deletions src/mono/wasm/runtime/logging.ts
Original file line number Diff line number Diff line change
@@ -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 { CharPtr, VoidPtr } from "./types/emscripten";

const wasm_func_map = new Map<number, string>();
export const wasm_func_map = new Map<number, string>();
const regexes: any[] = [];

// V8
Expand Down Expand Up @@ -183,31 +182,15 @@ export function setup_proxy_console(id: string, console: Console, origin: string
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;
}
}
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`);
}
2 changes: 0 additions & 2 deletions src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { init_managed_exports } from "./managed-exports";
import { cwraps_internal } from "./exports-internal";
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 { readSymbolMapFile } from "./logging";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { preAllocatePThreadWorkerPool, instantiateWasmPThreadWorkerPool } from "./pthreads/browser";
import { export_linker } from "./exports-linker";
Expand Down Expand Up @@ -267,7 +266,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
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,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 symbols for the wasm native code

export type RuntimeHelpers = {
runtime_interop_module: MonoAssembly;
Expand All @@ -216,7 +217,6 @@ export type RuntimeHelpers = {
_i52_error_scratch_buffer: Int32Ptr;
mono_wasm_runtime_is_ready: boolean;
mono_wasm_bindings_is_ready: boolean;
mono_wasm_symbols_are_ready: boolean;

loaded_files: string[];
maxParallelDownloads: number;
Expand Down
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/types/emscripten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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;
addFunction(fn: Function, signature: string): number;
stackSave(): VoidPtr;
stackRestore(stack: VoidPtr): void;
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/wasm.proj
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@
<EmccExportedRuntimeMethod Include="FS_createDataFile" />
<EmccExportedRuntimeMethod Include="removeRunDependency" />
<EmccExportedRuntimeMethod Include="addRunDependency" />
<EmccExportedRuntimeMethod Include="FS_readFile" />
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
<EmccExportedRuntimeMethod Include="addFunction" />
<EmccExportedRuntimeMethod Include="getWasmTableEntry" />

<EmccExportedFunction Include="_malloc" />
<EmccExportedFunction Include="_memalign" />
Expand Down Expand Up @@ -272,6 +270,7 @@
<EmccExportedFunction Include="_free" />
</ItemGroup>
<PropertyGroup>
<_EmccExportedLibraryFunction>&quot;[@(EmccExportedLibraryFunction -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedLibraryFunction>
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
<_EmccExportedRuntimeMethods>&quot;[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedRuntimeMethods>
<_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',')</_EmccExportedFunctions>
<EmccInitialHeapSize>16777216</EmccInitialHeapSize>
Expand All @@ -290,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" />
Expand Down Expand Up @@ -375,7 +375,7 @@
<CMakeConfigurationEmccFlags Condition="'$(Configuration)' == 'Debug'">-g -Os -s -DDEBUG=1 -DENABLE_AOT_PROFILER=1 -DENABLE_BROWSER_PROFILER=1</CMakeConfigurationEmccFlags>
<CMakeConfigurationEmccFlags Condition="'$(Configuration)' == 'Release'">-Oz -DENABLE_BROWSER_PROFILER=1</CMakeConfigurationEmccFlags>

<CMakeConfigurationLinkFlags Condition="'$(Configuration)' == 'Debug'" >$(CMakeConfigurationEmccFlags)</CMakeConfigurationLinkFlags>
<CMakeConfigurationLinkFlags Condition="'$(Configuration)' == 'Debug'" >$(CMakeConfigurationEmccFlags) -s ASSERTIONS=1 </CMakeConfigurationLinkFlags>
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
<CMakeConfigurationLinkFlags Condition="'$(Configuration)' == 'Release'">-O2</CMakeConfigurationLinkFlags>

<CMakeConfigurationLinkFlags>$(CMakeConfigurationLinkFlags) -s EXPORT_ES6=1</CMakeConfigurationLinkFlags>
Expand Down
Loading