Example #1
0
        public static IEnumerable <CCCheckOutputAssemblyMethodCheck> GetChecks(string xmlfile)
        {
            Contract.Requires(xmlfile != null);
            Contract.Ensures(Contract.Result <IEnumerable <CCCheckOutputAssemblyMethodCheck> >() != null);

            CCCheckOutput xmldata;

            if (!XmlDoc.TryReadXml(xmlfile, out xmldata))
            {
                Output.WriteError("Can't load the xml file {0}", xmlfile);
                return(Helpers.IEnumerable.EmptyEnumeration <CCCheckOutputAssemblyMethodCheck>());
            }

            var checks     = new List <CCCheckOutputAssemblyMethodCheck>();
            var assemblies = xmldata.Items.OfType <CCCheckOutputAssembly>();

            foreach (var assembly in assemblies)
            {
                foreach (var methodgroup in assembly.Method.GroupBy(x => x.Name))
                {
                    var method = methodgroup.Last();
                    if (method.Check != null)
                    {
                        checks = checks.Union(method.Check, new CommentEqualitiyComparer()).ToList();
                    }
                }
            }
            return(checks);
        }
Example #2
0
        private static bool TryRunMSBuild(string solutionPath, string MSBuildPath)
        {
            Contract.Requires(solutionPath != null);
            Contract.Requires(MSBuildPath != null);

            Output.WriteLine("Building the solution {0}", solutionPath);
            Output.WriteLine("Path for msbuild: {0}", MSBuildPath);

            var p = new Process();

            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.FileName               = MSBuildPath;
            p.StartInfo.Arguments              = solutionPath;
            p.StartInfo.RedirectStandardOutput = true;

            p.Start();
            var buildOutputAsync = p.StandardOutput.ReadToEndAsync();

            Contract.Assume(buildOutputAsync != null, "Missing contract");
            p.WaitForExit(); // It may take a while, as it also runs the CC static checker

            /*
             * while(!p.HasExited)
             * {
             * var id = p.Id;
             *
             * var procs = Process.GetProcessesByName("cccheck.exe");
             *
             * Process child = procs[0];
             *
             * foreach (Process proc in procs)
             * {
             *  proc.Par
             *  if (id != proc.Id)
             *
             *    child = proc;
             * }
             * }
             */

            // Write the output to the file
            var buildOutputFileName = Constants.String.BuildOutputDir(Path.GetFileNameWithoutExtension(solutionPath));
            var text = buildOutputAsync.Result;

            Contract.Assume(text != null);
            Helpers.IO.DumpBuildOutput(buildOutputFileName, text);

            if (p.ExitCode != 0)
            {
                Output.WriteError("Building the solution failed. Check the build output file {0}", buildOutputFileName);
                return(false);
            }

            return(true);
        }
Example #3
0
        public static bool TryRunClousot(string outputFile, string ClousotPath, string ClousotOptions, string rspPath)
        {
            Contract.Requires(ClousotPath != null);

            Output.WritePhase("Running the CC static checker -- It may take a while, please be patient");

            if (!File.Exists(rspPath))
            {
                Output.WriteError("The rsp file does not exists");
                return(false);
            }

            string  text    = null;
            Process p       = null;
            var     rspFile = "@" + rspPath;

            try
            {
                // Run Clousot
                p = new Process();
                p.StartInfo.FileName               = ClousotPath;
                p.StartInfo.Arguments              = rspFile + " " + ClousotOptions;
                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();
                var output = p.StandardOutput;
                var reader = output.ReadToEndAsync();
                Contract.Assume(reader != null, "Missing contract");
                p.WaitForExit();
                text = reader.Result;
            }
            catch (Exception e)
            {
                Output.WriteError("Some exception happen while running Clousot. Details {0}", e.Message);
                return(false);
            }
            Contract.Assume(text != null);

            try
            {
                Helpers.IO.CreateDirAndWriteAllText(outputFile, text);
            }
            catch (Exception e)
            {
                Output.WriteError("Can't write the xml file with Clousot output. {0}File Path: {1}{0}Exception{2}", Environment.NewLine, outputFile, e.Message);
                return(false);
            }

            if (p.ExitCode != 0)
            {
                // The most common error is that Clousot cannot load the dll

                if (text.Contains(Constants.String.CannotLoadAssembly))
                {
                    Output.WriteError("Clousot can't load the assembly, did you forgot to stage the enable static checking?");
                }
                else
                {
                    Output.WriteError("Something failed in running Clousot");
                }

                return(false);
            }

            return(true);
        }