dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.21k stars 4.72k forks source link

[wasm] problem with native linking against skiasharp and emscripten 3.1.56 #109289

Open radekdoulik opened 6 hours ago

radekdoulik commented 6 hours ago

Application referencing skiasharp packages in Debug configuration are triggering these errors during runtime in chrome:

CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [error] Error in mono_download_assets: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [error] Error in mono_download_assets: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [ERROR] PageError: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [ERROR] PageError: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [ERROR] PageError: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [ERROR] PageError: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [ERROR] PageError: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [ERROR] PageError: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [ERROR] PageError: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
  [ERROR] PageError: Error: Failed to start platform. Reason: CompileError: WebAssembly.compileStreaming(): Compiling function #16745:"mono_ppdb_load_file" failed: expected 0 elements on the stack for fallthru, found 4 @+5989718
      at en (http://localhost:5191/_framework/blazor.webassembly.js:1:55760)

It is also reproducible on CI, https://github.com/dotnet/runtime/pull/109232 is now triggering this problem.

radekdoulik commented 6 hours ago

I tried various version of llvm to test assumption of this being related to the recent llvm bump to 19.1.0.

The problem exists on 19.1.0, 19.0.x alpha (same commit as emsdk) and even with 16.x after our emscripten bump to 3.1.56. So it looks unrelated.

radekdoulik commented 5 hours ago

Later I was trying to find which part of mono_ppdb_load_file might be causing issues.

When I removed code guarded by #ifndef DISABLE_EMBEDDED_PDB, the mono_ppdb_load_file wasn't causing problems anymore. Instead I am getting similar problem with

[error] MONO_WASM: instantiate_wasm_module() failed CompileError: WebAssembly.compileStreaming(): Compiling function #26259:"CompressionNative_InflateInit2_" failed: expected 1 elements on the stack for fallthru, found 4 @+7856472

Which lead me to the zlib linking, which I think is the issue here. What is happening here is that this line

int res = inflateInit2 (&stream, -15);

is being inlined and the same issue as with the CompressionNative_InflateInit2_ is occurring here. Later I found that the CompressionNative_Inflate has similar issue.

I wonder if we compile and/or link against two different zlib versions. I will dig deeper.

lewing commented 5 hours ago

cc @carlossanlop

dotnet-policy-service[bot] commented 5 hours ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 5 hours ago

Tagging subscribers to this area: @dotnet/area-system-io-compression See info in area-owners.md if you want to be subscribed.

radekdoulik commented 5 hours ago

The inflateInit2_ function looks like this

/* Function used by zlib.h and zlib-ng version 2.0 macros */
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
...
}

and there's redefining macro with only 2 parameters

#  define inflateInit2(strm, windowBits) \
... inflateInit2_ ...

and function with same parameters as the redefining macro

/* ===========================================================================
 * Initialize inflate state and buffers.
 * This function is hidden in ZLIB_COMPAT builds.
 */
int32_t ZNG_CONDEXPORT PREFIX(inflateInit2)(PREFIX3(stream) *strm, int32_t windowBits) {
...
}

in our .wasm binary we have

(func Cr_z_inflateInit2_(param $0 i32, $1 i32, $2 i32, $3 i32) (result i32))
(func inflateInit2_(param $0 i32, $1 i32) (result i32))

It looks like possibly wrong function was inlined, maybe wasm-ld or wasm-opt issue?