-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathAddKeepAlivesStep.cs
More file actions
147 lines (119 loc) · 4.05 KB
/
AddKeepAlivesStep.cs
File metadata and controls
147 lines (119 loc) · 4.05 KB
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
using System;
using System.Linq;
using Java.Interop.Tools.Cecil;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Linker.Steps;
using Xamarin.Android.Tasks;
namespace MonoDroid.Tuner
{
public class AddKeepAlivesStep : BaseStep, IAssemblyModifierPipelineStep
{
public void ProcessAssembly (AssemblyDefinition assembly, StepContext context)
{
// Only run this step on user Android assemblies
if (!context.IsAndroidUserAssembly)
return;
context.IsAssemblyModified |= AddKeepAlives (assembly);
}
internal bool AddKeepAlives (AssemblyDefinition assembly)
{
if (!assembly.MainModule.HasTypeReference ("Java.Lang.Object"))
return false;
// Anything that was built against .NET for Android will have
// keep-alives already compiled in.
if (MonoAndroidHelper.IsDotNetAndroidAssembly (assembly))
return false;
bool changed = false;
foreach (TypeDefinition type in assembly.MainModule.Types)
changed |= ProcessType (type);
return changed;
}
bool ProcessType (TypeDefinition type)
{
bool changed = false;
if (MightNeedFix (type))
changed |= AddKeepAlives (type);
if (type.HasNestedTypes) {
foreach (var t in type.NestedTypes) {
changed |= ProcessType (t);
}
}
return changed;
}
bool MightNeedFix (TypeDefinition type)
{
return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", Context);
}
MethodDefinition? methodKeepAlive = null;
bool AddKeepAlives (TypeDefinition type)
{
bool changed = false;
foreach (MethodDefinition method in type.Methods) {
if (method.Parameters.Count == 0)
continue;
if (!method.CustomAttributes.Any (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute"))
continue;
var instructions = method.Body.Instructions;
var found = false;
for (int off = Math.Max (0, instructions.Count - 6); off < instructions.Count; off++) {
var current = instructions [off];
if (current.OpCode == OpCodes.Call && current.Operand.ToString ().Contains ("System.GC::KeepAlive")) {
found = true;
break;
}
}
if (found)
continue;
var processor = method.Body.GetILProcessor ();
var module = method.DeclaringType.Module;
var end = instructions.Last ();
if (end.Previous.OpCode == OpCodes.Endfinally)
end = end.Previous;
for (int i = 0; i < method.Parameters.Count; i++) {
if (method.Parameters [i].ParameterType.IsValueType || method.Parameters [i].ParameterType.FullName == "System.String")
continue;
if (methodKeepAlive == null)
methodKeepAlive = GetKeepAliveMethod ();
if (methodKeepAlive == null) {
LogMessage ("Unable to add KeepAlive call, did not find System.GC.KeepAlive method.");
break;
}
processor.InsertBefore (end, GetLoadArgumentInstruction (method.IsStatic ? i : i + 1, method.Parameters [i]));
processor.InsertBefore (end, Instruction.Create (OpCodes.Call, module.ImportReference (methodKeepAlive)));
changed = true;
}
}
return changed;
}
protected virtual AssemblyDefinition GetCorlibAssembly ()
{
return Context.GetAssembly ("System.Private.CoreLib");
}
MethodDefinition? GetKeepAliveMethod ()
{
var corlibAssembly = GetCorlibAssembly ();
if (corlibAssembly == null)
return null;
var gcType = Extensions.GetType (corlibAssembly, "System.GC");
if (gcType == null)
return null;
return Extensions.GetMethod (gcType, "KeepAlive", new string [] { "System.Object" });
}
public override void LogMessage (string message)
{
Context.LogMessage (message);
}
// Adapted from src/Mono.Android.Export/Mono.CodeGeneration/CodeArgumentReference.cs
static Instruction GetLoadArgumentInstruction (int argNum, ParameterDefinition parameter)
{
switch (argNum) {
case 0: return Instruction.Create (OpCodes.Ldarg_0);
case 1: return Instruction.Create (OpCodes.Ldarg_1);
case 2: return Instruction.Create (OpCodes.Ldarg_2);
case 3: return Instruction.Create (OpCodes.Ldarg_3);
default: return Instruction.Create (OpCodes.Ldarg, parameter);
}
}
}
}