dotnet / runtime

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

UnmanagedCallersOnly methods crash at runtime without relink #106529

Open SingleAccretion opened 1 month ago

SingleAccretion commented 1 month ago

Reproduction:

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.JavaScript;

public unsafe class Program
{
    public static void Main()
    {
        ((delegate* unmanaged<int, void>)&RpiContext.NativeToManagedMethod)(42);
    }
}

static class RpiContext
{
    [UnmanagedCallersOnly]
    public static void NativeToManagedMethod(int context)
    {
        Console.WriteLine($"NativeToManagedMethod called with: {context}");
    }
}
> dotnet run
MONO_WASM: null function or function signature mismatch
RuntimeError: null function or function signature mismatch
    at wasm://wasm/00b598c2:wasm-function[290]:0x1ef46
    at wasm://wasm/00b598c2:wasm-function[221]:0x1c652
    at wasm://wasm/00b598c2:wasm-function[212]:0xebf4
    at wasm://wasm/00b598c2:wasm-function[249]:0x1d1cd
    at wasm://wasm/00b598c2:wasm-function[3228]:0xf0b77
    at wasm://wasm/00b598c2:wasm-function[2549]:0xc66e2
    at wasm://wasm/00b598c2:wasm-function[2548]:0xc6672
    at wasm://wasm/00b598c2:wasm-function[1907]:0xa0d44
    at wasm://wasm/00b598c2:wasm-function[290]:0x1efee
    at wasm://wasm/00b598c2:wasm-function[221]:0x1c652
; Add <WasmNativeStrip>false</WasmNativeStrip> to the project file to trigger relink
dotnet run
NativeToManagedMethod called with: 42
dotnet-policy-service[bot] commented 1 month ago

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

lewing commented 2 weeks ago

The issue is here is compiling code with a UCO basically means WasmBuildNative must be true but we'd need something like a compat analyzer to detect this because tasks that could detect if also require WasmBuildNavtive=true (and the workload) to run.

maraf commented 1 week ago

Could we use Roslyn source generator to output something that we would read in MSBuild and set WasmBuildNative=true accordingly?