dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.73k stars 1.07k forks source link

Blazor WASM AOT segfault in wasm-opt on linux-musl-x64 when FSharp.Core is referenced #44933

Open zotanmew opened 1 day ago

zotanmew commented 1 day ago

Describe the bug

Publish fails at the wasm-opt stage of blazor WASM AOT compilation with error message:

emcc : error : '/usr/share/dotnet/packs/Microsoft.NET.Runtime.Emscripten.3.1.56.Sdk.linux-musl-x64/9.0.0/tools/bin/wasm-opt --strip-target-features --post-emscripten -O2 --low-memory-unused --zero-filled-memory --pass-arg=directize-initial-contents-immutable /test/iceshrimp.net/Iceshrimp.Frontend/obj/Release/net9.0/wasm/for-publish/dotnet.native.wasm -o /test/iceshrimp.net/Iceshrimp.Frontend/obj/Release/net9.0/wasm/for-publish/dotnet.native.wasm -g --mvp-features --enable-bulk-memory --enable-exception-handling --enable-multivalue --enable-mutable-globals --enable-reference-types --enable-sign-ext --enable-simd' failed (received SIGSEGV (-11))
    /usr/share/dotnet/packs/Microsoft.NET.Runtime.WebAssembly.Sdk/9.0.0/Sdk/BrowserWasmApp.targets(494,5): error MSB3073: The command "emcc "@/usr/share/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.browser-wasm/9.0.0/runtimes/browser-wasm/native/src/emcc-default.rsp" -msimd128 "@/usr/share/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.browser-wasm/9.0.0/runtimes/browser-wasm/native/src/emcc-link.rsp" "@/test/iceshrimp.net/Iceshrimp.Frontend/obj/Release/net9.0/wasm/for-publish/emcc-link.rsp"" exited with code 1.

Workaround: publish with -p:EmccLinkOptimizationFlag="-O0"

This problem does not occur on glibc-based linux-x64 systems.

Copying the dotnet.native.wasm from the failed build on a musl system to a glibc system & invoking wasm-opt manually also does not reproduce the issue.

To Reproduce

docker run --rm -it mcr.microsoft.com/dotnet/sdk:9.0-alpine
dotnet workload install wasm-tools
apk add python3
mkdir test
cd test
dotnet new blazorwasm
dotnet add package FSharp.Core
dotnet publish -p:RunAOTCompilation=true

To prove it works on glibc:

docker run --rm -it mcr.microsoft.com/dotnet/sdk:9.0
dotnet workload install wasm-tools
apt update && apt install python3 -y
mkdir test
cd test
dotnet new blazorwasm
dotnet add package FSharp.Core
dotnet publish -p:RunAOTCompilation=true

Further technical details

dotnet --info
.NET SDK:
 Version:           9.0.100
 Commit:            59db016f11
 Workload version:  9.0.100-manifests.c6f19616
 MSBuild version:   17.12.7+5b8665660

Runtime Environment:
 OS Name:     alpine
 OS Version:  3.20
 OS Platform: Linux
 RID:         linux-musl-x64
 Base Path:   /usr/share/dotnet/sdk/9.0.100/

.NET workloads installed:
 [wasm-tools]
   Installation Source: SDK 9.0.100
   Manifest Version:    9.0.0/9.0.100
   Manifest Path:       /usr/share/dotnet/sdk-manifests/9.0.100/microsoft.net.workload.mono.toolchain.current/9.0.0/WorkloadManifest.json
   Install Type:        FileBased

Configured to use loose manifests when installing new manifests.

Host:
  Version:      9.0.0
  Architecture: x64
  Commit:       9d5a6a9aa4

.NET SDKs installed:
  9.0.100 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 9.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 9.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download
zotanmew commented 1 day ago

Problem seems to be stack size. Running https://github.com/yaegashi/muslstack on wasm-opt seems to solve the issue:

apk add go
go install github.com/yaegashi/muslstack@latest
~/go/bin/muslstack -s 0x800000 /usr/share/dotnet/packs/Microsoft.NET.Runtime.Emscripten.3.1.56.Sdk.linux-musl-x64/9.0.0/tools/bin/wasm-opt

# rerun publish
zotanmew commented 1 day ago

I've patched this in our downstream dotnet-sdk dockerfile: https://iceshrimp.dev/iceshrimp/Iceshrimp.NET/commit/1511692b1e7c113e2448c1eabe39848e4c7aab2d

Might be a good idea to build emscripten with a higher stack-size=, so this is not necessary anymore.