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.Analysis.Numerical;
36 using Mono.CodeContracts.Static.ControlFlow;
37 using Mono.CodeContracts.Static.Providers;
38 using Mono.CodeContracts.Static.Proving;
40 namespace Mono.CodeContracts.Static {
41 public class Checker {
42 private readonly CheckOptions options;
43 private CodeContractsAnalysisDriver<IMethodResult<SymbolicValue>> analysis_driver;
44 private Dictionary<string, IMethodAnalysis> analyzers;
46 private Checker (CheckOptions options)
48 this.options = options;
51 public static CheckResults Check (CheckOptions options)
53 var checker = new Checker (options);
54 return checker.Analyze ();
57 private CheckResults Analyze ()
59 if (this.options.Assembly == null)
60 return CheckResults.Error ("No assembly given to check");
62 DebugOptions.Debug = this.options.ShowDebug;
64 this.analyzers = new Dictionary<string, IMethodAnalysis> {
65 {"non-null", new NonNullAnalysisFacade ()},
66 {"arithmetic", new Analysers.Arithmetic ()}
69 this.analysis_driver = new CodeContractsAnalysisDriver<IMethodResult<SymbolicValue>> (
70 new BasicAnalysisDriver (MetaDataProvider.Instance, CodeContractDecoder.Instance));
72 return AnalyzeAssembly (this.options.Assembly);
75 private CheckResults AnalyzeAssembly (string assemblyPath)
77 IMetaDataProvider metadataDecoder = this.analysis_driver.MetaDataProvider;
78 AssemblyNode assembly;
80 if (!metadataDecoder.TryLoadAssembly (assemblyPath, out assembly, out reason))
81 return CheckResults.Error (string.Format ("Cannot load assembly: {0}", reason));
83 var proofResults = new Dictionary<string, ICollection<string>> ();
84 foreach (Method method in metadataDecoder.Methods (assembly))
85 AnalyzeMethod (method, proofResults);
86 if (proofResults.Count == 0)
87 return CheckResults.Error ("No methods found.");
89 return new CheckResults (null, null, proofResults);
92 private void AnalyzeMethod (Method method, Dictionary<string, ICollection<string>> proofResults)
94 IMetaDataProvider metadataDecoder = this.analysis_driver.MetaDataProvider;
95 if (!metadataDecoder.HasBody (method))
97 if (this.options.Method != null && !metadataDecoder.FullName (method).Contains (this.options.Method))
100 var results = new List<string> ();
101 proofResults.Add (method.FullName, results);
103 AnalyzeMethodInternal (method, results);
104 } catch (Exception e) {
105 results.Add ("Exception: " + e.Message);
109 results.Add (string.Format ("Checked {0} assertions", results.Count));
112 private void AnalyzeMethodInternal (Method method, List<string> proofResults)
114 string fullMethodName = method.FullName;
115 IMethodDriver<LabeledSymbol<APC, SymbolicValue>, SymbolicValue> methodDriver = this.analysis_driver.CreateMethodDriver (method);
117 methodDriver.RunHeapAndExpressionAnalyses ();
119 var results = new List<IMethodResult<SymbolicValue>> (this.analyzers.Values.Count);
120 foreach (IMethodAnalysis analysis in this.analyzers.Values) {
121 IMethodResult<SymbolicValue> result = analysis.Analyze (fullMethodName, methodDriver);
122 results.Add (result);
125 ComposedFactQuery<SymbolicValue> facts = CreateFactQuery (methodDriver.BasicFacts.IsUnreachable, results);
126 foreach (var methodResult in results)
127 methodResult.ValidateImplicitAssertions (facts, proofResults);
129 AssertionFinder.ValidateAssertions (facts, methodDriver, proofResults);
132 private ComposedFactQuery<Variable> CreateFactQuery<Variable> (Predicate<APC> isUnreachable, IEnumerable<IMethodResult<Variable>> results)
134 var res = new ComposedFactQuery<Variable> (isUnreachable);
135 res.Add (new ConstantPropagationFactQuery<Variable> ());
136 foreach (var methodResult in results)
137 res.Add (methodResult.FactQuery);