Skip to content

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: #709
1 parent ac914ce commit 03c2272

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

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
AssemblyDefinition Destination { get; }
1616
string DestinationPath { get; }
1717
Dictionary<string, System.Reflection.Emit.TypeBuilder> Types { get; }
18+
DirectoryAssemblyResolver Resolver { get; }
1819

1920
MethodReference consoleWriteLine;
2021
TypeDefinitionCache cache;
@@ -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
return false;
401403

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

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

519-
MethodReference GetSingleParameterMethod (DirectoryAssemblyResolver resolver, ModuleDefinition module, string assemblyName, string typeName, string methodName, string parameterTypeName)
530+
TypeDefinition GetType (string assemblyName, string typeName)
520531
{
521-
var assembly = resolver.Resolve (assemblyName);
532+
var assembly = Resolver.Resolve (assemblyName);
522533
if (assembly == null)
523534
return null;
524535

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

539+
MethodReference GetSingleParameterMethod (ModuleDefinition module, string assemblyName, string typeName, string methodName, string parameterTypeName)
540+
{
541+
var typeTD = GetType (assemblyName, typeName);
529542
foreach (var md in typeTD.Methods)
530543
if (md.Name == methodName && md.HasParameters && md.Parameters.Count == 1 && md.Parameters [0].ParameterType.FullName == parameterTypeName)
531544
return GetUpdatedMethod (md, module);

0 commit comments

Comments
 (0)