Ejemplo n.º 1
0
        /// <summary>
        /// Checks whether there is a definition clear path from the beginning of the
        /// method to the current usage node. here v represents a usage node
        /// </summary>
        /// <param name="v"></param>
        /// <param name="otherDefOffsets"></param>
        public bool HasDefClearPathFromBeginning(InstructionVertex v, HashSet <int> otherDefOffsets)
        {
            Stack <InstructionVertex> verticesStack = new Stack <InstructionVertex>();

            verticesStack.Push(v);

            HashSet <InstructionVertex> visitiedVertices = new HashSet <InstructionVertex>();

            while (verticesStack.Count > 0)
            {
                InstructionVertex iv = verticesStack.Pop();
                visitiedVertices.Add(iv);

                //Reached an end node, showing a feasible path
                if (this.m_VisitedGraph.NumInEdges(iv) == 0)
                {
                    return(true);
                }

                foreach (var inelem in this.m_VisitedGraph.InEdges(iv))
                {
                    Edge inEdge = inelem as Edge;
                    var  source = inEdge.Source as InstructionVertex;
                    //Visit a vertex further only if it is not in the redefined set
                    if (!visitiedVertices.Contains(source) && !otherDefOffsets.Contains(source.Instruction.Offset))
                    {
                        verticesStack.Push(source);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks whether the current vertex has a feasible path without encountering
        /// the other offsets
        /// </summary>
        /// <param name="v"></param>
        /// <param name="otherDefOffsets"></param>
        public bool HasDefClearPathToEnd(InstructionVertex v, HashSet <int> otherDefOffsets)
        {
            Stack <InstructionVertex> verticesStack = new Stack <InstructionVertex>();

            verticesStack.Push(v);

            HashSet <InstructionVertex> visitiedVertices = new HashSet <InstructionVertex>();

            while (verticesStack.Count > 0)
            {
                InstructionVertex iv = verticesStack.Pop();
                visitiedVertices.Add(iv);

                //Reached an end node, showing a feasible path
                if (this.m_VisitedGraph.NumOutEdges(iv) == 0)
                {
                    return(true);
                }

                foreach (var outelem in this.m_VisitedGraph.OutEdges(iv))
                {
                    Edge outEdge = outelem as Edge;
                    var  target  = outEdge.Target as InstructionVertex;
                    //Visit a vertex further only if it is not in the redefined set
                    if (!visitiedVertices.Contains(target) && !otherDefOffsets.Contains(target.Instruction.Offset))
                    {
                        verticesStack.Push(target);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks whether the current vertex has a feasible path without encountering
        /// the other offsets to the given other node
        /// </summary>
        /// <param name="v"></param>
        /// <param name="otherDefOffsets"></param>
        public bool HasDefClearPathBetweenNodes(InstructionVertex source, InstructionVertex target, HashSet <int> otherDefOffsets)
        {
            Stack <InstructionVertex> verticesStack = new Stack <InstructionVertex>();

            verticesStack.Push(source);

            HashSet <InstructionVertex> visitiedVertices = new HashSet <InstructionVertex>();

            while (verticesStack.Count > 0)
            {
                InstructionVertex iv = verticesStack.Pop();
                visitiedVertices.Add(iv);

                foreach (var outelem in this.m_VisitedGraph.OutEdges(iv))
                {
                    Edge outEdge    = outelem as Edge;
                    var  edgetarget = outEdge.Target as InstructionVertex;
                    if (target.Equals(edgetarget))
                    {
                        return(true);
                    }

                    //Visit a vertex further only if it is not in the redefined set
                    if (!visitiedVertices.Contains(edgetarget) && !otherDefOffsets.Contains(edgetarget.Instruction.Offset))
                    {
                        verticesStack.Push(edgetarget);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Checks whether there is a definition clear path from the beginning of the 
        /// method to the current usage node. here v represents a usage node
        /// </summary>
        /// <param name="v"></param>
        /// <param name="otherDefOffsets"></param>
        public bool HasDefClearPathFromBeginning(InstructionVertex v, HashSet<int> otherDefOffsets)
        {
            Stack<InstructionVertex> verticesStack = new Stack<InstructionVertex>();
            verticesStack.Push(v);

            HashSet<InstructionVertex> visitiedVertices = new HashSet<InstructionVertex>();
            while (verticesStack.Count > 0)
            {
                InstructionVertex iv = verticesStack.Pop();
                visitiedVertices.Add(iv);

                //Reached an end node, showing a feasible path
                if (this.m_VisitedGraph.NumInEdges(iv) == 0)
                    return true;

                foreach (var inelem in this.m_VisitedGraph.InEdges(iv))
                {
                    Edge inEdge = inelem as Edge;
                    var source = inEdge.Source as InstructionVertex;
                    //Visit a vertex further only if it is not in the redefined set
                    if (!visitiedVertices.Contains(source) && !otherDefOffsets.Contains(source.Instruction.Offset))
                    {
                        verticesStack.Push(source);
                    }
                }
            }

            return false;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Checks whether the current vertex has a feasible path without encountering
        /// the other offsets to the given other node
        /// </summary>
        /// <param name="v"></param>
        /// <param name="otherDefOffsets"></param>
        public bool HasDefClearPathBetweenNodes(InstructionVertex source, InstructionVertex target, HashSet<int> otherDefOffsets)
        {
            Stack<InstructionVertex> verticesStack = new Stack<InstructionVertex>();
            verticesStack.Push(source);

            HashSet<InstructionVertex> visitiedVertices = new HashSet<InstructionVertex>();
            while (verticesStack.Count > 0)
            {
                InstructionVertex iv = verticesStack.Pop();
                visitiedVertices.Add(iv);

                foreach (var outelem in this.m_VisitedGraph.OutEdges(iv))
                {
                    Edge outEdge = outelem as Edge;
                    var edgetarget = outEdge.Target as InstructionVertex;
                    if (target.Equals(edgetarget))
                        return true;

                    //Visit a vertex further only if it is not in the redefined set
                    if (!visitiedVertices.Contains(edgetarget) && !otherDefOffsets.Contains(edgetarget.Instruction.Offset))
                    {
                        verticesStack.Push(edgetarget);
                    }
                }
            }

            return false;
        }
Ejemplo n.º 6
0
        public override bool Equals(object obj)
        {
            InstructionVertex iv = obj as InstructionVertex;

            if (iv == null)
            {
                return(false);
            }

            return(this.instruction.Offset == iv.instruction.Offset);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Add a new InstructionVertex to the graph and returns it.
        /// </summary>
        /// <returns>
        /// Created vertex
        /// </returns>
        public InstructionVertex AddVertex(Instruction instruction)
        {
            InstructionVertex v = new InstructionVertex(instruction);

            if (this.vertices.Count == 0)
                this.rootVertex = v;

            List<Edge> outEdges = new List<Edge>();
            this.vertexOutEdges.Add(v, outEdges);

            List<Edge> inEdges = new List<Edge>();
            this.vertexInEdges.Add(v, inEdges);

            return v;
        }
Ejemplo n.º 8
0
        public Edge AddEdge(
            InstructionVertex source,
            InstructionVertex target
            )
        {
            // look for the vertex in the list
            if (!this.vertexOutEdges.ContainsKey(source))
                throw new Exception("Could not find source vertex");
            if (!this.vertexOutEdges.ContainsKey(target))
                throw new Exception("Could not find target vertex");

            // create edge
            Edge e = new Edge(source, target);
            this.vertexOutEdges[source].Add(e);
            this.vertexInEdges[target].Add(e);
            return e;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Add a new InstructionVertex to the graph and returns it.
        /// </summary>
        /// <returns>
        /// Created vertex
        /// </returns>
        public InstructionVertex AddVertex(Instruction instruction)
        {
            InstructionVertex v = new InstructionVertex(instruction);

            if (this.vertices.Count == 0)
            {
                this.rootVertex = v;
            }

            List <Edge> outEdges = new List <Edge>();

            this.vertexOutEdges.Add(v, outEdges);

            List <Edge> inEdges = new List <Edge>();

            this.vertexInEdges.Add(v, inEdges);

            return(v);
        }
Ejemplo n.º 10
0
        public Edge AddEdge(
            InstructionVertex source,
            InstructionVertex target
            )
        {
            // look for the vertex in the list
            if (!this.vertexOutEdges.ContainsKey(source))
            {
                throw new Exception("Could not find source vertex");
            }
            if (!this.vertexOutEdges.ContainsKey(target))
            {
                throw new Exception("Could not find target vertex");
            }

            // create edge
            Edge e = new Edge(source, target);

            this.vertexOutEdges[source].Add(e);
            this.vertexInEdges[target].Add(e);
            return(e);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Populates all vertices
        /// </summary>
        /// <param name="method"></param>
        private void PopulateVertices(Method method)
        {
            MethodBodyEx body;

            if (!method.TryGetBody(out body) || !body.HasInstructions)
            {
                return;
            }

            int         offset = 0;
            Instruction instruction;

            while (body.TryGetInstruction(offset, out instruction))
            {
                SafeDebug.AssumeNotNull(instruction, "instruction");
                OpCode opCode = instruction.OpCode;

                InstructionVertex iv = this.AddVertex(instruction);
                this.vertices[offset] = iv;
                offset = instruction.NextOffset;
            }
        }
Ejemplo n.º 12
0
        public void Traverse(InstructionVertex v)
        {
            Stack <InstructionVertex> verticesStack = new Stack <InstructionVertex>();

            verticesStack.Push(v);

            HashSet <InstructionVertex> visitiedVertices = new HashSet <InstructionVertex>();

            while (verticesStack.Count > 0)
            {
                InstructionVertex iv = verticesStack.Pop();
                visitiedVertices.Add(iv);

                foreach (var outelem in this.m_VisitedGraph.OutEdges(iv))
                {
                    Edge outEdge = outelem as Edge;
                    var  target  = outEdge.Target as InstructionVertex;
                    if (!visitiedVertices.Contains(target))
                    {
                        verticesStack.Push(target);
                    }
                }
            }
        }
Ejemplo n.º 13
0
        public void Traverse(InstructionVertex v)
        {
            Stack<InstructionVertex> verticesStack = new Stack<InstructionVertex>();
            verticesStack.Push(v);

            HashSet<InstructionVertex> visitiedVertices = new HashSet<InstructionVertex>();
            while (verticesStack.Count > 0)
            {
                InstructionVertex iv = verticesStack.Pop();
                visitiedVertices.Add(iv);

                foreach (var outelem in this.m_VisitedGraph.OutEdges(iv))
                {
                    Edge outEdge = outelem as Edge;
                    var target = outEdge.Target as InstructionVertex;
                    if (!visitiedVertices.Contains(target))
                    {
                        verticesStack.Push(target);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Checks whether the current vertex has a feasible path without encountering
        /// the other offsets
        /// </summary>
        /// <param name="v"></param>
        /// <param name="otherDefOffsets"></param>
        public bool HasDefClearPathToEnd(InstructionVertex v, HashSet<int> otherDefOffsets)
        {
            Stack<InstructionVertex> verticesStack = new Stack<InstructionVertex>();
            verticesStack.Push(v);

            HashSet<InstructionVertex> visitiedVertices = new HashSet<InstructionVertex>();
            while (verticesStack.Count > 0)
            {
                InstructionVertex iv = verticesStack.Pop();
                visitiedVertices.Add(iv);

                //Reached an end node, showing a feasible path
                if (this.m_VisitedGraph.NumOutEdges(iv) == 0)
                    return true;

                foreach (var outelem in this.m_VisitedGraph.OutEdges(iv))
                {
                    Edge outEdge = outelem as Edge;
                    var target = outEdge.Target as InstructionVertex;
                    //Visit a vertex further only if it is not in the redefined set
                    if (!visitiedVertices.Contains(target) && !otherDefOffsets.Contains(target.Instruction.Offset))
                    {
                        verticesStack.Push(target);
                    }
                }
            }

            return false;
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Adds edges to the graph
        /// </summary>
        /// <param name="method"></param>
        private void PopulateEdges(Method method)
        {
            MethodBodyEx body;

            if (!method.TryGetBody(out body) || !body.HasInstructions)
            {
                return;
            }

            int               offset = 0;
            Instruction       instruction;
            InstructionVertex cv = null;

            //make the graph
            while (body.TryGetInstruction(offset, out instruction))
            {
                SafeDebug.AssumeNotNull(instruction, "instruction");
                OpCode opCode = instruction.OpCode;

                InstructionVertex iv = this.vertices[offset];
                if (cv != null)
                {
                    this.AddEdge(cv, iv);
                }

                if (MethodOrFieldAnalyzer.BranchOpCodes.Contains(opCode))
                {
                    InstructionVertex alternatev = this.vertices[instruction.BrTargetOffset];
                    this.AddEdge(iv, alternatev);
                    cv = iv;
                }
                else if (opCode == OpCodes.Switch)
                {
                    foreach (var switchoff in instruction.SwitchOffsets)
                    {
                        InstructionVertex alternatev = this.vertices[switchoff];
                        this.AddEdge(iv, alternatev);
                    }
                    cv = iv;
                }
                else if (opCode == OpCodes.Br || opCode == OpCodes.Br_S)
                {
                    InstructionVertex alternatev = this.vertices[instruction.BrTargetOffset];
                    this.AddEdge(iv, alternatev);
                    cv = null;
                }
                else if (opCode == OpCodes.Break)
                {
                    InstructionVertex alternatev = this.vertices[instruction.BrTargetOffset];
                    this.AddEdge(iv, alternatev);
                    cv = null;
                }
                else if (opCode == OpCodes.Ret || opCode == OpCodes.Throw)
                {
                    cv = null;
                }
                else
                {
                    cv = iv;
                }
                offset = instruction.NextOffset;
            }
        }