The base architecture for code-contracts analysis
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static / Checker.cs
1 // 
2 // Checker.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2011 Alexander Chebaturkin
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // 
28
29 using System;
30 using System.Collections.Generic;
31 using Mono.CodeContracts.Static.AST;
32 using Mono.CodeContracts.Static.Analysis.Drivers;
33 using Mono.CodeContracts.Static.Analysis.HeapAnalysis;
34 using Mono.CodeContracts.Static.Analysis.NonNull;
35 using Mono.CodeContracts.Static.ControlFlow;
36 using Mono.CodeContracts.Static.Providers;
37 using Mono.CodeContracts.Static.Proving;
38
39 namespace Mono.CodeContracts.Static {
40         public class Checker {
41                 private readonly CheckOptions options;
42                 private CodeContractsAnalysisDriver<IMethodResult<SymbolicValue>> analysis_driver;
43                 private Dictionary<string, IMethodAnalysis> analyzers;
44
45                 private Checker (CheckOptions options)
46                 {
47                         this.options = options;
48                 }
49
50                 public static CheckResults Check (CheckOptions options)
51                 {
52                         var checker = new Checker (options);
53                         return checker.Analyze ();
54                 }
55
56                 private CheckResults Analyze ()
57                 {
58                         if (this.options.Assembly == null)
59                                 return CheckResults.Error ("No assembly given to check");
60
61                         DebugOptions.Debug = this.options.ShowDebug;
62
63                         this.analyzers = new Dictionary<string, IMethodAnalysis> {{"non-null", new NonNullAnalysisFacade ()}};
64                         this.analysis_driver = new CodeContractsAnalysisDriver<IMethodResult<SymbolicValue>> (
65                                 new BasicAnalysisDriver (MetaDataProvider.Instance, CodeContractDecoder.Instance));
66
67                         return AnalyzeAssembly (this.options.Assembly);
68                 }
69
70                 private CheckResults AnalyzeAssembly (string assemblyPath)
71                 {
72                         IMetaDataProvider metadataDecoder = this.analysis_driver.MetaDataProvider;
73                         AssemblyNode assembly;
74                         string reason;
75                         if (!metadataDecoder.TryLoadAssembly (assemblyPath, out assembly, out reason))
76                                 return CheckResults.Error (string.Format ("Cannot load assembly: {0}", reason));
77
78                         var proofResults = new Dictionary<string, ICollection<string>> ();
79                         foreach (Method method in metadataDecoder.Methods (assembly))
80                                 AnalyzeMethod (method, proofResults);
81                         if (proofResults.Count == 0)
82                                 return CheckResults.Error ("No methods found.");
83
84                         return new CheckResults (null, null, proofResults);
85                 }
86
87                 private void AnalyzeMethod (Method method, Dictionary<string, ICollection<string>> proofResults)
88                 {
89                         IMetaDataProvider metadataDecoder = this.analysis_driver.MetaDataProvider;
90                         if (!metadataDecoder.HasBody (method))
91                                 return;
92                         if (this.options.Method != null && !metadataDecoder.FullName (method).Contains (this.options.Method))
93                                 return;
94
95                         var results = new List<string> ();
96                         proofResults.Add (method.FullName, results);
97                         try {
98                                 AnalyzeMethodInternal (method, results);
99                         } catch (Exception e) {
100                                 results.Add ("Exception: " + e.Message);
101                                 return;
102                         }
103
104                         results.Add (string.Format ("Checked {0} assertions", results.Count));
105                 }
106
107                 private void AnalyzeMethodInternal (Method method, List<string> proofResults)
108                 {
109                         string fullMethodName = method.FullName;
110                         IMethodDriver<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> methodDriver = this.analysis_driver.CreateMethodDriver (method);
111
112                         methodDriver.RunHeapAndExpressionAnalyses ();
113
114                         var results = new List<IMethodResult<SymbolicValue>> (this.analyzers.Values.Count);
115                         foreach (IMethodAnalysis analysis in this.analyzers.Values) {
116                                 IMethodResult<SymbolicValue> result = analysis.Analyze (fullMethodName, methodDriver);
117                                 results.Add (result);
118                         }
119
120                         ComposedFactQuery<SymbolicValue> facts = CreateFactQuery (methodDriver.BasicFacts.IsUnreachable, results);
121                         foreach (var methodResult in results)
122                                 methodResult.ValidateImplicitAssertions (facts, proofResults);
123
124                         AssertionFinder.ValidateAssertions (facts, methodDriver, proofResults);
125                 }
126
127                 private ComposedFactQuery<Variable> CreateFactQuery<Variable> (Predicate<APC> isUnreachable, IEnumerable<IMethodResult<Variable>> results)
128                 {
129                         var res = new ComposedFactQuery<Variable> (isUnreachable);
130                         res.Add (new ConstantPropagationFactQuery<Variable> ());
131                         foreach (var methodResult in results)
132                                 res.Add (methodResult.FactQuery);
133                         return res;
134                 }
135         }
136 }