Skip to content

[NativeAOT] Honor Android's JNI exception policy - #13153

Merged
simonrozsival merged 1 commit into
mainfrom
simonrozsival-nativeaot-exception-policy
Oct 16, 2026
Merged

simonrozsival merged 1 commit into
mainfrom
simonrozsival-nativeaot-exception-policy

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

NativeAOT's JreRuntime inherits JniRuntime.OnUserUnhandledException directly, unlike CoreCLR's AndroidRuntime. Consequently, exceptions from JniMarshal.SafeInvoke* callbacks bypass AndroidEnvironment.UnhandledExceptionRaiser, including its Handled policy.

Add the equivalent NativeAOT override: call TryRaiseUnhandledException exactly 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 JniRuntimeExceptionPolicy category 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 existing SafeInvokeAction/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.

Command Outcome
make prepare CONFIGURATION=Release Passed
make all CONFIGURATION=Release Initially failed during local workload setup because API 37.1 reference output was missing; solution compilation itself passed
./dotnet-local.sh build Microsoft.Android.slnx -t:BuildExtraApiLevels -c Release -v minimal Passed; generated missing API 37.1/37.2 outputs
make all CONFIGURATION=Release (retry) Passed, including local workload configuration
git diff --check Passed

Release 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 minimal

Release ARM64 CoreCLR tests — 10 passed, 0 failed, 0 skipped:

cd tests/Mono.Android-Tests/Mono.Android-Tests
ANDROID_SERIAL=emulator-5584 ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:RuntimeIdentifier=android-arm64 -p:IncludeCategories=JniRuntimeExceptionPolicy '-p:AdbTarget=-s emulator-5584' --device emulator-5584 --report-trx --results-directory ../../../bin/TestRelease/JniRuntimeExceptionPolicy-CoreCLR

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 minimal

Release ARM64 NativeAOT tests — 10 passed, 0 failed, 0 skipped:

cd tests/Mono.Android-Tests/Mono.Android-Tests
ANDROID_SERIAL=emulator-5584 ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:RuntimeIdentifier=android-arm64 -p:IncludeCategories=JniRuntimeExceptionPolicy '-p:AdbTarget=-s emulator-5584' -p:PublishAot=true --device emulator-5584 --report-trx --results-directory ../../../bin/TestRelease/JniRuntimeExceptionPolicy-NativeAOT

NativeAOT was confirmed by ILC compilation, the packaged libMono.Android.NET-Tests.so, and device NativeAotRuntimeProvider initialization. CoreCLR used the default CoreCLR runtime pack and loaded libmonodroid.so. Builds retain existing runtime/test trim/AOT warnings. An initial CoreCLR test invocation without --device was 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.

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]>
Copilot AI balanced review requested due to automatic review settings October 15, 2026 12:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 Handled before 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

@simonrozsival simonrozsival changed the title [NativeAOT] Honor Android's JNI exception policy [Unsafe][NativeAOT] Honor Android's JNI exception policy Oct 15, 2026
@simonrozsival simonrozsival changed the title [Unsafe][NativeAOT] Honor Android's JNI exception policy [NativeAOT] Honor Android's JNI exception policy Oct 15, 2026
@jonathanpeppers

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13153

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 💡 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”

@simonrozsival
simonrozsival enabled auto-merge (squash) October 16, 2026 07:21
@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Oct 16, 2026
@simonrozsival

Copy link
Copy Markdown
Member Author

@dalexsoto review

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@simonrozsival
simonrozsival merged commit 5ba84d9 into main Oct 16, 2026
42 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-nativeaot-exception-policy branch October 16, 2026 08:11
simonrozsival added a commit that referenced this pull request Oct 16, 2026
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]>
simonrozsival added a commit that referenced this pull request Oct 30, 2026
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]>
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants