示例#1
0
        static int CallClousot1(string[] args)
        {
            var assemblyCache = new System.Collections.Hashtable();

            // We handle the platform option here as we can't do it from within Clousot
            // as it is very CCI specific.
            //
            //SetTargetPlatform(args, assemblyCache);

            try
            {
                var textWriter    = Console.Out;
                var outputFactory = new FullTextWriterOutputFactory <Method, AssemblyNode>(textWriter, "ServiceOutput");

                int returnCode = Clousot.ClousotMain(args,
                                                     CCIMDDecoder.Value,
                                                     CCIContractDecoder.Value,
                                                     assemblyCache,
                                                     outputFactory,
                                                     null
                                                     );

                return(returnCode);
            }
            catch (ExitRequestedException e)
            {
                return(e.ExitCode);
            }
            finally
            {
                DisposeAssemblies(assemblyCache.Values);
            }
        }
        private static int CallClousotInternally(string[] args, TextWriter textWriter)
        {
            var cciilProvider   = CciILCodeProvider.CreateCodeProvider();
            var metadataDecoder = cciilProvider.MetaDataDecoder;
            var contractDecoder = cciilProvider.ContractDecoder;
            var assemblyCache   = new System.Collections.Hashtable();

            var outputFactory = new FullTextWriterOutputFactory <MethodReferenceAdaptor, IAssemblyReference>(textWriter, "ServiceOutput");

            try
            {
                // We now call Clousot
                int returnCode = Clousot.ClousotMain(args,
                                                     metadataDecoder,
                                                     contractDecoder,
                                                     assemblyCache,
                                                     outputFactory: outputFactory,
                                                     cacheAccessorFactories: null
                                                     );
                return(returnCode);
            }
            finally
            {
                cciilProvider.Dispose(); // make sure all open files are closed
            }
        }
示例#3
0
        private void Initialize()
        {
            this.host        = new ClousotGlueHost();
            this.cciProvider = Microsoft.Cci.Analysis.CciILCodeProvider.CreateCodeProvider(host);
            var metadataDecoder = this.cciProvider.MetaDataDecoder;
            var contractDecoder = this.cciProvider.ContractDecoder;
            var args            = new string[] { "-nonnull", "-bounds", "-arithmetic", "-sortwarns:-" };
            var output          = new RoslynOutput(this.analysisResults);

            this.methodAnalyzer = Clousot.GetIndividualMethodAnalyzer(metadataDecoder, contractDecoder, args, output, null);
        }
示例#4
0
        public static int Main(string[] args, IOutputFullResultsFactory <MethodReferenceAdaptor, IAssemblyReference> outputFactory, IEnumerable <Caching.IClousotCacheFactory> cacheAccessorFactories)
        {
            Contract.Requires(args != null);

            if (args.Length > 0 && args[0] == "-cloudot")
            {
                try
                {
                    return(ClousotViaService.Main(args));
                }
                catch (Exception)
                {
                    Console.WriteLine("Process terminated with an exception".PrefixWithCurrentTime());
                    return(-1);
                }
            }
            else
            {
                var cciilProvider   = CciILCodeProvider.CreateCodeProvider();
                var metadataDecoder = cciilProvider.MetaDataDecoder;
                var contractDecoder = cciilProvider.ContractDecoder;
                var assemblyCache   = new System.Collections.Hashtable();

                try
                {
                    int returnCode = Clousot.ClousotMain(args,
                                                         metadataDecoder,
                                                         contractDecoder,
                                                         assemblyCache,
                                                         outputFactory: outputFactory,
                                                         cacheAccessorFactories: cacheAccessorFactories
                                                         );

                    return(returnCode); // important for regressions
                }
                catch (ExitRequestedException e)
                {
                    return(e.ExitCode);
                }
                finally
                {
                    cciilProvider.Dispose();
                }
            }
        }
示例#5
0
        static int CallClousot2(string[] args)
        {
            var cciilProvider   = CciILCodeProvider.CreateCodeProvider();
            var metadataDecoder = cciilProvider.MetaDataDecoder;
            var contractDecoder = cciilProvider.ContractDecoder;
            var assemblyCache   = new System.Collections.Hashtable();

            var textWriter    = Console.Out;
            var outputFactory = new FullTextWriterOutputFactory <MethodReferenceAdaptor, IAssemblyReference>(textWriter, "ServiceOutput");

            try
            {
                // We now call Clousot
                int returnCode = Clousot.ClousotMain(args,
                                                     metadataDecoder,
                                                     contractDecoder,
                                                     assemblyCache,
                                                     outputFactory: outputFactory,
                                                     cacheAccessorFactories: null
                                                     );

                return(returnCode);
            }
            catch (ExitRequestedException e)
            {
                return(e.ExitCode);
            }
            catch (Exception e)
            {
                // We need to catch all exception, otherwise the service is turned into an faulted state and remains unusable
                textWriter.WriteLine("[{0}]: Many Clousot: caught exception: {1}", DateTime.Now, e);
                if (e.InnerException != null)
                {
                    textWriter.WriteLine("Inner exception: {0}", e.InnerException);
                }
                return(-4444);
            }
            finally
            {
                Console.WriteLine("Analysis done".PrefixWithCurrentTime());
                textWriter.Close();      // make sure all pending output are flushed
                cciilProvider.Dispose(); // make sure all open files are closed
            }
        }
示例#6
0
        public static int Main(string[] args, IOutputFullResultsFactory <System.Compiler.Method, System.Compiler.AssemblyNode> outputFactory, IEnumerable <Caching.IClousotCacheFactory> cacheAccessorFactories)
        {
            Contract.Requires(args != null);

            var assemblyCache = new Hashtable();

            try
            {
                return(Clousot.ClousotMain(args, CCIMDDecoder.Value, CCIContractDecoder.Value, assemblyCache, outputFactory, cacheAccessorFactories));
            }
            catch (ExitRequestedException e)
            {
                return(e.ExitCode);
            }
            finally
            {
                DisposeAssemblies(assemblyCache.Values);
            }
        }
示例#7
0
        public static int Main(string[] args, IOutputFullResultsFactory <System.Compiler.Method, System.Compiler.AssemblyNode> outputFactory, IEnumerable <Caching.IClousotCacheFactory> cacheAccessorFactories)
        {
            Contract.Requires(args != null);

#if USECLOUDOT
            if (OptionsHelper.UseCloudot(args))
            {
                // Just an horrible shortcut to invoke the debugger
                if (args.Contains("-break"))
                {
                    System.Diagnostics.Debugger.Launch();
                }

                for (var i = 0; i < CloudotAnalysesRetry; i++)
                {
                    string[] clousotArgs = null;
                    try
                    {
                        return(ClousotViaService.Main(args, out clousotArgs));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Connection to Cloudot terminated with an exception".PrefixWithCurrentTime());
                        Console.WriteLine("Exception details: {0}", e.ToString());

                        if (i == CloudotAnalysesRetry - 1)
                        {
                            Console.WriteLine("We gave up with the service. Reverting to local execution");
                        }
                        else
                        {
                            Console.WriteLine("Try to use the Cloudot again (remaining {0})", CloudotAnalysesRetry - (i - 1));
                        }

                        args = clousotArgs; // We use the service specific options in the case we revert to local execution
                    }
                }
            }
#endif
            var assemblyCache = new Hashtable();
            try
            {
#if DEBUG
#if USECLOUDOT
                if (args != null)
#endif
                {
                    args = args.Where(s => s != StringConstants.DoNotUseCloudot).ToArray();
                }
#endif
                return(Clousot.ClousotMain(args, CCIMDDecoder.Value, CCIContractDecoder.Value, assemblyCache, outputFactory, cacheAccessorFactories));
            }
            catch (ExitRequestedException e)
            {
                return(e.ExitCode);
            }
            finally
            {
                DisposeAssemblies(assemblyCache.Values);
            }
        }
示例#8
0
        /// <summary>
        /// Checks the <paramref name="compilationUnitDeclaration"/> for syntax and semantic
        /// errors and returns an empty enumerable if any are found.
        /// </summary>
        public IEnumerable <ClousotOutput> AnalyzeMeAUnit(
            Microsoft.CodeAnalysis.Document document,
            CompilationUnitSyntax compilationUnitDeclaration,
            CancellationToken cancellationToken,
            ClousotOptions options,
            string[] extraOptions,
            bool showOnlyAnswersToAskClousot = false
            )
        {
            if (options == null)
            {
                options = new ClousotOptions();
            }

            // Don't do anything if there are syntactic errors.
            if (compilationUnitDeclaration.ContainsDiagnostics)
            {
                yield break;
            }

            var semanticModel = document.GetSemanticModel(cancellationToken);

            // Don't do anything if there are semantic errors.
            var diagnostics = semanticModel.GetDiagnostics(cancellationToken);

            if (diagnostics.Any(d => d.Info.Severity == DiagnosticSeverity.Error || d.Info.IsWarningAsError))
            {
                yield break;
            }

            this.host = new ClousotGlueHost((document.Project).MetadataReferences);

            this.sourceLocationProvider = new SourceLocationProvider();
            string             exceptionMessage = null;
            IAssemblyReference ar = null;

            try {
                // Constructs a full metadata model for the assembly the semantic model is from
                var transformer = new NodeVisitor(this.host, semanticModel, sourceLocationProvider);

                // Constructs the method bodies all of the methods in the compilation unit
                // var tree = document.GetSyntaxTree(cancellationToken);
                var tree2 = transformer.Visit(compilationUnitDeclaration);
                ar = tree2 as IAssemblyReference;
            } catch (ConverterException e) {
                exceptionMessage = e.Message;
            } catch (OperationCanceledException) {
                // just return nothing
                yield break;
            }
            if (exceptionMessage != null)
            {
                yield return(new ClousotOutput(null, exceptionMessage, compilationUnitDeclaration.GetFirstToken().Span, null, (ICodeAction)null, ClousotOutput.ExtraInfo.None));

                yield break;
            }

            var spanToMethodMap = MethodSpanFinder.GetMethodSpans(compilationUnitDeclaration);

            lock (this) { // Clousot is single-threaded
                var cciProvider = Microsoft.Cci.Analysis.CciILCodeProvider.CreateCodeProvider(host);
                var unit        = ar;
                this.host.RegisterUnit(unit.ResolvedUnit);
                cciProvider.MetaDataDecoder.RegisterSourceLocationProvider(unit, sourceLocationProvider);
                var metadataDecoder = cciProvider.MetaDataDecoder;
                var contractDecoder = cciProvider.ContractDecoder;

                var defaultargs = new string[] {
                    "-nonnull",
                    "-bounds",
                    "-arithmetic",
                    "-sortwarns:-",
                    //"-arrays",
                    "-cache",
                    //"-suggest=methodensures",
                    "-suggest=propertyensures",
                    "-suggest=objectinvariants",
                    //"-infer=requires",
                    //"-infer=objectinvariants",
                    //"-clearcache",
                    //"-prefrompost"
                };
                var codefixesargs = new string[] {
                    "-nonnull",
                    "-bounds",
                    "-arithmetic",
                    "-suggest=codefixes",
                    "-cache",
                    "-libpaths:\"c:\\program files (x86)\\Microsoft\\Contracts\\Contracts\\.NetFramework\\v4.0\"",
                    //"-suggest=methodensures",
                    "-suggest=objectinvariants",
                    "-infer=objectinvariants",
                    "-suggest=assumes",
                    "-premode=backwards",
                };
                var args = codefixesargs;
                if (extraOptions != null)
                {
                    args = args.Concat(extraOptions).ToArray();
                }

                var w = (options == null || String.IsNullOrWhiteSpace(options.WarningLevel))
          ? "low"
          : options.WarningLevel;

                var warninglevel = String.Format("-warninglevel={0}", w);
                var x            = new string[] { warninglevel, };
                args = args.Concat(x).ToArray();

                if (options != null)
                {
                    var otherOptions = options.OtherOptions;
                    if (!String.IsNullOrWhiteSpace(otherOptions))
                    {
                        var otherOpts = otherOptions.Split(' ');
                        args = args.Concat(otherOpts).ToArray();
                    }
                }

                this.analysisResults = new List <ClousotOutput>();
                var output         = new RoslynOutput(showOnlyAnswersToAskClousot, this.analysisResults, document, spanToMethodMap, null);
                var methodAnalyzer = Clousot.GetIndividualMethodAnalyzer(metadataDecoder, contractDecoder, args, output,
                                                                         new[] { this.cacheFactory });
                foreach (var path in methodAnalyzer.Options.libPaths)
                {
                    host.AddLibPath(path);
                }
                methodAnalyzer.AnalyzeAssembly(ar);
            }

            foreach (var result in this.analysisResults)
            {
                yield return(result);
            }
        }