Skip to content

SkiaSharp crash under CoreCLR & NativeAOT investigation notes #21656

Closed

Description

@jonpryor

On unoplatform/uno CI, we occasionally see the Tests - Android+CoreCLR Skia and Tests - Android+NativeAOT Skia stages crash within SkiaSharp.

Instead of "polluting" investigation notes in #21643, place the investigation notes here.

Activity

  1. added theissue type on Oct 21, 2025
  2. added
    triage/untriaged
    difficulty/tbd
    on Oct 21, 2025
  3. jonpryor commented on Oct 21, 2025

    @jonpryor
    ContributorAuthor

    #21643 has a crash in the NativeAOT stage!

    F DEBUG   : Build fingerprint: 'google/sdk_gphone64_x86_64/emu64xa:14/UE1A.230829.036.A4/12096271:user/release-keys'
    F DEBUG   : Revision: '0'
    F DEBUG   : ABI: 'x86_64'
    F DEBUG   : Timestamp: 2025-10-21 15:32:36.000677344+0000
    F DEBUG   : Process uptime: 16s
    F DEBUG   : Cmdline: uno.platform.samplesapp.skia.nativeaot
    F DEBUG   : pid: 4732, tid: 4820, name: GLThread 46  >>> uno.platform.samplesapp.skia.nativeaot <<<
    F DEBUG   : uid: 10190
    F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0000000000000610
    F DEBUG   : Cause: null pointer dereference
    F DEBUG   :     rax 0000000000000108  rbx 000073a6f12f0210  rcx 0000000000000000  rdx 000000000000007a
    F DEBUG   :     r8  d92d00000010200b  r9  00007364a8b8ea00  r10 0000000000000000  r11 0000000000000000
    F DEBUG   :     r12 0000000000000610  r13 0000000000000061  r14 000073a6612f43f0  r15 00007364a76012d8
    F DEBUG   :     rdi 0000000000000610  rsi 00007364a76012d8
    F DEBUG   :     rbp 0000000000000000  rsp 00007364a7601230  rip 00007364a8db47c7
    F DEBUG   : 25 total frames
    F DEBUG   : backtrace:
    F DEBUG   :       #00 pc 00000000003057c7  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSkiaSharp.so
    F DEBUG   :       #01 pc 00000000003053ce  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSkiaSharp.so
    F DEBUG   :       #02 pc 00000000002e4c74  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSkiaSharp.so
    F DEBUG   :       #03 pc 00000000002749a4  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSkiaSharp.so
    F DEBUG   :       #04 pc 00000000002747bd  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSkiaSharp.so
    F DEBUG   :       #05 pc 000000000024f36f  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSkiaSharp.so (sk_canvas_draw_picture+95)
    F DEBUG   :       #06 pc 0000000009acb685  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSamplesApp.so (SkiaSharp_SkiaSharp_SkiaApi__sk_canvas_draw_picture+101) (BuildId: 5f9d8dc654dfed4438273840dd8a19b67d66bc03)
    F DEBUG   :       #07 pc 000000000aa5a09d  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSamplesApp.so (Uno_UI_Uno_UI_Helpers_SkiaRenderHelper__RenderPicture+141) (BuildId: 5f9d8dc654dfed4438273840dd8a19b67d66bc03)
    F DEBUG   :       #08 pc 000000000a2d49a9  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSamplesApp.so (Uno_UI_Microsoft_UI_Xaml_Media_CompositionTarget__Draw+777) (BuildId: 5f9d8dc654dfed4438273840dd8a19b67d66bc03)
    F DEBUG   :       #09 pc 000000000a2d5c09  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSamplesApp.so (Uno_UI_Microsoft_UI_Xaml_Media_CompositionTarget__OnNativePlatformFrameRequested+553) (BuildId: 5f9d8dc654dfed4438273840dd8a19b67d66bc03)
    F DEBUG   :       #10 pc 000000000b05777e  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSamplesApp.so (Uno_UI_Runtime_Skia_Android_Uno_UI_Runtime_Skia_Android_UnoSKCanvasView_InternalRenderer__Android_Opengl_GLSurfaceView_IRenderer_OnDrawFrame+270) (BuildId: 5f9d8dc654dfed4438273840dd8a19b67d66bc03)
    F DEBUG   :       #11 pc 00000000086a4401  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSamplesApp.so (Mono_Android_Android_Opengl_GLSurfaceView_IRendererInvoker__n_OnDrawFrame_Ljavax_microedition_khronos_opengles_GL10_+193) (BuildId: 5f9d8dc654dfed4438273840dd8a19b67d66bc03)
    F DEBUG   :       #12 pc 000000000ca286ba  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/lib/x86_64/libSamplesApp.so (Internal_CompilerGenerated__Module___<ReverseOpenStaticDelegateStub>Mono_Android__JniMarshal_PPL_V+90) (BuildId: 5f9d8dc654dfed4438273840dd8a19b67d66bc03)
    F DEBUG   :       #13 pc 0000000000391a4b  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+219) (BuildId: b6dc79e02101ea00827a35a55ab6597a)
    F DEBUG   :       #14 pc 000000000036faed  /apex/com.android.art/lib64/libart.so (nterp_helper+3837) (BuildId: b6dc79e02101ea00827a35a55ab6597a)
    F DEBUG   :       #15 pc 0000000000466eb4  /data/app/~~ZYVR5YT-bvI6JuPKmxmqTg==/uno.platform.samplesapp.skia.nativeaot-xixpSuclgZh9X9KMhU1l5A==/oat/x86_64/base.vdex (crc64d465fbe34e0c11e4.UnoSKCanvasView_InternalRenderer.onDrawFrame+0)
    F DEBUG   :       #16 pc 00000000003708a1  /apex/com.android.art/lib64/libart.so (nterp_helper+7345) (BuildId: b6dc79e02101ea00827a35a55ab6597a)
    F DEBUG   :       #17 pc 00000000004dfed0  /system/framework/framework.jar (android.opengl.GLSurfaceView$GLThread.guardedRun+1096)
    F DEBUG   :       #18 pc 000000000036fa88  /apex/com.android.art/lib64/libart.so (nterp_helper+3736) (BuildId: b6dc79e02101ea00827a35a55ab6597a)
    F DEBUG   :       #19 pc 00000000004e0508  /system/framework/framework.jar (android.opengl.GLSurfaceView$GLThread.run+52)
    F DEBUG   :       #20 pc 00000000003784c4  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+756) (BuildId: b6dc79e02101ea00827a35a55ab6597a)
    F DEBUG   :       #21 pc 00000000003c535c  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+204) (BuildId: b6dc79e02101ea00827a35a55ab6597a)
    F DEBUG   :       #22 pc 0000000000853176  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback(void*)+1510) (BuildId: b6dc79e02101ea00827a35a55ab6597a)
    F DEBUG   :       #23 pc 00000000000cd06a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+58) (BuildId: fa337969c798946280caa45e2d71a2e7)
    F DEBUG   :       #24 pc 0000000000062d88  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: fa337969c798946280caa45e2d71a2e7)
    

    Now that we have native debug symbols for libSkiaSharp.so, we can follow the symbolification instructions:

    % $HOME/android-toolchain/ndk/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-addr2line -paCif -e libSkiaSharp.so 00000000003057c7
    0x3057c7: SkRecord::Record::type() const at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkRecord.h:147
     (inlined by) decltype(fp((SkRecords::NoOp)())) SkRecord::Record::visit<SkRecords::Draw&>(SkRecords::Draw&) const at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkRecord.h:154

    which gives a native stack trace of:

    0x3057c7: SkRecord::Record::type() const at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkRecord.h:147
     (inlined by) decltype(fp((SkRecords::NoOp)())) SkRecord::Record::visit<SkRecords::Draw&>(SkRecords::Draw&) const at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkRecord.h:154
    
    0x3053ce: decltype(fp0((SkRecords::NoOp)())) SkRecord::visit<SkRecords::Draw&>(int, SkRecords::Draw&) const at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkRecord.h:46
     (inlined by) SkRecordDraw(SkRecord const&, SkCanvas*, SkPicture const* const*, SkDrawable* const*, int, SkBBoxHierarchy const*, SkPicture::AbortCallback*) at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkRecordDraw.cpp:83
    
    0x2e4c74: SkBigPicture::playback(SkCanvas*, SkPicture::AbortCallback*) const at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkBigPicture.cpp:37
    
    0x2749a4: SkCanvas::onDrawPicture(SkPicture const*, SkMatrix const*, SkPaint const*) at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkCanvas.cpp:2627
    
    0x2747bd: SkCanvas::drawPicture(SkPicture const*, SkMatrix const*, SkPaint const*) at C:/a/_work/1/s/externals/skia/out/android/x86_64/../../../src/core/SkCanvas.cpp:2616
    

    Or, in GitHub link form:

  4. jonpryor commented on Oct 21, 2025

    @jonpryor
    ContributorAuthor

    Because NativeAOT causes "managed" methods to be native code, we can also infer the managed call stack!

    The caller of UnoSkCanvasView.OnDrawFrame() is Android.Opengl.GLSurfaceView.IRendererInvoker.n_OnDrawFrame_Ljavax_microedition_khronos_opengles_GL10_(), which is the generated code provided below:

    namespace Android.Opengl {
    	public partial class GLSurfaceView : Android.Views.SurfaceView, Android.Views.ISurfaceHolderCallback, Android.Views.ISurfaceHolderCallback2 {
    		internal partial class IRendererInvoker : global::Java.Lang.Object, IRenderer {
    			[global::System.Diagnostics.DebuggerDisableUserUnhandledExceptions]
    			static void n_OnDrawFrame_Ljavax_microedition_khronos_opengles_GL10_ (IntPtr jnienv, IntPtr native__this, IntPtr native_gl)
    			{
    				if (!global::Java.Interop.JniEnvironment.BeginMarshalMethod (jnienv, out var __envp, out var __r))
    					return;
    
    				try {
    					var __this = global::Java.Lang.Object.GetObject<Android.Opengl.GLSurfaceView.IRenderer> (jnienv, native__this, JniHandleOwnership.DoNotTransfer)!;
    					var gl = (Javax.Microedition.Khronos.Opengles.IGL10?)global::Java.Lang.Object.GetObject<Javax.Microedition.Khronos.Opengles.IGL10> (native_gl, JniHandleOwnership.DoNotTransfer);
    					__this.OnDrawFrame (gl);
    				} catch (global::System.Exception __e) {
    					__r.OnUserUnhandledException (ref __envp, __e);
    				} finally {
    					global::Java.Interop.JniEnvironment.EndMarshalMethod (ref __envp);
    				}
    			}
    		}
    	}
    }
  5. jonpryor commented on Oct 21, 2025

    @jonpryor
    ContributorAuthor

    Useful additional reading: Chris Brumme's Lifetime, GC.KeepAlive, handle recycling

  6. jonpryor commented on Oct 21, 2025

    @jonpryor
    ContributorAuthor

    Now begins the digging and conjecture. (Fun!)

    The SIGSEGV in SkRecord::Record::type() implies that the Record is null. This in turn implies that a member of SkRecord::fRecords is null: https://github.com/mono/skia/blob/927041a58f130e0dd0562ba86cb4170989ad39e9/src/core/SkRecord.h#L177

    Not the SkRecord itself! Rather, SkRecord::fRecords contains a null value!

    Which doesn't actually make sense: I don't see anything in SkRecord.h that would allow nullptr to be inserted into SkRecord::fRecords…

    Unless SkRecordDraw() is doing an out-of-bounds invocation? I can't see how that would happen.

    Alternatively, if the SkRecord itself were freed "from elsewhere"…. Following that thought fragment, the SkRecord comes from SkBigPicture::playback(), as *fRecord, i.e. the SkRecord lifetime is tied to the lifetime of SkBigPicture.

    The lastRenderedFrame.frame in CompositionTarget.Draw() / the SkiaRenderHelper.RenderPicture() invocation is the SkBigPicture. What's the lifetime of lastRenderedFrame.frame?

    lastRenderedFrame is from CompositionHelper._lastRenderedFrame:

    (SKPicture frame, SKPath nativeElementClipPath)? lastRenderedFrameNullable;
    lock (_frameGate)
    {
    lastRenderedFrameNullable = _lastRenderedFrame;
    }
    if (lastRenderedFrameNullable is not { } lastRenderedFrame)

    When CompositionHelper._lastRenderedFrame is changed, then lastRenderedFrame.frame will only be preserved by the GC for as long as that instance is referenced by managed code, i.e. it's eligible for collection here:

    (yes, a blank line… see below.)

    Meanwhile, when is CompositionHelper._lastRenderedFrame changed? This comment block describes the lifetime:

    // +---------+ +-------------------------------------------+ +---------------+ +-----------------------------+ +-------------------+
    // | Visual | | CompositionTargetNotNecessarilyOnUIThread | | IXamlRootHost | | CompositionTargetOnUIThread | | NativeDispatcher |
    // +---------+ +-------------------------------------------+ +---------------+ +-----------------------------+ +-------------------+
    // ------------------------\ | | | | |
    // | some property changes |-| | | | |
    // |-----------------------| | | | | |
    // | | | | |
    // | RequestNewFrame | | | |
    // |-------------------------------------->| | | |
    // | | | | |
    // | | InvalidateRender (the native platform will later call us back likely on the next monitor VSync) | | |
    // | |---------------------------------------------------------------------------------------------------------------->| | |
    // ------------------------\ | | | | |
    // | some property changes |-| | | | |
    // |-----------------------| | | | | |
    // | | | | |
    // | RequestNewFrame | | | |
    // |-------------------------------------->| | | |
    // | -----------------------------\ | | | |
    // | | RequestNewFrame is ignored |-| | | |
    // | |----------------------------| | | | |
    // | | | | |
    // | RequestNewFrame | | | |
    // |-------------------------------------->| | | |
    // | -----------------------------\ | | | |
    // | | RequestNewFrame is ignored |-| | | |
    // | |----------------------------| | | | |
    // | | ------------------------------------------------------------------------------------\ | | |
    // | | | native platform render callback in response to the previous InvalidateRender call |-| | |
    // | | |-----------------------------------------------------------------------------------| | | |
    // | | | | |
    // | | OnNativePlatformFrameRequested | | |
    // | |<----------------------------------------------------------------------------------------------------------------| | |
    // | | | | |
    // | | Draw the pixels from the last SKPicture and returns native element clip path pair generated in Render() | | |
    // | |---------------------------------------------------------------------------------------------------------------->| | |
    // | | | | |
    // | | EnqueueRender (the NativeDispatcher will call us back when it thinks it's the best time to do so) | | |
    // | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------->|
    // | | | | |
    // | | | | EnqueueRenderCallback |
    // | | | |<--------------------------------|
    // | | | -----------\ | |
    // | | | | Render() |-| |
    // | | | |----------| | |
    // ------------------------\ | | | | |
    // | some property changes |-| | | | |
    // |-----------------------| | | | | |
    // ------------\ | | | | |
    // | Repeat... |-| | | | |
    // |-----------| | | | | |
    // | | | | |

    Of note, every time Render() is invoked, _lastRenderedFrame is updated:

    _lastRenderedFrame = lastRenderedFrame;

    Now we have a plausible explanation.

    From Lifetime, GC.KeepAlive, handle recycling:

    The GC’s reachability will ensure that objects won’t be collected until we are finished with them. But what if class C has a Finalize() method which closes _handle? When we call C.OperateOnHandle(), we now have a race between the application and the GC / Finalizer. Eventually, that’s a race we’re going to lose.
    …
    So far we’ve seen how the Finalizer thread and the application can race when the resource can be separated from its enclosing object.

    What could be happening is:

    1. Main thread: calls CompositionHelper.Render(), which sets CompositionHelper._lastRenderedFrame.
    2. Render thread: reads CompositionHelper._lastRenderedFrame into lastRenderedFrame, eventually calls canvas.DrawPicture(lastRenderedFrame.frame);
    3. Render thread is suspended within native code. We're "paused" within canvas.DrawPicture(lastRenderedFrame.frame)
    4. GC thread comes along, sees that (picture parameter in SkiaRenderHelper.RenderPicture() / lastRenderedFrame.frame value within CompositionTarget.Draw()) is no longer referenced from managed code.
    5. GC thread invokes lastRenderedFrame.frame.Finalize()
    6. SKPicture (SkiaSharp) / SkBigPicture (Skia) is finalized, presumably memory is zero'd out.
    7. Render thread resumes within native code, accesses the SkBigPicture destroyed in (6).
    8. SIGSEGV

    Furthermore, this is all "random", tied to the interaction between Render thread and the finalizer.

  7. jonpryor commented on Oct 22, 2025

    @jonpryor
    ContributorAuthor

    In retrospect, this actually feels like it should be fixed in SkiaSharp: mono/SkiaSharp#3393

    In the meantime, here's a plausible workaround for Uno: #21660

    No further auditing has been done within unoplatform/uno to look for places where lifetime should be extended. Fixing it in SkiaSharp makes more sense.

    Closing this issue, as the investigation has been concluded.

  8. github-actions commented on May 1, 2026

    @github-actions
    Contributor

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or feature requests.

  9. locked as resolved and limited conversation to collaborators on May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty/tbdtriage/untriaged

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions