Skip to content

[Wasi]Wasm export functions with name space cannot be invoked correctly #106310

Closed

Description

@ReneeGA2020

Description

If I put a wasm export function into a name space, it allows me to build, but it cannot be invoked correctly.

Reproduction Steps

Let‘s’ take the native interop sample as example: https://github.com/dotnet/runtime/tree/main/src/mono/sample/wasi/native

This works and can be successfully invoked by outside code via name 'ManagedFunc'.

    public static class A
    {

        [UnmanagedCallersOnly(EntryPoint = "ManagedFunc")]
        public static int MyExport(int number)
        {
            // called from MyImport aka UnmanagedFunc
            Console.WriteLine($"MyExport({number}) -> 42");
            return 42;
        }
    }

but if I give it a namespace, then run the code, wasmtime gave me an error.

namespace ATest
    public static class A
    {

        [UnmanagedCallersOnly(EntryPoint = "ManagedFunc")]
        public static int MyExport(int number)
        {
            // called from MyImport aka UnmanagedFunc
            Console.WriteLine($"MyExport({number}) -> 42");
            return 42;
        }
    }
}

Expected behavior

The code run correctly.

Actual behavior

Error: failed to run main module dotnet.wasm

Caused by:
0: failed to invoke command default
1: error while executing at wasm backtrace:
0: 0x2884e1 - dotnet.wasm!abort
1: 0x28a2b5 - dotnet.wasm!__assert_fail
2: 0x8230 - mono_wasm_marshal_get_managed_wrapper
at C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Runtime.Mono.wasi-wasm\9.0.0-preview.6.24327.7\runtimes\wasi-wasm\native\src\runtime.c:380:2
3: 0x69e2 - ManagedFunc
at E:/Source/Repos/EventTest/EventTest/E:/source/repos/EventTest/EventTest/obj/Debug/net9.0/wasi-wasm/wasm/for-build/pinvoke-table.h:470:4
4: 0x283c44 - UnmanagedFunc
at E:/Source/Repos/EventTest/EventTest/local.c:9:11
5: 0x1e96e - do_icall
at /__w/1/s/src/mono/mono/mini/interp/interp.c:2356:10
6: 0x1d316 - do_icall_wrapper
at /__w/1/s/src/mono/mono/mini/interp/interp.c:2466:3
7: 0xd463 - mono_interp_exec_method
at /__w/1/s/src/mono/mono/mini/interp/interp.c:4226:4
8: 0xc0ef - interp_runtime_invoke
at /__w/1/s/src/mono/mono/mini/interp/interp.c:2215:2
9: 0x135c9f - mono_jit_runtime_invoke
at /__w/1/s/src/mono/mono/mini/mini-runtime.c:3485:10
10: 0xf353e - do_runtime_invoke
at /__w/1/s/src/mono/mono/metadata/object.c:2572:11
11: 0xf3fac - mono_runtime_try_invoke
at /__w/1/s/src/mono/mono/metadata/object.c:2729:9
12: 0xf88d8 - do_try_exec_main
at /__w/1/s/src/mono/mono/metadata/object.c:4753:3
13: 0xf8343 - mono_runtime_try_exec_main
at /__w/1/s/src/mono/mono/metadata/object.c:4821:9 - mono_runtime_run_main
at /__w/1/s/src/mono/mono/metadata/object.c:4343:9
14: 0xa99a - __main_argc_argv
at C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Runtime.Mono.wasi-wasm\9.0.0-preview.6.24327.7\runtimes\wasi-wasm\native\src\main.c:107:12
15: 0x288085 - dotnet.wasm!__main_void
16: 0x6312 - dotnet.wasm!_start
2: wasm trap: wasm unreachable instruction executed

Regression?

No response

Known Workarounds

Don't put export functions into a namespace

Configuration

No response

Other information

Now, I'm not quite sure if it's a feature or a bug.
Perhaps wasm export functions with namespace actually works, and I just need to change the way the local.c file invoke them? I missed a prefix somehow?

Activity

  1. ghost added
    needs-area-label
    on Aug 13, 2024
  2. added
    untriaged
    on Aug 13, 2024
  3. added and removed
    needs-area-label
    on Aug 13, 2024
  4. changed the title [-]Wasm export functions with name space cannot be invoked correctly?[/-] [+][Wasi]Wasm export functions with name space cannot be invoked correctly[/+] on Aug 14, 2024
  5. ReneeGA2020 commented on Aug 14, 2024

    @ReneeGA2020
    Author

    I guess this is not much of area JS interop, as it's more of a wasi interprop issue... Changed the title to
    include a [wasi] tag.

    It looks like the generated pinvoke table does not have correct a namespace parameter, as the namespace was conjoined into the type parameter.

  6. lewing commented on Aug 14, 2024

    @lewing
    Member

    @mkhamoyan when the entry point is defined we need to use it without the namespace, lets discuss

  7. added this to the 9.0.0 milestone on Aug 14, 2024
  8. added
    arch-wasm
    and removed
    untriaged
    on Aug 14, 2024
  9. dotnet-policy-service commented on Aug 14, 2024

    @dotnet-policy-service
    Contributor

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

  10. added
    os-wasi
    and removed on Aug 15, 2024
  11. pavelsavara commented on Aug 19, 2024

    @pavelsavara
    Member

    What changed recently is that I enabled <TrimMode>full</TrimMode> for WASI.
    Which for some reason trims also UnmanagedCallersOnly methods.

    If I add ILLink.Descriptors.xml to protect the method the sample works fine.

    After that namespace change works well for me too.

  12. added
    in-pr
    on Aug 19, 2024
  13. modified the milestones: 9.0.0, 10.0.0 on Aug 21, 2024
  14. ReneeGA2020 commented on Aug 21, 2024

    @ReneeGA2020
    Author

    What changed recently is that I enabled <TrimMode>full</TrimMode> for WASI. Which for some reason trims also UnmanagedCallersOnly methods.

    If I add ILLink.Descriptors.xml to protect the method the sample works fine.

    After that namespace change works well for me too.

    Thanks for the information, <TrimMode>partial</TrimMode> works for me most of the time. Sometimes the generated appbundle folder does not have a icudt.dat file, which cased execution fail.

    I copy icudt.dat from other appbundle to the ones that miss the file, and it seems work fine. But I'm not sure if it's the correct workarounds?

    Also may I ask an additional question? I guess the wasi C# still does not support any kind of break point debug, does it? Will such a support be planned in the future?

  15. pavelsavara commented on Aug 21, 2024

    @pavelsavara
    Member

    Sometimes the generated appbundle folder does not have a icudt.dat file, which cased execution fail.

    Separate issue for that, steps to reproduce and binlog would help.

    Also may I ask an additional question? I guess the wasi C# still does not support any kind of break point debug, does it? Will such a support be planned in the future?

    WASI is very immature, unsupported experiment for us at the moment. There are many gaps and this is one of them. We had moment where C# debugging worked, but currently sockets need to be fixed.

  16. lewing commented on Aug 23, 2024

    @lewing
    Member

    Sometimes the generated appbundle folder does not have a icudt.dat file, which cased execution fail.

    Separate issue for that, steps to reproduce and binlog would help.

    This might be a problem with the incremental build if you just changed InvariantGlobalization between builds?

  17. enghch commented on Sep 23, 2024

    @enghch

    @pavelsavara @lewing I apologize for hijacking this conversation. Can you point me to where I might ask about getting SkiaSharp (which includes a native libskiasharp library) working in a Wasi Console app? It compiles just fine, but at runtime complains with a DllNotFoundException for the native library. I tried adding the Skia.NativeAssets.WebAssembly package, but I don't think it has targets for Wasi wasm.

  18. lewing commented on Sep 24, 2024

    @lewing
    Member

    @pavelsavara @lewing I apologize for hijacking this conversation. Can you point me to where I might ask about getting SkiaSharp (which includes a native libskiasharp library) working in a Wasi Console app? It compiles just fine, but at runtime complains with a DllNotFoundException for the native library. I tried adding the Skia.NativeAssets.WebAssembly package, but I don't think it has targets for Wasi wasm.

    that is something we should discuss at https://github.com/mono/SkiaSharp/issues

  19. enghch commented on Sep 24, 2024

    @enghch

    @pavelsavara @lewing OK! I created this issue over there: mono/SkiaSharp#3015

  20. locked and limited conversation to collaborators on Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-Interop-monoin-pros-wasi

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions