Esempio n. 1
0
        private DAGNode AdjustGoto(IlIns il, DAGNode node, bool isDecompile)
        {
            string label = AdjustGoto(il, isDecompile);

            if (label != null)
            {
                node.IsComment = true;
                il.Csharps.Add(node);
                if (node is UnaryNode)
                {
                    DAGNode newNode = DAGNode.CreateUnary(true, "goto", DAGNode.CreateLeaf(label));
                    return(newNode);
                }
                else if (node is BinaryNode)
                {
                    DAGNode newNode = DAGNode.CreateBinary(true, "if", node.NextNodes[0], DAGNode.CreateLeaf(label));
                    if (node.OtherOperators.Count > 0)
                    {
                        newNode.OtherOperators.Add(node.OtherOperators[0]);
                    }
                    return(newNode);
                }
                else if (node is TernaryNode)
                {
                    DAGNode newNode = DAGNode.CreateTernary(true, "if", node.NextNodes[0], node.NextNodes[1], DAGNode.CreateLeaf(label));
                    newNode.OtherOperators.Add(node.OtherOperators[0]);
                    return(newNode);
                }
            }
            return(node);
        }
Esempio n. 2
0
 /// <summary>
 /// 结束当前流图结点的反编译,遗传语义值与变量当前引用给未访问后继结点
 /// ,处理利用栈传出的语义值。
 /// </summary>
 /// <param name="node"></param>
 /// <param name="il"></param>
 private void EndGraphNode(FlowGraphNode node, bool isDecompile)
 {
     if (semanticStack.Count > 0)
     {
         if (isDecompile)
         {
             //将传出块的语义值改为结点引用,并将结点引用设置在块的结束语句上
             IlIns il = ilLines[node.EndLabelValue];
             foreach (SemanticValue val in semanticStack)
             {
                 DAGNode n = val.Node;
                 n = DAGNode.CreateNodeRef(n);
                 if (!il.Csharps.Contains(n))
                 {
                     il.Csharps.Insert(0, n);
                 }
                 val.IsConst = false;
                 val.Node    = n;
             }
         }
         foreach (FlowGraphNode n in node.NextNodes)
         {
             n.SemanticStacks.Add(semanticStack.Clone());
             if (n.PrevNodes.Count == 1)
             {
                 //只有一个前导块的块将继承其前导块的变量当前引用,有多个前导块的块不能继承变量当前引用
                 n.VarReferencesList.Add(new Dictionary <string, DAGNode>(varReferences));
             }
         }
     }
 }
Esempio n. 3
0
        public void AddEHClause(string opcode)
        {
            lastLabelValue++;
            IlIns il = new IlIns();

            il.Opcode     = opcode;
            il.LabelValue = lastLabelValue;
            ilLines.Add(lastLabelValue, il);

            IlOpcode op = IlOpcodes.GetEH(opcode);

            switch (op)
            {
            case IlOpcode.TRY:
            {
                curEHBlock           = curEHNode.AddNewBlock();
                curEHNode            = curEHBlock.TryNode;
                curEHNode.ScopeStart = lastLabelValue;
            }
            break;

            case IlOpcode.FINALLY:
            {
                curEHNode            = curEHBlock.AddNewFinally();
                curEHNode.ScopeStart = lastLabelValue;
            }
            break;

            case IlOpcode.FAULT:
            {
                curEHNode            = curEHBlock.AddNewFault();
                curEHNode.ScopeStart = lastLabelValue;
            }
            break;

            case IlOpcode.FILTER:
            {
                curEHNode            = curEHBlock.AddNewFilter();
                curEHNode.ScopeStart = lastLabelValue;
            }
            break;

            case IlOpcode.ENDTRY:
            case IlOpcode.ENDCATCH:
            case IlOpcode.ENDFINALLY:
            case IlOpcode.ENDFAULT:
            case IlOpcode.ENDFILTER:
            {
                curEHNode.ScopeEnd = lastLabelValue;
                curEHBlock         = curEHNode.EncloseBlock;
                curEHNode          = curEHBlock.EncloseNode;
            }
            break;
            }
        }
Esempio n. 4
0
        public void AddCatch(TypeSpecOperand operand)
        {
            lastLabelValue++;
            IlIns il = new IlIns();

            il.Opcode     = ".catch";
            il.Operand    = operand;
            il.LabelValue = lastLabelValue;
            ilLines.Add(lastLabelValue, il);

            curEHNode            = curEHBlock.AddNewFilter();
            curEHNode.ScopeStart = lastLabelValue;
        }
Esempio n. 5
0
        public void AddIl()
        {
            long v = tempIl.LabelValue;

            if (v < 0)
            {
                v = lastLabelValue + 1;
                tempIl.LabelValue = v;
            }
            lastLabelValue = v;

            if (tempIl.Operand is SwitchOperand && !switches.Contains(tempIl.LabelValue))
            {
                switches.Add(tempIl.LabelValue);
            }

            ilLines.Add(v, tempIl);
            tempIl = new IlIns();
        }
Esempio n. 6
0
        private string AdjustGoto(IlIns il, bool isDecompile)
        {
            long   lv    = IlIns.LabelToValue(il.Value);
            string label = null;

            if (switchBlocks.ContainsKey(lv))
            {
                FlowGraphNode gnode = switchBlocks[lv];
                DAGNode       dnode = switchExpressions[gnode.EndLabelValue];
                while (dnode is ReferenceNode)
                {
                    dnode = dnode.NextNodes[0];
                }
                if (dnode is LeafNode)
                {
                    DAGNode r = GetVarReference(dnode.Data);
                    if (r != null)
                    {
                        int           v        = (int)ConstUtility.ParseLong(r.Data);
                        IlIns         switchIl = ilLines[gnode.EndLabelValue];
                        SwitchOperand op       = switchIl.Operand as SwitchOperand;
                        if (v >= 0 && v < op.Labels.Count)
                        {
                            label = op.Labels[v];
                            lv    = IlIns.LabelToValue(op.Labels[v]);
                        }
                    }
                }
            }
            string label2 = AdjustGoto(il.LabelValue, lv, isDecompile);

            if (label2 != null)
            {
                return(label2);
            }
            else if (label != null)
            {
                return(label);
            }
            return(null);
        }
Esempio n. 7
0
 private void IlToCsharps()
 {
     foreach (long lv in switches)
     {
         FlowGraphNode n = flowGraph.Find(lv);
         if (n != null)
         {
             switchBlocks[n.LabelValue] = n;
         }
     }
     flowGraph.Reset();
     for (; flowGraph.CurNode != null; flowGraph.Next())
     {
         FlowGraphNode node = flowGraph.CurNode;
         int           i    = ilLines.Keys.IndexOf(node.LabelValue);
         for (; i < ilLines.Values.Count; i++)
         {
             IlIns il = ilLines.Values[i];
             if (il.LabelValue == node.LabelValue)
             {
                 BeginGraphNode(node);
             }
             else if (il.LabelValue != node.LabelValue && flowGraph.IsNode(il.LabelValue))
             {
                 EndGraphNode(node, true);
                 break;
             }
             IlOpcode op0 = IlOpcodes.GetEH(il.Opcode);
             if (op0 != IlOpcode.Prefixref)
             {
                 EvaluateEH(op0, il, true);
             }
             else
             {
                 OpCode op   = IlOpcodes.Get(il.Opcode);
                 int    type = EvaluateIl(op, il, true);
             }
         }
     }
 }
Esempio n. 8
0
        private string AdjustGoto(long srcLv, long _destLv, bool isDecompile)
        {
            string label  = null;
            long   destLv = _destLv;

            for (;;)
            {
                int    ix  = ilLines.Keys.IndexOf(destLv);
                IlIns  il  = ilLines.Values[ix];
                OpCode opc = IlOpcodes.Get(il.Opcode);
                if (opc == OpCodes.Br || opc == OpCodes.Br_S)
                {
                    label  = il.Value;
                    destLv = IlIns.LabelToValue(il.Value);
                }
                else
                {
                    break;
                }
            }
            EHBlockNode ns = rootEHNode.Find(srcLv);
            EHBlockNode nd = ns.Find(destLv);

            if (nd != null && ns != nd)
            {
                EHBlockNode d = nd;
                while (d != null && ns != d.EncloseBlock.EncloseNode)
                {
                    d = d.EncloseBlock.EncloseNode;
                }
                return("IL_" + d.ScopeStart.ToString("X6"));
            }
            if (label != null)
            {
                return(label);
            }
            return(null);
        }
Esempio n. 9
0
 private void CheckSemantic()
 {
     for (int i = 0; i < ilLines.Values.Count; i++)
     {
         IlIns il = ilLines.Values[i];
         if (flowGraph.IsNode(il.LabelValue))
         {
             FlowGraphNode node = flowGraph[il.LabelValue];
             if (node.SemanticStacks.Count > 0)
             {
                 //检查是否存在多个前导且存在传入的语义栈,这是需要处理跨块共用栈的情形,添加一个警告信息。
                 if (node.PrevNodes.Count > 1)
                 {
                     DAGNode enode = DAGNode.CreateLeaf(true, "warning ! semantic stack doesn't empty at joint !");
                     enode.IsComment = true;
                     il.Csharps.Add(enode);
                 }
                 //检查多个前导传入的语义栈的语义值数目是否一致,这表明程序或者分析出现了错误。
                 int  n     = -1;
                 bool error = false;
                 foreach (SemanticStack ss in node.SemanticStacks)
                 {
                     if (n <= 0)
                     {
                         n = ss.Count;
                     }
                     else if (ss.Count != n)
                     {
                         DAGNode enode = DAGNode.CreateLeaf(true, "error ! semantic stack doesn't balance at joint !");
                         enode.IsComment = true;
                         il.Csharps.Add(enode);
                         error = true;
                         break;
                     }
                 }
                 if (!error)
                 {
                     //对多个前导传入的语义栈里的语义值统一一个临时变量名。
                     TypeInfo[]        types = new TypeInfo[n];
                     string[]          tvars = new string[n];
                     SemanticValue[][] vals0 = new SemanticValue[node.SemanticStacks.Count][];
                     for (int ii = 0; ii < vals0.Length; ii++)
                     {
                         vals0[ii] = node.SemanticStacks[ii].ToArray();
                     }
                     SemanticValue[][] vals = new SemanticValue[n][];
                     for (int ii = 0; ii < vals.Length; ii++)
                     {
                         vals[ii] = new SemanticValue[node.SemanticStacks.Count];
                         for (int j = 0; j < vals[ii].Length; j++)
                         {
                             vals[ii][j] = vals0[j][ii];
                         }
                     }
                     int ix = 0;
                     for (; ix < n; ix++)
                     {
                         TypeInfo type = GetCompatibleType(vals[ix]);
                         if (type.IsReference)//仅对非引用值换名
                         {
                             types[ix] = type;
                             tvars[ix] = "";
                         }
                         else
                         {
                             types[ix] = type;
                             tvars[ix] = NewAlias(type);
                         }
                     }
                     foreach (SemanticStack ss in node.SemanticStacks)
                     {
                         ix = 0;
                         foreach (SemanticValue val in ss)
                         {
                             if (val.Node is ReferenceNode)
                             {
                                 if (!types[ix].IsReference)//仅对非引用值换名
                                 {
                                     val.Node.SetID(tvars[ix], types[ix]);
                                     AdjustConstForType(val.Node as ReferenceNode, types[ix]);
                                 }
                             }
                             else
                             {
                                 //error
                             }
                             ix++;
                         }
                     }
                 }
             }
         }
     }
 }
Esempio n. 10
0
        private void AdjustFlowGraph()
        {
            flowGraph.Reset();
            for (; flowGraph.CurNode != null; flowGraph.Next())
            {
                FlowGraphNode node = flowGraph.CurNode;
                int           i    = ilLines.Keys.IndexOf(node.LabelValue);
                for (; i < ilLines.Values.Count; i++)
                {
                    IlIns il = ilLines.Values[i];
                    if (il.LabelValue == node.LabelValue)
                    {
                        BeginGraphNode(node);
                    }
                    else if (il.LabelValue != node.LabelValue && flowGraph.IsNode(il.LabelValue))
                    {
                        //行进到块的开始语句才发现新开始块是异常构造块或跳转目标倒致的块
                        EndGraphNode(node, false);
                        break;
                    }
                    IlOpcode op0 = IlOpcodes.GetEH(il.Opcode);
                    if (op0 != IlOpcode.Prefixref)
                    {
                        EvaluateEH(op0, il, false);
                    }
                    else
                    {
                        OpCode op   = IlOpcodes.Get(il.Opcode);
                        int    type = EvaluateIl(op, il, false);
                        switch (type)
                        {
                        case -2:    //永不成立的条件跳转,当前结点与分支目标块断开
                        {
                            long target = IlIns.LabelToValue(il.Value);
                            if (flowGraph.IsNode(target))
                            {
                                FlowGraphNode n = flowGraph[target];
                                node.DelNext(n);
                            }
                            EndGraphNode(node, false);
                        }
                        break;

                        case -1:    //永成立的条件跳转,当前结点与下一条指令开始的块断开
                        {
                            if (i + 1 < ilLines.Values.Count)
                            {
                                if (flowGraph.IsNode(ilLines.Values[i + 1].LabelValue))
                                {
                                    FlowGraphNode n = flowGraph[ilLines.Values[i + 1].LabelValue];
                                    node.DelNext(n);
                                }
                            }
                            EndGraphNode(node, false);
                        }
                        break;

                        case 0:    //正常指令流
                        {
                        }
                        break;

                        case 1:    //无条件跳转
                        case 2:    //条件跳转
                        case 3:    //switch分支
                        {
                            EndGraphNode(node, false);
                        }
                        break;

                        case 4:    //结束类语句
                        {
                            EndGraphNode(node, false);
                        }
                        break;
                        }
                        if (type != 0)
                        {
                            break;
                        }
                    }
                }
            }
            //合并无分支的顺序块
            flowGraph.Reset();
            for (; flowGraph.CurNode != null; flowGraph.Next())
            {
                FlowGraphNode node = flowGraph.CurNode;
                while (node.NextNodes.Count == 1 && node.NextNodes[0].PrevNodes.Count == 1)
                {
                    FlowGraphNode n = node.NextNodes[0];
                    int           i = ilLines.Keys.IndexOf(node.EndLabelValue);
                    int           j = ilLines.Keys.IndexOf(n.LabelValue);
                    if (i + 1 == j)
                    {
                        flowGraph.Combine(node, n);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 11
0
        private void BuildFlowGraph()
        {
            flowGraph = new FlowGraph();
            FlowGraphNode curNode     = null;
            FlowGraphNode prevNode    = null;
            bool          nextIsBlock = true;

            for (int i = 0; i < ilLines.Count; i++)
            {
                IlIns il = ilLines.Values[i];
                if (nextIsBlock)
                {
                    nextIsBlock = false;
                    FlowGraphNode node = flowGraph.NewNode(il.LabelValue);
                    curNode = node;
                    if (prevNode != null)
                    {
                        prevNode.AddNext(node);
                    }
                }
                else
                {
                    if (flowGraph.IsNode(il.LabelValue))
                    {
                        FlowGraphNode node = flowGraph[il.LabelValue];
                        curNode.EndLabelValue = GetPrevIlLabelValue(il.LabelValue);
                        curNode.AddNext(node);
                        curNode = node;
                    }
                }
                IlOpcode op0 = IlOpcodes.GetEH(il.Opcode);
                if (op0 != IlOpcode.Prefixref)
                {
                    switch (op0)
                    {
                    case IlOpcode.TRY:
                    {
                        FlowGraphNode node = flowGraph.NewNode(il.LabelValue);
                        if (curNode != null)
                        {
                            curNode.AddNext(node);
                        }
                        curNode = node;
                    }
                    break;

                    case IlOpcode.CATCH:
                    {
                        FlowGraphNode node = flowGraph.NewNode(il.LabelValue);
                        curNode = node;
                    }
                    break;

                    case IlOpcode.FINALLY:
                    {
                        FlowGraphNode node = flowGraph.NewNode(il.LabelValue);
                        curNode = node;
                    }
                    break;

                    case IlOpcode.FILTER:
                    {
                        FlowGraphNode node = flowGraph.NewNode(il.LabelValue);
                        curNode = node;
                    }
                    break;

                    case IlOpcode.FAULT:
                    {
                        FlowGraphNode node = flowGraph.NewNode(il.LabelValue);
                        curNode = node;
                    }
                    break;

                    case IlOpcode.ENDTRY:
                    case IlOpcode.ENDCATCH:
                    case IlOpcode.ENDFINALLY:
                    case IlOpcode.ENDFILTER:
                    case IlOpcode.ENDFAULT:
                    {
                        curNode.EndLabelValue = il.LabelValue;
                        prevNode    = null;
                        nextIsBlock = true;
                    }
                    break;
                    }
                }
                else
                {
                    OpCode op = IlOpcodes.Get(il.Opcode);
                    switch ((IlOpcode)op.Value)
                    {
                    case IlOpcode.Br:
                    case IlOpcode.Br_S:
                    {
                        long          val  = IlIns.LabelToValue(il.Value);
                        FlowGraphNode node = null;
                        if (val > il.LabelValue)
                        {
                            node = flowGraph.NewNode(val);
                            curNode.AddNext(node);
                        }
                        else
                        {
                            long          prevVal = GetPrevIlLabelValue(val);
                            FlowGraphNode d       = flowGraph.Find(val);
                            node = flowGraph.Split(val, prevVal);
                            if (curNode != d)
                            {
                                curNode.AddNext(node);
                            }
                            else
                            {
                                node.AddNext(node);
                                curNode = node;
                            }
                        }
                        curNode.EndLabelValue = il.LabelValue;
                        prevNode    = null;
                        nextIsBlock = true;
                    }
                    break;

                    case IlOpcode.Brtrue:
                    case IlOpcode.Brfalse:
                    case IlOpcode.Brtrue_S:
                    case IlOpcode.Brfalse_S:
                    case IlOpcode.Beq_S:
                    case IlOpcode.Bge_S:
                    case IlOpcode.Bgt_S:
                    case IlOpcode.Ble_S:
                    case IlOpcode.Blt_S:
                    case IlOpcode.Bne_Un_S:
                    case IlOpcode.Bge_Un_S:
                    case IlOpcode.Bgt_Un_S:
                    case IlOpcode.Ble_Un_S:
                    case IlOpcode.Blt_Un_S:
                    case IlOpcode.Beq:
                    case IlOpcode.Bge:
                    case IlOpcode.Bgt:
                    case IlOpcode.Ble:
                    case IlOpcode.Blt:
                    case IlOpcode.Bne_Un:
                    case IlOpcode.Bge_Un:
                    case IlOpcode.Bgt_Un:
                    case IlOpcode.Ble_Un:
                    case IlOpcode.Blt_Un:
                    {
                        long          val  = IlIns.LabelToValue(il.Value);
                        FlowGraphNode node = null;
                        if (val > il.LabelValue)
                        {
                            node = flowGraph.NewNode(val);
                            curNode.AddNext(node);
                        }
                        else
                        {
                            long          prevVal = GetPrevIlLabelValue(val);
                            FlowGraphNode d       = flowGraph.Find(val);
                            node = flowGraph.Split(val, prevVal);
                            if (curNode != d)
                            {
                                curNode.AddNext(node);
                            }
                            else
                            {
                                node.AddNext(node);
                                curNode = node;
                            }
                        }
                        curNode.EndLabelValue = il.LabelValue;
                        prevNode    = curNode;
                        nextIsBlock = true;
                    }
                    break;

                    case IlOpcode.Switch:
                    {
                        SwitchOperand opVal = il.Operand as SwitchOperand;
                        foreach (string label in opVal.Labels)
                        {
                            long          val  = IlIns.LabelToValue(label);
                            FlowGraphNode node = null;
                            if (val > il.LabelValue)
                            {
                                node = flowGraph.NewNode(val);
                                curNode.AddNext(node);
                            }
                            else
                            {
                                long          prevVal = GetPrevIlLabelValue(val);
                                FlowGraphNode d       = flowGraph.Find(val);
                                node = flowGraph.Split(val, prevVal);
                                if (curNode != d)
                                {
                                    curNode.AddNext(node);
                                }
                                else
                                {
                                    node.AddNext(node);
                                    curNode = node;
                                }
                            }
                        }
                        curNode.EndLabelValue = il.LabelValue;
                        prevNode    = curNode;
                        nextIsBlock = true;
                    }
                    break;

                    case IlOpcode.Ret:
                    case IlOpcode.Break:
                    case IlOpcode.Throw:
                    case IlOpcode.Rethrow:
                    case IlOpcode.Leave:
                    case IlOpcode.Leave_S:
                    case IlOpcode.Endfilter:
                    case IlOpcode.Endfinally:
                    {
                        curNode.EndLabelValue = il.LabelValue;
                        prevNode    = null;
                        nextIsBlock = true;
                    }
                    break;
                    }
                }
            }
        }
Esempio n. 12
0
 public static void Set(IlIns il)
 {
     il.Operand = temp;
     temp       = new SwitchOperand();
 }