Skip to content

[mono] [wasi][wasm] Wasm AOT breaks wasm export functions #101276

Open

Description

@ReneeGA2020

Description

After a recent fix, wasm interpreter no longer require a unmanaged delegate as workaround to initialize exports. But it looks like the AOT version still get the uninitialized element error.

Reproduction Steps

  1. Clone the runtime https://github.com/dotnet/runtime/
  2. git pull with the leastest (was testing with a344abd of april)
  3. Follow the steps here to build wasi runtime https://github.com/dotnet/runtime/blob/main/src/mono/wasi/README.md
  4. Test the wasi native sample with RunAOTCompilation, the samaple features import and export functions to native:
  5. ./dotnet.cmd publish /p:RunAOTCompilation=true /p:TargetOS=wasi /p:Configuration=Release /t:RunSample src/mono/sample/wasi/native

Expected behavior

The console prints:

main: 0
UnmanagedFunc calling ManagedFunc
MyExport(123) -> 42
ManagedFunc returned 42

Actual behavior

The console prints:

main: 0
UnmanagedFunc calling ManagedFunc
EXEC : error : failed to run main module Wasi.Native.Sample.wasm [E:\runtime\src\mono\sample\wasi\native\Wasi.Native.
Sample.csproj]

Caused by:
0: failed to invoke command default
1: error while executing at wasm backtrace:
2: wasm trap: uninitialized element

Regression?

No response

Known Workarounds

No response

Configuration

NET9 + Windows + Wasmtime + WASI SDK 21
Tested with .NET 9 alpha local build with latest commit a344abd
Tested with .NET 9 Preview 3 installed from the official website

Other information

Directly export function to Wasmtime lead to the same issue.
It worked fine with no RunAOTCompilation

Activity

  1. ghost added
    needs-area-label
    on Apr 19, 2024
  2. added
    untriaged
    on Apr 19, 2024
  3. removed
    needs-area-label
    on Apr 19, 2024
  4. added this to the 9.0.0 milestone on Apr 25, 2024
  5. removed
    untriaged
    on Apr 25, 2024
  6. added
    os-wasi
    on Apr 25, 2024
  7. lewing commented on Apr 26, 2024

    @lewing
    Member
    main: 0
    UnmanagedFunc calling ManagedFunc
    Error: failed to run main module `.\testwasinative.wasm`
    
    Caused by:
        0: failed to invoke command default
        1: error while executing at wasm backtrace:
               0: 0xd956 - <unknown>!<wasm function 33>
               1: 0xca2d5e - <unknown>!<wasm function 17873>
               2: 0x5e3a9c - <unknown>!<wasm function 7396>
               3: 0x5e3944 - <unknown>!<wasm function 7394>
               4: 0x957886 - <unknown>!<wasm function 11235>
               5: 0x935521 - <unknown>!<wasm function 11156>
               6: 0xc7f0a9 - <unknown>!<wasm function 17326>
               7: 0xc7df17 - <unknown>!<wasm function 17306>
               8: 0xc44e0e - <unknown>!<wasm function 16337>
               9: 0xc4587c - <unknown>!<wasm function 16346>
              10: 0xc49dff - <unknown>!<wasm function 16415>
              11: 0xc498de - <unknown>!<wasm function 16412>
              12: 0x108b9 - <unknown>!<wasm function 80>
              13: 0xca6a39 - <unknown>!<wasm function 17901>
              14: 0xd6c5 - <unknown>!<wasm function 28>
        2: wasm trap: uninitialized element
    

    I can reproduce this. Works with trimming but no aot, does not work with /p:RunAotCompilation=true

  8. pavelsavara commented on Aug 19, 2024

    @pavelsavara
    Member

    I also have a repro

    Caused by:
    0: failed to invoke `run` function
    1: error while executing at wasm backtrace:
    0: 0x18a33 - ManagedFunc
                    at C:\Dev\runtime\artifacts\bin\Wasi.Build.Tests\Debug\net9.0\win-x64\wbt artifacts\Release_nsxhe3qu_k4m\C:/Dev/runtime/artifacts/bin/Wasi.Build.Tests/Debug/net9.0/win-x64/wbt artifacts/Release_nsxhe3qu_k4m/obj/Release/net9.0/wasi-wasm/wasm/for-publish\pinvoke-table.h:262:3
    1: 0x2fb8360 - UnmanagedFunc
                    at C:\Dev\runtime\artifacts\bin\Wasi.Build.Tests\Debug\net9.0\win-x64\wbt artifacts\Release_nsxhe3qu_k4m\native.c:9:11
    2: 0xb8b983 - .tmpnwLnie!aot_wrapper_Wasi_dot_Native_dot_Test_pinvoke_Wasi_dot_Native_dot_Test_Sample_Sample_dot_Test__MyImport_pinvoke_void_void_
    3: 0xb8b815 - .tmpnwLnie!Wasi_Native_Test_Sample_Test_Main_string__
    4: 0x2230d49 - .tmpnwLnie!aot_instances_aot_wrapper_gsharedvt_out_sig_pinvoke_i4_obj
    5: 0x220e21d - .tmpnwLnie!aot_instances_wrapper_runtime_invoke_object_runtime_invoke_sig_void_intptr_intptr_intptr_object_intptr_intptr_intptr
    6: 0x2de5908 - mono_llvmonly_runtime_invoke
                    at C:\Dev\runtime\artifacts\obj\mono\wasi.wasm.Debug\C:/Dev/runtime/src/mono/mono/mini\mini-runtime.c:3424:2
    7: 0x2de0938 - mono_jit_runtime_invoke
                    at C:\Dev\runtime\artifacts\obj\mono\wasi.wasm.Debug\C:/Dev/runtime/src/mono/mono/mini\mini-runtime.c:3688:12
    8: 0x2d1389c - do_runtime_invoke
                    at C:\Dev\runtime\artifacts\obj\mono\wasi.wasm.Debug\C:/Dev/runtime/src/mono/mono/metadata\object.c:2572:11
    9: 0x2d15a7d - mono_runtime_try_invoke
                    at C:\Dev\runtime\artifacts\obj\mono\wasi.wasm.Debug\C:/Dev/runtime/src/mono/mono/metadata\object.c:2729:9
    10: 0x2d2539f - do_try_exec_main
                    at C:\Dev\runtime\artifacts\obj\mono\wasi.wasm.Debug\C:/Dev/runtime/src/mono/mono/metadata\object.c:4739:9
    11: 0x2d24c14 - mono_runtime_try_exec_main
                    at C:\Dev\runtime\artifacts\obj\mono\wasi.wasm.Debug\C:/Dev/runtime/src/mono/mono/metadata\object.c:4821:9
    12: 0x2d23ec7 - mono_runtime_run_main
                    at C:\Dev\runtime\artifacts\obj\mono\wasi.wasm.Debug\C:/Dev/runtime/src/mono/mono/metadata\object.c:4343:9
    13: 0x1bf91 - __main_argc_argv
                    at C:\Dev\runtime\artifacts\bin\dotnet-latest\packs\Microsoft.NETCore.App.Runtime.Mono.wasi-wasm\9.0.0-dev\runtimes\wasi-wasm\native\src\main.c:108:12
    14: 0x2fbc571 - .tmpnwLnie!__main_void
    15: 0x1867c - _start
                    at C:\wasi-sdk\sysroot\wasi-libc-wasm32-wasip2\libc-bottom-half/crt\crt1-command.c:43:13
    16: 0x3d73efa - wit-component:adapter:wasi_snapshot_preview1!wasi:cli/[email protected]#run
    
    2: wasm trap: uninitialized element
    
  9. modified the milestones: 9.0.0, 10.0.0 on Aug 21, 2024
  10. modified the milestones: 10.0.0, Future on Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-Codegen-AOT-monoos-wasi

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions