File: ControlFlowGraphBuilder.cs

package info (click to toggle)
cecil-flowanalysis 0.1~vcs20110809.r1.b34edf6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,080 kB
  • sloc: cs: 5,473; sh: 22; makefile: 18; ansic: 7; php: 1
file content (370 lines) | stat: -rw-r--r-- 10,558 bytes parent folder | download | duplicates (3)
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
#region license
//
// (C) db4objects Inc. http://www.db4o.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using Cecil.FlowAnalysis.Utilities;
using Cecil.FlowAnalysis.ControlFlow;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;

namespace Cecil.FlowAnalysis.ControlFlow {

	/// <summary>
	/// </summary>
	class ControlFlowGraphBuilder {

		MethodBody _body;
		Dictionary<int, InstructionData> _instructionData;
		Dictionary<int, InstructionBlock> _blocks = new Dictionary<int, InstructionBlock> ();

		InstructionBlock [] RegisteredBlocks {
			get { return ToArray (new InstructionBlock [BlockCount]); }
		}

		int BlockCount {
			get { return _blocks.Count; }
		}

		internal ControlFlowGraphBuilder (MethodDefinition method)
		{
			_body = method.Body;
		}

		public ControlFlowGraph BuildGraph ()
		{
			DelimitBlocks ();
			ConnectBlocks ();
			ComputeInstructionData ();
			return new ControlFlowGraph (_body, RegisteredBlocks, _instructionData);
		}

		void DelimitBlocks ()
		{
			var instructions = _body.Instructions;

			MarkBlockStarts (instructions);
			MarkBlockEnds (instructions);
		}

		void MarkBlockStarts (Collection<Instruction> instructions)
		{
			Instruction instruction = instructions [0];

			// the first instruction starts a block
			MarkBlockStart (instruction);
			for (int i = 1; i < instructions.Count; ++i) {
				instruction = instructions [i];
				if (!IsBlockDelimiter (instruction))
					continue;

				if (HasMultipleBranches (instruction)) {
					// each switch case first instruction starts a block
					foreach (Instruction target in GetBranchTargets (instruction))
						if (target != null)
							MarkBlockStart (target);
				} else {
					// the target of a branch starts a block
					Instruction target = GetBranchTarget (instruction);
					if (null != target) MarkBlockStart (target);
				}

				// the next instruction after a branch starts a block
				if (null != instruction.Next) MarkBlockStart (instruction.Next);
			}
		}

		void MarkBlockEnds (Collection<Instruction> instructions)
		{
			InstructionBlock [] blocks = this.RegisteredBlocks;
			InstructionBlock current = blocks [0];

			for (int i = 1; i < blocks.Length; ++i) {
				InstructionBlock block = blocks [i];
				current.SetLastInstruction (block.FirstInstruction.Previous);
				current = block;
			}

			current.SetLastInstruction (instructions [instructions.Count - 1]);
		}

		static bool IsBlockDelimiter (Instruction instruction)
		{
			FlowControl control = instruction.OpCode.FlowControl;
			switch (control) {
			case FlowControl.Break:
			case FlowControl.Branch:
			case FlowControl.Return:
			case FlowControl.Cond_Branch:
				return true;
			}
			return false;
		}

		void MarkBlockStart (Instruction instruction)
		{
			InstructionBlock block = GetBlock (instruction);
			if (null != block) return;

			block = new InstructionBlock (instruction);
			RegisterBlock (block);
		}

		void ComputeInstructionData ()
		{
			_instructionData = new Dictionary<int, InstructionData> ();
			var visited = new HashSet<InstructionBlock> ();
			ComputeInstructionData (visited, 0, GetFirstBlock ());
		}

		InstructionBlock GetFirstBlock ()
		{
			return GetBlock (_body.Instructions [0]);
		}

		void ComputeInstructionData (HashSet<InstructionBlock> visited, int stackHeight, InstructionBlock block)
		{
			if (visited.Contains (block)) return;
			visited.Add (block);

			foreach (Instruction instruction in block) {
				stackHeight = ComputeInstructionData (stackHeight, instruction);
			}

			foreach (InstructionBlock successor in block.Successors) {
				ComputeInstructionData (visited, stackHeight, successor);
			}
		}

		int ComputeInstructionData (int stackHeight, Instruction instruction)
		{
			int before = stackHeight;
			int after = ComputeNewStackHeight (stackHeight, instruction);
			_instructionData.Add (instruction.Offset, new InstructionData (before, after));
			return after;
		}

		int ComputeNewStackHeight (int stackHeight, Instruction instruction)
		{
			return stackHeight + GetPushDelta (instruction) - GetPopDelta (stackHeight, instruction);
		}

		static int GetPushDelta (Instruction instruction)
		{
			OpCode code = instruction.OpCode;
			switch (code.StackBehaviourPush) {
			case StackBehaviour.Push0:
				return 0;

			case StackBehaviour.Push1:
			case StackBehaviour.Pushi:
			case StackBehaviour.Pushi8:
			case StackBehaviour.Pushr4:
			case StackBehaviour.Pushr8:
			case StackBehaviour.Pushref:
				return 1;

			case StackBehaviour.Push1_push1:
				return 2;

			case StackBehaviour.Varpush:
				if (code.FlowControl == FlowControl.Call) {
					MethodReference method = (MethodReference)instruction.Operand;
					return IsVoid (method.ReturnType) ? 0 : 1;
				}

				break;
			}
			throw new ArgumentException (Formatter.FormatInstruction (instruction));
		}

		int GetPopDelta (int stackHeight, Instruction instruction)
		{
			OpCode code = instruction.OpCode;
			switch (code.StackBehaviourPop) {
			case StackBehaviour.Pop0:
				return 0;
			case StackBehaviour.Popi:
			case StackBehaviour.Popref:
			case StackBehaviour.Pop1:
				return 1;

			case StackBehaviour.Pop1_pop1:
			case StackBehaviour.Popi_pop1:
			case StackBehaviour.Popi_popi:
			case StackBehaviour.Popi_popi8:
			case StackBehaviour.Popi_popr4:
			case StackBehaviour.Popi_popr8:
			case StackBehaviour.Popref_pop1:
			case StackBehaviour.Popref_popi:
				return 2;

			case StackBehaviour.Popi_popi_popi:
			case StackBehaviour.Popref_popi_popi:
			case StackBehaviour.Popref_popi_popi8:
			case StackBehaviour.Popref_popi_popr4:
			case StackBehaviour.Popref_popi_popr8:
			case StackBehaviour.Popref_popi_popref:
				return 3;

			case StackBehaviour.PopAll:
				return stackHeight;

			case StackBehaviour.Varpop:
				if (code.FlowControl == FlowControl.Call) {
					MethodReference method = (MethodReference)instruction.Operand;
					int count = method.Parameters.Count;
					if (method.HasThis && OpCodes.Newobj.Value != code.Value)
						++count;

					return count;
				}

				if (code.Value == OpCodes.Ret.Value)
					return IsVoidMethod () ? 0 : 1;

				break;
			}
			throw new ArgumentException (Formatter.FormatInstruction (instruction));
		}

		bool IsVoidMethod ()
		{
			return IsVoid (_body.Method.ReturnType);
		}

		static bool IsVoid (TypeReference type)
		{
			return type.MetadataType == MetadataType.Void;
		}

		InstructionBlock [] ToArray (InstructionBlock [] blocks)
		{
			_blocks.Values.CopyTo (blocks, 0);
			Array.Sort (blocks);
			return blocks;
		}

		void ConnectBlocks ()
		{
			foreach (InstructionBlock block in _blocks.Values)
				ConnectBlock (block);
		}

		void ConnectBlock (InstructionBlock block)
		{
			if (block.LastInstruction == null)
				throw new ArgumentException ("Undelimited block at offset " + block.FirstInstruction.Offset);

			Instruction instruction = block.LastInstruction;
			switch (instruction.OpCode.FlowControl) {
			case FlowControl.Branch:
			case FlowControl.Cond_Branch: {
				if (HasMultipleBranches (instruction)) {
					InstructionBlock [] blocks = GetBranchTargetsBlocks (instruction);
					if (instruction.Next != null)
						blocks = AddBlock (GetBlock (instruction.Next), blocks);

					block.SetSuccessors (blocks);
					break;
				}

				InstructionBlock target = GetBranchTargetBlock (instruction);
				if (instruction.OpCode.FlowControl == FlowControl.Cond_Branch && instruction.Next != null)
					block.SetSuccessors (new InstructionBlock [] { target, GetBlock (instruction.Next) });
				else
					block.SetSuccessors (new InstructionBlock [] { target });
				break;
			}
			case FlowControl.Call:
			case FlowControl.Next:
				if (null != instruction.Next)
					block.SetSuccessors (new InstructionBlock [] { GetBlock (instruction.Next) });

				break;
			case FlowControl.Return:
				break;
			default:
				throw new ArgumentException (
					string.Format ("Unhandled instruction flow behavior {0}: {1}",
					               instruction.OpCode.FlowControl,
					               Formatter.FormatInstruction (instruction)));
			}
		}

		static InstructionBlock [] AddBlock (InstructionBlock block, InstructionBlock [] blocks)
		{
			InstructionBlock [] result = new InstructionBlock [blocks.Length + 1];
			Array.Copy (blocks, result, blocks.Length);
			result [result.Length - 1] = block;

			return result;
		}

		static bool HasMultipleBranches (Instruction instruction)
		{
			return instruction.OpCode.Code == Code.Switch;
		}

		InstructionBlock [] GetBranchTargetsBlocks (Instruction instruction)
		{
			Instruction [] targets = GetBranchTargets (instruction);
			InstructionBlock [] blocks = new InstructionBlock [targets.Length];
			for (int i = 0; i < targets.Length; i++)
				blocks [i] = GetBlock (targets [i]);

			return blocks;
		}

		static Instruction [] GetBranchTargets (Instruction instruction)
		{
			return (Instruction []) instruction.Operand;
		}

		InstructionBlock GetBranchTargetBlock (Instruction instruction)
		{
			return GetBlock (GetBranchTarget (instruction));
		}

		static Instruction GetBranchTarget (Instruction instruction)
		{
			return (Instruction) instruction.Operand;
		}

		void RegisterBlock (InstructionBlock block)
		{
			_blocks.Add (block.FirstInstruction.Offset, block);
		}

		InstructionBlock GetBlock (Instruction firstInstruction)
		{
			InstructionBlock block;
			_blocks.TryGetValue (firstInstruction.Offset, out block);
			return block;
		}
	}
}