示例#1
0
 internal static void DoThrow(DarksVMContext ctx, object ex)
 {
     if (ex is Exception)
     {
         EHHelper.Rethrow((Exception)ex, GetIP(ctx));
     }
     throw Throw(ex);
 }
示例#2
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;
        }