Repository navigation
[NativeAOT] Honor Android's JNI exception policy - #13153
Conversation
NativeAOT bypasses AndroidEnvironment.UnhandledExceptionRaiser when a SafeInvoke callback fails. Apply the same policy as CoreCLR before scheduling the exception for Java publication and debugger notification. A handled exception returns without publishing a Java throwable. Add direct SafeInvoke and Java-driven UCO action/function regression tests for exactly-once notification, exception identity, pending-exception state, handled suppression, default results, and Java catch/finally/rethrow. Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
The runtime exception-boundary change warrants human sign-off because reported device validation and current CI were not independently verified.
0 open findings
What changed in this PR
Aligns NativeAOT’s JNI callback exception handling with Android’s existing unhandled-exception policy.
Changes:
- Notifies Android exception handlers once and respects
Handledbefore base exception processing. - Adds ten regression cases covering policy notification, return values, exception identity, and Java catch/finally behavior.
- Integrates and preserves the Java test fixture.
| File | Description |
|---|---|
tests/Mono.Android-Tests/Mono.Android-Tests/proguard.cfg |
Preserves the Java fixture during shrinking. |
tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj |
Includes tests and disables fixture binding generation. |
tests/Mono.Android-Tests/Mono.Android-Tests/java/net/dot/android/test/JniRuntimeExceptionPolicy.java |
Exercises callback continuation, catch, finally, and rethrow. |
tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/JniRuntimeExceptionPolicyTests.cs |
Verifies exception policy and JNI transport behavior. |
src/Microsoft.Android.Runtime.NativeAOT/Java.Interop/JreRuntime.cs |
Applies Android exception policy before base handling. |
🧠 Review effort: Balanced
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
✅ LGTM — 0 errors, 0 warnings, 1 documentation suggestion.
The NativeAOT override correctly matches the established CoreCLR exception-policy path: handled exceptions stop before JNI publication, while unhandled exceptions retain the existing deferred transition behavior. The added device tests cover both action/function callbacks, handled and unhandled paths, exception identity, Java catch/finally/rethrow behavior, and successful returns. All 42 checks, including the Azure dotnet-android build, are green.
Generated by Android PR Reviewer for #13153 · copilot · gpt56 · 66.5 AIC · ⌖ 11.1 AIC · ⊞ 26.4K
Comment /review to run again
|
|
||
| public override void OnUserUnhandledException (ref JniTransition transition, Exception e) | ||
| { | ||
| if (Android.Runtime.AndroidEnvironment.TryRaiseUnhandledException (e)) { |
There was a problem hiding this comment.
🤖 💡 Documentation — This intentionally mirrors AndroidRuntime.OnUserUnhandledException, but the NativeAOT copy omits that implementation’s rationale and link to #10654. Please carry over the explanation and note that the two runtime paths must stay aligned, so a future policy change is less likely to update only one runtime.
Rule: Comments explain “why”
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
The NativeAOT hook now applies Android's unhandled-exception policy exactly once before the existing deferred JNI publication. Handled exceptions stop before the base path; unhandled callbacks preserve the existing transition and notification order. The callback, test-discovery, UCO registration and fixture-packaging paths are consistent with that contract, with no blocking issue remaining.
Read implementation callback bodies and signatures instead of inferring exception safety from assembly format or package versions. Forward trusted helper and complete debugger-aware inline boundaries directly; protect raw or unavailable managed callbacks with SafeInvoke and retain equivalent full policy for signatures outside its overload set. Keep already-native UCO callbacks native-only, remove redundant legacy bridge/report calls, and include ownership in reuse, fingerprints, and cross-RID consistency checks. Add adversarial metadata, emitted-IL, body-only build invalidation, and generated-UCO device coverage. NativeAOT event-policy equivalence requires the independent JreRuntime policy fix in #13153; it is not duplicated here. Co-authored-by: Copilot App <[email protected]>
Read implementation callback bodies and signatures instead of inferring exception safety from assembly format or package versions. Forward trusted helper and complete debugger-aware inline boundaries directly; protect raw or unavailable managed callbacks with SafeInvoke and retain equivalent full policy for signatures outside its overload set. Keep already-native UCO callbacks native-only, remove redundant legacy bridge/report calls, and include ownership in reuse, fingerprints, and cross-RID consistency checks. Add adversarial metadata, emitted-IL, body-only build invalidation, and generated-UCO device coverage. NativeAOT event-policy equivalence requires the independent JreRuntime policy fix in #13153; it is not duplicated here. Co-authored-by: Copilot App <[email protected]>
Pull Request
title and
description
should follow the
commit-messages.mdworkflow documentation, and in particular should include:NativeAOT's
JreRuntimeinheritsJniRuntime.OnUserUnhandledExceptiondirectly, unlike CoreCLR'sAndroidRuntime. Consequently, exceptions fromJniMarshal.SafeInvoke*callbacks bypassAndroidEnvironment.UnhandledExceptionRaiser, including itsHandledpolicy.Add the equivalent NativeAOT override: call
TryRaiseUnhandledExceptionexactly once, return when handled, and otherwise delegate to the base implementation. This keeps policy notification before deferred Java exception publication and debugger notification, without changing Java.Interop APIs/timing or invoking the legacy unhandled-exception path a second time.No linked issue. This PR is independent and based on
main.Add the uniquely named
JniRuntimeExceptionPolicycategory and Java fixture. Ten cases cover success returns, handled/unhandled exceptions, original exception identity, exactly-once events, no pending Java exception during notification (including a usable Java call), suppression of publication when handled, default non-void failure results, and Java catch/finally/rethrow. Java calls enter registered UCO callbacks using the existingSafeInvokeAction/SafeInvokeFunc; a supplemental direct function test observes the default result and pending throwable.Local validation
Built this worktree's own SDK and tested on its owned API 35 ARM64 emulator,
copilot_nativeaot_policy_21aef999/emulator-5584.make prepare CONFIGURATION=Releasemake all CONFIGURATION=Release./dotnet-local.sh build Microsoft.Android.slnx -t:BuildExtraApiLevels -c Release -v minimalmake all CONFIGURATION=Release(retry)git diff --checkRelease ARM64 CoreCLR build/install — passed:
ANDROID_SERIAL=emulator-5584 ./dotnet-local.sh build -t:Install -c Release tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:RuntimeIdentifier=android-arm64 -p:IncludeCategories=JniRuntimeExceptionPolicy '-p:AdbTarget=-s emulator-5584' -v minimalRelease ARM64 CoreCLR tests — 10 passed, 0 failed, 0 skipped:
Release ARM64 NativeAOT build/install — passed:
ANDROID_SERIAL=emulator-5584 ./dotnet-local.sh build -t:Install -c Release tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:RuntimeIdentifier=android-arm64 -p:IncludeCategories=JniRuntimeExceptionPolicy '-p:AdbTarget=-s emulator-5584' -p:PublishAot=true -v minimalRelease ARM64 NativeAOT tests — 10 passed, 0 failed, 0 skipped:
NativeAOT was confirmed by ILC compilation, the packaged
libMono.Android.NET-Tests.so, and deviceNativeAotRuntimeProviderinitialization. CoreCLR used the default CoreCLR runtime pack and loadedlibmonodroid.so. Builds retain existing runtime/test trim/AOT warnings. An initial CoreCLR test invocation without--devicewas rejected because multiple emulators were available; the explicit-device commands above passed.No debugger was attached. These results verify policy and JNI transport, not debugger break location or Continue behavior. Handler/publication failures and generated callback compatibility are outside this PR's scope.