Skip to content

[Android] Stack overflow crash when using dotnet-trace for profiled aot #71143

Closed

Description

@steveisok

Description

A stack overflow crash occurs on Android when stack walking in order to gather the right data for MethodDetails events. It appears to be class_loading profiler events which get fired over and over until we overflow the stack and crash.

Reproduction Steps

  1. Start dotnet-dsrouter

Once a device is connected via USB or an emulator is started:

dotnet-dsrouter server-server -ipcs ~/mylocalport -tcps 127.0.0.1:9000 --forward-port Android

  1. Build runtime and run the Android sample

In the Makefile, uncomment RUNTIME_COMPONENTS and DIAGNOSTIC_PORTS

make run AOT=true MONO_CONFIG=Debug MONO_ARCH=<target-arch>

  1. Start dotnet-trace

dotnet-trace collect --diagnostic-port ~/mylocalport,connect --providers Microsoft-Windows-DotNETRuntime:0x1F000080018:5

Expected behavior

The trace should run and produce a nettrace file

Actual behavior

The app crashes. You'll notice the stack trace repeats over and over like:

mono_walk_stack_with_ctx mini-exceptions.c:1217
mono_runtime_walk_stack_with_ctx mini-exceptions.c:1194
ep_rt_mono_walk_managed_stack_for_thread ep-rt-mono.c:2650
ep_rt_walk_managed_stack_for_thread ep-rt-mono.h:696
ep_walk_managed_stack_for_current_thread ep.h:236
ep_buffer_manager_write_event ep-buffer-manager.c:974
ep_session_write_event ep-session.c:482
write_event_2 ep.c:768
write_event ep.c:702
ep_write_event ep.c:1494
EventPipeWriteEventTypeLoadStart dotnetruntime.c:802
FireEtwTypeLoadStart clretwallmain.h:1313
ep_rt_mono_write_event_type_load_start ep-rt-mono.c:3998
runtime_profiler_class_loading ep-rt-mono.c:4489
mono_profiler_raise_class_loading profiler-events.h:54
mono_class_create_from_typedef class-init.c:482
mono_class_get_checked class.c:2886
mono_get_method_from_token loader.c:1062
mono_get_method_checked loader.c:1195
mono_aot_find_jit_info aot-runtime.c:3619
mono_jit_info_table_find_internal jit-info.c:330
mini_jit_info_table_find_ext mini-exceptions.c:1583
mono_find_jit_info_ext mini-exceptions.c:668
unwinder_unwind_frame mini-exceptions.c:796
mono_walk_stack_full mini-exceptions.c:1349
mono_walk_stack_with_ctx mini-exceptions.c:1217
mono_runtime_walk_stack_with_ctx mini-exceptions.c:1194
ep_rt_mono_walk_managed_stack_for_thread ep-rt-mono.c:2650
ep_rt_walk_managed_stack_for_thread ep-rt-mono.h:696
ep_walk_managed_stack_for_current_thread ep.h:236
ep_buffer_manager_write_event ep-buffer-manager.c:974
ep_session_write_event ep-session.c:482
write_event_2 ep.c:768
write_event ep.c:702
ep_write_event ep.c:1494
EventPipeWriteEventTypeLoadStart dotnetruntime.c:802
FireEtwTypeLoadStart clretwallmain.h:1313

Full stack trace: message.txt

Regression?

No

Known Workarounds

None

Configuration

Latest main using Android with any architecture we support (arm64, x64, x86)

Other information

No response

Activity

  1. ghost added
    untriaged
    on Jun 22, 2022
  2. added and removed
    untriaged
    on Jun 22, 2022
  3. added this to the 7.0.0 milestone on Jun 22, 2022
  4. steveisok commented on Jun 22, 2022

    @steveisok
    MemberAuthor
  5. lateralusX commented on Jul 1, 2022

    @lateralusX
    Member

    Happens in full AOT mode where methods have been compiled as direct calls (happens within an assembly). In that case, the method is not known by the runtime (not present in the JIT info table). When a new class is loading, the class_loading profiler callback is called. The EventPipe implementation of that callback will take a stackwalk (to be inline with CoreCLR), but taking a stackwalk will look up each managed function on the callstack against the JIT info table, if its not there it will load it, that in turn could case additional recursive class_loading event to fire, that will again take the same callstack, ending up in an infinite recursion.

    The issue is even a little more problematic, since it turns out you cannot do anything inside the class_loading callback that might end up triggering a class load, since that might lead up to recursively loading the same type, that in turn will assert when outer class_loading call returns and tries to add it into the loaded class cache.

    Fix will harden the stackwalking logic inside EventPipe to detect recursive stackwalks (doing a stackwalk triggering another stackwalk) and if that is the case, the stackwalk will be done using the async_context stackwalking capabilities since that won't trigger any additional profiler events, breaking recursion.

    Fix also adds logic to the class_loading profiler callback in EventPipe to also utilize async_context stackwalking making sure it won't trigger any additional profiler events, leading up to potential issues loading the same class twice.

    For 1:1 mapping of old Mono profiler events issued into EventPipe, they have been aligned with old Mono log profiler events callstack behaviours, meaning it will only emit callstacks on a few limited profiler events, mitigating the issues seen in the runtime profiler provider.

  6. lateralusX commented on Jul 4, 2022

    @lateralusX
    Member

    Also note that this case is not what profiled AOT will use on Android, since it will run in JIT mode or potential partial AOT and not full AOT. JIT mode is not hit by this issue, since all calls will go over the JIT trampoline, making sure all methods on callstack will end up in JIT info table.

  7. lateralusX commented on Jul 4, 2022

    @lateralusX
    Member

    Should be fixed by #71610.

  8. ghost locked as resolved and limited conversation to collaborators on Aug 3, 2022
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

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions