Esempio n. 1
0
        public override EmulationResult Emulate(Context ctx)
        {
            if (!(ctx.Instruction.Operand is IMethod iMethod))
            {
                throw new InvalidILException(ctx.Instruction.ToString());
            }

            var method = (iMethod is MethodDef methodDef) ? methodDef : iMethod.ResolveMethodDef();

            if ((method.IsPinvokeImpl || !method.HasBody) &&
                !(ctx.Emulator.DynamicContext is null) &&
                ctx.Emulator.DynamicContext.AllowInvocation)
            {
                var    methodInfo  = ctx.Emulator.DynamicContext.LookupMember <MethodInfo>(iMethod.MDToken.ToInt32());
                var    parameters  = ctx.Stack.PopObjects(methodInfo.GetParameters().Length).Reverse().ToArray();
                var    thisPtr     = method.IsStatic ? null : ((ObjectValue)ctx.Stack.Pop()).Value;
                object returnValue = methodInfo.Invoke(thisPtr, parameters);
                if (method.ReturnType.ElementType != ElementType.Void)
                {
                    ctx.Stack.Push(Value.FromObject(returnValue));
                }
                return(new NormalResult());
            }
            var emulator = new CILEmulator(method, ctx.Emulator.DynamicContext, ctx.Stack.Pop(method.Parameters.Count).Reverse());

            emulator.Emulate();

            if (method.ReturnType.ElementType != ElementType.Void)
            {
                ctx.Stack.Push(emulator.ValueStack.Pop());
            }

            return(new NormalResult());
        }
Esempio n. 2
0
        public override EmulationResult Emulate(Context ctx)
        {
            if (!(ctx.Instruction.Operand is IMethod iMethod))
            {
                throw new InvalidILException(ctx.Instruction.ToString());
            }

            var method   = iMethod is MethodDef methodDef ? methodDef : iMethod.ResolveMethodDef();
            var emulator = new CILEmulator(method, ctx.Stack.Pop(method.Parameters.Count).Reverse());

            ctx.Stack.Clear();
            emulator.Emulate();
            return(new ReturnResult());
        }
Esempio n. 3
0
        public void CalculateSize()
        {
            var assembly       = typeof(SizeofTest).Assembly;
            var module         = ModuleDefMD.Load(assembly.Location);
            var dynamicContext = new DynamicContext(assembly);
            var cilEmulator    = new CILEmulator(module.FindNormal("DNEmulator.Tests.Misc.SizeofTest").FindMethod("TestMethod"),
                                                 dynamicContext);

            cilEmulator.Emulate();
            var value = cilEmulator.ValueStack.Pop();

            Assert.AreEqual(typeof(I4Value), value.GetType());
            var emulatedSize = (I4Value)value;
            int size         = TestMethod();

            Assert.AreEqual(size, emulatedSize.Value);
        }
Esempio n. 4
0
        public void IsDateTime()
        {
            var assembly = typeof(Isinst).Assembly;
            var module   = ModuleDefMD.Load(assembly.Location);

            Resolver.Resolve(module);
            var dynamicContext = new DynamicContext(assembly);
            var cilEmulator    = new CILEmulator(module.FindNormal("DNEmulator.Tests.Conversions.Isinst").FindMethod("TestMethod"),
                                                 dynamicContext);

            cilEmulator.Emulate();
            var value = cilEmulator.ValueStack.Pop();

            Assert.AreEqual(typeof(I4Value), value.GetType());
            var  emulatedIsDateTime = (I4Value)value;
            bool isDateTime         = TestMethod();

            Assert.AreEqual(isDateTime, emulatedIsDateTime.Value != 0);
        }
Esempio n. 5
0
        public override EmulationResult Emulate(Context ctx)
        {
            if (!(ctx.Instruction.Operand is IMethod iMethod))
            {
                throw new InvalidILException(ctx.Instruction.ToString());
            }

            var method   = (iMethod is MethodDef methodDef) ? methodDef : iMethod.ResolveMethodDef();
            var emulator = new CILEmulator(method, ctx.Stack.Pop(method.Parameters.Count).Reverse());

            emulator.Emulate();

            if (method.ReturnType.ElementType != ElementType.Void)
            {
                ctx.Stack.Push(emulator.ValueStack.Pop());
            }

            return(new NormalResult());
        }
        public void ToStringTest()
        {
            var assembly       = typeof(CallvirtTest).Assembly;
            var module         = ModuleDefMD.Load(assembly.Location);
            var dynamicContext = new DynamicContext(assembly)
            {
                AllowInvocation = true,
            };
            var cilEmulator = new CILEmulator(module.FindNormal("DNEmulator.Tests.Invocation.CallvirtTest").FindMethod("TestMethod"),
                                              dynamicContext);

            cilEmulator.Emulate();
            var value = cilEmulator.ValueStack.Pop();

            Assert.AreEqual(typeof(ObjectValue), value.GetType());
            var emulatedToString = (ObjectValue)value;
            var toString         = TestMethod();

            Assert.AreEqual(toString, (string)emulatedToString.Value);
        }