예제 #1
0
        private static string calculateTreeNodeText(IO2Finding o2Finding, string propertyToUse, string filterToUse)
        {
            string nodeText;

            try
            {
                switch (propertyToUse)
                {
                case "severity":
                    return(OzasmtUtils.getSeverityFromId(o2Finding.severity));

                case "confidence":
                    return(OzasmtUtils.getConfidenceFromId(o2Finding.confidence));

                case "o2Traces":
                    var allO2Traces = OzasmtUtils.getAllTraces(o2Finding.o2Traces);
                    return((allO2Traces.Keys.Count > 0) ? "# nodes: {0}".format(allO2Traces.Keys.Count) : "");

                default:
                    nodeText = PublicDI.reflection.getProperty(propertyToUse, o2Finding).ToString();
                    break;
                }
                if (nodeText != "")
                {
                    if (RegEx.findStringInString(nodeText, filterToUse) || nodeText.index(filterToUse) > -1)
                    {
                        return(nodeText);
                    }
                    else
                    {
                        return("");
                    }
                }
                return(nodeText);
            }
            catch (Exception ex)
            {
                PublicDI.log.error("in calculateTreeNodeText: {0}", ex.Message);
                return("[O2 Error (check logs for details)]");
            }
        }
        /// <summary>
        /// This will populate the parent finding with all traces from the provided ICirFunction
        /// caution: use the createNewFindingOnExternalCall carefully since it can create a stupid amount of traces (and it is much slower)
        /// </summary>
        /// <param name="cirFunction"></param>
        /// <param name="lineNumber"></param>
        /// <param name="o2Traces"></param>
        /// <param name="parentTraces"></param>
        /// <param name="rootO2Finding"></param>
        /// <param name="o2FindingsCreated"></param>
        /// <param name="createNewFindingOnExternalCall"></param>
        /// <param name="fileName"></param>
        public static void createTracesAndFindingsFromCirFunction(ICirFunction cirFunction, string fileName, UInt32 lineNumber, List <IO2Trace> o2Traces, List <IO2Trace> parentTraces, IO2Finding rootO2Finding, List <IO2Finding> o2FindingsCreated, bool createNewFindingOnExternalCall)
        {
            int maxParentDepth    = 10; //30; //10;
            var maxNumberOfTraces = 20; //50; //300; //50
            var filteredSignature = new FilteredSignature(cirFunction);
            var functionSignature = filteredSignature.sSignature;

            var o2Trace = new O2Trace(functionSignature, cirFunction.ClassNameFunctionNameAndParameters)
            {
                file       = fileName,
                lineNumber = lineNumber
            };

            // add file references

            // handle the case where this is a recursive call or a call to a method already added in the current tree
            var recursiveCall = false;

            foreach (var o2ParentTrace in parentTraces)
            {
                if (o2ParentTrace.signature == functionSignature)
                {
                    recursiveCall = true;
                    break;
                }
            }
            parentTraces.Add(o2Trace);
            // add this trace to the current trace tree (since we might need to create a copy of it below
            o2Traces.Add(o2Trace);
            if (recursiveCall)
            {
                var nodeText = String.Format("{0} : {1} : {2}", cirFunction, "....(Recursive Call so not expanding child traces", functionSignature);
                o2Trace.childTraces.Add(new O2Trace(nodeText));
            }
            else
            {
                if (parentTraces.Count > maxParentDepth)
                {
                    o2Trace.childTraces.Add(new O2Trace(" ... {Max trace depth reached} (" + maxParentDepth + ")"));
                }
                else
                {
                    //
                    var numberOfTraces = OzasmtUtils.getAllTraces(rootO2Finding.o2Traces);
                    if (numberOfTraces.Count > maxNumberOfTraces)
                    {
                        o2Trace.childTraces.Add(new O2Trace("**** Max number of traces reached(" + maxNumberOfTraces + ") aborting trace execution"));
                        return;
                    }

                    if (cirFunction.FunctionsCalled.Count == 0) // means we don't have the code for this one, so
                    {
                        // let make it a lost sink
                        var originalTraceTypeValue = o2Trace.traceType; // we might need this below
                        o2Trace.traceType = TraceType.Lost_Sink;
                        if (createNewFindingOnExternalCall)             // and if createNewFindingOnExternalCall add it as finding
                        {
                            // create a copy of the parent finding (which incudes the above trace
                            var newFinding = OzasmtCopy.createCopy(rootO2Finding);
                            // make the first call a source (so that we have a source-> pair
                            newFinding.o2Traces[0].traceType = TraceType.Source;
                            // add it
                            o2FindingsCreated.Add(newFinding);
                            // since the crawl will continue we must restore the originalTraceTypeValue
                            o2Trace.traceType = originalTraceTypeValue;
                        }
                    }
                    else
                    {
                        foreach (var functionCalled in cirFunction.FunctionsCalled)
                        {
                            createTracesAndFindingsFromCirFunction(functionCalled.cirFunction, functionCalled.fileName, (UInt32)functionCalled.lineNumber, o2Trace.childTraces, parentTraces, rootO2Finding, o2FindingsCreated, createNewFindingOnExternalCall);
                        }
                    }
                }
            }

            // now remove the signature since we are only interrested in non repeats on the same parent
            parentTraces.Remove(o2Trace);
        }