CurtHagenlocher opened a new issue, #502:
URL: https://github.com/apache/arrow-dotnet/issues/502

   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   On .NET Framework (net462/net472), the C Data Interface code calls foreign 
function pointers by turning them back into delegates:
   
   ```csharp
   
Marshal.GetDelegateForFunctionPointer<CArrowSchemaExporter.ReleaseArrowSchema>(schema->release)(schema);
   ```
   
   When the pointer was made by `Marshal.GetFunctionPointerForDelegate`, .NET 
Framework's `GetDelegateForFunctionPointer` returns the **original delegate** 
instead of a new one of the requested type. If that delegate's type isn't the 
library's own internal delegate type, the call fails with:
   
   ```
   System.InvalidCastException: [A]OnSchemaDelegate cannot be cast to 
[B]OnSchemaDelegate.
   Type A originates from 'Apache.Arrow.Tests, ...'. Type B originates from 
'Apache.Arrow, ...'.
   ```
   
   So on .NET Framework, any **managed** code in the same process that hands 
Apache.Arrow C Data structures whose callbacks were made from its own delegate 
types will crash Apache.Arrow when it calls them. For example, another managed 
library implementing a producer or consumer, or test code. Native callers 
(C/C++, Python, Go) aren't affected, and neither is .NET 5+, which uses 
`delegate* unmanaged` and `UnmanagedCallersOnly`.
   
   I ran into this while writing a test for the async device stream work. The 
test had to reuse the library's internal delegate types to work around it.
   
   **To reproduce (net462 or net472):**
   1. Declare your own delegate type, e.g. `unsafe delegate void 
MyRelease(CArrowSchema* s);`. Keep an instance alive and put 
`Marshal.GetFunctionPointerForDelegate(instance)` into a `CArrowSchema`'s 
`release` field, along with a valid format string.
   2. Pass the schema to `CArrowSchemaImporter.ImportSchema`. When it releases 
the schema, it throws `InvalidCastException`.
   
   **Suggested fix:** on the .NET Framework paths, call through an unmanaged 
function pointer instead of making a delegate. For example:
   
   ```csharp
   ((delegate* unmanaged[Cdecl]<CArrowSchema*, void>)schema->release)(schema);
   ```
   
   C# 9 function pointers compile for any target framework. Only 
`UnmanagedCallersOnly`, which is for exporting, needs .NET 5+. A call through 
an `unmanaged[Cdecl]` pointer compiles to a `calli`, which works on .NET 
Framework whether the target is native code or a delegate thunk. Spell the 
calling convention out as `unmanaged[Cdecl]`: plain `delegate* unmanaged<...>` 
needs .NET 5 runtime support. This should also be cheaper than calling 
`GetDelegateForFunctionPointer` on every call, as the current code does.
   
   There are about 24 call sites in `src/Apache.Arrow/C/*.cs` (array, schema, 
array stream and device array stream import/release, plus the new async device 
stream code). The export side (`NativeDelegate`) doesn't need to change.
   
   ### Component(s)
   
   C Data Interface
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to