-
Notifications
You must be signed in to change notification settings - Fork 0
/
MethodProcessor.cs
145 lines (132 loc) · 4.63 KB
/
MethodProcessor.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
using System;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
namespace TuPack
{
internal class MethodProcessor
{
MethodDefinition method;
MethodBody body;
string name;
TracerFunctions tracerFunctions;
public MethodProcessor(MethodDefinition method, TracerFunctions funcs)
{
this.method = method;
name = method.FullName;
tracerFunctions = funcs;
}
public void Process()
{
try
{
InnerProcess();
}
catch (Exception e)
{
throw new Exception($"An error occurred processing '{method.FullName}'. Error: {e.Message}", e);
}
}
void InnerProcess()
{
body = method.Body;
body.SimplifyMacros();
var firstInstruction = FirstInstructionSkipCtor();
var indexOf = body.Instructions.IndexOf(firstInstruction);
var returnInstruction = FixReturns();
InjectTracerBegin(indexOf);
var indexOfReturn = body.Instructions.IndexOf(returnInstruction);
var beforeReturn = Instruction.Create(OpCodes.Nop);
body.Instructions.Insert(indexOfReturn, beforeReturn);
indexOfReturn = body.Instructions.IndexOf(returnInstruction);
InjectTracerEnd(indexOfReturn);
var handler = new ExceptionHandler(ExceptionHandlerType.Finally)
{
TryStart = firstInstruction,
TryEnd = beforeReturn,
HandlerStart = beforeReturn,
HandlerEnd = returnInstruction
};
body.ExceptionHandlers.Add(handler);
body.InitLocals = true;
body.OptimizeMacros();
}
void InjectTracerBegin(int index)
{
body.Insert(index, new[]
{
Instruction.Create(OpCodes.Ldstr, name),
Instruction.Create(OpCodes.Call, tracerFunctions.Begin)
});
}
void InjectTracerEnd(int index)
{
body.Insert(index, new[]
{
Instruction.Create(OpCodes.Ldstr, name),
Instruction.Create(OpCodes.Call, tracerFunctions.End),
Instruction.Create(OpCodes.Endfinally)
});
}
Instruction FirstInstructionSkipCtor()
{
if (method.IsInstanceConstructor())
{
foreach (var instruction in body.Instructions)
{
if (instruction.OpCode != OpCodes.Call)
{
continue;
}
var methodReference = instruction.Operand as MethodReference;
if (methodReference.Name != ".ctor")
{
continue;
}
if (methodReference.DeclaringType != method.DeclaringType.BaseType)
{
continue;
}
return instruction.Next;
}
}
return body.Instructions.First();
}
Instruction FixReturns()
{
var instructions = body.Instructions;
if (method.ReturnType.FullName == method.Module.TypeSystem.Void.FullName)
{
var lastRet = Instruction.Create(OpCodes.Ret);
foreach (var instruction in instructions)
{
if (instruction.OpCode == OpCodes.Ret)
{
instruction.OpCode = OpCodes.Leave;
instruction.Operand = lastRet;
}
}
instructions.Add(lastRet);
return lastRet;
}
var returnVariable = new VariableDefinition(method.ReturnType);
body.Variables.Add(returnVariable);
var lastLd = Instruction.Create(OpCodes.Ldloc, returnVariable);
for (var index = 0; index < instructions.Count; index++)
{
var instruction = instructions[index];
if (instruction.OpCode == OpCodes.Ret)
{
instruction.OpCode = OpCodes.Stloc;
instruction.Operand = returnVariable;
index++;
instructions.Insert(index, Instruction.Create(OpCodes.Leave, lastLd));
}
}
instructions.Add(lastLd);
instructions.Add(Instruction.Create(OpCodes.Ret));
return lastLd;
}
}
}