You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.net8 AOT compiled library targeting arm64 crashes when invoked using DllImport from .net8 code #95257
I can repro this on Raspberry Pi. This FailFasts when the second copy of the runtime tries to initialize. The GC initialization returns a failure. Stepping through it, the GC is mmapping an insane memory range and the mmap fails:
#0 0x0000007f6020117c in GCToOSInterface::VirtualReserve (size=274877906944, alignment=<optimized out>,
flags=<optimized out>, node=<optimized out>) at /__w/1/s/src/coreclr/gc/unix/gcenv.unix.cpp:570
#1 0x0000007f601c717c in WKS::virtual_alloc (size=274877906944, numa_node=65535, use_large_pages_p=<optimized out>)
at /__w/1/s/src/coreclr/nativeaot/Runtime/../../gc/gc.cpp:5766
#2 WKS::gc_heap::initialize_gc (soh_segment_size=soh_segment_size@entry=268435456,
loh_segment_size=loh_segment_size@entry=549755809776, poh_segment_size=poh_segment_size@entry=549755809776)
at /__w/1/s/src/coreclr/nativeaot/Runtime/../../gc/gc.cpp:14252
#3 0x0000007f601f2404 in WKS::GCHeap::Initialize (this=<optimized out>)
at /__w/1/s/src/coreclr/nativeaot/Runtime/../../gc/gc.cpp:48484
#4 0x0000007f601b8688 in RedhawkGCInterface::InitializeSubsystems ()
at /__w/1/s/src/coreclr/nativeaot/Runtime/gcrhenv.cpp:112
#5 0x0000007f601bc420 in InitDLL (hPalInstance=0x7f601b0000) at /__w/1/s/src/coreclr/nativeaot/Runtime/startup.cpp:151
#6 RhInitialize (isDll=<optimized out>) at /__w/1/s/src/coreclr/nativeaot/Runtime/startup.cpp:376
#7 0x0000007f601b6ab8 in InitializeRuntime () at /__w/1/s/src/coreclr/nativeaot/Bootstrap/main.cpp:167
#8 0x0000007f601bd93c in Thread::EnsureRuntimeInitialized (this=0x7ff7ff88f0)
at /__w/1/s/src/coreclr/nativeaot/Runtime/thread.cpp:1219
#9 Thread::ReversePInvokeAttachOrTrapThread (this=0x7ff7ff88f0, pFrame=0x7ffffff130)
at /__w/1/s/src/coreclr/nativeaot/Runtime/thread.cpp:1181
#10 0x0000007f60245c6c in aotlib_AotLib_NativeEntryPoints__Add (x=<optimized out>, y=<optimized out>)
at /home/michals/net8aot/AotLib/Class1.cs:11
#11 0x000000555560ec04 in test_LibTest_Program__add () at /home/michals/net8aot/LibTest/Program.cs:13
#12 0x000000555560eb04 in test_LibTest_Program__Main (args=...) at /home/michals/net8aot/LibTest/Program.cs:11
#13 0x000000555562f098 in test__Module___StartupCodeMain ()
at /_/src/coreclr/nativeaot/Common/src/System/Collections/Generic/LowLevelDictionary.cs:289
#14 0x0000007ff7e27780 in __libc_start_call_main (main=main@entry=0x5555558bc8 <main(int, char**)>, argc=argc@entry=1,
argv=argv@entry=0x7ffffff3c8) at ../sysdeps/nptl/libc_start_call_main.h:58
It appears we're asking for 274 GB of address space.
The contents of registers before the call to mmap is:
So I think it matches what gdb showed in debug information as the size parameter to GCToOSInterface::VirtualReserve and this is the real number (not a case of bad debug info).
@dotnet/gc is it expected that the GC would try to reserve such huge amount of memory? The raspberry Pi I'm running this on has 8 GB of RAM and 32 GB of total storage (swap is a small fraction of that).
I can repro this on Raspberry Pi. This FailFasts when the second copy of the runtime tries to initialize. The GC initialization returns a failure. Stepping through it, the GC is mmapping an insane memory range and the mmap fails:
#0 0x0000007f6020117c in GCToOSInterface::VirtualReserve (size=274877906944, alignment=<optimized out>,
flags=<optimized out>, node=<optimized out>) at /__w/1/s/src/coreclr/gc/unix/gcenv.unix.cpp:570
#1 0x0000007f601c717c in WKS::virtual_alloc (size=274877906944, numa_node=65535, use_large_pages_p=<optimized out>)
at /__w/1/s/src/coreclr/nativeaot/Runtime/../../gc/gc.cpp:5766
#2 WKS::gc_heap::initialize_gc (soh_segment_size=soh_segment_size@entry=268435456,
loh_segment_size=loh_segment_size@entry=549755809776, poh_segment_size=poh_segment_size@entry=549755809776)
at /__w/1/s/src/coreclr/nativeaot/Runtime/../../gc/gc.cpp:14252
#3 0x0000007f601f2404 in WKS::GCHeap::Initialize (this=<optimized out>)
at /__w/1/s/src/coreclr/nativeaot/Runtime/../../gc/gc.cpp:48484
#4 0x0000007f601b8688 in RedhawkGCInterface::InitializeSubsystems ()
at /__w/1/s/src/coreclr/nativeaot/Runtime/gcrhenv.cpp:112
#5 0x0000007f601bc420 in InitDLL (hPalInstance=0x7f601b0000) at /__w/1/s/src/coreclr/nativeaot/Runtime/startup.cpp:151
#6 RhInitialize (isDll=<optimized out>) at /__w/1/s/src/coreclr/nativeaot/Runtime/startup.cpp:376
#7 0x0000007f601b6ab8 in InitializeRuntime () at /__w/1/s/src/coreclr/nativeaot/Bootstrap/main.cpp:167
#8 0x0000007f601bd93c in Thread::EnsureRuntimeInitialized (this=0x7ff7ff88f0)
at /__w/1/s/src/coreclr/nativeaot/Runtime/thread.cpp:1219
#9 Thread::ReversePInvokeAttachOrTrapThread (this=0x7ff7ff88f0, pFrame=0x7ffffff130)
at /__w/1/s/src/coreclr/nativeaot/Runtime/thread.cpp:1181
#10 0x0000007f60245c6c in aotlib_AotLib_NativeEntryPoints__Add (x=<optimized out>, y=<optimized out>)
at /home/michals/net8aot/AotLib/Class1.cs:11
#11 0x000000555560ec04 in test_LibTest_Program__add () at /home/michals/net8aot/LibTest/Program.cs:13
#12 0x000000555560eb04 in test_LibTest_Program__Main (args=...) at /home/michals/net8aot/LibTest/Program.cs:11
#13 0x000000555562f098 in test__Module___StartupCodeMain ()
at /_/src/coreclr/nativeaot/Common/src/System/Collections/Generic/LowLevelDictionary.cs:289
#14 0x0000007ff7e27780 in __libc_start_call_main (main=main@entry=0x5555558bc8 <main(int, char**)>, argc=argc@entry=1,
argv=argv@entry=0x7ffffff3c8) at ../sysdeps/nptl/libc_start_call_main.h:58
It appears we're asking for 274 GB of address space.
The contents of registers before the call to mmap is:
So I think it matches what gdb showed in debug information as the size parameter to GCToOSInterface::VirtualReserve and this is the real number (not a case of bad debug info).
@dotnet/gc is it expected that the GC would try to reserve such huge amount of memory? The raspberry Pi I'm running this on has 8 GB of RAM and 32 GB of total storage (swap is a small fraction of that).
I have updated the table and the repo on the issue as well, I can add that, when the native interface is invoked from c++ code, it works fine. So of course the issue is when multiple "runtimes" are running, one on the lib, and one from the invoking code. The issue is only arm64 , on x64 (linux and windows), it works fine.
@anoop331 you can work around this by configuring the garbage collector not to reserve insane amounts of address space. For example, setting the DOTNET_GCHeapHardLimitPercent=8 environment variable before starting the process fixes things for me. (See more at GC configuration options to set heap limits at https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector. You can also hardcode this at compile time by setting <ItemGroup><RuntimeHostConfigurationOption Include="System.GC.HeapHardLimitPercent" Value="8" /></ItemGroup> in the csproj)
@anoop331 what machine are you running this on? Trying to see if both of us tried Raspberry Pi, or if this is a more general Arm64 Linux issue.
@MichalStrehovsky Thanks will try out the work around. We are running on a custom built yocto distribution that runs on QEMU. So it seems like a general arm64 issue. It seems to work fine on x64 linux distros.
adding @janvorli as well since we have done some recent changes related to VirtualMemoryLimits. Would be interesting to run with those changes to check if there is a behavior difference here.
Activity
fanyang-mono commentedon Nov 29, 2023
This seems to be related to NativeAOT rather than Mono AOT.
jkotas commentedon Nov 30, 2023
Is there anything printed to the console? What is the exit code?
MichalStrehovsky commentedon Nov 30, 2023
I can repro this on Raspberry Pi. This FailFasts when the second copy of the runtime tries to initialize. The GC initialization returns a failure. Stepping through it, the GC is mmapping an insane memory range and the mmap fails:
It appears we're asking for 274 GB of address space.
The contents of registers before the call to mmap is:
So I think it matches what gdb showed in debug information as the
sizeparameter toGCToOSInterface::VirtualReserveand this is the real number (not a case of bad debug info).@dotnet/gc is it expected that the GC would try to reserve such huge amount of memory? The raspberry Pi I'm running this on has 8 GB of RAM and 32 GB of total storage (swap is a small fraction of that).
mangod9 commentedon Nov 30, 2023
correct, with regions the GC tries to reserve 256gb of address space or up to 1/2 of available virtual memory:
https://github.com/dotnet/runtime/blob/3805c174d0a72dadfdbef98011b10b32df9e93f3/src/coreclr/gc/gc.cpp#L48097
do we know how much of virtual address space available on the pi? Maybe for this scenario it has to be less than 1/2?
anoop331 commentedon Nov 30, 2023
/
It says "aborted", here is the updated table.
<style> </style>anoop331 commentedon Nov 30, 2023
I have updated the table and the repo on the issue as well, I can add that, when the native interface is invoked from c++ code, it works fine. So of course the issue is when multiple "runtimes" are running, one on the lib, and one from the invoking code. The issue is only arm64 , on x64 (linux and windows), it works fine.
MichalStrehovsky commentedon Nov 30, 2023
@anoop331 you can work around this by configuring the garbage collector not to reserve insane amounts of address space. For example, setting the DOTNET_GCHeapHardLimitPercent=8 environment variable before starting the process fixes things for me. (See more at GC configuration options to set heap limits at https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector. You can also hardcode this at compile time by setting
<ItemGroup><RuntimeHostConfigurationOption Include="System.GC.HeapHardLimitPercent" Value="8" /></ItemGroup>in the csproj)@anoop331 what machine are you running this on? Trying to see if both of us tried Raspberry Pi, or if this is a more general Arm64 Linux issue.
anoop331 commentedon Dec 1, 2023
@MichalStrehovsky Thanks will try out the work around. We are running on a custom built yocto distribution that runs on QEMU. So it seems like a general arm64 issue. It seems to work fine on x64 linux distros.
3 remaining items
mangod9 commentedon Dec 1, 2023
adding @janvorli as well since we have done some recent changes related to VirtualMemoryLimits. Would be interesting to run with those changes to check if there is a behavior difference here.
fix from dotnet/runtime#95257
anoop331 commentedon Dec 4, 2023
@MichalStrehovsky The fix you proposed worked, both running with CLR mode and AOT compiled client code. I have updated my repo with the fix as well https://github.com/anoop331/net8aot. Thanks a lot.
mangod9 commentedon Jul 19, 2024
I am guessing manually configuring the
DOTNET_GCRegionRangeto be something smaller should be the preferred way to work around this issue.@anoop331, assume that is a satisfactory workaround for you and we can close this issue?
JinShil commentedon May 31, 2025
Burned by this bug...Again! Please fix it. Or, if you're not going to fix it, please add some useful output instead of just aborting.
JinShil commentedon Nov 13, 2025
Was this fixed in .NET 10? I hope so. I can't seem to reproduce it, unless I'm forgetting how.
MichalStrehovsky commentedon Nov 13, 2025
Yes, this was fixed in #118516.
anoop331 commentedon Nov 13, 2025
Fantastic :-)