/
MethodBodyWriterBase.cs
430 lines (394 loc) · 15.3 KB
/
MethodBodyWriterBase.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// dnlib: See LICENSE.txt for more info
using System.Collections.Generic;
using dnlib.DotNet.Emit;
namespace dnlib.DotNet.Writer {
/// <summary>
/// Base class of all CIL method body writers
/// </summary>
public abstract class MethodBodyWriterBase {
/// <summary/>
protected IList<Instruction> instructions;
/// <summary/>
protected IList<ExceptionHandler> exceptionHandlers;
readonly Dictionary<Instruction, uint> offsets = new Dictionary<Instruction, uint>();
uint firstInstructionOffset;
int errors;
MaxStackCalculator maxStackCalculator = MaxStackCalculator.Create();
/// <summary>
/// <c>true</c> if there was at least one error
/// </summary>
public bool ErrorDetected => errors > 0;
internal MethodBodyWriterBase() {
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="instructions">All instructions</param>
/// <param name="exceptionHandlers">All exception handlers</param>
protected MethodBodyWriterBase(IList<Instruction> instructions, IList<ExceptionHandler> exceptionHandlers) {
this.instructions = instructions;
this.exceptionHandlers = exceptionHandlers;
}
internal void Reset(IList<Instruction> instructions, IList<ExceptionHandler> exceptionHandlers) {
this.instructions = instructions;
this.exceptionHandlers = exceptionHandlers;
offsets.Clear();
firstInstructionOffset = 0;
errors = 0;
}
/// <summary>
/// Called when an error is detected (eg. a null pointer). The error can be
/// ignored but the method won't be valid.
/// </summary>
/// <param name="message">Error message</param>
protected void Error(string message) {
errors++;
ErrorImpl(message);
}
/// <summary>
/// Called when an error is detected (eg. a null pointer). The error can be
/// ignored but the method won't be valid.
/// </summary>
/// <param name="message">Error message</param>
protected virtual void ErrorImpl(string message) {
}
/// <summary>
/// Gets max stack value
/// </summary>
protected uint GetMaxStack() {
if (instructions.Count == 0)
return 0;
maxStackCalculator.Reset(instructions, exceptionHandlers);
if (!maxStackCalculator.Calculate(out uint maxStack)) {
Error("Error calculating max stack value. If the method's obfuscated, set CilBody.KeepOldMaxStack or MetadataOptions.Flags (KeepOldMaxStack, global option) to ignore this error. Otherwise fix your generated CIL code so it conforms to the ECMA standard.");
maxStack += 8;
}
return maxStack;
}
/// <summary>
/// Gets the offset of an instruction
/// </summary>
/// <param name="instr">The instruction</param>
/// <returns>The offset or <c>0</c> if <paramref name="instr"/> is <c>null</c> or not
/// present in the list of all instructions.</returns>
protected uint GetOffset(Instruction instr) {
if (instr is null) {
Error("Instruction is null");
return 0;
}
if (offsets.TryGetValue(instr, out uint offset))
return offset;
Error("Found some other method's instruction or a removed instruction. You probably removed an instruction that is the target of a branch instruction or an instruction that's the first/last instruction in an exception handler.");
return 0;
}
/// <summary>
/// Initializes instruction offsets and returns the total code size
/// </summary>
/// <returns>Size of code</returns>
protected uint InitializeInstructionOffsets() {
uint offset = 0;
var instructions = this.instructions;
for (int i = 0; i < instructions.Count; i++) {
var instr = instructions[i];
if (instr is null)
continue;
offsets[instr] = offset;
offset += GetSizeOfInstruction(instr);
}
return offset;
}
/// <summary>
/// Gets the size of an instruction
/// </summary>
/// <param name="instr">The instruction</param>
/// <returns>Size of the instruction in bytes</returns>
protected virtual uint GetSizeOfInstruction(Instruction instr) => (uint)instr.GetSize();
/// <summary>
/// Writes all instructions to <paramref name="writer"/> at its current offset
/// </summary>
/// <param name="writer">The instruction writer</param>
/// <returns>Number of bytes written</returns>
protected uint WriteInstructions(ref ArrayWriter writer) {
firstInstructionOffset = (uint)writer.Position;
var instructions = this.instructions;
for (int i = 0; i < instructions.Count; i++) {
var instr = instructions[i];
if (instr is null)
continue;
WriteInstruction(ref writer, instr);
}
return ToInstructionOffset(ref writer);
}
/// <summary>
/// Gets the current offset in the instruction stream. This offset is relative to
/// the first written instruction.
/// </summary>
/// <param name="writer">The instruction writer</param>
/// <returns>Current offset, relative to the first written instruction</returns>
protected uint ToInstructionOffset(ref ArrayWriter writer) => (uint)writer.Position - firstInstructionOffset;
/// <summary>
/// Writes an instruction
/// </summary>
/// <param name="writer">The instruction writer</param>
/// <param name="instr">The instruction</param>
protected virtual void WriteInstruction(ref ArrayWriter writer, Instruction instr) {
WriteOpCode(ref writer, instr);
WriteOperand(ref writer, instr);
}
/// <summary>
/// Writes an instruction's opcode
/// </summary>
/// <param name="writer">The instruction writer</param>
/// <param name="instr">The instruction</param>
protected void WriteOpCode(ref ArrayWriter writer, Instruction instr) {
var code = instr.OpCode.Code;
var hi = (ushort)code >> 8;
if ((ushort)code <= 0xFF)
writer.WriteByte((byte)code);
else if (hi == 0xFE || (hi >= 0xF0 && hi <= 0xFB)) {
writer.WriteByte((byte)((ushort)code >> 8));
writer.WriteByte((byte)code);
}
else if (code == Code.UNKNOWN1)
writer.WriteByte((byte)Code.Nop);
else if (code == Code.UNKNOWN2)
writer.WriteUInt16((ushort)(((ushort)Code.Nop << 8) | Code.Nop));
else {
Error("Unknown instruction");
writer.WriteByte((byte)Code.Nop);
}
}
/// <summary>
/// Writes an instruction's operand
/// </summary>
/// <param name="writer">The instruction writer</param>
/// <param name="instr">The instruction</param>
protected void WriteOperand(ref ArrayWriter writer, Instruction instr) {
switch (instr.OpCode.OperandType) {
case OperandType.InlineBrTarget: WriteInlineBrTarget(ref writer, instr); break;
case OperandType.InlineField: WriteInlineField(ref writer, instr); break;
case OperandType.InlineI: WriteInlineI(ref writer, instr); break;
case OperandType.InlineI8: WriteInlineI8(ref writer, instr); break;
case OperandType.InlineMethod: WriteInlineMethod(ref writer, instr); break;
case OperandType.InlineNone: WriteInlineNone(ref writer, instr); break;
case OperandType.InlinePhi: WriteInlinePhi(ref writer, instr); break;
case OperandType.InlineR: WriteInlineR(ref writer, instr); break;
case OperandType.InlineSig: WriteInlineSig(ref writer, instr); break;
case OperandType.InlineString: WriteInlineString(ref writer, instr); break;
case OperandType.InlineSwitch: WriteInlineSwitch(ref writer, instr); break;
case OperandType.InlineTok: WriteInlineTok(ref writer, instr); break;
case OperandType.InlineType: WriteInlineType(ref writer, instr); break;
case OperandType.InlineVar: WriteInlineVar(ref writer, instr); break;
case OperandType.ShortInlineBrTarget: WriteShortInlineBrTarget(ref writer, instr); break;
case OperandType.ShortInlineI: WriteShortInlineI(ref writer, instr); break;
case OperandType.ShortInlineR: WriteShortInlineR(ref writer, instr); break;
case OperandType.ShortInlineVar: WriteShortInlineVar(ref writer, instr); break;
default:
Error("Unknown operand type");
break;
}
}
/// <summary>
/// Writes an <see cref="OperandType.InlineBrTarget"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteInlineBrTarget(ref ArrayWriter writer, Instruction instr) {
uint displ = GetOffset(instr.Operand as Instruction) - (ToInstructionOffset(ref writer) + 4);
writer.WriteUInt32(displ);
}
/// <summary>
/// Writes an <see cref="OperandType.InlineField"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected abstract void WriteInlineField(ref ArrayWriter writer, Instruction instr);
/// <summary>
/// Writes an <see cref="OperandType.InlineI"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteInlineI(ref ArrayWriter writer, Instruction instr) {
if (instr.Operand is int)
writer.WriteInt32((int)instr.Operand);
else {
Error("Operand is not an Int32");
writer.WriteInt32(0);
}
}
/// <summary>
/// Writes an <see cref="OperandType.InlineI8"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteInlineI8(ref ArrayWriter writer, Instruction instr) {
if (instr.Operand is long)
writer.WriteInt64((long)instr.Operand);
else {
Error("Operand is not an Int64");
writer.WriteInt64(0);
}
}
/// <summary>
/// Writes an <see cref="OperandType.InlineMethod"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected abstract void WriteInlineMethod(ref ArrayWriter writer, Instruction instr);
/// <summary>
/// Writes an <see cref="OperandType.InlineNone"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteInlineNone(ref ArrayWriter writer, Instruction instr) {
}
/// <summary>
/// Writes an <see cref="OperandType.InlinePhi"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteInlinePhi(ref ArrayWriter writer, Instruction instr) {
}
/// <summary>
/// Writes an <see cref="OperandType.InlineR"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteInlineR(ref ArrayWriter writer, Instruction instr) {
if (instr.Operand is double)
writer.WriteDouble((double)instr.Operand);
else {
Error("Operand is not a Double");
writer.WriteDouble(0);
}
}
/// <summary>
/// Writes an <see cref="OperandType.InlineSig"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected abstract void WriteInlineSig(ref ArrayWriter writer, Instruction instr);
/// <summary>
/// Writes an <see cref="OperandType.InlineString"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected abstract void WriteInlineString(ref ArrayWriter writer, Instruction instr);
/// <summary>
/// Writes an <see cref="OperandType.InlineSwitch"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteInlineSwitch(ref ArrayWriter writer, Instruction instr) {
var targets = instr.Operand as IList<Instruction>;
if (targets is null) {
Error("switch operand is not a list of instructions");
writer.WriteInt32(0);
}
else {
uint offsetAfter = (uint)(ToInstructionOffset(ref writer) + 4 + targets.Count * 4);
writer.WriteInt32(targets.Count);
for (int i = 0; i < targets.Count; i++) {
var target = targets[i];
writer.WriteUInt32(GetOffset(target) - offsetAfter);
}
}
}
/// <summary>
/// Writes an <see cref="OperandType.InlineTok"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected abstract void WriteInlineTok(ref ArrayWriter writer, Instruction instr);
/// <summary>
/// Writes an <see cref="OperandType.InlineType"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected abstract void WriteInlineType(ref ArrayWriter writer, Instruction instr);
/// <summary>
/// Writes an <see cref="OperandType.InlineVar"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteInlineVar(ref ArrayWriter writer, Instruction instr) {
var variable = instr.Operand as IVariable;
if (variable is null) {
Error("Operand is not a local/arg");
writer.WriteUInt16(0);
}
else {
int index = variable.Index;
if (ushort.MinValue <= index && index <= ushort.MaxValue)
writer.WriteUInt16((ushort)index);
else {
Error("Local/arg index doesn't fit in a UInt16");
writer.WriteUInt16(0);
}
}
}
/// <summary>
/// Writes a <see cref="OperandType.ShortInlineBrTarget"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteShortInlineBrTarget(ref ArrayWriter writer, Instruction instr) {
int displ = (int)(GetOffset(instr.Operand as Instruction) - (ToInstructionOffset(ref writer) + 1));
if (sbyte.MinValue <= displ && displ <= sbyte.MaxValue)
writer.WriteSByte((sbyte)displ);
else {
Error("Target instruction is too far away for a short branch. Use the long branch or call CilBody.SimplifyBranches() and CilBody.OptimizeBranches()");
writer.WriteByte(0);
}
}
/// <summary>
/// Writes a <see cref="OperandType.ShortInlineI"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteShortInlineI(ref ArrayWriter writer, Instruction instr) {
if (instr.Operand is sbyte)
writer.WriteSByte((sbyte)instr.Operand);
else if (instr.Operand is byte)
writer.WriteByte((byte)instr.Operand);
else {
Error("Operand is not a Byte or a SByte");
writer.WriteByte(0);
}
}
/// <summary>
/// Writes a <see cref="OperandType.ShortInlineR"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteShortInlineR(ref ArrayWriter writer, Instruction instr) {
if (instr.Operand is float)
writer.WriteSingle((float)instr.Operand);
else {
Error("Operand is not a Single");
writer.WriteSingle(0);
}
}
/// <summary>
/// Writes a <see cref="OperandType.ShortInlineVar"/> operand
/// </summary>
/// <param name="writer">Instruction writer</param>
/// <param name="instr">Instruction</param>
protected virtual void WriteShortInlineVar(ref ArrayWriter writer, Instruction instr) {
var variable = instr.Operand as IVariable;
if (variable is null) {
Error("Operand is not a local/arg");
writer.WriteByte(0);
}
else {
int index = variable.Index;
if (byte.MinValue <= index && index <= byte.MaxValue)
writer.WriteByte((byte)index);
else {
Error("Local/arg index doesn't fit in a Byte. Use the longer ldloc/ldarg/stloc/starg instruction.");
writer.WriteByte(0);
}
}
}
}
}