예제 #1
0
        public void SimpleFunctionWithIfElseBranching()
        {
            //Arrange
            Func <Int32> func = () =>
            {
                Int32 a      = 5;
                Int32 b      = 3;
                Int32 result = 0;
                if (a - b > 0)
                {
                    result = 1;
                }
                else
                {
                    result = -1;
                }
                return(result);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
        public void CallingOneFunctionFromAnother()
        {
            //Arrange
            Func <Int32, Int32> addFive = (Int32 num) =>
            {
                Int32 r = num + 5;
                return(r);
            };

            Func <Int32> mainFunc = () =>
            {
                Int32 num = 10;
                Int32 r   = addFive(num);
                return(r);
            };

            MethodLoader mainFuncLoader = new MethodLoader(mainFunc.GetMethodInfo());
            MethodLoader addFiveLoader  = new MethodLoader(addFive.GetMethodInfo());

            MethodDesc mainFuncMethodDesc = mainFuncLoader.ConstructMethodDesc();
            MethodDesc addFiveMethodDesc  = addFiveLoader.ConstructMethodDesc();

            mainFuncMethodDesc.EntryPoint().AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            addFiveMethodDesc.AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);

            var executor = GetExecutionEngineForGlobalEntryPoint(mainFuncMethodDesc, addFiveMethodDesc);

            //Act
            executor.Start();

            //Assert
            var res = mainFunc();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
        public void Simple_AND_Branching_False_True()
        {
            //Arrange
            Func <Int32> func = () =>
            {
                Boolean a      = false;
                Boolean b      = true;
                Int32   result = 0;
                if (a && b)
                {
                    result = 1;
                }
                else
                {
                    result = -1;
                }
                return(result);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
예제 #4
0
        public void TestMethod2()
        {
            //Arrange

            Action method = () =>
            {
                Int32 a = 5;
                try
                {
                    a = 6;
                }
                finally
                {
                    a = 7;
                }
            };

            //Act
            MethodLoader loader  = new MethodLoader(method.GetMethodInfo());
            IEHTable     ehTable = loader.EHTable;

            //Assert
            var handlers = ehTable.GetAllHandlers();

            Assert.AreEqual(14, loader.ILInstructions.Count);
            Assert.AreEqual(1, handlers.Count);
            Assert.AreEqual(3, handlers[0].TryOffset);
            Assert.AreEqual(4, handlers[0].TryLength);
            Assert.AreEqual(8, handlers[0].HandlerOffset);
            Assert.AreEqual(4, handlers[0].HandlerLength);
            Assert.AreEqual(EHHandlerType.Finally, handlers[0].HandlerType);
            Assert.AreEqual(null, handlers[0].ExceptionToken);
        }
        public void Int32_Simple_Greater_Operation_True()
        {
            //Arrange
            Func <Boolean> func = () =>
            {
                Int32   a      = 42;
                Int32   b      = 22;
                Boolean result = a > b;
                return(result);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
예제 #6
0
        public void TestMethod4()
        {
            //Arrange
            Action method = () =>
            {
                Int32  a = 5;
                string b = "qwerty";
                try
                {
                    a = 6;
                }
                finally
                {
                    a = 7;
                }
            };

            //Act
            MethodLoader loader = new MethodLoader(method.GetMethodInfo());

            var locals = loader.Locals;

            //Assert
            Assert.AreEqual(2, locals.Length);

            Assert.AreEqual(false, locals[0].IsPinned);
            Assert.AreEqual(null, locals[0].Name);
            Assert.IsTrue(locals[0].TypeToken.Equals(new ClassToken(typeof(System.Int32))));

            Assert.AreEqual(false, locals[1].IsPinned);
            Assert.AreEqual(null, locals[1].Name);
            Assert.IsTrue(locals[1].TypeToken.Equals(new ClassToken(typeof(System.String))));
        }
        public void Int32_Simple_Adding_Positive_Zero()
        {
            //Arrange
            Func <Int32> func = () =>
            {
                Int32 a      = 435;
                Int32 b      = 0;
                Int32 result = a + b;
                return(result);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
        public void Int32_Simple_Multiplication_Negative_Overflow()
        {
            //Arrange
            Func <Int32> func = () =>
            {
                Int32 a      = -2147483648;
                Int32 b      = 3;
                Int32 result = a * b;
                return(result);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
        public void SimpleWhileIterationWithIncrement()
        {
            //Arrange
            Func <Int32> func = () =>
            {
                Int32 a = 5;
                while (a < 20)
                {
                    a = a + 1;
                }
                return(a);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
예제 #10
0
        public void Convert_UInt16_To_SByte()
        {
            //Arrange
            Func <SByte> func = () =>
            {
                UInt16 source = (UInt16)2500;
                SByte  target = (SByte)source;
                return(target);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
예제 #11
0
        public void ArithmeticFunctionWithTwoTryFinallies()
        {
            //Arrange
            Func <Int32> func = () =>
            {
                Int32 a = 5;
                try
                {
                    try
                    {
                        Int32 b = a + 33;
                        Int32 c = b * 4;
                        return(a + b + c);
                    }
                    finally
                    {
                        a = a + 1;
                    }
                }
                finally
                {
                    a = a * 2;
                }
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
예제 #12
0
        public void FunctionWithMixedIntAndFloatArithmeticsAndBranching()
        {
            //Arrange
            Func <Int32> func = () =>
            {
                Int32  a  = 5;
                Int32  b  = -3;
                Int32  c  = a + b;
                Single s1 = 21.6f;
                Single s2 = 33.24f;
                Single s  = s1 + s2;
                Byte   bt = 32;
                if ((s == 54.84f) && (c == -8) && (true == true))
                {
                    Byte mult = 2;
                    bt = (Byte)(bt * mult);
                }

                bt = (Byte)(bt & 6);

                return((Int32)bt);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            var res = func();

            Assert.IsTrue(executor.EntryPointReturn.Equals(res));
        }
예제 #13
0
        public void SimpleFunctionReturnsResultOfArithmeticOpers()
        {
            //Arrange
            Func <Int32> func = () =>
            {
                Int32 a = 40;
                Int32 b = a + 2;
                return(b);
            };

            MethodLoader loader     = new MethodLoader(func.GetMethodInfo());
            MethodDesc   methodDesc = loader.ConstructMethodDesc();

            methodDesc.EntryPoint()
            .AddInheritanceAttributeFlag(MethodInheritanceAttributes.Global);
            var executor = GetExecutionEngineForGlobalEntryPoint(methodDesc);

            //Act
            executor.Start();

            //Assert
            Assert.IsTrue(executor.EntryPointReturn.Equals(42));
        }
예제 #14
0
        private void btnofd_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = ".NET Assembly| *.exe| .NET DLL| *.dll";
            ofd.Title  = "Choose Assembly";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                tbpath.Text = ofd.FileName;
                try
                {
                    Globals.md = ModuleDefMD.Load(tbpath.Text);
                }catch (Exception)
                {
                    MessageBox.Show(null, "Failed while loading Assembly", NAME(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                List <String> methods = MethodLoader.LoadMethods();
                foreach (var item in methods)
                {
                    cbmethods.Items.Add(item);
                }
            }
        }
예제 #15
0
        public Schema Generate(string url = "", Dictionary <string, string> headers = null)
        {
            var schema = new Schema {
                Headers = headers, Url = url
            };

            var controllers = TypeLoader.Get(_assemblies, x => x.FullName.EndsWith("Controller"));

            foreach (var ctrl in controllers)
            {
                var controllerUrl = $"{url}/{ctrl.Name.Replace("Controller", "")}";
                var schCtrl       = new Controller {
                    Url = controllerUrl
                };

                var methods = MethodLoader.Get(ctrl, x => !x.IsSpecialName && x.DeclaringType?.FullName == ctrl.FullName);

                foreach (var method in methods)
                {
                    var methodUrl = $"{controllerUrl}/{method.Name}";
                    var endp      = new Endpoint {
                        Url = methodUrl, Verb = VerbLoader.Get(method)
                    };

                    foreach (var p in ParamLoader.Get(method))
                    {
                        endp.Input.Add(p.Name, p.ParameterType.IsPrimitive || p.ParameterType == typeof(string) ? p.ParameterType.FullName : GetBody(p));
                    }

                    schCtrl.Endpoints.Add(endp);
                }

                schema.Controllers.Add(schCtrl);
            }

            return(schema);
        }
예제 #16
0
        public void TestMethod1()
        {
            //Arrange
            MethodInfo method = typeof(Int32).GetMethods()[1];
            MethodBody mbody  = method.GetMethodBody();

            byte[]       ilBytes = mbody.GetILAsByteArray();
            MethodLoader loader  = new MethodLoader(method);

            //Act
            var rawInstructions = loader.GetRawInstructionsFromBytes(ilBytes);
            var ilInstructions  = loader.ConvertRawInstructions(rawInstructions);

            //Assert
            Assert.AreEqual(14, ilInstructions.Count);
            Assert.AreEqual(ByteCode.Ldarg_0, ilInstructions[0].ILOperation);
            Assert.AreEqual(ByteCode.Ldind_I4, ilInstructions[1].ILOperation);
            Assert.AreEqual(ByteCode.Ldarg_1, ilInstructions[2].ILOperation);

            Assert.AreEqual(ByteCode.Bge_S, ilInstructions[3].ILOperation);
            Assert.AreEqual(6, ilInstructions[3].Operand);

            Assert.AreEqual(ByteCode.Ldc_I4_M1, ilInstructions[4].ILOperation);
            Assert.AreEqual(ByteCode.Ret, ilInstructions[5].ILOperation);
            Assert.AreEqual(ByteCode.Ldarg_0, ilInstructions[6].ILOperation);
            Assert.AreEqual(ByteCode.Ldind_I4, ilInstructions[7].ILOperation);
            Assert.AreEqual(ByteCode.Ldarg_1, ilInstructions[8].ILOperation);

            Assert.AreEqual(ByteCode.Ble_S, ilInstructions[9].ILOperation);
            Assert.AreEqual(12, ilInstructions[9].Operand);

            Assert.AreEqual(ByteCode.Ldc_I4_1, ilInstructions[10].ILOperation);
            Assert.AreEqual(ByteCode.Ret, ilInstructions[11].ILOperation);
            Assert.AreEqual(ByteCode.Ldc_I4_0, ilInstructions[12].ILOperation);
            Assert.AreEqual(ByteCode.Ret, ilInstructions[13].ILOperation);
        }
예제 #17
0
        public void TestMethod3()
        {
            //Arrange
            Action method = () =>
            {
                Int32 a = 5;
                try
                {
                    a = 6;
                    try
                    {
                        a = 9;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception in try");
                    }
                }
                finally
                {
                    try
                    {
                        a = 7;
                    }
                    catch (Exception ex)
                    {
                        a = 11;
                        Console.WriteLine("Exception in finally");
                    }
                }
            };

            //Act
            MethodLoader loader  = new MethodLoader(method.GetMethodInfo());
            IEHTable     ehTable = loader.EHTable;

            //Assert
            Assert.AreEqual(38, loader.ILInstructions.Count);
            var handlers = ehTable.GetAllHandlers();

            Assert.AreEqual(3, handlers.Count);

            Assert.AreEqual(6, handlers[0].TryOffset);
            Assert.AreEqual(4, handlers[0].TryLength);
            Assert.AreEqual(11, handlers[0].HandlerOffset);
            Assert.AreEqual(6, handlers[0].HandlerLength);
            Assert.AreEqual(EHHandlerType.Catch, handlers[0].HandlerType);
            Assert.IsTrue(handlers[0].ExceptionToken.Equals(new ClassToken(typeof(System.Exception))));


            Assert.AreEqual(21, handlers[1].TryOffset);
            Assert.AreEqual(4, handlers[1].TryLength);
            Assert.AreEqual(26, handlers[1].HandlerOffset);
            Assert.AreEqual(8, handlers[1].HandlerLength);
            Assert.AreEqual(EHHandlerType.Catch, handlers[1].HandlerType);
            Assert.IsTrue(handlers[1].ExceptionToken.Equals(new ClassToken(typeof(System.Exception))));


            Assert.AreEqual(3, handlers[2].TryOffset);
            Assert.AreEqual(16, handlers[2].TryLength);
            Assert.AreEqual(20, handlers[2].HandlerOffset);
            Assert.AreEqual(16, handlers[2].HandlerLength);
            Assert.AreEqual(EHHandlerType.Finally, handlers[2].HandlerType);
            Assert.AreEqual(null, handlers[2].ExceptionToken);
        }
예제 #18
0
		protected BaseGraphicsContext()
		{
			Loader = new MethodLoader(this);
		}