Esempio n. 1
0
        // This function is known to the compiler backend.
        protected void InitializeOpenInstanceWithoutThunk(object firstParameter, IntPtr functionPointer, IntPtr functionPointerThunk)
        {
            // This sort of delegate is invoked by calling the thunk function pointer with the arguments to the delegate + a reference to the delegate object itself.
            m_firstParameter  = this;
            m_functionPointer = GetThunk(OpenInstanceThunk);
            OpenMethodResolver instanceMethodResolver = new OpenMethodResolver(default(RuntimeTypeHandle), functionPointer, default(GCHandle), 0);

            m_extraFunctionPointerOrData = instanceMethodResolver.ToIntPtr();
        }
Esempio n. 2
0
        public sealed override Object Invoke(Object thisObject, Object[] arguments)
        {
            MethodInvokerUtils.ValidateThis(thisObject, _declaringTypeHandle);

            IntPtr resolvedVirtual = OpenMethodResolver.ResolveMethod(MethodInvokeInfo.VirtualResolveData, thisObject);

            Object result = RuntimeAugments.CallDynamicInvokeMethod(
                thisObject, resolvedVirtual, null, MethodInvokeInfo.DynamicInvokeMethod, MethodInvokeInfo.DynamicInvokeGenericDictionary, MethodInvokeInfo.DefaultValueString, arguments,
                invokeMethodHelperIsThisCall: false, methodToCallIsThisCall: true);

            return(result);
        }
Esempio n. 3
0
        public sealed override Object Invoke(Object thisObject, Object[] arguments)
        {
            MethodInvokerUtils.ValidateThis(thisObject, _declaringTypeHandle);

            IntPtr resolvedVirtual = OpenMethodResolver.ResolveMethod(MethodInvokeInfo.VirtualResolveData, thisObject);

            Object result = RuntimeAugments.CallDynamicInvokeMethod(
                thisObject,
                resolvedVirtual,
                null /*thisPtrDynamicInvokeMethod*/,
                MethodInvokeInfo.DynamicInvokeMethod,
                MethodInvokeInfo.DynamicInvokeGenericDictionary,
                MethodInvokeInfo.MethodInfo,
                arguments,
                invokeMethodHelperIsThisCall: false,
                methodToCallIsThisCall: true);

            System.Diagnostics.DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
            return(result);
        }
Esempio n. 4
0
        protected sealed override object Invoke(object thisObject, object[] arguments, BinderBundle binderBundle, bool wrapInTargetInvocationException)
        {
            ValidateThis(thisObject, _declaringTypeHandle);

            IntPtr resolvedVirtual = OpenMethodResolver.ResolveMethod(MethodInvokeInfo.VirtualResolveData, thisObject);

            object result = RuntimeAugments.CallDynamicInvokeMethod(
                thisObject,
                resolvedVirtual,
                MethodInvokeInfo.DynamicInvokeMethod,
                MethodInvokeInfo.DynamicInvokeGenericDictionary,
                MethodInvokeInfo.MethodInfo,
                arguments,
                binderBundle,
                wrapInTargetInvocationException: wrapInTargetInvocationException,
                methodToCallIsThisCall: true);

            System.Diagnostics.DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
            return(result);
        }
Esempio n. 5
0
        protected sealed override object?Invoke(object?thisObject, object?[]?arguments, BinderBundle binderBundle, bool wrapInTargetInvocationException)
        {
            IntPtr resolvedVirtual = IntPtr.Zero;

            if (MethodInvokeInfo.IsSupportedSignature) // Workaround to match expected argument validation order
            {
                ValidateThis(thisObject, _declaringTypeHandle);

                resolvedVirtual = OpenMethodResolver.ResolveMethod(MethodInvokeInfo.VirtualResolveData, thisObject);
            }

            object?result = MethodInvokeInfo.Invoke(
                thisObject,
                resolvedVirtual,
                arguments,
                binderBundle,
                wrapInTargetInvocationException);

            System.Diagnostics.DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
            return(result);
        }
Esempio n. 6
0
 public sealed override Delegate CreateDelegate(RuntimeTypeHandle delegateType, Object target, bool isStatic, bool isVirtual, bool isOpen)
 {
     if (!isOpen)
     {
         // We're creating a delegate to a virtual override of this method, so resolve the virtual now.
         IntPtr resolvedVirtual = OpenMethodResolver.ResolveMethod(MethodInvokeInfo.VirtualResolveData, target);
         return(RuntimeAugments.CreateDelegate(
                    delegateType,
                    resolvedVirtual,
                    target,
                    isStatic: false,
                    isOpen: isOpen));
     }
     else
     {
         // Create an open virtual method by providing the virtual resolver to the delegate type.
         return(RuntimeAugments.CreateDelegate(
                    delegateType,
                    MethodInvokeInfo.VirtualResolveData,
                    target,
                    isStatic: false,
                    isOpen: isOpen));
     }
 }
Esempio n. 7
0
 protected IntPtr GetActualTargetFunctionPointer(object thisObject)
 {
     return(OpenMethodResolver.ResolveMethod(m_extraFunctionPointerOrData, thisObject));
 }
Esempio n. 8
0
        internal CallConversionParameters(CallConversionInfo conversionInfo, IntPtr callerTransitionBlockParam)
        {
            // Make sure the thred static variable has been initialized for this thread
            s_pinnedGCHandles = s_pinnedGCHandles ?? new GCHandleContainer();

            _conversionInfo            = conversionInfo;
            _callerTransitionBlock     = (byte *)callerTransitionBlockParam.ToPointer();
            _functionPointerToCall     = conversionInfo.TargetFunctionPointer;
            _instantiatingStubArgument = conversionInfo.InstantiatingStubArgument;
            _delegateData                 = default(DelegateData);
            _calleeArgs                   = default(ArgIterator);
            _invokeReturnValue            = IntPtr.Zero;
            _copyReturnValue              = true;
            _dynamicInvokeParams          = null;
            _dynamicInvokeByRefObjectArgs = null;

            //
            // Setup input argument iterator for the caller
            //
            ArgIteratorData callerIteratorData;

            if (conversionInfo.IsDelegateDynamicInvokeThunk)
            {
                callerIteratorData = s_delegateDynamicInvokeImplArgIteratorData;
            }
            else if (conversionInfo.IsReflectionDynamicInvokerThunk)
            {
                callerIteratorData = s_reflectionDynamicInvokeImplArgIteratorData;
            }
            else
            {
                callerIteratorData = conversionInfo.ArgIteratorData;
            }

            _callerArgs = new ArgIterator(callerIteratorData,
                                          callerIteratorData.HasThis() ?
                                          CallingConvention.ManagedInstance :
                                          CallingConvention.ManagedStatic,
                                          conversionInfo.CallerHasParamType,
                                          conversionInfo.CallerHasExtraParameterWhichIsFunctionTarget,
                                          conversionInfo.CallerForcedByRefData,
                                          false, false);     // Setup input

            bool forceCalleeHasParamType = false;

            // If the callee MAY have a param type, we need to know before we create the callee arg iterator
            // To do this we need to actually load the target address and see if it has the generic method pointer
            // bit set.
            if (conversionInfo.CalleeMayHaveParamType)
            {
                ArgIterator callerArgsLookupTargetFunctionPointer = new ArgIterator(conversionInfo.ArgIteratorData,
                                                                                    conversionInfo.ArgIteratorData.HasThis() ?
                                                                                    CallingConvention.ManagedInstance :
                                                                                    CallingConvention.ManagedStatic,
                                                                                    conversionInfo.CallerHasParamType,
                                                                                    conversionInfo.CallerHasExtraParameterWhichIsFunctionTarget,
                                                                                    conversionInfo.CallerForcedByRefData,
                                                                                    false, false);

                // Find the last valid caller offset. That's the offset of the target function pointer.
                int ofsCallerValid = TransitionBlock.InvalidOffset;
                while (true)
                {
                    // Setup argument offsets.
                    int ofsCallerTemp = callerArgsLookupTargetFunctionPointer.GetNextOffset();

                    // Check to see if we've handled all the arguments that we are to pass to the callee.
                    if (TransitionBlock.InvalidOffset == ofsCallerTemp)
                    {
                        break;
                    }

                    ofsCallerValid = ofsCallerTemp;
                }

                if (ofsCallerValid == TransitionBlock.InvalidOffset)
                {
                    throw new InvalidProgramException();
                }

                int stackSizeCaller = callerArgsLookupTargetFunctionPointer.GetArgSize();
                Debug.Assert(stackSizeCaller == IntPtr.Size);
                void * pSrc = _callerTransitionBlock + ofsCallerValid;
                IntPtr tempFunctionPointer = *((IntPtr *)pSrc);

                forceCalleeHasParamType = UpdateCalleeFunctionPointer(tempFunctionPointer);
            }

            // Retrieve target function pointer and instantiation argument for delegate thunks
            if (conversionInfo.IsDelegateThunk)
            {
                Debug.Assert(_callerArgs.HasThis() && !_conversionInfo.IsUnboxingThunk);

                IntPtr locationOfThisPointer = (IntPtr)(_callerTransitionBlock + ArgIterator.GetThisOffset());
                _delegateData._delegateObject = (Delegate)Unsafe.As <IntPtr, Object>(ref *(IntPtr *)locationOfThisPointer);
                Debug.Assert(_delegateData._delegateObject != null);

                RuntimeAugments.GetDelegateData(
                    _delegateData._delegateObject,
                    out _delegateData._firstParameter,
                    out _delegateData._helperObject,
                    out _delegateData._extraFunctionPointerOrData,
                    out _delegateData._functionPointer);

                if (conversionInfo.TargetDelegateFunctionIsExtraFunctionPointerOrDataField)
                {
                    if (conversionInfo.IsOpenInstanceDelegateThunk)
                    {
                        _delegateData._boxedFirstParameter = BoxedCallerFirstArgument;
                        Debug.Assert(_delegateData._boxedFirstParameter != null);
                        _callerArgs.Reset();

                        IntPtr resolvedTargetFunctionPointer = OpenMethodResolver.ResolveMethod(_delegateData._extraFunctionPointerOrData, _delegateData._boxedFirstParameter);
                        forceCalleeHasParamType = UpdateCalleeFunctionPointer(resolvedTargetFunctionPointer);
                    }
                    else
                    {
                        forceCalleeHasParamType = UpdateCalleeFunctionPointer(_delegateData._extraFunctionPointerOrData);
                    }
                }
                else if (conversionInfo.IsMulticastDelegate)
                {
                    _delegateData._multicastTargetCount = (int)_delegateData._extraFunctionPointerOrData;
                }
            }

            //
            // Setup output argument iterator for the callee
            //
            _calleeArgs = new ArgIterator(conversionInfo.ArgIteratorData,
                                          (conversionInfo.ArgIteratorData.HasThis() && !conversionInfo.IsStaticDelegateThunk) ?
                                          CallingConvention.ManagedInstance :
                                          CallingConvention.ManagedStatic,
                                          forceCalleeHasParamType || conversionInfo.CalleeHasParamType,
                                          false,
                                          conversionInfo.CalleeForcedByRefData,
                                          conversionInfo.IsOpenInstanceDelegateThunk,
                                          conversionInfo.IsClosedStaticDelegate);

            // The function pointer, 'hasParamType', and 'hasThis' flags for the callee arg iterator need to be computed/read from the caller's
            // input arguments in the case of a reflection invoker thunk (the target method pointer and 'hasThis' flags are
            // passed in as parameters from the caller, not loaded from a static method signature in native layout)
            if (conversionInfo.IsReflectionDynamicInvokerThunk)
            {
                ComputeCalleeFlagsAndFunctionPointerForReflectionInvokeThunk();
            }

#if CALLINGCONVENTION_CALLEE_POPS
            // Ensure that the count of bytes in the stack is available
            _callerArgs.CbStackPop();
#endif
        }
Esempio n. 9
0
 internal IntPtr ResolveTarget(RuntimeTypeHandle type)
 {
     return(OpenMethodResolver.ResolveMethod(MethodInvokeInfo.VirtualResolveData, type));
 }