Crash when Netcore ConsoleApp use LibraryImport to call a function in a so generated by Native AOT in linux-arm64

Open
#118,433 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
32/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, linux

Research direction

Reproduce the linux-arm64 case using the NativeAOT publish commands for grgbanking_io and ConsoleTest, then run the published app under gdb. Start at the native_add and ReversePInvokeAttachOrTrapThread frames and compare the result with linux-x64. Done means the same LibraryImport call completes on linux-arm64 and prints Result: 8 without an abort.

Written by the indexing model from the issue text.

Description

area-GC-coreclr
Description

In linux-arm64, I generated a simple test so by Native AOT. But when I call the so function in a netcore ConsoleApp, it crash.
But i test it in linux-x64, it works fine.

Reproduction Steps
  1. Create a C# Library project "grgbanking_io", with the simple code:
using System.Runtime.InteropServices;

namespace grgbanking_io
{
    public class StdApi
    {
        [UnmanagedCallersOnly(EntryPoint = "native_add")]
        public static int NativeAdd(int a, int b) => a + b;

    }
}
  1. Publish the NativeAOT so with the command:
dotnet publish -c Release -r linux-arm64 --self-contained /p:PublishAot=true
  1. Create a netcore ConsoleApp project "ConsoleTest", with the simple code:
using System.Runtime.InteropServices;

namespace ConsoleTest
{
    public partial class Program
    {
        
        private const string _soName = "grgbanking_io";

        [LibraryImport(_soName, EntryPoint = "native_add")]
        private static partial int _nativeAdd(int a, int b);


        static void Main(string[] args)
        {
            try
            {
                int result = _nativeAdd(3, 5);
                Console.WriteLine($"Result: {result}");
            }

            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex);
            }
            
        }
    }
}
  1. Publish the ConsoleApp with the same command:
dotnet publish -c Release -r linux-arm64 --self-contained /p:PublishAot=true
  1. Copy the so and so.dbg files into the ConsoleApp's publish dir
  2. Run the ConsoleApp in gdb, will find the crash info.
Expected behavior

work normal

Actual behavior

The gdb info:

Thread 1 "ConsoleTest" received signal SIGABRT, Aborted.
0x0000007ff7d68eac in raise () from /lib/aarch64-linux-gnu/libc.so.6
(gdb) bt
#0  0x0000007ff7d68eac in raise () from /lib/aarch64-linux-gnu/libc.so.6
#1  0x0000007ff7d55aa0 in abort () from /lib/aarch64-linux-gnu/libc.so.6
#2  0x0000007ff4c3da20 in RhFailFast() () from /root/grgbanking_io/ConsoleTest/bin/Release/net10.0/linux-arm64/publish/grgbanking_io.so
#3  0x0000007ff4c4f85c in Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame*) () from /root/grgbanking_io/ConsoleTest/bin/Release/net10.0/linux-arm64/publish/grgbanking_io.so
#4  0x0000007ff4cf9a7c in native_add () from /root/grgbanking_io/ConsoleTest/bin/Release/net10.0/linux-arm64/publish/grgbanking_io.so
#5  0x00000055555bfa64 in ConsoleTest_ConsoleTest_Program___nativeAdd ()
#6  0x00000055555bfabc in ConsoleTest_ConsoleTest_Program__Main ()
#7  0x0000005555626c30 in __managed__Main ()
#8  0x0000007ff7d55e18 in __libc_start_main () from /lib/aarch64-linux-gnu/libc.so.6
#9  0x0000005555556d3c in _start () #
Regression?

I test in net8 ,net9 and net10preview6,it all crashed.
But in net10preview6, it shows the error info:

Fatal error. .NET runtime failed to initialize.
Known Workarounds

No response

Configuration

.NET Version: net8 ,net9 and net10preview6
OS: linux-arm64, Debian 11

Other information

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.