示例#1
0
        private void ComputeStackDepths(int startIndex, int stack)
        {
            int index = startIndex;
            int count = Instructions.Count;

            while (index < count)
            {
                ILInstruction ili = Instructions[index];
                int           npop, npush;
                StackInfluenceAnalysis.GetStackBilance(ili, Method, out npop, out npush);
                int delta = npush - npop;

                _preStackDepth[ili.Index] = stack;
                stack += delta;
                if (stack < 0)
                {
                    throw new InvalidOperationException("The MSIL code is ill-formed (stack underrun)");
                }

                if (_postStackDepth[ili.Index] < 0)
                {
                    _postStackDepth[ili.Index] = stack;
                }
                else if (_postStackDepth[ili.Index] != stack)
                {
                    throw new InvalidOperationException("The MSIL code is ill-formed (inconsistent stack behavior)");
                }
                else
                {
                    return;
                }

                ILInstruction[] succs = GetSuccessorsOf(ili);
                if (succs.Length == 0)
                {
                    return;
                }
                else if (succs.Length == 1)
                {
                    index = succs[0].Index;
                    if (index == InstructionInfo.Marshal.Index)
                    {
                        return;
                    }
                }
                else
                {
                    foreach (ILInstruction succ in succs)
                    {
                        if (succ.Index != InstructionInfo.Marshal.Index)
                        {
                            ComputeStackDepths(succ.Index, stack);
                        }
                    }
                    return;
                }
            }
        }
示例#2
0
        private IEnumerable <ReferenceInfo> HandleCall(ILInstruction ili, ControlFlowGraph <ILInstruction> cfg)
        {
            MethodBase mb = (MethodBase)ili.Operand;
            List <ReferenceInfo.EMode> modes = new List <ReferenceInfo.EMode>();

            if (!mb.IsStatic && !mb.IsConstructor)
            {
                modes.Add(ReferenceInfo.EMode.Read | ReferenceInfo.EMode.Write);
            }
            ParameterInfo[] pis = mb.GetParameters();
            foreach (ParameterInfo pi in pis)
            {
                if (pi.ParameterType.IsByRef)
                {
                    if (pi.IsOut)
                    {
                        modes.Add(ReferenceInfo.EMode.Write);
                    }
                    else
                    {
                        modes.Add(ReferenceInfo.EMode.Read |
                                  ReferenceInfo.EMode.Write);
                    }
                }
                else
                {
                    modes.Add(ReferenceInfo.EMode.NoAccess);
                }
            }
            if (ili.Code.Equals(OpCodes.Calli))
            {
                modes.Add(ReferenceInfo.EMode.Read | ReferenceInfo.EMode.Write);
            }
            return(modes.Select((m, i) => new ReferenceInfo(m, ReferenceInfo.EKind.Indirect,
                                                            StackInfluenceAnalysis.GetStackElementDefinitions(ili.Index, i - modes.Count + 1, (MethodCode)cfg))));
        }
示例#3
0
 /// <summary>
 /// Returns all instructions which might define a stack element at a certain program location
 /// </summary>
 /// <param name="ilIndex">instruction index</param>
 /// <param name="stackLevel">stack index (0 is top)</param>
 /// <returns>all instructions which might define given program location</returns>
 public IEnumerable <int> GetStackElementDefinitions(int ilIndex, int stackLevel)
 {
     return(StackInfluenceAnalysis.GetStackElementDefinitions(ilIndex, stackLevel, CFG));
 }
示例#4
0
 private IEnumerable <ReferenceInfo> HandleStind(ILInstruction ili, ControlFlowGraph <ILInstruction> cfg)
 {
     yield return(new ReferenceInfo(ReferenceInfo.EMode.Write, ReferenceInfo.EKind.Assignment,
                                    StackInfluenceAnalysis.GetStackElementDefinitions(ili.Index, -1, (MethodCode)cfg)));
 }