Example #1
0
        private static void TestAssembly(string imageDir, string imagePath)
        {
            string imageName = Path.GetFileName(imagePath);
            string subDir    = GetRelativePath(imageDir, TestCaseDir);

            Il2cppContext context = new Il2cppContext(imagePath);

            foreach (TypeDef typeDef in context.Module.Types)
            {
                TestType(context, typeDef, imageDir, imageName, subDir);
            }
        }
Example #2
0
        private void TestAssembly(string imageDir, string imagePath)
        {
            string imageName = Path.GetFileName(imagePath);
            string subDir    = GetRelativePath(imageDir, TestDir);

            try
            {
                Il2cppContext context = new Il2cppContext(imagePath);

                foreach (TypeDef typeDef in context.Module.Types)
                {
                    OnType(context, typeDef, imageDir, imageName, subDir);
                }
            }
            catch (BadImageFormatException)
            {
                Console.WriteLine("* Load Error: \"{0}{1}\" is not a .NET assembly.", subDir, imageName);
            }
        }
Example #3
0
        private static void TestBinding(
            Il2cppContext context, TypeDef typeDef,
            string imageDir, string imageName, string subDir)
        {
            MethodDef metDef = IsTestBinding(typeDef);

            if (metDef == null)
            {
                return;
            }

            string testName = string.Format("[{0}]{1}", imageName, typeDef.FullName);
            var    oldColor = Console.ForegroundColor;

            Console.Write("{0} {1}: ", subDir, testName);

            context.AddEntry(metDef);

            var sw = new Stopwatch();

            sw.Start();
            string exceptionMsg = null;

            try
            {
                context.Resolve();
            }
            catch (TypeLoadException ex)
            {
                exceptionMsg = ex.Message;
            }
            sw.Stop();
            long elapsedMS = sw.ElapsedMilliseconds;

            Console.Write("{0}ms, ", elapsedMS);

            StringBuilder sb = new StringBuilder();

            if (exceptionMsg != null)
            {
                sb.Append(exceptionMsg);
            }
            else
            {
                HierarchyDump dumper = new HierarchyDump(context);

                /*sb.Append("* MethodTables:\n");
                 * dumper.DumpMethodTables(sb);*/
                sb.Append("* Types:\n");
                dumper.DumpTypes(sb);
            }

            var dumpData = Encoding.UTF8.GetBytes(sb.ToString());

            string validatedName = ValidatePath(testName);

            File.WriteAllBytes(
                Path.Combine(imageDir, validatedName + ".dump"),
                dumpData);

            byte[] cmpData = null;
            try
            {
                cmpData = File.ReadAllBytes(Path.Combine(imageDir, validatedName + ".txt"));
                cmpData = ReplaceNewLines(cmpData);
            }
            catch
            {
            }

            if (cmpData != null && dumpData.SequenceEqual(cmpData))
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("PASS");
                Console.ForegroundColor = oldColor;

                ++PassedTests;
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("FAIL");
                Console.ForegroundColor = oldColor;
            }

            ++TotalTests;

            context.Reset();
        }
Example #4
0
        private static void TestCodeGen(
            Il2cppContext context, TypeDef typeDef,
            string imageDir, string imageName, string subDir)
        {
            MethodDef metDef = IsTestCodeGen(typeDef);

            if (metDef == null)
            {
                return;
            }

            string testName = string.Format("[{0}]{1}", imageName, typeDef.FullName);
            var    oldColor = Console.ForegroundColor;

            Console.Write("{0,-52}", string.Format("{0} {1}:", subDir, testName));

            context.AddEntry(metDef);

            var sw = new Stopwatch();

            sw.Start();

            context.Resolve();

            sw.Stop();
            long elapsedMS = sw.ElapsedMilliseconds;

            Console.Write("{0,-12}", string.Format("Res({0}ms)", elapsedMS));

            string strRecLogs = context.GetRecordLogs();

            if (strRecLogs != null)
            {
                Console.WriteLine('\n' + strRecLogs);
            }

            sw.Restart();
            var genResult = context.Generate();

            sw.Stop();
            elapsedMS = sw.ElapsedMilliseconds;
            Console.Write("{0,-12}", string.Format("Gen({0}ms)", elapsedMS));

            string validatedName = ValidatePath(testName);
            string genDir        = Path.Combine(imageDir, "../../gen/", validatedName);

            // 生成入口测试代码
            if (!File.Exists(Path.Combine(genDir, "main.cpp")))
            {
                var mainUnit = new CompileUnit();
                genResult.UnitList.Add(mainUnit);
                mainUnit.Name = "main";
                string metName = genResult.GetMethodName(metDef, out var metUnitName);
                mainUnit.ImplDepends.Add(metUnitName);
                mainUnit.ImplCode =
                    "#include <stdio.h>\n" +
                    "#include <time.h>\n" +
                    "#include <string>\n\n" +
                    "int main()\n" +
                    "{\n" +
                    "	il2cpp_Init();\n"+
                    "	auto start = clock();\n"+
                    "	auto result = "+ metName + "();\n" +
                    "	auto elapsed = clock() - start;\n"+
                    "	printf(\"Result(%s), %ldms\", std::to_string(result).c_str(), elapsed);\n"+
                    "	return 0;\n"+
                    "}\n";
            }

            genResult.GenerateIncludes();
            Il2cppContext.SaveToFolder(
                genDir,
                genResult.UnitList,
                new HashSet <string> {
                "main"
            });
            genDir = Path.GetFullPath(genDir);

            Console.Write("Building");
            bool            hasBuildErr = false;
            Action <string> actOutput   = strOut =>
            {
                if (!hasBuildErr)
                {
                    if (strOut.IndexOf("warning") != -1)
                    {
                        Console.WriteLine("\n{0}", strOut);
                    }
                    if (strOut.IndexOf("error") != -1)
                    {
                        Console.WriteLine();
                        hasBuildErr = true;
                    }
                    else if (strOut.IndexOf(":") != -1 && strOut.IndexOf("Skipped:") == -1)
                    {
                        Console.Write(".");
                    }
                }

                if (hasBuildErr)
                {
                    Console.Error.WriteLine("{0}", strOut);
                }
            };

            string result = null;

            RunCommand(
                null,
                "build.cmd",
                genDir,
                actOutput,
                actOutput);

            if (!hasBuildErr)
            {
                Console.Write(" Running ");
                string runOutput = null;
                RunCommand(
                    null,
                    "final.exe",
                    genDir,
                    strOut => runOutput = strOut,
                    Console.Error.WriteLine);

                Console.Write("{0,-20}", runOutput);

                if (runOutput != null)
                {
                    result = GetRunResult(runOutput);
                }
            }

            if (result == "0")
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("PASS");
                Console.ForegroundColor = oldColor;

                ++PassedTests;
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("FAIL");
                Console.ForegroundColor = oldColor;
            }

            ++TotalTests;

            context.Reset();
        }
Example #5
0
        private static void TestCodeGen(
            Il2cppContext context, TypeDef typeDef,
            string imageDir, string imageName, string subDir)
        {
            MethodDef metDef = IsTestCodeGen(typeDef);

            if (metDef == null)
            {
                return;
            }

            string testName = string.Format("[{0}]{1}", imageName, typeDef.FullName);
            var    oldColor = Console.ForegroundColor;

            Console.Write("{0} {1}: ", subDir, testName);

            context.AddEntry(metDef);

            var sw = new Stopwatch();

            sw.Start();
            string exceptionMsg = null;

            try
            {
                context.Resolve();
            }
            catch (TypeLoadException ex)
            {
                exceptionMsg = ex.Message;
            }
            sw.Stop();
            long elapsedMS = sw.ElapsedMilliseconds;

            Console.Write("Resolve: {0}ms, ", elapsedMS);

            sw.Restart();
            var genResult = context.Generate();

            sw.Stop();
            elapsedMS = sw.ElapsedMilliseconds;
            Console.Write("Generate: {0}ms, ", elapsedMS);

            var mainUnit = new CompileUnit();

            genResult.UnitList.Add(mainUnit);
            mainUnit.Name = "main";
            string metName = genResult.GetMethodName(metDef, out var metUnitName);

            mainUnit.ImplDepends.Add(metUnitName);
            mainUnit.ImplCode =
                "#include <stdio.h>\n" +
                "#include <time.h>\n" +
                "#include <string>\n\n" +
                "int main()\n" +
                "{\n" +
                "	il2cpp_Init();\n"+
                "	auto start = clock();\n"+
                "	auto result = "+ metName + "();\n" +
                "	auto elapsed = clock() - start;\n"+
                "	printf(\"Result: %s, Elapsed: %ldms\", std::to_string(result).c_str(), elapsed);\n"+
                "	getchar();\n"+
                "	return 0;\n"+
                "}\n";

            genResult.GenerateIncludes();

            string validatedName = ValidatePath(testName);

            Il2cppContext.SaveToFolder(
                Path.Combine(imageDir, "../../gen/", validatedName),
                genResult.UnitList);

            Console.WriteLine();

            context.Reset();
        }
Example #6
0
 public NameManager(Il2cppContext context)
 {
     Context = context;
 }