~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.Decompiler/ILAst/SymbolicExecution.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Diagnostics;
 
22
using Mono.Cecil;
 
23
 
 
24
namespace ICSharpCode.Decompiler.ILAst
 
25
{
 
26
        /// <summary>
 
27
        /// This exception is thrown when we find something else than we expect from the C# compiler.
 
28
        /// This aborts the analysis and makes the whole transform fail.
 
29
        /// </summary>
 
30
        class SymbolicAnalysisFailedException : Exception {}
 
31
        
 
32
        enum SymbolicValueType
 
33
        {
 
34
                /// <summary>
 
35
                /// Unknown value
 
36
                /// </summary>
 
37
                Unknown,
 
38
                /// <summary>
 
39
                /// int: Constant (result of ldc.i4)
 
40
                /// </summary>
 
41
                IntegerConstant,
 
42
                /// <summary>
 
43
                /// int: State + Constant
 
44
                /// </summary>
 
45
                State,
 
46
                /// <summary>
 
47
                /// This pointer (result of ldarg.0)
 
48
                /// </summary>
 
49
                This,
 
50
                /// <summary>
 
51
                /// bool: State == Constant
 
52
                /// </summary>
 
53
                StateEquals,
 
54
                /// <summary>
 
55
                /// bool: State != Constant
 
56
                /// </summary>
 
57
                StateInEquals
 
58
        }
 
59
        
 
60
        struct SymbolicValue
 
61
        {
 
62
                public readonly int Constant;
 
63
                public readonly SymbolicValueType Type;
 
64
                
 
65
                public SymbolicValue(SymbolicValueType type, int constant = 0)
 
66
                {
 
67
                        this.Type = type;
 
68
                        this.Constant = constant;
 
69
                }
 
70
                
 
71
                public override string ToString()
 
72
                {
 
73
                        return string.Format("[SymbolicValue {0}: {1}]", this.Type, this.Constant);
 
74
                }
 
75
        }
 
76
        
 
77
        class SymbolicEvaluationContext
 
78
        {
 
79
                readonly FieldDefinition stateField;
 
80
                readonly List<ILVariable> stateVariables = new List<ILVariable>();
 
81
                
 
82
                public SymbolicEvaluationContext(FieldDefinition stateField)
 
83
                {
 
84
                        this.stateField = stateField;
 
85
                }
 
86
                
 
87
                public void AddStateVariable(ILVariable v)
 
88
                {
 
89
                        if (!stateVariables.Contains(v))
 
90
                                stateVariables.Add(v);
 
91
                }
 
92
                
 
93
                SymbolicValue Failed()
 
94
                {
 
95
                        return new SymbolicValue(SymbolicValueType.Unknown);
 
96
                }
 
97
                
 
98
                public SymbolicValue Eval(ILExpression expr)
 
99
                {
 
100
                        SymbolicValue left, right;
 
101
                        switch (expr.Code) {
 
102
                                case ILCode.Sub:
 
103
                                        left = Eval(expr.Arguments[0]);
 
104
                                        right = Eval(expr.Arguments[1]);
 
105
                                        if (left.Type != SymbolicValueType.State && left.Type != SymbolicValueType.IntegerConstant)
 
106
                                                return Failed();
 
107
                                        if (right.Type != SymbolicValueType.IntegerConstant)
 
108
                                                return Failed();
 
109
                                        return new SymbolicValue(left.Type, unchecked ( left.Constant - right.Constant ));
 
110
                                case ILCode.Ldfld:
 
111
                                        if (Eval(expr.Arguments[0]).Type != SymbolicValueType.This)
 
112
                                                return Failed();
 
113
                                        if (CecilExtensions.ResolveWithinSameModule(expr.Operand as FieldReference) != stateField)
 
114
                                                return Failed();
 
115
                                        return new SymbolicValue(SymbolicValueType.State);
 
116
                                case ILCode.Ldloc:
 
117
                                        ILVariable loadedVariable = (ILVariable)expr.Operand;
 
118
                                        if (stateVariables.Contains(loadedVariable))
 
119
                                                return new SymbolicValue(SymbolicValueType.State);
 
120
                                        else if (loadedVariable.IsParameter && loadedVariable.OriginalParameter.Index < 0)
 
121
                                                return new SymbolicValue(SymbolicValueType.This);
 
122
                                        else
 
123
                                                return Failed();
 
124
                                case ILCode.Ldc_I4:
 
125
                                        return new SymbolicValue(SymbolicValueType.IntegerConstant, (int)expr.Operand);
 
126
                                case ILCode.Ceq:
 
127
                                case ILCode.Cne:
 
128
                                        left = Eval(expr.Arguments[0]);
 
129
                                        right = Eval(expr.Arguments[1]);
 
130
                                        if (left.Type != SymbolicValueType.State || right.Type != SymbolicValueType.IntegerConstant)
 
131
                                                return Failed();
 
132
                                        // bool: (state + left.Constant == right.Constant)
 
133
                                        // bool: (state == right.Constant - left.Constant)
 
134
                                        return new SymbolicValue(expr.Code == ILCode.Ceq ? SymbolicValueType.StateEquals : SymbolicValueType.StateInEquals, unchecked(right.Constant - left.Constant));
 
135
                                case ILCode.LogicNot:
 
136
                                        SymbolicValue val = Eval(expr.Arguments[0]);
 
137
                                        if (val.Type == SymbolicValueType.StateEquals)
 
138
                                                return new SymbolicValue(SymbolicValueType.StateInEquals, val.Constant);
 
139
                                        else if (val.Type == SymbolicValueType.StateInEquals)
 
140
                                                return new SymbolicValue(SymbolicValueType.StateEquals, val.Constant);
 
141
                                        else
 
142
                                                return Failed();
 
143
                                default:
 
144
                                                return Failed();
 
145
                        }
 
146
                }
 
147
        }
 
148
}