Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ILProcessorTests.cs « Mono.Cecil.Tests « Test - github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c5854033f13a6dae2efe46202c4d983da56f7684 (plain)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Mdb;
using Mono.Cecil.Pdb;
using NUnit.Framework;

namespace Mono.Cecil.Tests {

	[TestFixture]
	public class ILProcessorTests : BaseTestFixture {

		[Test]
		public void Append ()
		{
			var method = CreateTestMethod ();
			var il = method.GetILProcessor ();

			var ret = il.Create (OpCodes.Ret);
			il.Append (ret);

			AssertOpCodeSequence (new [] { OpCodes.Ret }, method);
		}

		[Test]
		public void InsertBefore ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			var ldloc_2 = method.Instructions.Where (i => i.OpCode == OpCodes.Ldloc_2).First ();

			il.InsertBefore (
				ldloc_2,
				il.Create (OpCodes.Ldloc_1));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
		}

		[Test]
		public void InsertBeforeIssue697 ()
		{
			var parameters = new ReaderParameters { SymbolReaderProvider = new MdbReaderProvider () };
			using (var module = GetResourceModule ("Issue697.dll", parameters))
			{
				var pathGetterDef = module.GetTypes ()
					.SelectMany (t => t.Methods)
					.First (m => m.Name.Equals ("get_Defer"));

				var body = pathGetterDef.Body;
				var worker = body.GetILProcessor ();
				var initialBody = body.Instructions.ToList ();
				var head = initialBody.First ();
				var opcode = worker.Create (OpCodes.Ldc_I4_1);
				worker.InsertBefore (head, opcode);
				worker.InsertBefore (head, worker.Create (OpCodes.Ret));
				initialBody.ForEach (worker.Remove);
				AssertOpCodeSequence (new [] { OpCodes.Ldc_I4_1, OpCodes.Ret }, pathGetterDef.body);	
			}
		}

		[Test]
		public void InsertBeforeIssue697bis ()
		{
			var parameters = new ReaderParameters { SymbolReaderProvider = new MdbReaderProvider () };
			using (var module = GetResourceModule ("Issue697.dll", parameters)) {
				var pathGetterDef = module.GetTypes ()
					.SelectMany (t => t.Methods)
					.First (m => m.Name.Equals ("get_Defer"));

				var body = pathGetterDef.Body;
				var worker = body.GetILProcessor ();
				var initialBody = body.Instructions.ToList ();
				Console.WriteLine (initialBody.Sum (i => i.GetSize ()));

				var head = initialBody.First ();
				var opcode = worker.Create (OpCodes.Ldc_I4_1);
				worker.InsertBefore (head, opcode);

				Assert.That (pathGetterDef.DebugInformation.Scope.Start.IsEndOfMethod, Is.False);
				foreach (var subscope in pathGetterDef.DebugInformation.Scope.Scopes)
					Assert.That (subscope.Start.IsEndOfMethod, Is.False);

				// big test -- we can write w/o crashing
				var unique = Guid.NewGuid ().ToString ();
				var output = Path.GetTempFileName ();
				var outputdll = output + ".dll";

				var writer = new WriterParameters () {
					SymbolWriterProvider = new MdbWriterProvider (),
					WriteSymbols = true
				};
				using (var sink = File.Open (outputdll, FileMode.Create, FileAccess.ReadWrite)) {
					module.Write (sink, writer);
				}

				Assert.Pass ();
			}
		}

		[Test]
		public void InsertAfter ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			var ldloc_0 = method.Instructions.First ();

			il.InsertAfter (
				ldloc_0,
				il.Create (OpCodes.Ldloc_1));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
		}

		[Test]
		public void InsertAfterUsingIndex ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			il.InsertAfter (
				0,
				il.Create (OpCodes.Ldloc_1));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
		}

		[Test]
		public void ReplaceUsingIndex ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			il.Replace (1, il.Create (OpCodes.Nop));

			AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Nop, OpCodes.Ldloc_3 }, method);
		}

		[Test]
		public void Clear ()
		{
			var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
			var il = method.GetILProcessor ();

			il.Clear ();

			AssertOpCodeSequence (new OpCode[] { }, method);
		}

		[TestCase (RoundtripType.None, false, false)]
		[TestCase (RoundtripType.Pdb, false, false)]
		[TestCase (RoundtripType.Pdb, true, false)]
		[TestCase (RoundtripType.Pdb, true, true)]
		[TestCase (RoundtripType.PortablePdb, false, false)]
		[TestCase (RoundtripType.PortablePdb, true, false)]
		[TestCase (RoundtripType.PortablePdb, true, true)]
		public void InsertAfterWithSymbolRoundtrip (RoundtripType roundtripType, bool forceUnresolved, bool reverseScopes)
		{
			var methodBody = CreateTestMethodWithLocalScopes ();
			methodBody = RoundtripMethodBody (methodBody, roundtripType, forceUnresolved, reverseScopes);

			var il = methodBody.GetILProcessor ();
			il.InsertAfter (1, il.Create (OpCodes.Ldstr, "Test"));

			methodBody = RoundtripMethodBody (methodBody, roundtripType, false, reverseScopes);
			var wholeBodyScope = VerifyWholeBodyScope (methodBody);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [0], 1, 3);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1], 4, null);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1].Scopes [0], 5, 6);

			methodBody.Method.Module.Dispose ();
		}

		[TestCase (RoundtripType.None, false, false)]
		[TestCase (RoundtripType.Pdb, false, false)]
		[TestCase (RoundtripType.Pdb, true, false)]
		[TestCase (RoundtripType.Pdb, true, true)]
		[TestCase (RoundtripType.PortablePdb, false, false)]
		[TestCase (RoundtripType.PortablePdb, true, false)]
		[TestCase (RoundtripType.PortablePdb, true, true)]
		public void RemoveWithSymbolRoundtrip (RoundtripType roundtripType, bool forceUnresolved, bool reverseScopes)
		{
			var methodBody = CreateTestMethodWithLocalScopes ();
			methodBody = RoundtripMethodBody (methodBody, roundtripType, forceUnresolved, reverseScopes);

			var il = methodBody.GetILProcessor ();
			il.RemoveAt (1);

			methodBody = RoundtripMethodBody (methodBody, roundtripType, false, reverseScopes);
			var wholeBodyScope = VerifyWholeBodyScope (methodBody);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [0], 1, 1);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1], 2, null);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1].Scopes [0], 3, 4);

			methodBody.Method.Module.Dispose ();
		}

		[TestCase (RoundtripType.None, false, false)]
		[TestCase (RoundtripType.Pdb, false, false)]
		[TestCase (RoundtripType.Pdb, true, false)]
		[TestCase (RoundtripType.Pdb, true, true)]
		[TestCase (RoundtripType.PortablePdb, false, false)]
		[TestCase (RoundtripType.PortablePdb, true, false)]
		[TestCase (RoundtripType.PortablePdb, true, true)]
		public void ReplaceWithSymbolRoundtrip (RoundtripType roundtripType, bool forceUnresolved, bool reverseScopes)
		{
			var methodBody = CreateTestMethodWithLocalScopes ();
			methodBody = RoundtripMethodBody (methodBody, roundtripType, forceUnresolved, reverseScopes);

			var il = methodBody.GetILProcessor ();
			il.Replace (1, il.Create (OpCodes.Ldstr, "Test"));

			methodBody = RoundtripMethodBody (methodBody, roundtripType, false, reverseScopes);
			var wholeBodyScope = VerifyWholeBodyScope (methodBody);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [0], 1, 2);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1], 3, null);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1].Scopes [0], 4, 5);

			methodBody.Method.Module.Dispose ();
		}

		[TestCase (RoundtripType.None, false, false)]
		[TestCase (RoundtripType.Pdb, false, false)]
		[TestCase (RoundtripType.Pdb, true, false)]
		[TestCase (RoundtripType.Pdb, true, true)]
		[TestCase (RoundtripType.PortablePdb, false, false)]
		[TestCase (RoundtripType.PortablePdb, true, false)]
		[TestCase (RoundtripType.PortablePdb, true, true)]
		public void EditBodyWithScopesAndSymbolRoundtrip (RoundtripType roundtripType, bool forceUnresolved, bool reverseScopes)
		{
			var methodBody = CreateTestMethodWithLocalScopes ();
			methodBody = RoundtripMethodBody (methodBody, roundtripType, forceUnresolved, reverseScopes);

			var il = methodBody.GetILProcessor ();
			il.Replace (4, il.Create (OpCodes.Ldstr, "Test"));
			il.InsertAfter (5, il.Create (OpCodes.Ldloc_3));
			var tempVar3 = new VariableDefinition (methodBody.Method.Module.ImportReference (typeof (string)));
			methodBody.Variables.Add (tempVar3);
			methodBody.Method.DebugInformation.Scope.Scopes [reverseScopes ? 0 : 1].Scopes.Insert (reverseScopes ? 0 : 1,
				new ScopeDebugInformation (methodBody.Instructions [5], methodBody.Instructions [6]) {
					Variables = { new VariableDebugInformation (tempVar3, "tempVar3") }
				});

			methodBody = RoundtripMethodBody (methodBody, roundtripType, false, reverseScopes);
			var wholeBodyScope = VerifyWholeBodyScope (methodBody);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [0], 1, 2);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1], 3, null);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1].Scopes [0], 4, 5);
			AssertLocalScope (methodBody, wholeBodyScope.Scopes [1].Scopes [1], 5, 6);

			methodBody.Method.Module.Dispose ();
		}

		[Test]
		public void EditWithDebugInfoButNoLocalScopes()
		{
			var methodBody = CreateTestMethod (OpCodes.Ret);
			methodBody.Method.DebugInformation = new MethodDebugInformation (methodBody.Method);

			var il = methodBody.GetILProcessor ();
			il.Replace (methodBody.Instructions [0], il.Create (OpCodes.Nop));

			Assert.AreEqual (1, methodBody.Instructions.Count);
			Assert.AreEqual (Code.Nop, methodBody.Instructions [0].OpCode.Code);
			Assert.Null (methodBody.Method.DebugInformation.Scope);
		}

		static void AssertOpCodeSequence (OpCode [] expected, MethodBody body)
		{
			var opcodes = body.Instructions.Select (i => i.OpCode).ToArray ();
			Assert.AreEqual (expected.Length, opcodes.Length);

			for (int i = 0; i < opcodes.Length; i++)
				Assert.AreEqual (expected [i], opcodes [i]);
		}

		static MethodBody CreateTestMethod (params OpCode [] opcodes)
		{
			var method = new MethodDefinition {
				Name = "function",
				Attributes = MethodAttributes.Public | MethodAttributes.Static
			};

			var il = method.Body.GetILProcessor ();

			foreach (var opcode in opcodes)
				il.Emit (opcode);

			var instructions = method.Body.Instructions;
			int size = 0;
			for (int i = 0; i < instructions.Count; i++) {
				var instruction = instructions [i];
				instruction.Offset = size;
				size += instruction.GetSize ();
			}

			return method.Body;
		}

		static ScopeDebugInformation VerifyWholeBodyScope (MethodBody body)
		{
			var debug_info = body.Method.DebugInformation;
			Assert.IsNotNull (debug_info);
			AssertLocalScope (body, debug_info.Scope, 0, null);
			return debug_info.Scope;
		}

		static void AssertLocalScope (MethodBody methodBody, ScopeDebugInformation scope, int startIndex, int? endIndex)
		{
			Assert.IsNotNull (scope);
			Assert.AreEqual (methodBody.Instructions [startIndex], scope.Start.ResolvedInstruction);
			if (endIndex.HasValue)
				Assert.AreEqual (methodBody.Instructions [endIndex.Value], scope.End.ResolvedInstruction);
			else
				Assert.IsTrue (scope.End.IsEndOfMethod);
		}

		static MethodBody CreateTestMethodWithLocalScopes ()
		{
			var module = ModuleDefinition.CreateModule ("TestILProcessor", ModuleKind.Dll);
			var type = new TypeDefinition ("NS", "TestType", TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed, module.ImportReference (typeof (object)));
			module.Types.Add (type);

			var methodBody = CreateTestMethod (OpCodes.Nop, OpCodes.Ldloc_0, OpCodes.Nop, OpCodes.Ldloc_1, OpCodes.Nop, OpCodes.Ldloc_2, OpCodes.Nop);
			var method = methodBody.Method;
			method.ReturnType = module.ImportReference (typeof (void));
			type.Methods.Add (method);

			methodBody.InitLocals = true;
			var tempVar1 = new VariableDefinition (module.ImportReference (typeof (string)));
			methodBody.Variables.Add (tempVar1);
			var tempVar2 = new VariableDefinition (module.ImportReference (typeof (string)));
			methodBody.Variables.Add (tempVar2);

			var debug_info = method.DebugInformation;

			// Must add a sequence point, otherwise the native PDB writer will not actually write the method's debug info
			// into the PDB.
			foreach (var instr in methodBody.Instructions) {
				var sequence_point = new SequencePoint (instr, new Document (@"C:\test.cs")) {
					StartLine = 0,
					StartColumn = 0,
					EndLine = 0,
					EndColumn = 4,
				};
				debug_info.SequencePoints.Add (sequence_point);
			}

			// The method looks like this:
			//            | Scope | Scope.Scopes[0] | Scope.Scopes[1] | Scope.Scopes[1].Scopes[0]
			// #0 Nop     | >     |                 |                 |
			// #1 Ldloc_0 | .     | >               |                 |
			// #2 Nop     | .     | <               |                 |
			// #3 Ldloc_1 | .     |                 | >               | 
			// #4 Nop     | .     |                 | .               | >
			// #5 Ldloc_2 | .     |                 | .               | <
			// #6 Nop     | .     |                 | .               |
			// <end of m> | <     |                 | <               |  

			var instructions = methodBody.Instructions;
			debug_info.Scope = new ScopeDebugInformation (instructions[0], null) {
				Scopes = {
					new ScopeDebugInformation (instructions[1], instructions[2]) {
						Variables = { new VariableDebugInformation(tempVar1, "tempVar1") }
					},
					new ScopeDebugInformation (instructions[3], null) {
						Scopes = {
							new ScopeDebugInformation (instructions[4], instructions[5]) {
								Variables = { new VariableDebugInformation(tempVar2, "tempVar2") }
							}
						}
					}
				}
			};

			return methodBody;
		}

		public enum RoundtripType {
			None,
			Pdb,
			PortablePdb
		}

		static MethodBody RoundtripMethodBody(MethodBody methodBody, RoundtripType roundtripType, bool forceUnresolvedScopes = false, bool reverseScopeOrder = false)
		{
			var newModule = RoundtripModule (methodBody.Method.Module, roundtripType);
			var newMethodBody = newModule.GetType ("NS.TestType").GetMethod ("function").Body;

			if (forceUnresolvedScopes)
				UnresolveScopes (newMethodBody.Method.DebugInformation.Scope);

			if (reverseScopeOrder)
				ReverseScopeOrder (newMethodBody.Method.DebugInformation.Scope);

			return newMethodBody;
		}

		static void UnresolveScopes(ScopeDebugInformation scope)
		{
			scope.Start = new InstructionOffset (scope.Start.Offset);
			if (!scope.End.IsEndOfMethod)
				scope.End = new InstructionOffset (scope.End.Offset);

			foreach (var subScope in scope.Scopes)
				UnresolveScopes (subScope);
		}

		static void ReverseScopeOrder(ScopeDebugInformation scope)
		{
			List<ScopeDebugInformation> subScopes = scope.Scopes.ToList ();
			subScopes.Reverse ();
			scope.Scopes.Clear ();
			foreach (var subScope in subScopes)
				scope.Scopes.Add (subScope);

			foreach (var subScope in scope.Scopes)
				ReverseScopeOrder (subScope);
		}

		static ModuleDefinition RoundtripModule(ModuleDefinition module, RoundtripType roundtripType)
		{
			if (roundtripType == RoundtripType.None)
				return module;

			var file = Path.Combine (Path.GetTempPath (), "TestILProcessor.dll");
			if (File.Exists (file))
				File.Delete (file);

			ISymbolWriterProvider symbolWriterProvider;
			switch (roundtripType) {
			case RoundtripType.Pdb when Platform.HasNativePdbSupport:
				symbolWriterProvider = new PdbWriterProvider ();
				break;
			case RoundtripType.PortablePdb:
			default:
				symbolWriterProvider = new PortablePdbWriterProvider ();
				break;
			}

			module.Write (file, new WriterParameters {
				SymbolWriterProvider = symbolWriterProvider,
			});
			module.Dispose ();

			ISymbolReaderProvider symbolReaderProvider;
			switch (roundtripType) {
			case RoundtripType.Pdb when Platform.HasNativePdbSupport:
				symbolReaderProvider = new PdbReaderProvider ();
				break;
			case RoundtripType.PortablePdb:
			default:
				symbolReaderProvider = new PortablePdbReaderProvider ();
				break;
			}

			return ModuleDefinition.ReadModule (file, new ReaderParameters {
				SymbolReaderProvider = symbolReaderProvider,
				InMemory = true
			});
		}
	}
}