Esempio n. 1
0
        public IEnumerable <Traceable> GetHeapTraceables(PTGNode node, IFieldReference field)
        {
            var location           = new Location(node, field);
            var originalTraceables = GetLocationTraceables(location);

            var traceables = new HashSet <Traceable>();


            if (field.Type.IsClassOrStruct())
            {
                foreach (var target in PTG.GetTargets(node, field))
                {
                    if (Dependencies.A2_References.ContainsKey(target) && target != SimplePointsToGraph.NullNode)
                    {
                        traceables.UnionWith(Dependencies.A2_References[target]);
                    }
                }
            }

            if (traceables.Count > originalTraceables.Count)
            {
            }

            originalTraceables.UnionWith(traceables);

            return(originalTraceables);
        }
Esempio n. 2
0
        public PTGNode ProcessGetEnum(SimplePointsToGraph ptg, uint offset, IVariable collectionVariable, IVariable result)
        {
            var ptgId = new PTGID(new MethodContex(this.method), (int)offset);

            var enumNode = this.NewNode(ptg, ptgId, PlatformTypes.IEnumerator);

            ptg.RemoveRootEdges(result);
            ptg.PointsTo(result, enumNode);

            var nodes = ptg.GetTargets(collectionVariable);

            if (nodes.Count == 1 && nodes.Single() == SimplePointsToGraph.NullNode)
            {
                var collectionNode = new PTGNode(ptgId, collectionVariable.Type);
                ptg.PointsTo(collectionVariable, collectionNode);
            }

            var collecitonField = new FieldReference("$collection", PlatformTypes.Object, this.method.ContainingType);

            this.ProcessStore(ptg, offset, result, collecitonField, collectionVariable);
            //foreach (var colNode in ptg.GetTargets(collectionVariable))
            //{
            //    ptg.PointsTo(enumNode, collecitonField, colNode);
            //}
            return(enumNode);
        }
Esempio n. 3
0
        public void AssignHeapTraceables(PTGNode ptgNode, IFieldReference field, IEnumerable <Traceable> traceables)
        {
            var newTraceables = new HashSet <Traceable>(traceables);

            // If it is a scalar field we modify A3_Field
            if (!field.Type.IsClassOrStruct())
            {
                var location = new Location(ptgNode, field);
                //this.Dependencies.A3_Fields[location] = new HashSet<Traceable>(traceables);
                // TODO:  a weak update
                this.Dependencies.A3_Fields[location] = newTraceables;
            }

            // This should be only for references
            // we modify A2_Refs(n) where n \in PT(ptgNode, field)
            if (field.Type.IsClassOrStruct())
            {
                var targets = PTG.GetTargets(ptgNode, field);// .Except(new HashSet<PTGNode>() { SimplePointsToGraph.NullNode } );
                if (targets.Any())
                {
                    foreach (var targetNode in targets)
                    {
                        if (targetNode != SimplePointsToGraph.NullNode)
                        {
                            // TODO: Change for Add
                            this.Dependencies.A2_References[targetNode] = newTraceables;
                            //this.Dependencies.A2_References[targetNode] = new HashSet<Traceable>(traceables);
                        }
                    }
                }
                else
                {
                }
            }
        }
Esempio n. 4
0
        public void PointsTo(IVariable variable, PTGNode target)
        {
            this.Add(target);
            this.roots.Add(variable, target);

            target.Variables.Add(variable);
        }
Esempio n. 5
0
        private void CreateInitialGraph()
        {
            var ptg       = new PointsToGraph();
            var variables = cfg.GetVariables();

            foreach (var variable in variables)
            {
                if (variable.Type.IsValueType)
                {
                    continue;
                }

                if (variable.IsParameter)
                {
                    var isThisParameter = variable.Name == "this";
                    var kind            = isThisParameter ? PTGNodeKind.Object : PTGNodeKind.Unknown;
                    var node            = new PTGNode(nextPTGNodeId++, variable.Type, 0, kind);

                    ptg.Add(node);
                    ptg.PointsTo(variable, node);
                }
                else
                {
                    ptg.Add(variable);
                }
            }

            this.initialGraph = ptg;
        }
Esempio n. 6
0
        public void ProcessStore(SimplePointsToGraph ptg, uint offset, IVariable instance, IFieldReference field, IVariable src)
        {
            if (!field.Type.IsClassOrStruct() || !src.Type.IsClassOrStruct())
            {
                return;
            }

            var nodes   = ptg.GetTargets(instance, false);
            var targets = ptg.GetTargets(src).Except(new HashSet <PTGNode>()
            {
                SimplePointsToGraph.NullNode
            });

            if (targets.Any())
            {
                foreach (var node in nodes)
                {
                    foreach (var target in targets)
                    {
                        ptg.PointsTo(node, field, target);
                    }
                }
            }
            else
            {
                // Create a fake node for the target
                var ptgID    = new PTGID(new MethodContex(this.method), (int)offset);
                var fakeNode = new PTGNode(ptgID, src.Type);
                foreach (var node in nodes)
                {
                    ptg.PointsTo(node, field, fakeNode);
                }
            }
        }
Esempio n. 7
0
        private PTGNode NewNode(SimplePointsToGraph ptg, PTGID ptgID, IType type, PTGNodeKind kind = PTGNodeKind.Object)
        {
            PTGNode node;

            node = new PTGNode(ptgID, type, kind);
            //node = ptg.GetNode(ptgID, type, kind);
            return(node);
        }
Esempio n. 8
0
        public ISet <PTGNode> GetTargets(PTGNode source, IFieldReference field)
        {
            var result    = new HashSet <PTGNode>();
            var nodeField = new NodeField(source, field);

            if (this.edges.ContainsKey(nodeField))
            {
                result.UnionWith(this.edges[nodeField]);
            }
            return(result);
        }
Esempio n. 9
0
        public MapSet <IFieldReference, PTGNode> GetTargets(PTGNode ptgNode)
        {
            //var result = this.edges.Where(kv => kv.Key.Source.Equals(ptgNode)).GroupBy(kv => kv.Key.Field, kv=> kv.Value);
            //return result.ToDictionary( kv => kv.Key, kv => kv.SelectMany(kv => kv.));
            var result = new MapSet <IFieldReference, PTGNode>();

            foreach (var edge in this.edges.Where(kv => kv.Key.Source.Equals(ptgNode)))
            {
                result.AddRange(edge.Key.Field, edge.Value);
            }
            return(result);
        }
Esempio n. 10
0
        private static string Serialize(PTGNode node)
        {
            string result;

            switch (node.Kind)
            {
            case PTGNodeKind.Null: result = "null"; break;

            default: result = TypeHelper.GetTypeName(node.Type); break;
            }

            return(result);
        }
Esempio n. 11
0
        private bool MayReacheableFromParameter(SimplePointsToGraph ptg, PTGNode n)
        {
            var rootNodes = method.Body.Parameters.SelectMany(p => ptg.GetTargets(p)).Union(new HashSet <PTGNode>()
            {
                SimplePointsToGraph.GlobalNode
            });
            var reachable = ptg.ReachableNodes(rootNodes).Contains(n);

            // This version does not need the inverted mapping of nodes-> variables (which may be expensive to maintain)
            // var result = method.Body.Parameters.Any(p =>ptg.GetTargets(p).Contains(n));
            return(reachable);

            ;
        }
Esempio n. 12
0
        private static string Serialize(PTGNode node)
        {
            string result;

            switch (node.Kind)
            {
            case PTGNodeKind.Null: result = "null"; break;

            default:
                result = string.Format("Node ID: {0}{1}{2}", node.Id, Environment.NewLine, node.Type);
                break;
            }

            return(result);
        }
Esempio n. 13
0
        public void PointsTo(PTGNode source, IFieldReference field, PTGNode target)
        {
            if (source.Equals(SimplePointsToGraph.NullNode))
            {
                return;
            }

            this.nodes.Add(target);
            this.nodes.Add(source);

            var currentTargets = GetTargets(source, field);

            if (currentTargets.Count == 1 && currentTargets.Single() == SimplePointsToGraph.NullNode)
            {
                this.RemoveTargets(source, field);
            }
            this.AddEdge(source, field, target);
        }
Esempio n. 14
0
        public static bool IsReachable(this PointsToGraph ptg, IVariable variable, PTGNode target)
        {
            var result       = false;
            var visitedNodes = new HashSet <PTGNode>();
            var worklist     = new Queue <PTGNode>();
            var nodes        = ptg.GetTargets(variable);

            foreach (var node in nodes)
            {
                worklist.Enqueue(node);
                visitedNodes.Add(node);
            }

            while (worklist.Any())
            {
                var node = worklist.Dequeue();

                if (node.Equals(ptg.Null))
                {
                    continue;
                }

                if (node.Equals(target))
                {
                    result = true;
                    break;
                }

                foreach (var targets in node.Targets.Values)
                {
                    foreach (var nodeTarget in targets)
                    {
                        if (!visitedNodes.Contains(nodeTarget))
                        {
                            worklist.Enqueue(nodeTarget);
                            visitedNodes.Add(nodeTarget);
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 15
0
        public bool Reachable(IVariable v1, PTGNode n)
        {
            var             ptg          = this;
            var             result       = false;
            ISet <PTGNode>  visitedNodes = new HashSet <PTGNode>();
            Queue <PTGNode> workList     = new Queue <PTGNode>();
            var             nodes        = ptg.GetTargets(v1, false);

            if (nodes.Contains(n) && !n.Equals(SimplePointsToGraph.NullNode))
            {
                return(true);
            }

            foreach (var ptgNode in nodes)
            {
                workList.Enqueue(ptgNode);
            }
            while (workList.Any())
            {
                var ptgNode = workList.Dequeue();
                visitedNodes.Add(ptgNode);
                if (ptgNode.Equals(SimplePointsToGraph.NullNode))
                {
                    continue;
                }
                if (ptgNode.Equals(n))
                {
                    return(true);
                }
                foreach (var adjacents in ptg.GetTargets(ptgNode).Values)
                {
                    foreach (var adjacent in adjacents)
                    {
                        if (!visitedNodes.Contains(adjacent))
                        {
                            workList.Enqueue(adjacent);
                        }
                    }
                }
            }
            return(result);
        }
Esempio n. 16
0
        private PTGNode GetNode(PointsToGraph ptg, uint offset, ITypeReference type, PTGNodeKind kind = PTGNodeKind.Object)
        {
            PTGNode node;

            if (nodeIdAtOffset.ContainsKey(offset))
            {
                var nodeId = nodeIdAtOffset[offset];
                node = ptg.GetNode(nodeId);
            }
            else
            {
                var nodeId = nextPTGNodeId++;
                node = new PTGNode(nodeId, type, offset, kind);

                ptg.Add(node);
                nodeIdAtOffset.Add(offset, nodeId);
            }

            return(node);
        }
Esempio n. 17
0
        private ICollection <int> InternalGetLastDefs(PTGNode ptgNode, IFieldReference f)
        {
            IDictionary <IFieldReference, ICollection <int> > lastDefsDict = new Dictionary <IFieldReference, ICollection <int> >();
            ICollection <int> lastDefs = new HashSet <int>();

            if (LastDefsPtg.ContainsKey(ptgNode))
            {
                lastDefsDict = LastDefsPtg[ptgNode];
            }
            else
            {
                LastDefsPtg[ptgNode] = lastDefsDict;
            }
            if (lastDefsDict.ContainsKey(f))
            {
                lastDefs = lastDefsDict[f];
            }
            else
            {
                lastDefsDict[f] = lastDefs;
            }

            return(lastDefs);
        }
Esempio n. 18
0
 public bool Contains(PTGNode node)
 {
     return(nodes.Contains(node));
 }
Esempio n. 19
0
 public NodeField(PTGNode source, IFieldReference field)
 {
     this.Source = source;
     this.Field  = field;
 }
Esempio n. 20
0
        public void AddEdge(PTGNode source, IFieldReference field, PTGNode target)
        {
            var nodeField = new NodeField(source, field);

            this.edges.Add(nodeField, target);
        }
Esempio n. 21
0
 public DependencyInfo(PTGNode symObj, string traceable)
 {
     SymbolicObject = symObj;
     Traceable      = traceable;
 }
Esempio n. 22
0
        private ICollection <int> LastDefGet(PTGNode ptgNode, IFieldReference f)
        {
            ICollection <int> lastDefs = InternalGetLastDefs(ptgNode, f);

            return(lastDefs);
        }
Esempio n. 23
0
        private void LastDefSet(PTGNode ptgNode, IFieldReference f, IEnumerable <int> locations)
        {
            ICollection <int> lastDefs = InternalGetLastDefs(ptgNode, f);

            lastDefs.AddRange(locations);
        }
Esempio n. 24
0
        private void LastDefSet(PTGNode ptgNode, IFieldReference f, int location)
        {
            ICollection <int> lastDefs = InternalGetLastDefs(ptgNode, f);

            lastDefs.Add(location);
        }
Esempio n. 25
0
        public void RemoveTargets(PTGNode source, IFieldReference field)
        {
            var nodeField = new NodeField(source, field);

            this.edges.Remove(nodeField);
        }
Esempio n. 26
0
        //private void Flow(SimplePointsToGraph ptg, Instruction instruction)
        //{
        //    var ptaVisitor = new PTAVisitor(ptg, this);
        //    ptaVisitor.Visit(instruction);
        //}

        private void CreateInitialGraph(bool createNodeForParams = true, SimplePointsToGraph initialGraph = null)
        {
            this.ReturnVariable = new LocalVariable(this.method.Name + "_" + "$RV")
            {
                Type = PlatformTypes.Object
            };

            //IteratorPointsToAnalysis.GlobalVariable= new LocalVariable("$Global");
            //IteratorPointsToAnalysis.GlobalVariable.Type = PlatformTypes.Object;

            var ptg = initialGraph == null ? new SimplePointsToGraph() : initialGraph.Clone();

            var variables = cfg.GetVariables();

            if (this.method.Body.Parameters != null)
            {
                variables.AddRange(this.method.Body.Parameters);
            }

            int       counter      = -1;
            IVariable thisVariable = null;
            PTGNode   thisNode     = null;

            foreach (var variable in variables)
            {
                // TODO: replace when Egdardo fixes type inferece
                if (variable.Type == null || !variable.Type.IsClassOrStruct())
                {
                    continue;
                }
                // if (variable.Type.TypeKind == TypeKind.ValueType) continue;

                if (variable.IsParameter)
                {
                    var isThisParameter = variable.Name == "this";
                    var kind            = isThisParameter ? PTGNodeKind.Object : PTGNodeKind.Unknown;
                    if (createNodeForParams)
                    {
                        var ptgId = new PTGID(new MethodContex(this.method), counter--);
                        var node  = new ParameterNode(ptgId, variable.Name, variable.Type);
                        ptg.Add(node);
                        ptg.PointsTo(variable, node);
                        if (isThisParameter)
                        {
                            thisVariable = variable;
                            thisNode     = node;
                        }
                    }
                    if (isThisParameter)
                    {
                        this.ThisVariable = variable;
                    }
                    ptg.Add(variable);
                }
                else
                {
                    //ptg.Add(variable);
                    ptg.PointsTo(variable, SimplePointsToGraph.NullNode);
                }
            }

            //foreach(var specialField in specialFields)
            //{
            //    counter = -1000;
            //    var variable = specialField.Value;
            //    var fieldName =  specialField.Key;
            //    var ptgId = new PTGID(new MethodContex(this.method), counter--);
            //    var node = new PTGNode(ptgId, variable.Type);
            //    ptg.Add(node);
            //    ptg.PointsTo(thisNode, new FieldReference(fieldName, variable.Type, method.ContainingType), node);
            //}
            ptg.Add(this.ReturnVariable);
            ptg.Add(IteratorPointsToAnalysis.GlobalVariable);
            ptg.PointsTo(IteratorPointsToAnalysis.GlobalVariable, SimplePointsToGraph.GlobalNode);
            this.initialGraph = ptg;
        }
Esempio n. 27
0
 public void Add(PTGNode node)
 {
     nodes.Add(node);
 }