Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

Commit 03c2272

Browse files
authored
[jnimarshalmethod-gen] Fix type resolution crash (#706)
The TypeMover was crashing, when we cannot resolve the type during register method optimization. Like this: Value cannot be null. Parameter name: key System.ArgumentNullException: Value cannot be null. Parameter name: key at System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) [0x00175] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/debug/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:470 at System.Collections.Generic.Dictionary`2[TKey,TValue].ContainsKey (TKey key) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/debug/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:286 at Xamarin.Android.Tools.JniMarshalMethodGenerator.TypeMover.Resolve (Mono.Cecil.TypeReference type) [0x00001] in /Users/rodo/git/xa-clean/external/Java.Interop/tools/jnimarshalmethod-gen/TypeMover.cs:143 at Xamarin.Android.Tools.JniMarshalMethodGenerator.TypeMover.GetActionConstructor (Mono.Cecil.TypeReference type, Mono.Cecil.ModuleDefinition module) [0x00012] in /Users/rodo/git/xa-clean/external/Java.Interop/tools/jnimarshalmethod-gen/TypeMover.cs:335 at Xamarin.Android.Tools.JniMarshalMethodGenerator.TypeMover.AnalyzeAndImprove (Mono.Collections.Generic.Collection`1[T] instructions, Mono.Collections.Generic.Collection`1[T] newInstructions, System.Int32 idx, System.String typeName, System.Int32& skipCount, Mono.Cecil.ModuleDefinition module) [0x003b6] in /Users/rodo/git/xa-clean/external/Java.Interop/tools/jnimarshalmethod-gen/TypeMover.cs:417 The tool was crashing on IL like this: IL_0122: ldstr "Delegate18$1" IL_0127: ldc.i4.1 IL_0128: call [mscorlib]System.Type [mscorlib]System.Type::GetType(string, bool) The fix makes us more error resistant. It doesn't fix the source of the problem, which will be fixed elsewhere. More information about the issue which led to the crash: https://github.com/xamarin/java.interop/issues/709
1 parent ac914ce commit 03c2272

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

‎tools/jnimarshalmethod-gen/TypeMover.cs‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class TypeMover
1515
AssemblyDefinitionDestination{get;}
1616
stringDestinationPath{get;}
1717
Dictionary<string,System.Reflection.Emit.TypeBuilder>Types{get;}
18+
DirectoryAssemblyResolverResolver{get;}
1819

1920
MethodReferenceconsoleWriteLine;
2021
TypeDefinitionCachecache;
@@ -25,10 +26,11 @@ public TypeMover (AssemblyDefinition source, AssemblyDefinition destination, str
2526
Destination=destination;
2627
DestinationPath=destinationPath;
2728
Types=types;
29+
Resolver=resolver;
2830
this.cache=cache;
2931

3032
if(App.Debug){
31-
consoleWriteLine=GetSingleParameterMethod(resolver,Destination.MainModule,"mscorlib","System.Console","WriteLine","System.String");
33+
consoleWriteLine=GetSingleParameterMethod(Destination.MainModule,"mscorlib","System.Console","WriteLine","System.String");
3234
if(consoleWriteLine==null){
3335
App.Warning(Message.WarningUnableToFindSCWriteLine);
3436
App.Debug=false;
@@ -400,6 +402,15 @@ bool AnalyzeAndImprove (Mono.Collections.Generic.Collection<Instruction> instruc
400402
returnfalse;
401403

402404
delegateType=module.GetType(delegateTypeName);
405+
if(delegateType==null){
406+
vart=Type.GetType(delegateTypeName);
407+
if(t==null)
408+
returnfalse;
409+
410+
delegateType=GetType(t.Assembly.GetName().ToString(),delegateTypeName);
411+
if(delegateType==null)
412+
returnfalse;
413+
}
403414

404415
skipCount=11;
405416
customDelegate=true;
@@ -516,16 +527,18 @@ MethodDefinition Duplicate (MethodDefinition src, ModuleDefinition module, TypeD
516527
returnmd;
517528
}
518529

519-
MethodReferenceGetSingleParameterMethod(DirectoryAssemblyResolverresolver,ModuleDefinitionmodule,stringassemblyName,stringtypeName,stringmethodName,stringparameterTypeName)
530+
TypeDefinitionGetType(stringassemblyName,stringtypeName)
520531
{
521-
varassembly=resolver.Resolve(assemblyName);
532+
varassembly=Resolver.Resolve(assemblyName);
522533
if(assembly==null)
523534
returnnull;
524535

525-
vartypeTD=assembly.MainModule.GetType(typeName);
526-
if(typeTD==null)
527-
returnnull;
536+
returnassembly.MainModule.GetType(typeName);
537+
}
528538

539+
MethodReferenceGetSingleParameterMethod(ModuleDefinitionmodule,stringassemblyName,stringtypeName,stringmethodName,stringparameterTypeName)
540+
{
541+
vartypeTD=GetType(assemblyName,typeName);
529542
foreach(varmdintypeTD.Methods)
530543
if(md.Name==methodName&&md.HasParameters&&md.Parameters.Count==1&&md.Parameters[0].ParameterType.FullName==parameterTypeName)
531544
returnGetUpdatedMethod(md,module);

0 commit comments

Comments
 (0)