[mono] Defer NEWOBJ type-load failures to runtime in AOT mode#129715
Merged
pavelsavara merged 2 commits intoJun 24, 2026
Conversation
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).
7 tasks
Contributor
There was a problem hiding this comment.
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_NEWOBJimport, when constructor resolution fails due to a type-load error under AOT, rewrite the method to throwTypeLoadExceptionat runtime and stop importing further IL. - In
MONO_CEE_NEWOBJimport, when the constructor’s declaring class has a load/init failure under AOT, rewrite the method to throwTypeLoadExceptioninstead of aborting compilation. - Re-enable
UnitTest_GVM_TypeLoadExceptionon Mono by removing a Mono-onlyActiveIssueattribute.
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. |
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.
This was referenced Jun 22, 2026
Open
BrzVlad
approved these changes
Jun 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the AOT compiler imports a
newobjwhose constructor target cannot be resolved because the declaring type fails to load ΓÇö for example an invalid covariant-return override that produces aTypeLoadExceptionduring vtable setup ΓÇö it set a type-load error on the compile and aborted compilation of the whole method, excluding it from the AOT image:Under JIT (or AOT with a JIT fallback) this is fine ΓÇö the method is (re)compiled on first call and the
TypeLoadExceptionis raised at the point of use. But under full-AOT (aot-only) there is no JIT, so calling the excluded method instead throws:A method that is expected to raise
TypeLoadExceptionat runtime therefore crashes with an unrelatedExecutionEngineExceptioninstead.Fix
Handle this the same way the
ldfld/ldsfldpath 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 theTypeLoadExceptionat runtime, matching the JIT behavior, instead of aborting compilation.The change only affects the type-load error path; normal
newobjcompilation is unchanged.Testing
Re-enables
Loader/classloader/MethodImpl/CovariantReturns/UnitTest/UnitTest_GVM_TypeLoadExceptionon Mono (the separateIsNativeAotannotation is kept). Validated with an x64 Mono LLVM full-AOT build:RunInvalidTestNmethods were excluded from the image ("Unable to compile" ×4) and full-AOT failed withExecutionEngineException(exit 1).TypeLoadExceptionunder full-AOT, the test catches all four and passes (exit 100, stable across repeated runs); JIT mode is unaffected.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_TypeLoadExceptiontest passes on theruntime-llvmAllSubsets_Mono_LLVMFULLAOT_RuntimeTestsleg.Contributes to #129508.