-
Notifications
You must be signed in to change notification settings - Fork 533
/
FixAbstractMethodsStep.cs
368 lines (295 loc) · 10 KB
/
FixAbstractMethodsStep.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Mono.Cecil;
using Java.Interop.Tools.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;
using Mono.Tuner;
#if ILLINK
using Microsoft.Android.Sdk.ILLink;
using Resources = Microsoft.Android.Sdk.ILLink.Properties.Resources;
#else // !ILLINK
using Resources = Xamarin.Android.Tasks.Properties.Resources;
#endif // !ILLINK
namespace MonoDroid.Tuner
{
/// <summary>
/// NOTE: this step is subclassed so it can be called directly from Xamarin.Android.Build.Tasks
/// </summary>
public class FixAbstractMethodsStep :
#if ILLINK
BaseMarkHandler
#else // !ILLINK
BaseStep
#endif // !ILLINK
{
#if ILLINK
public override void Initialize (LinkContext context, MarkContext markContext)
{
this.cache = context;
base.Initialize (context, markContext);
markContext.RegisterMarkTypeAction (type => ProcessType (type));
}
#else // !ILLINK
public FixAbstractMethodsStep (IMetadataResolver cache)
{
this.cache = cache;
}
readonly
#endif // !ILLINK
IMetadataResolver cache;
bool CheckShouldProcessAssembly (AssemblyDefinition assembly)
{
if (!Annotations.HasAction (assembly))
Annotations.SetAction (assembly, AssemblyAction.Skip);
if (IsProductOrSdkAssembly (assembly))
return false;
CheckAppDomainUsage (assembly, (string msg) =>
#if ILLINK
Context.LogMessage (MessageContainer.CreateCustomWarningMessage (Context, msg, 6200, new MessageOrigin (), WarnVersion.ILLink5))
#else // !ILLINK
Context.LogMessage (MessageImportance.High, "warning XA2000: " + msg)
#endif // !ILLINK
);
return assembly.MainModule.HasTypeReference ("Java.Lang.Object");
}
void UpdateAssemblyAction (AssemblyDefinition assembly)
{
if (Annotations.GetAction (assembly) == AssemblyAction.Copy)
Annotations.SetAction (assembly, AssemblyAction.Save);
}
#if ILLINK
protected void ProcessType (TypeDefinition type)
{
var assembly = type.Module.Assembly;
if (!CheckShouldProcessAssembly (assembly))
return;
if (!MightNeedFix (type))
return;
if (!FixAbstractMethods (type))
return;
UpdateAssemblyAction (assembly);
MarkAbstractMethodErrorType ();
}
#else // !ILLINK
protected override void ProcessAssembly (AssemblyDefinition assembly)
{
if (!CheckShouldProcessAssembly (assembly))
return;
if (FixAbstractMethods (assembly)) {
Context.SafeReadSymbols (assembly);
UpdateAssemblyAction (assembly);
MarkAbstractMethodErrorType ();
}
}
internal bool FixAbstractMethods (AssemblyDefinition assembly)
{
bool changed = false;
foreach (var type in assembly.MainModule.Types) {
if (MightNeedFix (type))
changed |= FixAbstractMethods (type);
}
return changed;
}
#endif // !ILLINK
readonly HashSet<string> warnedAssemblies = new (StringComparer.Ordinal);
internal void CheckAppDomainUsage (AssemblyDefinition assembly, Action<string> warn)
{
if (!warnedAssemblies.Add (assembly.Name.Name))
return;
if (!assembly.MainModule.HasTypeReference ("System.AppDomain"))
return;
foreach (var mr in assembly.MainModule.GetMemberReferences ()) {
if (mr.ToString ().Contains ("System.AppDomain System.AppDomain::CreateDomain")) {
warn (string.Format (CultureInfo.CurrentCulture, Resources.XA2000, assembly));
break;
}
}
}
bool IsProductOrSdkAssembly (AssemblyDefinition assembly) =>
IsProductOrSdkAssembly (assembly.Name.Name);
public bool IsProductOrSdkAssembly (string assemblyName) =>
Profile.IsSdkAssembly (assemblyName) || Profile.IsProductAssembly (assemblyName);
bool MightNeedFix (TypeDefinition type)
{
return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", cache);
}
bool CompareTypes (TypeReference iType, TypeReference tType)
{
if (iType.IsGenericParameter)
return true;
if (iType.IsArray) {
if (!tType.IsArray)
return false;
return CompareTypes (iType.GetElementType (), tType.GetElementType ());
}
if (iType.IsByReference) {
if (!tType.IsByReference)
return false;
return CompareTypes (iType.GetElementType (), tType.GetElementType ());
}
if (iType.Name != tType.Name)
return false;
if (iType.Namespace != tType.Namespace)
return false;
TypeDefinition iTypeDef = cache.Resolve (iType);
if (iTypeDef == null)
return false;
TypeDefinition tTypeDef = cache.Resolve (tType);
if (tTypeDef == null)
return false;
if (iTypeDef.Module.FullyQualifiedName != tTypeDef.Module.FullyQualifiedName)
return false;
if (iType is Mono.Cecil.GenericInstanceType && tType is Mono.Cecil.GenericInstanceType) {
GenericInstanceType iGType = iType as GenericInstanceType;
GenericInstanceType tGType = tType as GenericInstanceType;
if (iGType.GenericArguments.Count != tGType.GenericArguments.Count)
return false;
for (int i = 0; i < iGType.GenericArguments.Count; i++) {
if (iGType.GenericArguments [i].IsGenericParameter)
continue;
if (!CompareTypes (iGType.GenericArguments [i], tGType.GenericArguments [i]))
return false;
}
}
return true;
}
bool IsInOverrides (MethodDefinition iMethod, MethodDefinition tMethod)
{
if (!tMethod.HasOverrides)
return false;
foreach (var o in tMethod.Overrides)
if (o != null && iMethod.Name == o.Name && iMethod == cache.Resolve (o))
return true;
return false;
}
bool HaveSameSignature (TypeReference iface, MethodDefinition iMethod, MethodDefinition tMethod)
{
if (IsInOverrides (iMethod, tMethod))
return true;
if (iMethod.Name != tMethod.Name)
return false;
if (!CompareTypes (iMethod.MethodReturnType.ReturnType, tMethod.MethodReturnType.ReturnType))
return false;
if (iMethod.Parameters.Count != tMethod.Parameters.Count || iMethod.GenericParameters.Count != tMethod.GenericParameters.Count)
return false;
if (iMethod.HasParameters) {
List<ParameterDefinition> m1p = new List<ParameterDefinition> (iMethod.Parameters);
List<ParameterDefinition> m2p = new List<ParameterDefinition> (tMethod.Parameters);
for (int i = 0; i < m1p.Count; i++) {
if (!CompareTypes (m1p [i].ParameterType, m2p [i].ParameterType))
return false;
}
}
if (iMethod.HasGenericParameters) {
List<GenericParameter> m1p = new List<GenericParameter> (iMethod.GenericParameters);
List<GenericParameter> m2p = new List<GenericParameter> (tMethod.GenericParameters);
for (int i = 0; i < m1p.Count; i++)
if (!CompareTypes (m1p [i], m2p [i]))
return false;
}
return true;
}
bool FixAbstractMethods (TypeDefinition type)
{
if (!type.HasInterfaces)
return false;
bool rv = false;
List<MethodDefinition> typeMethods = new List<MethodDefinition> (type.Methods);
foreach (var baseType in type.GetBaseTypes (cache))
typeMethods.AddRange (baseType.Methods);
foreach (var ifaceInfo in type.Interfaces) {
var iface = ifaceInfo.InterfaceType;
var ifaceDef = cache.Resolve (iface);
if (ifaceDef == null) {
LogMessage ($"Unable to unresolve interface: {iface.FullName}");
continue;
}
if (ifaceDef.HasGenericParameters)
continue;
foreach (var iMethod in ifaceDef.Methods.Where (m => m.IsAbstract)) {
bool exists = false;
foreach (var tMethod in typeMethods) {
if (HaveSameSignature (iface, iMethod, tMethod)) {
exists = true;
break;
}
}
if (!exists) {
AddNewExceptionMethod (type, iMethod);
rv = true;
}
}
}
return rv;
}
TypeReference TryImportType (TypeDefinition declaringType, TypeReference type)
{
if (type.IsGenericParameter)
return type;
return declaringType.Module.Import (type);
}
void AddNewExceptionMethod (TypeDefinition type, MethodDefinition method)
{
var newMethod = new MethodDefinition (method.Name, (method.Attributes | MethodAttributes.Final) & ~MethodAttributes.Abstract, TryImportType (type, method.ReturnType));
foreach (var paramater in method.Parameters)
newMethod.Parameters.Add (new ParameterDefinition (paramater.Name, paramater.Attributes, TryImportType (type, paramater.ParameterType)));
var ilP = newMethod.Body.GetILProcessor ();
ilP.Append (ilP.Create (Mono.Cecil.Cil.OpCodes.Newobj, type.Module.Import (AbstractMethodErrorConstructor)));
ilP.Append (ilP.Create (Mono.Cecil.Cil.OpCodes.Throw));
type.Methods.Add (newMethod);
LogMessage ($"Added method: {method} to type: {type.FullName} scope: {type.Scope}");
}
MethodDefinition abstractMethodErrorConstructor;
MethodDefinition AbstractMethodErrorConstructor {
get {
if (abstractMethodErrorConstructor != null)
return abstractMethodErrorConstructor;
var assembly = GetMonoAndroidAssembly ();
if (assembly != null) {
var errorException = assembly.MainModule.GetType ("Java.Lang.AbstractMethodError");
if (errorException != null) {
foreach (var method in errorException.Methods) {
if (method.Name == ".ctor" && !method.HasParameters) {
abstractMethodErrorConstructor = method;
break;
}
}
}
}
if (abstractMethodErrorConstructor == null)
throw new Exception ("Unable to find Java.Lang.AbstractMethodError constructor in Mono.Android assembly");
return abstractMethodErrorConstructor;
}
}
bool markedAbstractMethodErrorType;
void MarkAbstractMethodErrorType ()
{
if (markedAbstractMethodErrorType)
return;
markedAbstractMethodErrorType = true;
var td = AbstractMethodErrorConstructor.DeclaringType;
Annotations.Mark (td);
Annotations.SetPreserve (td, TypePreserve.Nothing);
Annotations.AddPreservedMethod (td, AbstractMethodErrorConstructor);
}
public virtual void LogMessage (string message)
{
Context.LogMessage (message);
}
protected virtual AssemblyDefinition GetMonoAndroidAssembly ()
{
#if !ILLINK
foreach (var assembly in Context.GetAssemblies ()) {
if (assembly.Name.Name == "Mono.Android")
return assembly;
}
return null;
#else // ILLINK
return Context.GetLoadedAssembly ("Mono.Android");
#endif // ILLINK
}
}
}