Skip to content

Wasm UnmanagedCallersOnly doesn't work for >8 arguments #109338

Closed

Description

@RReverser

Description

We're using DllImport / UnmanagedCallersOnly to build Wasm libraries for a custom host API.

I noticed that some of our functions crash when invoked as Wasm exports, and through further experimentation with .NET 9 I finally pinpointed it to the number of arguments - any function with <= 8 arguments works fine with UnmanagedCallersOnly but those with 9+ crash.

Reproduction Steps

I'm having trouble extracting minimal config since we use a build that stubs out all the WASI imports, but I imagine the config used for https://github.com/dotnet/runtime/blob/9873d40043d3739e4b7efa99a4dbd47a8922603b/src/mono/wasi/testassets/Native.cs should reproduce the issue in exactly the same way.

As for the code, a minimal repro is:

using System.Runtime.InteropServices;

public static class Methods
{
    [UnmanagedCallersOnly(EntryPoint = "test8")]
    public static void Test8(uint a1, uint a2, uint a3, uint a4, uint a5, uint a6, uint a7, uint a8) {}

    [UnmanagedCallersOnly(EntryPoint = "test9")]
    public static void Test9(uint a1, uint a2, uint a3, uint a4, uint a5, uint a6, uint a7, uint a8, uint a9) {}
}

This is a sample script I'm using when WASI imports are stubbed out:

var bin = require('fs').readFileSync(
  `bin/Release/net9.0/AppBundle/StdbModule.wasm`
);
var mod = new WebAssembly.Module(bin);
var inst = new WebAssembly.Instance(mod, {});
console.log('Test 8:');
console.log(inst.exports.test8());
console.log('Test 9:');
console.log(inst.exports.test9());

Expected behavior

When invoked with 0, 0, 0, ..., both functions execute successfully.

Actual behavior

> node test
Test 8:
undefined
Test 9:
wasm://wasm/01610672:1


RuntimeError: null function or function signature mismatch
    at wasm://wasm/01610672:wasm-function[7]:0x5be3
    at Object.<anonymous> (D:\testwasm\test.js:9:26)
    at Module._compile (node:internal/modules/cjs/loader:1568:14)
    at Object..js (node:internal/modules/cjs/loader:1711:10)
    at Module.load (node:internal/modules/cjs/loader:1328:32)
    at Function._load (node:internal/modules/cjs/loader:1138:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49

Regression?

No response

Known Workarounds

No response

Configuration

.NET 9.0.0-rc.2.24473.5

Other information

No response

Activity

  1. ghost added
    needs-area-label
    on Oct 29, 2024
  2. added
    untriaged
    on Oct 29, 2024
  3. added
    arch-wasm
    on Oct 29, 2024
  4. dotnet-policy-service commented on Oct 29, 2024

    @dotnet-policy-service
    Contributor

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

  5. added and removed
    needs-area-label
    on Oct 29, 2024
  6. dotnet-policy-service commented on Oct 29, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @steveisok, @lambdageek
    See info in area-owners.md if you want to be subscribed.

  7. RReverser commented on Oct 29, 2024

    @RReverser
    Author

    Known Workarounds

    Looks like, as a temporary workaround, I can at least add a custom wrapper on the C side that collects all arguments into a struct, passes that struct by reference to the actual C# method, and exports itself instead:

    typedef struct {
      int32_t a1;
      int32_t a2;
      int32_t a3;
      int32_t a4;
      int32_t a5;
      int32_t a6;
      int32_t a7;
      int32_t a8;
      int32_t a9;
    } Args9;
    
    extern int32_t test9_impl(const Args9* args);
    
    __attribute__((export_name("test9")))
    void test9(int32_t a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5, int32_t a6, int32_t a7, int32_t a8, int32_t a9) {
      Args9 args = { a1, a2, a3, a4, a5, a6, a7, a8, a9 };
      test9_impl(&args);
    }
    [StructLayout(LayoutKind.Sequential)]
    public ref struct Args9
    {
        public uint a1;
        public uint a2;
        public uint a3;
        public uint a4;
        public uint a5;
        public uint a6;
        public uint a7;
        public uint a8;
        public uint a9;
    }
    
    [UnmanagedCallersOnly(EntryPoint = "test9_impl")]
    public static unsafe void Test9(Args9* args9) {}

    This approach works when C is linked via <NativeLibrary>, but, of course this is pretty noisy and brittle.

    As a side-note: why do I have to use Args9* on C# side? When I accept the struct itself, I'm getting stack overflow inside Wasm, even though from the Wasm C ABI standpoint both usages should be equivalent (it mandates that structs with >1 item are always passed by reference).

  8. added
    os-wasi
    on Nov 4, 2024
  9. added this to the 10.0.0 milestone on Nov 4, 2024
  10. removed
    untriaged
    on Nov 4, 2024
  11. modified the milestones: 10.0.0, Future on Feb 19, 2025
  12. added a commit that references this issue on Jul 20, 2026
    c39c3f3
  13. locked and limited conversation to collaborators on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-wasmarea-Codegen-meta-monoos-wasi

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions