Repository navigation
[WASM] callback fron native code to .Net: Parsing function pointer types in signatures is not supported #56145
Copy link
Copy link
Closed
Labels
arch-wasmWebAssembly architectureWebAssembly architecturearea-Build-monoarea-Interop-monoblockedIssue/PR is blocked on something - see commentsIssue/PR is blocked on something - see comments
Milestone
Description
I'm trying to do a call from .Net a native function which call backs to .Net. I'm using function pointers as suggested in discord channel, but I'm getting the following error (maybe related to #43791?):
1:7>Target "_GeneratePInvokeTable: (TargetId:229)" in file "C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets" from project "C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj" (target "_WasmBuildNativeCore" depends on it):
Added Item(s): _WasmPInvokeModules=libWasmNative
Added Item(s): _WasmPInvokeModules=libSystem.Native
Added Item(s): _WasmPInvokeModules=libSystem.IO.Compression.Native
Added Item(s): _WasmPInvokeModules=libSystem.Globalization.Native
Using "PInvokeTableGenerator" task from assembly "C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\..\tasks\net6.0\WasmAppBuilder.dll".
Task "PInvokeTableGenerator" (TaskId:151)
Task Parameter:
Assemblies=
C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\Wasm.Console.Sample.dll
C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Console.dll
C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Private.Runtime.InteropServices.JavaScript.dll
C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Private.Uri.dll
C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Runtime.CompilerServices.Unsafe.dll
C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Runtime.dll
C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Private.CoreLib.dll (TaskId:151)
Task Parameter:OutputPath=C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\wasm\pinvoke-table.h (TaskId:151)
Task Parameter:
Modules=
libWasmNative
libSystem.Native
libSystem.IO.Compression.Native
libSystem.Globalization.Native (TaskId:151)
Generating pinvoke table to 'C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\wasm\pinvoke-table.h'. (TaskId:151)
13:27:49.489 1:7>C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: The "PInvokeTableGenerator" task failed unexpectedly. [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: System.NotSupportedException: Parsing function pointer types in signatures is not supported. [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at System.Reflection.TypeLoading.Ecma.EcmaModule.GetFunctionPointerType(MethodSignature`1 signature) in System.Reflection.MetadataLoadContext.dll:token 0x6000619+0xa [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at System.Reflection.Metadata.Ecma335.SignatureDecoder`2.DecodeType(BlobReader& blobReader, Boolean allowTypeSpecifications, Int32 typeCode) in System.Reflection.Metadata.dll:token 0x6000abf+0x100 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at System.Reflection.Metadata.Ecma335.SignatureDecoder`2.DecodeMethodSignature(BlobReader& blobReader) in System.Reflection.Metadata.dll:token 0x6000ac1+0x5a [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at System.Reflection.TypeLoading.Ecma.EcmaMethodDecoder.SpecializeMethodSig(IRoMethodBase roMethodBase) in System.Reflection.MetadataLoadContext.dll:token 0x60005e8+0x0 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at System.Reflection.TypeLoading.RoDefinitionMethod`1.ComputeMethodSig() in System.Reflection.MetadataLoadContext.dll:token 0x6000245+0x7 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at System.Reflection.TypeLoading.RoMethod.get_MethodSig() in System.Reflection.MetadataLoadContext.dll:token 0x6000278+0xc [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at System.Reflection.TypeLoading.RoMethod.get_ReturnParameter() in System.Reflection.MetadataLoadContext.dll:token 0x6000276+0x0 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at System.Reflection.TypeLoading.RoMethod.get_ReturnType() in System.Reflection.MetadataLoadContext.dll:token 0x600027d+0x0 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at PInvokeTableGenerator.GenPInvokeDecl(PInvoke pinvoke) in WasmAppBuilder.dll:token 0x6000020+0xc9 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at PInvokeTableGenerator.EmitPInvokeTable(StreamWriter w, Dictionary`2 modules, List`1 pinvokes) in WasmAppBuilder.dll:token 0x600001e+0x59 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at PInvokeTableGenerator.GenPInvokeTable(String[] pinvokeModules, String[] assemblies) in WasmAppBuilder.dll:token 0x600001c+0xaf [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at PInvokeTableGenerator.Execute() in WasmAppBuilder.dll:token 0x600001b+0x75 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() in Microsoft.Build.dll:token 0x60015fb+0x3e [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) in Microsoft.Build.dll:token 0x600147a+0x2b5 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
Done executing task "PInvokeTableGenerator" -- FAILED. (TaskId:151)
This are the most relevants parts of the code:
[DllImport(DllName)]
public static extern unsafe void callCbk(delegate*<void> cbk);typedef void (*callback)(void);
void callCbk(callback cbk)
{
cbk();
}And I'm using version
Microsoft.AspNetCore.App 6.0.0-rc.1.21370.12 [C:\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.0-rc.1.21369.14 [C:\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.0-rc.1.21369.7 [C:\dotnet\shared\Microsoft.WindowsDesktop.App]
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
arch-wasmWebAssembly architectureWebAssembly architecturearea-Build-monoarea-Interop-monoblockedIssue/PR is blocked on something - see commentsIssue/PR is blocked on something - see comments
Type
Projects
- StatusShow more project fieldsNo status
Activity
emepetres commentedon Jul 22, 2021
Just for reference, I modified the test to use "old delegates" and MonoPInvokeCallback attribute and it is working,
vargaz commentedon Jul 22, 2021
#43791
10 remaining items
emepetres commentedon Aug 9, 2022
@devshgraphicsprogramming check my samples, you can find how to do this with MonoPInvokeCallback there.
https://github.com/emepetres/dotnet-wasm-sample/blob/3967999706c364eb475abf6f09199141a99d096f/src/console-native/Program.cs#L28
devshgraphicsprogramming commentedon Oct 21, 2022
Its a small world of dependencies, this essentially renders SWIG's Cross Language Polymorphism (Directors) feature unusable:
swig/swig#1262
Which we really need because C++ interfaces with JS (embind) bindings from which C# inherits is how we're working around:
dotnet/aspnetcore#44088
maraf commentedon Feb 23, 2023
Still blocked until we can update
System.Reflection.Metadatain theWasmAppBuilder. Once the API is available, the rest of the pipeline works.maraf commentedon Oct 30, 2023
An alternative would be to rewrite manage to native generator to use
System.Reflection.MetadataAPI instead ofSystem.Reflection.MetadataLoadContextlewing commentedon Jul 20, 2026
This was fixed in .Net 9 I believe.