Ejemplo n.º 1
0
        public static MyJson.JsonNode_Object Export(AntsModule module)
        {
            var outjson = new MyJson.JsonNode_Object();

            outjson.SetDictValue("hash", "hash");
            outjson.SetDictValue("entrypoint", "Main");

            var funcsigns = new MyJson.JsonNode_Array();

            outjson["functions"] = funcsigns;
            var eventsigns = new MyJson.JsonNode_Array();

            outjson["events"] = eventsigns;

            foreach (var function in module.mapMethods)
            {
                var mm = function.Value;
                if (mm.isPublic == false)
                {
                    continue;
                }
                var ps       = mm.name.Split(new char[] { ' ', '(' }, StringSplitOptions.RemoveEmptyEntries);
                var funcsign = new MyJson.JsonNode_Object();

                funcsigns.Add(funcsign);
                var funcname = ps[1];
                if (funcname.IndexOf("::") > 0)
                {
                    var sps = funcname.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries);
                    funcname = sps.Last();
                }
                funcsign.SetDictValue("name", funcname);
                var rtype = ConvType(mm.returntype);
                funcsign.SetDictValue("returntype", rtype);
                MyJson.JsonNode_Array funcparams = new MyJson.JsonNode_Array();
                funcsign["paramaters"] = funcparams;
                if (mm.paramtypes != null)
                {
                    foreach (var v in mm.paramtypes)
                    {
                        var ptype = ConvType(v.type);
                        var item  = new MyJson.JsonNode_Object();
                        funcparams.Add(item);

                        item.SetDictValue("name", v.name);
                        item.SetDictValue("type", ptype);
                    }
                }
            }
            return(outjson);
        }
Ejemplo n.º 2
0
        public AntsModule Convert(ILModule _in)
        {
            //logger.Log("beginConvert.");
            this.outModule = new AntsModule(this.logger);
            foreach (var t in _in.mapType)
            {
                if (t.Key[0] == '<')
                {
                    continue;                 //系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                     //event 自动生成的代码,不要
                    }
                    AntsMethod nm = new AntsMethod();
                    if (m.Key == ".cctor")
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace                 = m.Value.method.DeclaringType.FullName;
                    nm.name                       = m.Value.method.FullName;
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
            }
            foreach (var t in _in.mapType)
            {
                if (t.Key[0] == '<')
                {
                    continue;                 //系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key == ".cctor")
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                     //event 自动生成的代码,不要
                    }
                    var nm = this.methodLink[m.Value];

                    //try
                    {
                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                if (key.Contains("::Verify("))
                {
                    var m = outModule.mapMethods[key];
                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key.method;
                            if (srcm.DeclaringType.BaseType.Name == "VerificationCode" && srcm.ReturnType.FullName == "System.Boolean")
                            {
                                logger.Log("找到函数入口点:" + key);
                                if (mainmethod != "")
                                {
                                    throw new Exception("拥有多个函数入口点,请检查");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
                if (key.Contains("::Main("))
                {
                    var m = outModule.mapMethods[key];
                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key.method;
                            if (srcm.DeclaringType.BaseType.Name == "FunctionCode")
                            {
                                logger.Log("找到函数入口点:" + key);
                                if (mainmethod != "")
                                {
                                    throw new Exception("拥有多个函数入口点,请检查");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "")
            {
                throw new Exception("找不到入口函数,请检查");
            }
            outModule.mainMethod = mainmethod;
            //得找到第一个函数
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }
Ejemplo n.º 3
0
        public AntsModule Convert(JavaModule _in)
        {
            this.srcModule = _in;
            //logger.Log("beginConvert.");
            this.outModule = new AntsModule(this.logger);
            foreach (var c in _in.classes.Values)
            {
                if (c.skip)
                {
                    continue;
                }
                foreach (var m in c.methods)
                {
                    if (m.Value.skip)
                    {
                        continue;
                    }
                    if (m.Key[0] == '<')
                    {
                        continue;                 //系統函數不要
                    }
                    AntsMethod nm = new AntsMethod();
                    nm.name = c.classfile.Name + "::" + m.Key;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
            }

            foreach (var c in _in.classes.Values)
            {
                if (c.skip)
                {
                    continue;
                }
                foreach (var m in c.methods)
                {
                    if (m.Value.skip)
                    {
                        continue;
                    }
                    if (m.Key[0] == '<')
                    {
                        continue;                 //系統函數不要
                    }
                    var nm = this.methodLink[m.Value];
                    //try
                    {
                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                if (key.Contains("Verify"))
                {
                    var m = outModule.mapMethods[key];
                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key;
                            if (srcm.DeclaringType.superClass == "AntShares.SmartContract.Framework.VerificationCode" && srcm.returnType == "java.lang.Boolean")
                            {
                                logger.Log("找到函数入口点:" + key);
                                if (mainmethod != "")
                                {
                                    throw new Exception("拥有多个函数入口点,请检查");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
                var name = key.Substring(key.IndexOf("::") + 2);
                if (name == ("Main"))
                {
                    var m = outModule.mapMethods[key];
                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key;
                            if (srcm.DeclaringType.superClass == "AntShares.SmartContract.Framework.FunctionCode")
                            {
                                logger.Log("找到函数入口点:" + key);
                                if (mainmethod != "")
                                {
                                    throw new Exception("拥有多个函数入口点,请检查");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "")
            {
                throw new Exception("找不到入口函数,请检查");
            }
            outModule.mainMethod = mainmethod;
            //得找到第一个函数
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }
Ejemplo n.º 4
0
        public static void Parse(ILMethod from, AntsModule to)
        {
            calcStack = new Stack <object>();
            bool bEnd = false;

            foreach (var src in from.body_Codes.Values)
            {
                if (bEnd)
                {
                    break;
                }

                switch (src.code)
                {
                case CodeEx.Ret:
                    bEnd = true;
                    break;

                case CodeEx.Ldc_I4_M1:
                    calcStack.Push((int)-1);
                    break;

                case CodeEx.Ldc_I4_0:
                    calcStack.Push((int)0);
                    break;

                case CodeEx.Ldc_I4_1:
                    calcStack.Push((int)1);
                    break;

                case CodeEx.Ldc_I4_2:
                    calcStack.Push((int)2);
                    break;

                case CodeEx.Ldc_I4_3:
                    calcStack.Push((int)3);
                    break;

                case CodeEx.Ldc_I4_4:
                    calcStack.Push((int)4);
                    break;

                case CodeEx.Ldc_I4_5:
                    calcStack.Push((int)5);
                    break;

                case CodeEx.Ldc_I4_6:
                    calcStack.Push((int)6);
                    break;

                case CodeEx.Ldc_I4_7:
                    calcStack.Push((int)7);
                    break;

                case CodeEx.Ldc_I4_8:
                    calcStack.Push((int)8);
                    break;

                case CodeEx.Ldc_I4:
                case CodeEx.Ldc_I4_S:
                    calcStack.Push((int)src.tokenI32);
                    break;

                case CodeEx.Newarr:
                {
                    if (src.tokenType == "System.Byte")
                    {
                        var    count = (int)calcStack.Pop();
                        byte[] data  = new byte[count];
                        calcStack.Push(data);
                    }
                }
                break;

                case CodeEx.Dup:
                {
                    var _src  = calcStack.Peek();
                    var _dest = Dup(_src);
                    calcStack.Push(_dest);
                }
                break;

                case CodeEx.Ldtoken:
                {
                    calcStack.Push(src.tokenUnknown);
                }
                break;

                case CodeEx.Call:
                {
                    var m = src.tokenUnknown as Mono.Cecil.MethodReference;
                    if (m.DeclaringType.FullName == "System.Runtime.CompilerServices.RuntimeHelpers" && m.Name == "InitializeArray")
                    {
                        var p1 = (byte[])calcStack.Pop();
                        var p2 = (byte[])calcStack.Pop();
                        for (var i = 0; i < p2.Length; i++)
                        {
                            p2[i] = p1[i];
                        }
                    }
                } break;

                case CodeEx.Stsfld:
                {
                    var field = src.tokenUnknown as Mono.Cecil.FieldReference;
                    var fname = field.DeclaringType.FullName + "::" + field.Name;
                    to.staticfields[fname] = calcStack.Pop();
                }
                break;
                }
            }
        }
Ejemplo n.º 5
0
        public AntsModule Convert(ILModule _in)
        {
            //logger.Log("beginConvert.");
            this.outModule = new AntsModule(this.logger);
            foreach (var t in _in.mapType)
            {
                if (t.Key[0] == '<')
                {
                    continue;                 //系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    AntsMethod nm = new AntsMethod();
                    if (m.Key == ".cctor")
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace  = m.Value.method.DeclaringType.FullName;
                    nm.name        = m.Value.method.FullName;
                    nm.displayName = m.Value.method.Name;

                    Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = m.Value.method.CustomAttributes;
                    foreach (var attr in ca)
                    {
                        if (attr.AttributeType.Name == "DisplayNameAttribute")
                        {
                            nm.displayName = (string)attr.ConstructorArguments[0].Value;
                        }
                    }

                    nm.isPublic = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        AntsEvent ae = new AntsEvent();
                        ae._namespace  = e.Value.field.DeclaringType.FullName;
                        ae.name        = ae._namespace + "::" + e.Key;
                        ae.displayName = e.Value.displayName;
                        ae.returntype  = e.Value.returntype;
                        ae.paramtypes  = e.Value.paramtypes;
                        outModule.mapEvents[ae.name] = ae;
                    }
                }
            }

            Dictionary <byte, string> spmains = new Dictionary <byte, string>();

            foreach (var t in _in.mapType)
            {
                if (t.Key[0] == '<')
                {
                    continue;                 //系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key == ".cctor")
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    var  nm = this.methodLink[m.Value];
                    byte entryid;
                    if (IsEntryCall(m.Value.method, out entryid))
                    {
                        spmains[entryid] = nm.name;
                        logger.Log("找到函数入口点:[" + entryid + "]" + nm.name);
                    }

                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new AntsParam(src.name, src.type));
                        }

                        byte[] outcall; string name;
                        if (IsAppCall(m.Value.method, out outcall))
                        {
                            continue;
                        }
                        if (IsNonCall(m.Value.method))
                        {
                            continue;
                        }
                        if (IsOpCall(m.Value.method, out name))
                        {
                            continue;
                        }
                        if (IsSysCall(m.Value.method, out name))
                        {
                            continue;
                        }

                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                if (key.Contains("::Main("))
                {
                    AntsMethod m = outModule.mapMethods[key];

                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key.method;
                            if (srcm.DeclaringType.BaseType.Name == "SmartContract")
                            {
                                if (mainmethod != "")
                                {
                                    throw new Exception("拥有多个函数入口点,请检查");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "" && spmains.Count == 0)
            {
                throw new Exception("找不到入口函数,请检查");
            }
            else if (mainmethod != "" && spmains.Count > 0)
            {
                throw new Exception("同时拥有指定入口函数和默认入口函数,请检查");
            }
            else if (mainmethod != "")
            {
                //单一默认入口
                logger.Log("找到函数入口点:" + mainmethod);
            }
            else if (spmains.Count > 0) //拥有条件入口的情况
            {
                mainmethod = this.CreateJmpMain(spmains);
            }

            outModule.mainMethod = mainmethod;
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }