dotnet / runtime

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

LibraryImport with CLong produces SYSLIB1051 and requires disabling runtime marshalling #73975

Closed alexhelms closed 1 year ago

alexhelms commented 1 year ago

Description

Creating a LibraryImportP/Invoke function with CLong parameters produces a SYSLIB1051 error saying that runtime marshalling must be disabled in order to use a CLong as a parameter.

Reproduction Steps

I have this C function signature and the corresponding P/Invoke function.

/*
 * int CFITS_API ffghpr(fitsfile *fptr, int maxdim, int *simple, int *bitpix, int *naxis,
 *     long naxes[], long *pcount, long *gcount, int *extend, int *status);
 */
[LibraryImport(cfitsio, EntryPoint = "ffghpr")]
internal static partial ErrorCode ReadImageHeader(
    nint handle,
    int maxdim,
    out int simple,
    out ImageType bitpix,
    out int naxis,
    ref CLong naxes,
    out CLong pcount,
    out CLong gcount,
    out int extend,
    out ErrorCode status);

And the compiler will produce three errors (one for each CLong) -- error SYSLIB1051: Runtime marshalling must be disabled in this project ...

Expected behavior

CLong should work with LibraryImport without disabling runtime marshalling.

Actual behavior

CLong requires [assembly: DisableRuntimeMarshalling] in order to work.

Regression?

No response

Known Workarounds

Using a traditional DllImport works as expected.

Configuration

Windows 10 1909 x64

.NET SDK:
 Version:   7.0.100-preview.7.22377.5
 Commit:    ba310d9309

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.100-preview.7.22377.5\

Host:
  Version:      7.0.0-preview.7.22375.6
  Architecture: x64
  Commit:       eecb028078

Other information

It seems special consideration for CLong/UCLong may be required in the source generators for LibraryImport. I think there is precedence for this kind of special treatment seeing as the jit compiler also has special support for CLong/UCLong -- https://github.com/dotnet/runtime/blob/87d5ece77fbf9f7cf9f188262ad6791645857309/src/coreclr/jit/compiler.cpp#L455

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/interop-contrib See info in area-owners.md if you want to be subscribed.

Issue Details
### Description Creating a `LibraryImport `P/Invoke function with `CLong` parameters produces a SYSLIB1051 error saying that runtime marshalling must be disabled in order to use a `CLong` as a parameter. ### Reproduction Steps I have this C function signature and the corresponding P/Invoke function. ```csharp /* * int CFITS_API ffghpr(fitsfile *fptr, int maxdim, int *simple, int *bitpix, int *naxis, * long naxes[], long *pcount, long *gcount, int *extend, int *status); */ [LibraryImport(cfitsio, EntryPoint = "ffghpr")] internal static partial ErrorCode ReadImageHeader2( nint handle, int maxdim, out int simple, out ImageType bitpix, out int naxis, ref CLong naxes, out CLong pcount, out CLong gcount, out int extend, out ErrorCode status); ``` And the compiler will produce three errors (one for each `CLong`) -- `error SYSLIB1051: Runtime marshalling must be disabled in this project ...` ### Expected behavior `CLong` should work with `LibraryImport` without disabling runtime marshalling. ### Actual behavior `CLong` requires `[assembly: DisableRuntimeMarshalling]` in order to work. ### Regression? _No response_ ### Known Workarounds Using a traditional `DllImport` works as expected. ### Configuration Windows 10 1909 x64 ``` .NET SDK: Version: 7.0.100-preview.7.22377.5 Commit: ba310d9309 Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.100-preview.7.22377.5\ Host: Version: 7.0.0-preview.7.22375.6 Architecture: x64 Commit: eecb028078 ``` ### Other information It seems special consideration for CLong/UCLong may be required in the source generators for `LibraryImport`. I think there is precedence for this kind of special treatment seeing as the jit compiler also has special support for CLong/UCLong -- https://github.com/dotnet/runtime/blob/87d5ece77fbf9f7cf9f188262ad6791645857309/src/coreclr/jit/compiler.cpp#L455
Author: alexhelms
Assignees: -
Labels: `area-System.Runtime.InteropServices`
Milestone: -
AaronRobinsonMSFT commented 1 year ago

@alexhelms Appreciate this being called out. At this point I think we are unlikely to try and mitigate this for .NET 7, but this is something we should address in .NET 8. Please let us know if this is heavily impacting.

alexhelms commented 1 year ago

That is unfortunate but I understand, release is getting close! The DllImport workaround allows me to move forward and I don't require AOT support so this is not a blocker. I'm just experimenting with the new NativeLibrary feature.

AaronRobinsonMSFT commented 1 year ago

That is unfortunate but I understand, release is getting close!

Right. Thanks for understanding. We will get this done in .NET 8. Please ping us early next year if you don't see any traction so we can get on this and you can try it out in an early preview.

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/interop-contrib See info in area-owners.md if you want to be subscribed.

Issue Details
### Description Creating a `LibraryImport `P/Invoke function with `CLong` parameters produces a SYSLIB1051 error saying that runtime marshalling must be disabled in order to use a `CLong` as a parameter. ### Reproduction Steps I have this C function signature and the corresponding P/Invoke function. ```csharp /* * int CFITS_API ffghpr(fitsfile *fptr, int maxdim, int *simple, int *bitpix, int *naxis, * long naxes[], long *pcount, long *gcount, int *extend, int *status); */ [LibraryImport(cfitsio, EntryPoint = "ffghpr")] internal static partial ErrorCode ReadImageHeader( nint handle, int maxdim, out int simple, out ImageType bitpix, out int naxis, ref CLong naxes, out CLong pcount, out CLong gcount, out int extend, out ErrorCode status); ``` And the compiler will produce three errors (one for each `CLong`) -- `error SYSLIB1051: Runtime marshalling must be disabled in this project ...` ### Expected behavior `CLong` should work with `LibraryImport` without disabling runtime marshalling. ### Actual behavior `CLong` requires `[assembly: DisableRuntimeMarshalling]` in order to work. ### Regression? _No response_ ### Known Workarounds Using a traditional `DllImport` works as expected. ### Configuration Windows 10 1909 x64 ``` .NET SDK: Version: 7.0.100-preview.7.22377.5 Commit: ba310d9309 Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.100-preview.7.22377.5\ Host: Version: 7.0.0-preview.7.22375.6 Architecture: x64 Commit: eecb028078 ``` ### Other information It seems special consideration for CLong/UCLong may be required in the source generators for `LibraryImport`. I think there is precedence for this kind of special treatment seeing as the jit compiler also has special support for CLong/UCLong -- https://github.com/dotnet/runtime/blob/87d5ece77fbf9f7cf9f188262ad6791645857309/src/coreclr/jit/compiler.cpp#L455
Author: alexhelms
Assignees: -
Labels: `area-System.Runtime.InteropServices`
Milestone: 8.0.0
390620652 commented 1 year ago

I look forward to this function too.