Skip to content

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

Open

Description

@shshheyi

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

Activity

  1. added
    untriaged
    on Aug 6, 2025
  2. added
    needs-area-label
    on Aug 6, 2025
  3. MihuBot commented on Aug 6, 2025

    @MihuBot
  4. MichalStrehovsky commented on Aug 6, 2025

    @MichalStrehovsky
    Member

    Can you try setting the DOTNET_GCHeapHardLimitPercent environment variable to 8 before you run the program?

    Run export DOTNET_GCHeapHardLimitPercent=8 before launching the executable.

  5. removed
    needs-area-label
    on Aug 6, 2025
  6. added
    needs-author-action
    on Aug 6, 2025
  7. shshheyi commented on Aug 7, 2025

    @shshheyi
    Author

    @MichalStrehovsky , Thanks, I tried to set the environment variable DOTNET_GCHeapHardLimitPercent, and then run the consoleApp, it worked.

    root@emptech:~/grgbanking_io/ConsoleTest/bin/Release/net10.0/linux-arm64/publish# export DOTNET_GCHeapHardLimitPercent=8
    root@emptech:~/grgbanking_io/ConsoleTest/bin/Release/net10.0/linux-arm64/publish# ./ConsoleTest
    Result: 8

    Can you try setting the DOTNET_GCHeapHardLimitPercent environment variable to 8 before you run the program?

    Run export DOTNET_GCHeapHardLimitPercent=8 before launching the executable.

  8. removed
    needs-author-action
    on Aug 7, 2025
  9. MichalStrehovsky commented on Aug 7, 2025

    @MichalStrehovsky
    Member

    Thank you! Moving this to the GC area path. This is an ongoing support cost.

  10. dotnet-policy-service commented on Aug 7, 2025

    @dotnet-policy-service
    Contributor

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

  11. mangod9 commented on Aug 7, 2025

    @mangod9
    Member

    @MichalStrehovsky, assume this is the same issue of large reservation size?

  12. MichalStrehovsky commented on Aug 7, 2025

    @MichalStrehovsky
    Member

    @MichalStrehovsky, assume this is the same issue of large reservation size?

    We have two GCs in the process (JIT-based CoreCLR and native AOT), the runtime is failing to initialize in the second, and everything gets fixed with the export DOTNET_GCHeapHardLimitPercent=8 workaround, so my guess is that.

  13. mangod9 commented on Aug 8, 2025

    @mangod9
    Member

    Is there anything special about DOTNET_GCHeapHardLimitPercent=8, so generally any smaller value would do?

    We are planning to change the reservation size to 2x Physical Memory for NativeAOT case, wonder if that would help alleviate this issue?

  14. shshheyi commented on Aug 8, 2025

    @shshheyi
    Author

    @mangod9 When use only one NativeAOT so lib, I tested the DOTNET_GCHeapHardLimitPercent, if < 50%, it can work normal.But in my final App, may use 3 or more NativeAOT so libs. Can share the size in one process no matter how many NativeAOT so libs it used?

  15. MichalStrehovsky commented on Aug 8, 2025

    @MichalStrehovsky
    Member

    We are planning to change the reservation size to 2x Physical Memory for NativeAOT case, wonder if that would help alleviate this issue?

    Why only for native AOT?

    Most reports (including this and #95257 linked above by the bot) involve JIT-based CoreCLR loading a native AOT library. We know that 256 GB + 256 GB reservation is too much for many systems. Do we have data that 256 GB + 2 * Physical Memory would work?

    We have data that even the first 256 GB (without native AOT in the picture at all) is problematic:

    I haven't tried following all the linked issues, looks like there is more to it.

    Cc @agocke

    Is there anything special about DOTNET_GCHeapHardLimitPercent=8, so generally any smaller value would do?

    The number is a copypaste from #95257 (comment), I haven't put thought into it.

  16. mangod9 commented on Aug 8, 2025

    @mangod9
    Member

    We cannot change the default for SVR or WKS since it might cause regressions for larger applications. As an initial step we can update it for NativeAOT.

    We could also improve the error message and provide the configuration needed to get the scenario to work per the specific app requirements.

    @Maoni0

  17. MichalStrehovsky commented on Aug 8, 2025

    @MichalStrehovsky
    Member

    We cannot change the default for SVR or WKS since it might cause regressions for larger applications. As an initial step we can update it for NativeAOT.

    Having different defaults for things that affect GC perf makes it very hard to root cause perf issues between native AOT and CoreCLR. Random perf differences are even more expensive for us to root cause than a failure to initialize the runtime. I don't find a native AOT specific fix acceptable and I would prefer not to do anything then.

  18. mangod9 commented on Aug 8, 2025

    @mangod9
    Member

    @shshheyi, assuming the workaround to manually configure the hard limit works for you. We have another issue tracking the case of multiple runtimes within the same process case.

  19. removed
    untriaged
    on Aug 14, 2025
  20. added this to the Future milestone on Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions