Skip to content

Commit

Permalink
[wasi] Support builds with no relinking (#81648)
Browse files Browse the repository at this point in the history
* [wasi] support builds with no relinking

* wasi: update main() to work for no relinking

* wasi: fix command line for tests

* Bring back the workaround for xharness, as the fix hasn't been merged in runtime yet

* [wasi] Ignore test failures for runtime, and runtime-extra-platforms pipelineso

.. as the wasi library tests are unstable right now, and would block
mainline work.

- But when run as part of a wasm specific pipeline like `runtime-wasm`,
  then don't ignore the test failures
  • Loading branch information
radical authored Feb 6, 2023
1 parent 3f8df08 commit 9dd3bc8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ jobs:
isWasmOnlyBuild: ${{ parameters.isWasmOnlyBuild }}
alwaysRun: ${{ parameters.isWasmOnlyBuild }}

# Wasi
# Wasi - run only smoke tests by default
- template: /eng/pipelines/common/templates/wasm-library-tests.yml
parameters:
platforms:
Expand All @@ -186,6 +186,9 @@ jobs:
nameSuffix: '_Smoke'
extraBuildArgs: /p:EnableAggressiveTrimming=true /p:RunWasmSamples=true
shouldRunSmokeOnly: true
# ignore test failures for runtime-extra-platforms, but not when this
# is run as part of a wasm specific pipeline like runtime-wasm
shouldContinueOnError: ${{ not(parameters.isWasmOnlyBuild) }}
alwaysRun: ${{ variables.isRollingBuild }}
scenarios:
- normal
Expand Down
1 change: 1 addition & 0 deletions eng/pipelines/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ extends:
- wasi_wasm_win
nameSuffix: '_Smoke'
extraBuildArgs: /p:EnableAggressiveTrimming=true /p:RunWasmSamples=true
shouldContinueOnError: true
shouldRunSmokeOnly: true
alwaysRun: ${{ variables.isRollingBuild }}
scenarios:
Expand Down
8 changes: 5 additions & 3 deletions eng/testing/tests.wasi.targets
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-->
<WasmNativeStrip Condition="'$(WasmNativeStrip)' == ''">false</WasmNativeStrip>
<WasmEmitSymbolMap Condition="'$(WasmEmitSymbolMap)' == '' and '$(RunAOTCompilation)' != 'true'">true</WasmEmitSymbolMap>
<WasmSingleFileBundle>true</WasmSingleFileBundle>
<WasmSingleFileBundle Condition="'$(WasmSingleFileBundle)' == ''">false</WasmSingleFileBundle>

<WasmMainAssemblyFileName Condition="'$(WasmMainAssemblyFileName)' == ''">WasmTestRunner.dll</WasmMainAssemblyFileName>

Expand Down Expand Up @@ -61,9 +61,10 @@

<PropertyGroup>
<_AppArgs Condition="'$(WasmSingleFileBundle)' == 'true'">$([System.IO.Path]::GetFileNameWithoutExtension('$(WasmMainAssemblyFileName)')).wasm</_AppArgs>
<_AppArgs Condition="'$(WasmSingleFileBundle)' != 'true'">dotnet.wasm</_AppArgs>
<_AppArgs Condition="'$(WasmSingleFileBundle)' != 'true'">dotnet.wasm WasmTestRunner</_AppArgs>

<_AppArgs Condition="'$(IsFunctionalTest)' != 'true'">$(_AppArgs) $(AssemblyName).dll</_AppArgs>
<_AppArgs Condition="'$(IsFunctionalTest)' != 'true' and '$(WasmSingleFileBundle)' != 'true'">$(_AppArgs) managed/$(AssemblyName).dll</_AppArgs>
<_AppArgs Condition="'$(IsFunctionalTest)' != 'true' and '$(WasmSingleFileBundle)' == 'true'">$(_AppArgs) $(AssemblyName).dll</_AppArgs>
<_AppArgs Condition="'$(WasmTestAppArgs)' != ''">$(_AppArgs) -- $(WasmTestAppArgs)</_AppArgs>

<!-- FIXME: wasttime specific param name -->
Expand All @@ -78,6 +79,7 @@
<_XHarnessArgs Condition="'$(OS)' == 'Windows_NT' and '$(ContinuousIntegrationBuild)' != 'true'">$(_XHarnessArgs) --wasm-engine-path=$(WasmtimeDir)wasmtime.exe</_XHarnessArgs>
<_XHarnessArgs Condition="'$(OS)' == 'Windows_NT' and '$(ContinuousIntegrationBuild)' == 'true'">$(_XHarnessArgs) --wasm-engine-path=%HELIX_CORRELATION_PAYLOAD%\wasmtime\wasmtime.exe</_XHarnessArgs>

<_XHarnessArgs Condition="'$(WasmSingleFileBundle)' != 'true'" >$(_XHarnessArgs) --engine-arg=--dir=.</_XHarnessArgs>
<_XHarnessArgs Condition="'$(IsFunctionalTest)' == 'true'" >$(_XHarnessArgs) --expected-exit-code=$(ExpectedExitCode)</_XHarnessArgs>
<_XHarnessArgs Condition="'$(WasmXHarnessArgs)' != ''" >$(_XHarnessArgs) $(WasmXHarnessArgs)</_XHarnessArgs>
<_XHarnessArgs Condition="'$(_XHarnessTestsTimeout)' != '' " >$(_XHarnessArgs) &quot;--timeout=$(_XHarnessTestsTimeout)&quot;</_XHarnessArgs>
Expand Down
5 changes: 3 additions & 2 deletions src/mono/sample/wasi/wasi.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ endif
CONFIG?=Release

WASM_DEFAULT_BUILD_ARGS?=/p:TargetArchitecture=wasm /p:TargetOS=wasi /p:Configuration=$(CONFIG)
WASMTIME_PROV_PATH=$(realpath $(TOP)/artifacts/obj/wasmtime/wasmtime)
WASMTIME_PROV_DIR=$(realpath $(TOP)/artifacts/obj/wasmtime)
WASMTIME_PROV_PATH=${WASMTIME_PROV_DIR}/wasmtime

all: publish

Expand All @@ -23,4 +24,4 @@ clean:
rm -rf bin $(TOP)/artifacts/obj/mono/$(PROJECT_NAME:%.csproj=%)

run-console:
cd bin/wasi-wasm/AppBundle && $(WASMTIME_PROV_PATH) run $(PROJECT_NAME:.csproj=.wasm) $(ARGS)
cd bin/wasi-wasm/AppBundle && PATH=${WASMTIME_PROV_DIR}:${PATH} ./run-wasmtime.sh $(ARGS)
91 changes: 20 additions & 71 deletions src/mono/wasi/build/WasiApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<!-- if DebuggerSupport==true, then ensure that WasmDebugLevel isn't disabling debugging -->
<WasmDebugLevel Condition="('$(WasmDebugLevel)' == '' or '$(WasmDebugLevel)' == '0') and ('$(DebuggerSupport)' == 'true' or '$(Configuration)' == 'Debug')">-1</WasmDebugLevel>

<WasmSingleFileBundle Condition="'$(WasmSingleFileBundle)' == ''">true</WasmSingleFileBundle>
<WasmSingleFileBundle Condition="'$(WasmSingleFileBundle)' == ''">false</WasmSingleFileBundle>
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(WasmSingleFileBundle)' == 'true'">true</WasmBuildNative>

<WasiBundleAssemblies Condition="'$(WasiBundleAssemblies)' == ''">true</WasiBundleAssemblies>
Expand Down Expand Up @@ -317,29 +317,18 @@
</ItemGroup>
</Target>

<Target Name="_GetWasmGenerateAppBundleDependencies" />

<Target Name="_GetBrowserWasmGenerateAppBundleDependencies">
<Target Name="_GetWasiGenerateAppBundleDependencies">
<PropertyGroup>
<WasmIcuDataFileName Condition="'$(InvariantGlobalization)' != 'true'">icudt.dat</WasmIcuDataFileName>
<!--<WasmIcuDataFileName Condition="'$(InvariantGlobalization)' != 'true'">icudt.dat</WasmIcuDataFileName>-->

<_HasDotnetWasm Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.wasm'">true</_HasDotnetWasm>
<_HasDotnetJsWorker Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.worker.js'">true</_HasDotnetJsWorker>
<_HasDotnetJsSymbols Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js.symbols'">true</_HasDotnetJsSymbols>
<_HasDotnetJs Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js'">true</_HasDotnetJs>
</PropertyGroup>

<ItemGroup>
<!-- If dotnet.{wasm,js} weren't added already (eg. AOT can add them), then add the default ones -->
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.wasm" Condition="'$(_HasDotnetWasm)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js" Condition="'$(_HasDotnetJs)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.worker.js" Condition="'$(_HasDotnetJsWorker)' != 'true' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.worker.js')" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js.symbols"
Condition="'$(WasmEmitSymbolMap)' == 'true' and
'$(_HasDotnetJsSymbols)' != 'true' and
Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js.symbols')" />

<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(WasmIcuDataFileName)" Condition="'$(InvariantGlobalization)' != 'true'" />

<!--<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(WasmIcuDataFileName)" Condition="'$(InvariantGlobalization)' != 'true'" />-->
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.timezones.blat" />

<WasmFilesToIncludeInFileSystem Include="@(WasmNativeAsset)" Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js.symbols'" />
Expand All @@ -350,72 +339,32 @@
Inputs="@(_WasmAssembliesInternal);$(WasmIcuDataFileName);@(WasmNativeAsset)"
Outputs="$(WasmAppDir)\.stamp"
Condition="'$(WasmGenerateAppBundle)' == 'true'"
DependsOnTargets="_WasmGenerateRuntimeConfig;_GetWasmGenerateAppBundleDependencies" />

<Target Name="_BrowserWasmGenerateAppBundle"
Inputs="@(_WasmAssembliesInternal);$(WasmMainJSPath);$(WasmIcuDataFileName);@(WasmNativeAsset)"
Outputs="$(WasmAppDir)\.stamp"
Condition="'$(WasmGenerateAppBundle)' == 'true'"
DependsOnTargets="_WasmGenerateRuntimeConfig;_GetBrowserWasmGenerateAppBundleDependencies">
<Error Condition="'$(WasmMainJSPath)' == ''" Text="%24(WasmMainJSPath) property needs to be set" />
DependsOnTargets="_WasmGenerateRuntimeConfig;_GetWasiGenerateAppBundleDependencies;_WasiDefaultGenerateAppBundle;_GenerateRunWasmtimeScript" />

<PropertyGroup>
<_WasmAppIncludeThreadsWorker Condition="'$(WasmEnableThreads)' == 'true' or '$(WasmEnablePerfTracing)' == 'true'">true</_WasmAppIncludeThreadsWorker>
<!-- TODO: set this from some user-facing property? -1 means use the default baked into dotnet.js -->
<_WasmPThreadPoolSize Condition="'$(_WasmPThreadPoolSize)' == '' and ('$(WasmEnableThreads)' == 'true' or '$(WasmEnablePerfTracing)' == 'true')">-1</_WasmPThreadPoolSize>
</PropertyGroup>

<RemoveDir Directories="$(WasmAppDir)" />
<WasmAppBuilder
AppDir="$(WasmAppDir)"
MainJS="$(WasmMainJSPath)"
MainHTMLPath="$(WasmMainHTMLPath)"
Assemblies="@(_WasmAssembliesInternal)"
MainAssemblyName="$(WasmMainAssemblyFileName)"
HostConfigs="@(HostConfig)"
RuntimeArgsForHost="@(WasmMonoRuntimeArgs)"
DefaultHostConfig="$(DefaultWasmHostConfig)"
InvariantGlobalization="$(InvariantGlobalization)"
SatelliteAssemblies="@(_WasmSatelliteAssemblies)"
FilesToIncludeInFileSystem="@(WasmFilesToIncludeInFileSystem)"
IcuDataFileName="$(WasmIcuDataFileName)"
RemoteSources="@(WasmRemoteSources)"
ExtraFilesToDeploy="@(WasmExtraFilesToDeploy)"
ExtraConfig="@(WasmExtraConfig)"
NativeAssets="@(WasmNativeAsset)"
DebugLevel="$(WasmDebugLevel)"
IncludeThreadsWorker="$(_WasmAppIncludeThreadsWorker)"
PThreadPoolSize="$(_WasmPThreadPoolSize)"
>
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
</WasmAppBuilder>

<CallTarget Targets="_GenerateRunV8Script" Condition="'$(WasmGenerateRunV8Script)' == 'true'" />
<Message Text="Generated app bundle at $(WasmAppDir)" Importance="High" />

<Copy SourceFiles="@(_WasmAppHostFiles)" DestinationFiles="@(_WasmAppHostFiles->'$(WasmAppDir)\WasmAppHost\%(RecursiveDir)%(FileName)%(Extension)')" />

<WriteLinesToFile File="$(WasmAppDir)\.stamp" Lines="" Overwrite="true" />
<Target Name="_WasiDefaultGenerateAppBundle" Condition="'$(WasmSingleFileBundle)' != 'true'">
<!-- placeholder bundle generator -->
<Copy SourceFiles="@(_WasmAssembliesInternal)" DestinationFolder="$(WasmAppDir)\managed" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(WasmNativeAsset)" DestinationFolder="$(WasmAppDir)" SkipUnchangedFiles="true" />
</Target>

<Target Name="_GenerateRunV8Script">
<Target Name="_GenerateRunWasmtimeScript">
<PropertyGroup>
<WasmRunV8ScriptPath Condition="'$(WasmRunV8ScriptPath)' == ''">$(WasmAppDir)run-v8.sh</WasmRunV8ScriptPath>
<_WasmMainJSFileName>$([System.IO.Path]::GetFileName('$(WasmMainJSPath)'))</_WasmMainJSFileName>
<WasmRunWasmtimeScriptPath Condition="'$(WasmRunWasmtimeScriptPath)' == ''">$(WasmAppDir)run-wasmtime.sh</WasmRunWasmtimeScriptPath>
<_ScriptContent Condition="'$(WasmSingleFileBundle)' == 'true'">wasmtime run $([System.IO.Path]::GetFileNameWithoutExtension($(WasmMainAssemblyFileName))).wasm $*</_ScriptContent>
<_ScriptContent Condition="'$(WasmSingleFileBundle)' != 'true'">wasmtime run --dir . dotnet.wasm $([System.IO.Path]::GetFileNameWithoutExtension($(WasmMainAssemblyFileName))) $*</_ScriptContent>
</PropertyGroup>

<Error Condition="'$(WasmMainAssemblyFileName)' == ''" Text="%24(WasmMainAssemblyFileName) property needs to be set for generating $(WasmRunV8ScriptPath)." />
<Error Condition="'$(WasmMainAssemblyFileName)' == ''" Text="%24(WasmMainAssemblyFileName) property needs to be set for generating $(WasmRunWasmtimeScriptPath)." />
<WriteLinesToFile
File="$(WasmRunV8ScriptPath)"
Lines="v8 --expose_wasm $(_WasmMainJSFileName) -- ${RUNTIME_ARGS} --run $(WasmMainAssemblyFileName) $*"
Overwrite="true">
</WriteLinesToFile>
File="$(WasmRunWasmtimeScriptPath)"
Lines="$(_ScriptContent)"
Overwrite="true" />

<ItemGroup>
<FileWrites Include="$(WasmRunV8ScriptPath)" />
<FileWrites Include="$(WasmRunWasmtimeScriptPath)" />
</ItemGroup>

<Exec Condition="'$(OS)' != 'Windows_NT'" Command="chmod a+x $(WasmRunV8ScriptPath)" />
<Exec Condition="'$(OS)' != 'Windows_NT'" Command="chmod a+x $(WasmRunWasmtimeScriptPath)" />
</Target>

<Target Name="_WasmResolveReferences" Condition="'$(WasmResolveAssembliesBeforeBuild)' == 'true'">
Expand Down
26 changes: 17 additions & 9 deletions src/mono/wasi/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,21 +676,28 @@ mono_wasm_string_array_new (int size)
}

#ifdef _WASI_DEFAULT_MAIN
/*
* with wasmtime, this is run as:
* $ wasmtime run--dir . dotnet.wasm MainAssembly [args]
*
*
* arg0: dotnet.wasm
* arg1: MainAssembly
* arg2-..: args
*/
int main(int argc, char * argv[]) {
printf("TODOWASI: default main for non-relinked, non-aot apps, TODO\n");
if (argc < 2) {
printf("Error: First argument must be the name of the main assembly\n");
return 1;
}
// Assume the runtime pack has been copied into the output directory as 'runtime'
// Otherwise we have to mount an unrelated part of the filesystem within the WASM environment
// AJ: not needed right now as we are bundling all the assemblies in the .wasm
mono_set_assemblies_path(".:./runtime/native:./runtime/lib/net7.0");

mono_set_assemblies_path("managed");
mono_wasm_load_runtime("", 0);

MonoAssembly* assembly = mono_wasm_assembly_load (argv[1]);
const char *assembly_name = argv[1];
MonoAssembly* assembly = mono_wasm_assembly_load (assembly_name);
if (!assembly) {
printf("wasi: mono_wasm_assembly_load returned NULL!\n");
printf("Could not load assembly %s\n", assembly_name);
return 1;
}
MonoMethod* entry_method = mono_wasi_assembly_get_entry_point (assembly);
Expand All @@ -701,11 +708,12 @@ int main(int argc, char * argv[]) {

MonoObject* out_exc;
MonoObject* out_res;
int ret = mono_runtime_run_main(entry_method, argc, argv, &out_exc);
// Managed app will see: arg0: MainAssembly, arg1-.. [args]
int ret = mono_runtime_run_main(entry_method, argc - 1, &argv[1], &out_exc);
if (out_exc) {
mono_print_unhandled_exception(out_exc);
exit(1);
}
return ret;
return ret < 0 ? -ret : ret;
}
#endif
6 changes: 1 addition & 5 deletions src/mono/wasi/runtime/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ int main(int argc, char * argv[]) {
// This is supplied from the MSBuild itemgroup @(WasiAfterRuntimeLoaded)
WASI_AFTER_RUNTIME_LOADED_CALLS
#endif
// Assume the runtime pack has been copied into the output directory as 'runtime'
// Otherwise we have to mount an unrelated part of the filesystem within the WASM environment
// AJ: not needed right now as we are bundling all the assemblies in the .wasm
/*mono_set_assemblies_path(".:./runtime/native:./runtime/lib/net7.0");*/
mono_wasm_load_runtime("", 0);

const char* assembly_name = dotnet_wasi_getentrypointassemblyname();
Expand All @@ -44,5 +40,5 @@ int main(int argc, char * argv[]) {
mono_print_unhandled_exception(out_exc);
exit(1);
}
return ret;
return ret < 0 ? -ret : ret;
}

0 comments on commit 9dd3bc8

Please sign in to comment.