Beispiel #1
0
        internal override void EmulateVCall(EmuContext ctx)
        {
            VMSlot    slot      = ctx.Stack.Pop();
            VMSlot    slot2     = ctx.Stack.Pop();
            bool      flag      = (slot.U4 & 2147483648u) > 0u;
            FieldInfo fieldInfo = (FieldInfo)ctx.Header.References[slot.U4 & 2147483647u];
            bool      flag2     = !fieldInfo.IsStatic && slot2.O == null;

            if (flag2)
            {
                // throw new NullReferenceException();
                return;
            }
            if (flag)
            {
                ctx.Stack.Push(new VMSlot()
                {
                    O = new FieldRef(slot2.O, fieldInfo)
                });
            }
            else
            {
                bool   flag4 = fieldInfo.DeclaringType.IsValueType && slot2.O is IReference;
                object obj;
                if (flag4)
                {
                    obj = ((IReference)slot2.O).GetValue(ctx, PointerType.OBJECT).ToObject(fieldInfo.DeclaringType);
                }
                else
                {
                    obj = slot2.ToObject(fieldInfo.DeclaringType);
                }
                ctx.Stack.Push(VMSlot.FromObject(fieldInfo.GetValue(obj), fieldInfo.FieldType));
            }
        }
Beispiel #2
0
        internal unsafe override void EmulateVCall(EmuContext ctx)
        {
            VMSlot slot  = ctx.Stack.Pop();
            VMSlot value = ctx.Stack.Pop();
            bool   flag  = (slot.U4 & 2147483648u) > 0u;
            Type   type  = (Type)ctx.Header.References[slot.U4 & 2147483647u];

            if (flag)
            {
                TypedReference typedRef;
                Helpers.TypedReferenceHelpers.UnboxTypedRef(value.O, (void *)(&typedRef));
                TypedRef typedRef2 = new TypedRef(typedRef);
                value = VMSlot.FromObject(value.O, type);
                ctx.Stack.Push(value);
            }
            else
            {
                bool flag3 = type == typeof(object) && value.O != null;
                if (flag3)
                {
                    type = value.O.GetType();
                }
                value = VMSlot.FromObject(value.O, type);
                ctx.Stack.Push(value);
            }
        }
Beispiel #3
0
        public void Run(VMContext ctx, out ExecutionState state)
        {
            var sp       = ctx.Registers[Constants.REG_SP].U4;
            var typeSlot = ctx.Stack[sp--];
            var valSlot  = ctx.Stack[sp];

            bool unboxPtr = (typeSlot.U4 & 0x80000000) != 0;
            var  valType  = (Type)ctx.Instance.Data.LookupReference(typeSlot.U4 & ~0x80000000);

            if (unboxPtr)
            {
                unsafe {
                    TypedReference typedRef;
                    TypedReferenceHelpers.UnboxTypedRef(valSlot.O, &typedRef);
                    var reference = new TypedRef(typedRef);
                    valSlot       = VMSlot.FromObject(valSlot.O, valType);
                    ctx.Stack[sp] = valSlot;
                }
            }
            else
            {
                if (valType == typeof(object) && valSlot.O != null)
                {
                    valType = valSlot.O.GetType();
                }
                valSlot       = VMSlot.FromObject(valSlot.O, valType);
                ctx.Stack[sp] = valSlot;
            }

            ctx.Stack.SetTopPosition(sp);
            ctx.Registers[Constants.REG_SP].U4 = sp;
            state = ExecutionState.Next;
        }
Beispiel #4
0
        object Run(ulong codeAddr, uint key, VMFuncSig sig, object[] arguments)
        {
            if (currentCtx != null)
            {
                ctxStack.Push(currentCtx);
            }
            currentCtx = new VMContext(this);

            try {
                Debug.Assert(sig.ParamTypes.Length == arguments.Length);
                currentCtx.Stack.SetTopPosition((uint)arguments.Length + 1);
                for (uint i = 0; i < arguments.Length; i++)
                {
                    currentCtx.Stack[i + 1] = VMSlot.FromObject(arguments[i], sig.ParamTypes[i]);
                }
                currentCtx.Stack[(uint)arguments.Length + 1] = new VMSlot {
                    U8 = 1
                };

                currentCtx.Registers[Constants.REG_K1] = new VMSlot {
                    U4 = key
                };
                currentCtx.Registers[Constants.REG_BP] = new VMSlot {
                    U4 = 0
                };
                currentCtx.Registers[Constants.REG_SP] = new VMSlot {
                    U4 = (uint)arguments.Length + 1
                };
                currentCtx.Registers[Constants.REG_IP] = new VMSlot {
                    U8 = codeAddr
                };
                VMDispatcher.Run(currentCtx);
                Debug.Assert(currentCtx.EHStack.Count == 0);

                object retVal = null;
                if (sig.RetType != typeof(void))
                {
                    var retSlot = currentCtx.Registers[Constants.REG_R0];
                    if (Type.GetTypeCode(sig.RetType) == TypeCode.String && retSlot.O == null)
                    {
                        retVal = Data.LookupString(retSlot.U4);
                    }
                    else
                    {
                        retVal = retSlot.ToObject(sig.RetType);
                    }
                }

                return(retVal);
            }
            finally {
                currentCtx.Stack.FreeAllLocalloc();

                if (ctxStack.Count > 0)
                {
                    currentCtx = ctxStack.Pop();
                }
            }
        }
Beispiel #5
0
        public void Run(VMContext ctx, out ExecutionState state)
        {
            var sp        = ctx.Registers[Constants.REG_SP].U4;
            var fieldSlot = ctx.Stack[sp--];
            var objSlot   = ctx.Stack[sp];

            bool addr  = (fieldSlot.U4 & 0x80000000) != 0;
            var  field = (FieldInfo)ctx.Instance.Data.LookupReference(fieldSlot.U4 & 0x7fffffff);

            if (!field.IsStatic && objSlot.O == null)
            {
                throw new NullReferenceException();
            }

            if (addr)
            {
                ctx.Stack[sp] = new VMSlot {
                    O = new FieldRef(objSlot.O, field)
                }
            }
            ;
            else
            {
                object instance;
                if (field.DeclaringType.IsValueType && objSlot.O is IReference)
                {
                    instance = ((IReference)objSlot.O).GetValue(ctx, PointerType.OBJECT).ToObject(field.DeclaringType);
                }
                else
                {
                    instance = objSlot.ToObject(field.DeclaringType);
                }
                ctx.Stack[sp] = VMSlot.FromObject(field.GetValue(instance), field.FieldType);
            }

            ctx.Stack.SetTopPosition(sp);
            ctx.Registers[Constants.REG_SP].U4 = sp;
            state = ExecutionState.Next;
        }
Beispiel #6
0
        private void Run(ulong codeAddr, uint key, VMFuncSig sig, void *[] arguments, void *retTypedRef)
        {
            if (currentCtx != null)
            {
                ctxStack.Push(currentCtx);
            }
            currentCtx = new VMContext(this);

            try
            {
                Debug.Assert(sig.ParamTypes.Length == arguments.Length);
                currentCtx.Stack.SetTopPosition((uint)arguments.Length + 1);
                for (uint i = 0; i < arguments.Length; i++)
                {
                    var paramType = sig.ParamTypes[i];
                    if (paramType.IsByRef)
                    {
                        currentCtx.Stack[i + 1] = new VMSlot {
                            O = new TypedRef(arguments[i])
                        };
                    }
                    else
                    {
                        var typedRef = *(TypedReference *)arguments[i];
                        currentCtx.Stack[i + 1] = VMSlot.FromObject(TypedReference.ToObject(typedRef), __reftype(typedRef));
                    }
                }
                currentCtx.Stack[(uint)arguments.Length + 1] = new VMSlot {
                    U8 = 1
                };

                currentCtx.Registers[Constants.REG_K1] = new VMSlot {
                    U4 = key
                };
                currentCtx.Registers[Constants.REG_BP] = new VMSlot {
                    U4 = 0
                };
                currentCtx.Registers[Constants.REG_SP] = new VMSlot {
                    U4 = (uint)arguments.Length + 1
                };
                currentCtx.Registers[Constants.REG_IP] = new VMSlot {
                    U8 = codeAddr
                };
                VMDispatcher.Run(currentCtx);
                Debug.Assert(currentCtx.EHStack.Count == 0);

                if (sig.RetType != typeof(void))
                {
                    if (sig.RetType.IsByRef)
                    {
                        var retRef = currentCtx.Registers[Constants.REG_R0].O;
                        if (!(retRef is IReference))
                        {
                            throw new ExecutionEngineException();
                        }
                        ((IReference)retRef).ToTypedReference(currentCtx, retTypedRef, sig.RetType.GetElementType());
                    }
                    else
                    {
                        var    retSlot = currentCtx.Registers[Constants.REG_R0];
                        object retVal;
                        if (Type.GetTypeCode(sig.RetType) == TypeCode.String && retSlot.O == null)
                        {
                            retVal = Data.LookupString(retSlot.U4);
                        }
                        else
                        {
                            retVal = retSlot.ToObject(sig.RetType);
                        }
                        TypedReferenceHelpers.SetTypedRef(retVal, retTypedRef);
                    }
                }
            }
            finally
            {
                currentCtx.Stack.FreeAllLocalloc();

                if (ctxStack.Count > 0)
                {
                    currentCtx = ctxStack.Pop();
                }
            }
        }
Beispiel #7
0
        void InvokeTyped(VMContext ctx, MethodBase targetMethod, byte opCode, ref uint sp, out ExecutionState state)
        {
            var parameters = targetMethod.GetParameters();
            int paramCount = parameters.Length;

            if (!targetMethod.IsStatic && opCode != Constants.ECALL_NEWOBJ)
            {
                paramCount++;
            }

            Type constrainType = null;

            if (opCode == Constants.ECALL_CALLVIRT_CONSTRAINED)
            {
                constrainType = (Type)ctx.Instance.Data.LookupReference(ctx.Stack[sp--].U4);
            }

            int indexOffset = (targetMethod.IsStatic || opCode == Constants.ECALL_NEWOBJ) ? 0 : 1;

            IReference[] references = new IReference[paramCount];
            Type[]       types      = new Type[paramCount];
            for (int i = paramCount - 1; i >= 0; i--)
            {
                Type paramType;
                if (!targetMethod.IsStatic && opCode != Constants.ECALL_NEWOBJ)
                {
                    if (i == 0)
                    {
                        if (!targetMethod.IsStatic)
                        {
                            var thisSlot = ctx.Stack[sp];
                            if (thisSlot.O is ValueType && !targetMethod.DeclaringType.IsValueType)
                            {
                                Debug.Assert(targetMethod.DeclaringType.IsInterface);
                                Debug.Assert(opCode == Constants.ECALL_CALLVIRT);
                                // Interface dispatch on valuetypes => use constrained. invocation
                                constrainType = thisSlot.O.GetType();
                            }
                        }

                        if (constrainType != null)
                        {
                            paramType = constrainType.MakeByRefType();
                        }
                        else if (targetMethod.DeclaringType.IsValueType)
                        {
                            paramType = targetMethod.DeclaringType.MakeByRefType();
                        }
                        else
                        {
                            paramType = targetMethod.DeclaringType;
                        }
                    }
                    else
                    {
                        paramType = parameters[i - 1].ParameterType;
                    }
                }
                else
                {
                    paramType = parameters[i].ParameterType;
                }
                references[i] = PopRef(ctx, paramType, ref sp);
                if (paramType.IsByRef)
                {
                    paramType = paramType.GetElementType();
                }
                types[i] = paramType;
            }

            OpCode callOp;
            Type   retType;

            if (opCode == Constants.ECALL_CALL)
            {
                callOp  = System.Reflection.Emit.OpCodes.Call;
                retType = targetMethod is MethodInfo ? ((MethodInfo)targetMethod).ReturnType : typeof(void);
            }
            else if (opCode == Constants.ECALL_CALLVIRT ||
                     opCode == Constants.ECALL_CALLVIRT_CONSTRAINED)
            {
                callOp  = System.Reflection.Emit.OpCodes.Callvirt;
                retType = targetMethod is MethodInfo ? ((MethodInfo)targetMethod).ReturnType : typeof(void);
            }
            else if (opCode == Constants.ECALL_NEWOBJ)
            {
                callOp  = System.Reflection.Emit.OpCodes.Newobj;
                retType = targetMethod.DeclaringType;
            }
            else
            {
                throw new InvalidProgramException();
            }
            var proxy = DirectCall.GetTypedInvocationProxy(targetMethod, callOp, constrainType);

            object result = proxy(ctx, references, types);

            if (retType != typeof(void))
            {
                ctx.Stack[++sp] = VMSlot.FromObject(result, retType);
            }
            else if (opCode == Constants.ECALL_NEWOBJ)
            {
                ctx.Stack[++sp] = VMSlot.FromObject(result, retType);
            }

            ctx.Stack.SetTopPosition(sp);
            ctx.Registers[Constants.REG_SP].U4 = sp;
            state = ExecutionState.Next;
        }
Beispiel #8
0
        void InvokeNormal(VMContext ctx, MethodBase targetMethod, byte opCode, ref uint sp, out ExecutionState state)
        {
            uint   _sp        = sp;
            var    parameters = targetMethod.GetParameters();
            object self       = null;

            object[] args = new object[parameters.Length];
            if (opCode == Constants.ECALL_CALL && targetMethod.IsVirtual)
            {
                int indexOffset = targetMethod.IsStatic ? 0 : 1;
                args = new object[parameters.Length + indexOffset];
                for (int i = parameters.Length - 1; i >= 0; i--)
                {
                    args[i + indexOffset] = PopObject(ctx, parameters[i].ParameterType, ref sp);
                }
                if (!targetMethod.IsStatic)
                {
                    args[0] = PopObject(ctx, targetMethod.DeclaringType, ref sp);
                }

                targetMethod = DirectCall.GetDirectInvocationProxy(targetMethod);
            }
            else
            {
                args = new object[parameters.Length];
                for (int i = parameters.Length - 1; i >= 0; i--)
                {
                    args[i] = PopObject(ctx, parameters[i].ParameterType, ref sp);
                }
                if (!targetMethod.IsStatic && opCode != Constants.ECALL_NEWOBJ)
                {
                    self = PopObject(ctx, targetMethod.DeclaringType, ref sp);

                    if (self != null && !targetMethod.DeclaringType.IsInstanceOfType(self))
                    {
                        // ConfuserEx sometimes produce this to circumvent peverify (see ref proxy)
                        // Reflection won't allow it, so use typed invoke
                        InvokeTyped(ctx, targetMethod, opCode, ref _sp, out state);
                        return;
                    }
                }
            }

            object result;

            if (opCode == Constants.ECALL_NEWOBJ)
            {
                try {
                    result = ((ConstructorInfo)targetMethod).Invoke(args);
                }
                catch (TargetInvocationException ex) {
                    EHHelper.Rethrow(ex.InnerException, null);
                    throw;
                }
            }
            else
            {
                if (!targetMethod.IsStatic && self == null)
                {
                    throw new NullReferenceException();
                }

                Type selfType;
                if (self != null && (selfType = self.GetType()).IsArray && targetMethod.Name == "SetValue")
                {
                    Type valueType;
                    if (args[0] == null)
                    {
                        valueType = selfType.GetElementType();
                    }
                    else
                    {
                        valueType = args[0].GetType();
                    }
                    ArrayStoreHelpers.SetValue((Array)self, (int)args[1], args[0], valueType, selfType.GetElementType());
                    result = null;
                }
                else
                {
                    try {
                        result = targetMethod.Invoke(self, args);
                    }
                    catch (TargetInvocationException ex) {
                        VMDispatcher.DoThrow(ctx, ex.InnerException);
                        throw;
                    }
                }
            }

            if (targetMethod is MethodInfo && ((MethodInfo)targetMethod).ReturnType != typeof(void))
            {
                ctx.Stack[++sp] = VMSlot.FromObject(result, ((MethodInfo)targetMethod).ReturnType);
            }
            else if (opCode == Constants.ECALL_NEWOBJ)
            {
                ctx.Stack[++sp] = VMSlot.FromObject(result, targetMethod.DeclaringType);
            }

            ctx.Stack.SetTopPosition(sp);
            ctx.Registers[Constants.REG_SP].U4 = sp;
            state = ExecutionState.Next;
        }