5 // Alexander Chebaturkin (chebaturkin@gmail.com)
7 // Copyright (C) 2011 Alexander Chebaturkin
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
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;
45 private Checker (CheckOptions options)
47 this.options = options;
50 public static CheckResults Check (CheckOptions options)
52 var checker = new Checker (options);
53 return checker.Analyze ();
56 private CheckResults Analyze ()
58 if (this.options.Assembly == null)
59 return CheckResults.Error ("No assembly given to check");
61 DebugOptions.Debug = this.options.ShowDebug;
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));
67 return AnalyzeAssembly (this.options.Assembly);
70 private CheckResults AnalyzeAssembly (string assemblyPath)
72 IMetaDataProvider metadataDecoder = this.analysis_driver.MetaDataProvider;
73 AssemblyNode assembly;
75 if (!metadataDecoder.TryLoadAssembly (assemblyPath, out assembly, out reason))
76 return CheckResults.Error (string.Format ("Cannot load assembly: {0}", reason));
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.");
84 return new CheckResults (null, null, proofResults);
87 private void AnalyzeMethod (Method method, Dictionary<string, ICollection<string>> proofResults)
89 IMetaDataProvider metadataDecoder = this.analysis_driver.MetaDataProvider;
90 if (!metadataDecoder.HasBody (method))
92 if (this.options.Method != null && !metadataDecoder.FullName (method).Contains (this.options.Method))
95 var results = new List<string> ();
96 proofResults.Add (method.FullName, results);
98 AnalyzeMethodInternal (method, results);
99 } catch (Exception e) {
100 results.Add ("Exception: " + e.Message);
104 results.Add (string.Format ("Checked {0} assertions", results.Count));
107 private void AnalyzeMethodInternal (Method method, List<string> proofResults)
109 string fullMethodName = method.FullName;
110 IMethodDriver<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> methodDriver = this.analysis_driver.CreateMethodDriver (method);
112 methodDriver.RunHeapAndExpressionAnalyses ();
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);
120 ComposedFactQuery<SymbolicValue> facts = CreateFactQuery (methodDriver.BasicFacts.IsUnreachable, results);
121 foreach (var methodResult in results)
122 methodResult.ValidateImplicitAssertions (facts, proofResults);
124 AssertionFinder.ValidateAssertions (facts, methodDriver, proofResults);
127 private ComposedFactQuery<Variable> CreateFactQuery<Variable> (Predicate<APC> isUnreachable, IEnumerable<IMethodResult<Variable>> results)
129 var res = new ComposedFactQuery<Variable> (isUnreachable);
130 res.Add (new ConstantPropagationFactQuery<Variable> ());
131 foreach (var methodResult in results)
132 res.Add (methodResult.FactQuery);