Skip to content

[TrimmableTypeMap] Add exception handling to UCO constructor callbacks (nctor_*_uco) #11176

Closed

Description

@simonrozsival

Summary

The UCO constructor callbacks (nctor_0_uco, nctor_1_uco, etc.) generated by TypeMapAssemblyEmitter.EmitUcoConstructor lack exception handling. If the activation constructor throws, the exception crosses the JNI boundary unhandled, causing a SIGABRT.

Current behavior

// Generated nctor_0_uco — no try/catch
public static void nctor_0_uco(nint jnienv, nint self)
{
    if (!JniEnvironment.WithinNewObjectScope && !JavaPeerProxy.ShouldSkipActivation(self))
    {
        var obj = (MyType)RuntimeHelpers.GetUninitializedObject(typeof(MyType));
        obj..ctor(ref jniRef, JniObjectReferenceOptions.Copy);
        // If ctor throws → unhandled → SIGABRT
    }
}

Expected behavior

Should mirror ManagedPeer.Construct which wraps the body in:

  1. JniEnvironment.BeginMarshalMethod(jnienv, ...)
  2. try { ... } catch (Exception e) { __r?.OnUserUnhandledException(ref envp, e); } finally { JniEnvironment.EndMarshalMethod(ref envp); }

This ensures exceptions are properly reported through the JNI error mechanism instead of crashing the process.

Context

  • Regular UCO wrappers (n_OnCreate_uco_0, etc.) delegate to the existing n_* connector methods which already have BeginMarshalMethod/EndMarshalMethod/try-catch-finally. Only constructor UCOs are affected.
  • ManagedPeer.Construct (the non-trimmable equivalent) has this exception handling — see external/Java.Interop/src/Java.Interop/Java.Interop/ManagedPeer.cs:74-129.
  • The IL emission code is in src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/TypeMapAssemblyEmitter.cs in EmitUcoConstructor.

Files to modify

  • src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/TypeMapAssemblyEmitter.cs — emit BeginMarshalMethod/EndMarshalMethod + try/catch/finally in EmitUcoConstructor
  • May need new memberrefs for BeginMarshalMethod, EndMarshalMethod, OnUserUnhandledException

Part of #10788

Activity

  1. added
    copilot
    on Apr 22, 2026
  2. added
    needs-triage
    on Apr 22, 2026
  3. added this to the .NET 11 milestone on Apr 22, 2026
  4. removed
    needs-triage
    on Apr 22, 2026
  5. locked and limited conversation to collaborators on May 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

copilottrimmable-type-map

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions