Esempio n. 1
0
        private void Load(ulong codeAddr, uint key, DarksVMFuncSig sig, void *[] arguments, void *retTypedRef)
        {
            if (currentCtx != null)
            {
                ctxStack.Push(currentCtx);
            }
            currentCtx = new DarksVMContext(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 DarksVMSlot {
                            O = new TypedRef(arguments[i])
                        };
                    }
                    else
                    {
                        var typedRef = *(TypedReference *)arguments[i];
                        currentCtx.Stack[i + 1] = DarksVMSlot.FromObject(TypedReference.ToObject(typedRef), __reftype(typedRef));
                    }
                }
                currentCtx.Stack[(uint)arguments.Length + 1] = new DarksVMSlot {
                    U8 = 1
                };

                currentCtx.Registers[DarksVMConstants.REG_K1] = new DarksVMSlot {
                    U4 = key
                };
                currentCtx.Registers[DarksVMConstants.REG_BP] = new DarksVMSlot {
                    U4 = 0
                };
                currentCtx.Registers[DarksVMConstants.REG_SP] = new DarksVMSlot {
                    U4 = (uint)arguments.Length + 1
                };
                currentCtx.Registers[DarksVMConstants.REG_IP] = new DarksVMSlot {
                    U8 = codeAddr
                };
                DarksVMDispatcher.Load(currentCtx);
                Debug.Assert(currentCtx.EHStack.Count == 0);

                if (sig.RetType != typeof(void))
                {
                    if (sig.RetType.IsByRef)
                    {
                        var retRef = currentCtx.Registers[DarksVMConstants.REG_R0].O;
                        if (!(retRef is IReference))
                        {
                            throw new ExecutionEngineException();
                        }
                        ((IReference)retRef).ToTypedReference(currentCtx, retTypedRef, sig.RetType.GetElementType());
                    }
                    else
                    {
                        var    retSlot = currentCtx.Registers[DarksVMConstants.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();
                }
            }
        }
Esempio n. 2
0
        private void InvokeNormal(DarksVMContext ctx, MethodBase targetMethod, byte opCode, ref uint sp, out ExecutionState state)
        {
            uint _sp = sp;

            ParameterInfo[] parameters = targetMethod.GetParameters();
            object          self       = null;

            object[] args = new object[parameters.Length];
            if (opCode != DarksVMConstants.ECALL_CALL || !targetMethod.IsVirtual)
            {
                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 != DarksVMConstants.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
                        this.InvokeTyped(ctx, targetMethod, opCode, ref _sp, out state);
                        return;
                    }
                }
            }
            else
            {
                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);
            }

            object result;

            if (opCode != DarksVMConstants.ECALL_NEWOBJ)
            {
                if (!targetMethod.IsStatic && self == null)
                {
                    throw new NullReferenceException();
                }

                Type selfType;
                if (self == null || !(selfType = self.GetType()).IsArray || targetMethod.Name != "SetValue")
                {
                    try
                    {
                        result = targetMethod.Invoke(self, args);
                    }
                    catch (TargetInvocationException ex)
                    {
                        DarksVMDispatcher.DoThrow(ctx, ex.InnerException);
                        throw;
                    }
                }
                else
                {
                    Type valueType = args[0] == null?selfType.GetElementType() : args[0].GetType();

                    ArrayStoreHelpers.SetValue((Array)self, (int)args[1], args[0], valueType, selfType.GetElementType());
                    result = null;
                }
            }
            else
            {
                try
                {
                    result = ((ConstructorInfo)targetMethod).Invoke(args);
                }
                catch (TargetInvocationException ex)
                {
                    EHHelper.Rethrow(ex.InnerException, null);
                    throw;
                }
            }

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

            ctx.Stack.SetTopPosition(sp);
            ctx.Registers[DarksVMConstants.REG_SP].U4 = sp;
            state = ExecutionState.Next;
        }
Esempio n. 3
0
        private object Load(ulong codeAddr, uint key, DarksVMFuncSig sig, object[] arguments)
        {
            if (currentCtx != null)
            {
                ctxStack.Push(currentCtx);
            }
            currentCtx = new DarksVMContext(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] = DarksVMSlot.FromObject(arguments[i], sig.ParamTypes[i]);
                }
                currentCtx.Stack[(uint)arguments.Length + 1] = new DarksVMSlot {
                    U8 = 1
                };

                currentCtx.Registers[DarksVMConstants.REG_K1] = new DarksVMSlot {
                    U4 = key
                };
                currentCtx.Registers[DarksVMConstants.REG_BP] = new DarksVMSlot {
                    U4 = 0
                };
                currentCtx.Registers[DarksVMConstants.REG_SP] = new DarksVMSlot {
                    U4 = (uint)arguments.Length + 1
                };
                currentCtx.Registers[DarksVMConstants.REG_IP] = new DarksVMSlot {
                    U8 = codeAddr
                };
                DarksVMDispatcher.Load(currentCtx);
                Debug.Assert(currentCtx.EHStack.Count == 0);

                object retVal = null;
                if (sig.RetType != typeof(void))
                {
                    var retSlot = currentCtx.Registers[DarksVMConstants.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();
                }
            }
        }