Skip to content

[mono] Defer NEWOBJ type-load failures to runtime in AOT mode#129715

Merged
pavelsavara merged 2 commits into
dotnet:mainfrom
pavelsavara:mono-aot-newobj-typeload-throw
Jun 24, 2026
Merged

[mono] Defer NEWOBJ type-load failures to runtime in AOT mode#129715
pavelsavara merged 2 commits into
dotnet:mainfrom
pavelsavara:mono-aot-newobj-typeload-throw

Conversation

@pavelsavara

@pavelsavara pavelsavara commented Jun 22, 2026

Copy link
Copy Markdown
Member

Summary

When the AOT compiler imports a newobj whose constructor target cannot be resolved because the declaring type fails to load ΓÇö for example an invalid covariant-return override that produces a TypeLoadException during vtable setup ΓÇö it set a type-load error on the compile and aborted compilation of the whole method, excluding it from the AOT image:

Unable to compile method 'void CMain:RunInvalidTest1 ()' due to:
'Could not find method '.ctor' due to a type load error: ... but with an incompatible signature ...'

Under JIT (or AOT with a JIT fallback) this is fine ΓÇö the method is (re)compiled on first call and the TypeLoadException is raised at the point of use. But under full-AOT (aot-only) there is no JIT, so calling the excluded method instead throws:

System.ExecutionEngineException: Attempting to JIT compile method 'void CMain:RunInvalidTest1 ()'
while running in aot-only mode.

A method that is expected to raise TypeLoadException at runtime therefore crashes with an unrelated ExecutionEngineException instead.

Fix

Handle this the same way the ldfld/ldsfld path already does (method_make_alwaysthrow_typeloadfailure + goto all_bbs_done): in AOT mode, when constructor resolution fails with a type-load error ΓÇö or the declaring class otherwise has a load failure ΓÇö turn the method into one that throws the TypeLoadException at runtime, matching the JIT behavior, instead of aborting compilation.

The change only affects the type-load error path; normal newobj compilation is unchanged.

Testing

Re-enables Loader/classloader/MethodImpl/CovariantReturns/UnitTest/UnitTest_GVM_TypeLoadException on Mono (the separate IsNativeAot annotation is kept). Validated with an x64 Mono LLVM full-AOT build:

  • Before: the four RunInvalidTestN methods were excluded from the image ("Unable to compile" ├ù4) and full-AOT failed with ExecutionEngineException (exit 1).
  • After: the methods are emitted, each invalid type throws TypeLoadException under full-AOT, the test catches all four and passes (exit 100, stable across repeated runs); JIT mode is unaffected.
  • Regression spot-check: other newobj-heavy assemblies still AOT-compile cleanly and run correctly.

Note

This change was authored with the assistance of GitHub Copilot.


Part of the MonoAOT LLVM 23 full-AOT regression set tracked by #129508. Built and tested together with the Emscripten 5.0.6 / LLVM 23 bump on #129396, where the re-enabled UnitTest_GVM_TypeLoadException test passes on the runtime-llvm AllSubsets_Mono_LLVMFULLAOT_RuntimeTests leg.

Contributes to #129508.

When the AOT compiler failed to resolve the constructor target of a newobj because the declaring type fails to load (e.g. an invalid covariant-return override), it set a type-load error on the compile and aborted compilation of the whole method, excluding it from the AOT image. Under full-AOT (aot-only), calling such a method then attempted a JIT compile and threw ExecutionEngineException ('JIT compile while running in aot-only mode') instead of the TypeLoadException the runtime is expected to raise at the point of use.

Handle this the same way the LDFLD path already does: in AOT, turn the method into one that throws the TypeLoadException at runtime via method_make_alwaysthrow_typeloadfailure, matching the JIT behavior. The change only affects the type-load error path; normal newobj compilation is unaffected.

Re-enables the Loader/classloader/MethodImpl/CovariantReturns/UnitTest/UnitTest_GVM_TypeLoadException test on Mono (the IsNativeAot annotation is kept).

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.

Pull request overview

This PR updates Mono’s IL-to-IR importer so that, in AOT compilation, newobj constructor resolution failures caused by type-load failures do not abort method compilation (which would otherwise omit the method from the AOT image). Instead, the method is rewritten into an “always throw TypeLoadException” body to better match runtime/JIT behavior under full-AOT. It also re-enables an existing loader test on Mono by removing a Mono-only ActiveIssue annotation.

Changes:

  • In MONO_CEE_NEWOBJ import, when constructor resolution fails due to a type-load error under AOT, rewrite the method to throw TypeLoadException at runtime and stop importing further IL.
  • In MONO_CEE_NEWOBJ import, when the constructor’s declaring class has a load/init failure under AOT, rewrite the method to throw TypeLoadException instead of aborting compilation.
  • Re-enable UnitTest_GVM_TypeLoadException on Mono by removing a Mono-only ActiveIssue attribute.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/mono/mono/mini/method-to-ir.c Converts AOT newobj type-load failure paths into runtime TypeLoadException throwers instead of excluding methods from the AOT image.
src/tests/Loader/classloader/MethodImpl/CovariantReturns/UnitTest/UnitTest_GVM_TypeLoadException.il Removes Mono-only ActiveIssue skip to re-enable the test on Mono.

Comment thread src/mono/mono/mini/method-to-ir.c
@pavelsavara pavelsavara requested a review from lewing June 22, 2026 20:20
pavelsavara added a commit that referenced this pull request Jun 22, 2026
Re-link the disabled Mono full-AOT tests from the #129508 tracking issue to the individual PRs that fix them (nullabletypes -> #129702, call05_large/small -> #129708, WPF_3226 -> #129710, b143840 -> #129713, UnitTest_GVM_TypeLoadException -> #129715). The tests stay disabled; Runtime_105619 keeps the #129508 link.
@pavelsavara pavelsavara merged commit 603f759 into dotnet:main Jun 24, 2026
128 of 131 checks passed
@pavelsavara pavelsavara deleted the mono-aot-newobj-typeload-throw branch June 24, 2026 14:15
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants