Debian Codegraph @

external/cecil-legacy/Test/Mono.Cecil.Tests/MethodBodyTests.cs

raw symbols SCIP
1 using System;
2 using System.Linq;
3
4 using Mono.Cecil;
5 using Mono.Cecil.Cil;
6
7 using NUnit.Framework;
8
9 namespace Mono.Cecil.Tests {
10
11 [TestFixture]
12 public class MethodBodyTests : BaseTestFixture {
13
14 [TestIL ("hello.il")]
15 public void MultiplyMethod (ModuleDefinition module)
16 {
17 var foo = module.GetType ("Foo");
18 Assert.IsNotNull (foo);
19
20 var bar = foo.GetMethod ("Bar");
21 Assert.IsNotNull (bar);
22 Assert.IsTrue (bar.IsIL);
23
24 AssertCode (@"
25 .locals init (System.Int32 V_0)
26 IL_0000: ldarg.0
27 IL_0001: ldarg.1
28 IL_0002: mul
29 IL_0003: stloc.0
30 IL_0004: ldloc.0
31 IL_0005: call System.Void Foo::Baz(System.Int32)
32 IL_000a: ret
33 ", bar);
34 }
35
36 [TestIL ("hello.il")]
37 public void PrintStringEmpty (ModuleDefinition module)
38 {
39 var foo = module.GetType ("Foo");
40 Assert.IsNotNull (foo);
41
42 var print_empty = foo.GetMethod ("PrintEmpty");
43 Assert.IsNotNull (print_empty);
44
45 AssertCode (@"
46 .locals ()
47 IL_0000: ldsfld System.String System.String::Empty
48 IL_0005: call System.Void System.Console::WriteLine(System.String)
49 IL_000a: ret
50 ", print_empty);
51 }
52
53 [TestModule ("libhello.dll")]
54 public void Branch (ModuleDefinition module)
55 {
56 var lib = module.GetType ("Library");
57 Assert.IsNotNull (lib);
58
59 var method = lib.GetMethod ("GetHelloString");
60 Assert.IsNotNull (method);
61
62 AssertCode (@"
63 .locals init (System.String V_0)
64 IL_0000: nop
65 IL_0001: ldstr ""hello world of tomorrow""
66 IL_0006: stloc.0
67 IL_0007: br.s IL_0009
68 IL_0009: ldloc.0
69 IL_000a: ret
70 ", method);
71 }
72
73 [TestModule ("switch.exe")]
74 public void Switch (ModuleDefinition module)
75 {
76 var program = module.GetType ("Program");
77 Assert.IsNotNull (program);
78
79 var method = program.GetMethod ("Main");
80 Assert.IsNotNull (method);
81
82 AssertCode (@"
83 .locals init (System.Int32 V_0)
84 IL_0000: ldarg.0
85 IL_0001: ldlen
86 IL_0002: conv.i4
87 IL_0003: stloc.0
88 IL_0004: ldloc.0
89 IL_0005: ldc.i4.8
90 IL_0006: bgt.s IL_0026
91 IL_0008: ldloc.0
92 IL_0009: ldc.i4.1
93 IL_000a: sub
94 IL_000b: switch (IL_0032, IL_0034, IL_0038, IL_0034)
95 IL_0020: ldloc.0
96 IL_0021: ldc.i4.8
97 IL_0022: beq.s IL_0036
98 IL_0024: br.s IL_0038
99 IL_0026: ldloc.0
100 IL_0027: ldc.i4.s 16
101 IL_0029: beq.s IL_0036
102 IL_002b: ldloc.0
103 IL_002c: ldc.i4.s 32
104 IL_002e: beq.s IL_0036
105 IL_0030: br.s IL_0038
106 IL_0032: ldc.i4.0
107 IL_0033: ret
108 IL_0034: ldc.i4.1
109 IL_0035: ret
110 IL_0036: ldc.i4.2
111 IL_0037: ret
112 IL_0038: ldc.i4.s 42
113 IL_003a: ret
114 ", method);
115 }
116
117 [TestIL ("methodspecs.il")]
118 public void MethodSpec (ModuleDefinition module)
119 {
120 var tamtam = module.GetType ("Tamtam");
121
122 var bar = tamtam.GetMethod ("Bar");
123 Assert.IsNotNull (bar);
124
125 AssertCode (@"
126 .locals ()
127 IL_0000: ldc.i4.2
128 IL_0001: call System.Void Tamtam::Foo<System.Int32>(TFoo)
129 IL_0006: ret
130 ", bar);
131 }
132
133 [TestModule ("catch.exe")]
134 public void NestedTryCatchFinally (ModuleDefinition module)
135 {
136 var program = module.GetType ("Program");
137 var main = program.GetMethod ("Main");
138 Assert.IsNotNull (main);
139
140 AssertCode (@"
141 .locals ()
142 IL_0000: call System.Void Program::Foo()
143 IL_0005: leave.s IL_000d
144 IL_0007: call System.Void Program::Baz()
145 IL_000c: endfinally
146 IL_000d: leave.s IL_001f
147 IL_000f: pop
148 IL_0010: call System.Void Program::Bar()
149 IL_0015: leave.s IL_001f
150 IL_0017: pop
151 IL_0018: call System.Void Program::Bar()
152 IL_001d: leave.s IL_001f
153 IL_001f: leave.s IL_0027
154 IL_0021: call System.Void Program::Baz()
155 IL_0026: endfinally
156 IL_0027: ret
157 .try IL_0000 to IL_0007 finally handler IL_0007 to IL_000d
158 .try IL_0000 to IL_000f catch System.ArgumentException handler IL_000f to IL_0017
159 .try IL_0000 to IL_000f catch System.Exception handler IL_0017 to IL_001f
160 .try IL_0000 to IL_0021 finally handler IL_0021 to IL_0027
161 ", main);
162 }
163
164 [TestModule ("fptr.exe", Verify = false)]
165 public void FunctionPointersAndCallSites (ModuleDefinition module)
166 {
167 var type = module.Types [0];
168 var start = type.GetMethod ("Start");
169 Assert.IsNotNull (start);
170
171 AssertCode (@"
172 .locals init ()
173 IL_0000: ldc.i4.1
174 IL_0001: call method System.Int32 *(System.Int32) MakeDecision::Decide()
175 IL_0006: calli System.Int32(System.Int32)
176 IL_000b: call System.Void System.Console::WriteLine(System.Int32)
177 IL_0010: ldc.i4.1
178 IL_0011: call method System.Int32 *(System.Int32) MakeDecision::Decide()
179 IL_0016: calli System.Int32(System.Int32)
180 IL_001b: call System.Void System.Console::WriteLine(System.Int32)
181 IL_0020: ldc.i4.1
182 IL_0021: call method System.Int32 *(System.Int32) MakeDecision::Decide()
183 IL_0026: calli System.Int32(System.Int32)
184 IL_002b: call System.Void System.Console::WriteLine(System.Int32)
185 IL_0030: ret
186 ", start);
187 }
188
189 [TestIL ("hello.il")]
190 public void ThisParameter (ModuleDefinition module)
191 {
192 var type = module.GetType ("Foo");
193 var method = type.GetMethod ("Gazonk");
194
195 Assert.IsNotNull (method);
196
197 AssertCode (@"
198 .locals ()
199 IL_0000: ldarg 0
200 IL_0004: pop
201 IL_0005: ret
202 ", method);
203
204 Assert.AreEqual (method.Body.ThisParameter.ParameterType, type);
205 Assert.AreEqual (method.Body.ThisParameter, method.Body.Instructions [0].Operand);
206 }
207
208 [Test]
209 public void ThisParameterStaticMethod ()
210 {
211 var static_method = typeof (ModuleDefinition).ToDefinition ().Methods.Where (m => m.IsStatic).First ();
212 Assert.IsNull (static_method.Body.ThisParameter);
213 }
214
215 [Test]
216 public void ThisParameterPrimitive ()
217 {
218 var int32 = typeof (int).ToDefinition ();
219 var int_to_string = int32.Methods.Where (m => m.Name == "ToString" && m.Parameters.Count == 0).First();
220 Assert.IsNotNull (int_to_string);
221
222 var this_parameter_type = int_to_string.Body.ThisParameter.ParameterType;
223 Assert.IsTrue (this_parameter_type.IsPointer);
224
225 var element_type = ((PointerType) this_parameter_type).ElementType;
226 Assert.AreEqual (int32, element_type);
227 }
228
229 [Test]
230 public void ThisParameterValueType ()
231 {
232 var token = typeof (MetadataToken).ToDefinition ();
233 var token_to_string = token.Methods.Where (m => m.Name == "ToString" && m.Parameters.Count == 0).First ();
234 Assert.IsNotNull (token_to_string);
235
236 var this_parameter_type = token_to_string.Body.ThisParameter.ParameterType;
237 Assert.IsTrue (this_parameter_type.IsPointer);
238
239 var element_type = ((PointerType) this_parameter_type).ElementType;
240 Assert.AreEqual (token, element_type);
241 }
242
243 [Test]
244 public void ThisParameterObject ()
245 {
246 var module = typeof (MethodBodyTests).ToDefinition ().Module;
247 var @object = module.TypeSystem.Object.Resolve ();
248 var method = @object.Methods.Where (m => m.HasBody).First ();
249
250 var type = method.Body.ThisParameter.ParameterType;
251 Assert.IsFalse (type.IsValueType);
252 Assert.IsFalse (type.IsPrimitive);
253 Assert.IsFalse (type.IsPointer);
254 }
255
256 [TestIL ("hello.il")]
257 public void FilterMaxStack (ModuleDefinition module)
258 {
259 var type = module.GetType ("Foo");
260 var method = type.GetMethod ("TestFilter");
261
262 Assert.IsNotNull (method);
263 Assert.AreEqual (2, method.Body.MaxStackSize);
264 }
265
266 [TestModule ("iterator.exe")]
267 public void Iterator (ModuleDefinition module)
268 {
269 var method = module.GetType ("Program").GetMethod ("GetLittleArgs");
270 Assert.IsNotNull (method.Body);
271 }
272
273 [TestCSharp ("CustomAttributes.cs")]
274 public void LoadString (ModuleDefinition module)
275 {
276 var type = module.GetType ("FooAttribute");
277 var get_fiou = type.GetMethod ("get_Fiou");
278 Assert.IsNotNull (get_fiou);
279
280 var ldstr = get_fiou.Body.Instructions.Where (i => i.OpCode == OpCodes.Ldstr).First ();
281 Assert.AreEqual ("fiou", ldstr.Operand);
282 }
283
284 static void AssertCode (string expected, MethodDefinition method)
285 {
286 Assert.IsTrue (method.HasBody);
287 Assert.IsNotNull (method.Body);
288
289 Assert.AreEqual (Normalize (expected), Normalize (Formatter.FormatMethodBody (method)));
290 }
291
292 static string Normalize (string str)
293 {
294 return str.Trim ().Replace ("\r\n", "\n");
295 }
296
297 [Test]
298 public void AddInstruction ()
299 {
300 var object_ref = new TypeReference ("System", "Object", null, null, false);
301 var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
302 var body = new MethodBody (method);
303
304 var il = body.GetILProcessor ();
305
306 var first = il.Create (OpCodes.Nop);
307 var second = il.Create (OpCodes.Nop);
308
309 body.Instructions.Add (first);
310 body.Instructions.Add (second);
311
312 Assert.IsNull (first.Previous);
313 Assert.AreEqual (second, first.Next);
314 Assert.AreEqual (first, second.Previous);
315 Assert.IsNull (second.Next);
316 }
317
318 [Test]
319 public void InsertInstruction ()
320 {
321 var object_ref = new TypeReference ("System", "Object", null, null, false);
322 var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
323 var body = new MethodBody (method);
324
325 var il = body.GetILProcessor ();
326
327 var first = il.Create (OpCodes.Nop);
328 var second = il.Create (OpCodes.Nop);
329 var third = il.Create (OpCodes.Nop);
330
331 body.Instructions.Add (first);
332 body.Instructions.Add (third);
333
334 Assert.IsNull (first.Previous);
335 Assert.AreEqual (third, first.Next);
336 Assert.AreEqual (first, third.Previous);
337 Assert.IsNull (third.Next);
338
339 body.Instructions.Insert (1, second);
340
341 Assert.IsNull (first.Previous);
342 Assert.AreEqual (second, first.Next);
343 Assert.AreEqual (first, second.Previous);
344 Assert.AreEqual (third, second.Next);
345 Assert.AreEqual (second, third.Previous);
346 Assert.IsNull (third.Next);
347 }
348
349 [Test]
350 public void InsertAfterLastInstruction ()
351 {
352 var object_ref = new TypeReference ("System", "Object", null, null, false);
353 var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
354 var body = new MethodBody (method);
355
356 var il = body.GetILProcessor ();
357
358 var first = il.Create (OpCodes.Nop);
359 var second = il.Create (OpCodes.Nop);
360 var third = il.Create (OpCodes.Nop);
361
362 body.Instructions.Add (first);
363 body.Instructions.Add (second);
364
365 Assert.IsNull (first.Previous);
366 Assert.AreEqual (second, first.Next);
367 Assert.AreEqual (first, second.Previous);
368 Assert.IsNull (second.Next);
369
370 body.Instructions.Insert (2, third);
371
372 Assert.IsNull (first.Previous);
373 Assert.AreEqual (second, first.Next);
374 Assert.AreEqual (first, second.Previous);
375 Assert.AreEqual (third, second.Next);
376 Assert.AreEqual (second, third.Previous);
377 Assert.IsNull (third.Next);
378 }
379
380 [Test]
381 public void RemoveInstruction ()
382 {
383 var object_ref = new TypeReference ("System", "Object", null, null, false);
384 var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
385 var body = new MethodBody (method);
386
387 var il = body.GetILProcessor ();
388
389 var first = il.Create (OpCodes.Nop);
390 var second = il.Create (OpCodes.Nop);
391 var third = il.Create (OpCodes.Nop);
392
393 body.Instructions.Add (first);
394 body.Instructions.Add (second);
395 body.Instructions.Add (third);
396
397 Assert.IsNull (first.Previous);
398 Assert.AreEqual (second, first.Next);
399 Assert.AreEqual (first, second.Previous);
400 Assert.AreEqual (third, second.Next);
401 Assert.AreEqual (second, third.Previous);
402 Assert.IsNull (third.Next);
403
404 body.Instructions.Remove (second);
405
406 Assert.IsNull (first.Previous);
407 Assert.AreEqual (third, first.Next);
408 Assert.AreEqual (first, third.Previous);
409 Assert.IsNull (third.Next);
410 }
411 }
412 }