From 9898c013ac2a6b158619ea1dbe7e3b961fe5ffd4 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Fri, 31 Jul 2026 17:23:55 +0200 Subject: [PATCH] [wasm][R2R] Create manifest token when type reference wasn't pre-registered When crossgen2 targets WebAssembly, a method that would have registered a type token during IL token resolution can be skipped by the JIT (JitWasmNyiToR2RUnsupported) while a TypeFixupSignature for that type still exists. GetModuleTokenForType's reverse lookup (TryGetExistingEntityHandle) then returns null and the resolver throws NotImplementedException, failing the R2R compile. Create the manifest reference on demand via TryGetEntityHandle while new tokens are still allowed (before ManifestMetadataTableNode sets DisableNewTokens), mirroring how other manifest references are produced. --- .../ReadyToRun/ModuleTokenResolver.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs index 03b9b1e9864996..f4a326b12e76ce 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs @@ -84,6 +84,19 @@ public ModuleToken GetModuleTokenForType(TypeDesc type, bool allowDynamicallyCre { return new ModuleToken(_manifestMutableModule, handle.Value); } + + // The reference wasn't pre-registered while resolving IL tokens. This happens on wasm when the + // method that would have registered it was skipped by the JIT (JitWasmNyiToR2RUnsupported) yet a + // fixup for the type still exists. Dynamically create the manifest reference while new tokens are + // still allowed (this runs before ManifestMetadataTableNode sets DisableNewTokens). + if (!_manifestMutableModule.DisableNewTokens) + { + handle = _manifestMutableModule.TryGetEntityHandle(type); + if (handle.HasValue) + { + return new ModuleToken(_manifestMutableModule, handle.Value); + } + } } // Reverse lookup failed