Skip to content

[NativeAOT] Can't call a function pointer with a ref return value #72316

@kevingosse

Description

@kevingosse

Description

In some cases with the System V ABI, when a "complex" type is returned, the caller is expected to allocate memory for the return value, and pass a pointer to that memory as a hidden first argument.

I'm trying to call a function that uses that convention, so I looked at how to instruct the runtime to do so. It looks like that may be the case when returning by ref:

flags |= RETURN_HAS_RET_BUFFER;

(note: I may be misinterpreting this code, since I haven't been able to try)

However, when trying to call an unmanaged pointer with a ref return value, the NativeAOT compilation fails.

Reproduction Steps

Create a new project, declare a function pointer with a ref return value, and try calling it:

public struct TestStruct
{
}

public static unsafe void CreateFunctionPointer(IntPtr address)
{
    var functionPointer = (delegate* unmanaged[Cdecl, MemberFunction]<IntPtr, ref TestStruct>)address;
    functionPointer(IntPtr.Zero);
}

Then try compiling it with NativeAOT:

dotnet publish /p:SelfContained=true -r win-x64 -c Release

Expected behavior

The call should work.

Actual behavior

An error is thrown during compilation:

  Generating compatible native code. To optimize for size or speed, visit https://aka.ms/OptimizeNativeAOT
EXEC : error : VTable of type 'System.Runtime.InteropServices.MarshalDirectiveException' not computed by the IL scanner. You c
an work around by running the compilation with scanner disabled. [C:\Users\kevin\source\repos\TestNativeAot\TestNativeAot\Test
NativeAot.csproj]
  ILCompiler.ScannerFailedException: VTable of type 'System.Runtime.InteropServices.MarshalDirectiveException' not computed by
   the IL scanner. You can work around by running the compilation with scanner disabled.
     at ILCompiler.ILScanResults.ScannedVTableProvider.GetSlice(TypeDesc type) in /_/src/coreclr/tools/aot/ILCompiler.Compiler
  /Compiler/ILScanner.cs:line 282
     at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
     at ILCompiler.DependencyAnalysis.ConstructedEETypeNode.ComputeNonRelocationBasedDependencies(NodeFactory factory) in /_/s
  rc/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ConstructedEETypeNode.cs:line 64
     at ILCompiler.DependencyAnalysis.ObjectNode.GetStaticDependencies(NodeFactory factory) in /_/src/coreclr/tools/Common/Com
  piler/DependencyAnalysis/ObjectNode.cs:line 59
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependenciesImpl(DependencyNodeCore`1 node) in /_
  /src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 181
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependencies(DependencyNodeCore`1 node) in /_/src
  /coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 221
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ProcessMarkStack() in /_/src/coreclr/tools/aot/ILCompiler.
  DependencyAnalysisFramework/DependencyAnalyzer.cs:line 256
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() in /_/src/coreclr/tools/aot/ILCompile
  r.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 307
     at ILCompiler.RyuJitCompilation.CompileInternal(String outputFile, ObjectDumper dumper) in /_/src/coreclr/tools/aot/ILCom
  piler.RyuJit/Compiler/RyuJitCompilation.cs:line 88
     at ILCompiler.Compilation.ILCompiler.ICompilation.Compile(String outputFile, ObjectDumper dumper) in /_/src/coreclr/tools
  /aot/ILCompiler.Compiler/Compiler/Compilation.cs:line 526
     at ILCompiler.Program.Run(String[] args) in /_/src/coreclr/tools/aot/ILCompiler/Program.cs:line 898
     at ILCompiler.Program.Main(String[] args) in /_/src/coreclr/tools/aot/ILCompiler/Program.cs:line 1090

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions