1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4//
5// File: stubhelpers.h
6//
7
8//
9
10
11#ifndef __STUBHELPERS_h__
12#define __STUBHELPERS_h__
13
14#include "fcall.h"
15#include "clrvarargs.h"
16
17// Maximum number of deferred byref validation entries - we will trigger gen0 GC if we reach this number.
18#define BYREF_VALIDATION_LIST_MAX_SIZE (512 * 1024)
19
20class StubHelpers
21{
22#ifdef VERIFY_HEAP
23 struct ByrefValidationEntry
24 {
25 void *pByref; // pointer to GC heap
26 MethodDesc *pMD; // interop MD this byref was passed to
27 };
28
29 static CQuickArray<ByrefValidationEntry> s_ByrefValidationEntries;
30 static SIZE_T s_ByrefValidationIndex;
31 static CrstStatic s_ByrefValidationLock;
32
33 static void ValidateObjectInternal(Object *pObjUNSAFE, BOOL fValidateNextObj);
34 static MethodDesc *ResolveInteropMethod(Object *pThisUNSAFE, MethodDesc *pMD);
35 static void FormatValidationMessage(MethodDesc *pMD, SString &ssErrorString);
36
37public:
38 static void Init();
39 static void ProcessByrefValidationList();
40#else // VERIFY_HEAP
41public:
42 static void Init() { LIMITED_METHOD_CONTRACT; }
43#endif // VERIFY_HEAP
44
45 //-------------------------------------------------------
46 // PInvoke stub helpers
47 //-------------------------------------------------------
48
49 static FCDECL1_V(double, DateMarshaler__ConvertToNative, INT64 managedDate);
50 static FCDECL1_V(INT64, DateMarshaler__ConvertToManaged, double nativeDate);
51
52 static FCDECL4(void, ValueClassMarshaler__ConvertToNative, LPVOID pDest, LPVOID pSrc, MethodTable* pMT, OBJECTREF *ppCleanupWorkListOnStack);
53 static FCDECL3(void, ValueClassMarshaler__ConvertToManaged, LPVOID pDest, LPVOID pSrc, MethodTable* pMT);
54 static FCDECL2(void, ValueClassMarshaler__ClearNative, LPVOID pDest, MethodTable* pMT);
55
56#ifdef FEATURE_COMINTEROP
57 static FCDECL4(IUnknown*, GetCOMIPFromRCW, Object* pSrcUNSAFE, MethodDesc* pMD, void **ppTarget, CLR_BOOL* pfNeedsRelease);
58 static FCDECL3(IUnknown*, GetCOMIPFromRCW_WinRT, Object* pSrcUNSAFE, MethodDesc* pMD, void **ppTarget);
59 static FCDECL3(IUnknown*, GetCOMIPFromRCW_WinRTSharedGeneric, Object* pSrcUNSAFE, MethodDesc* pMD, void **ppTarget);
60 static FCDECL3(IUnknown*, GetCOMIPFromRCW_WinRTDelegate, Object* pSrcUNSAFE, MethodDesc* pMD, void **ppTarget);
61
62 static FCDECL2(FC_BOOL_RET, ShouldCallWinRTInterface, Object* pSrcUNSAFE, MethodDesc* pMD);
63 static FCDECL3(DelegateObject*, GetTargetForAmbiguousVariantCall, Object* pSrcUNSAFE, MethodTable* pMT, CLR_BOOL *pfUseString);
64
65 static FCDECL2(void, ObjectMarshaler__ConvertToNative, Object* pSrcUNSAFE, VARIANT* pDest);
66 static FCDECL1(Object*, ObjectMarshaler__ConvertToManaged, VARIANT* pSrc);
67 static FCDECL1(void, ObjectMarshaler__ClearNative, VARIANT* pSrc);
68
69 static FCDECL4(IUnknown*, InterfaceMarshaler__ConvertToNative, Object* pObjUNSAFE, MethodTable* pItfMT, MethodTable* pClsMT, DWORD dwFlags);
70 static FCDECL4(Object*, InterfaceMarshaler__ConvertToManaged, IUnknown **ppUnk, MethodTable *pItfMT, MethodTable *pClsMT, DWORD dwFlags);
71 static void QCALLTYPE InterfaceMarshaler__ClearNative(IUnknown * pUnk);
72 static FCDECL1(Object *, InterfaceMarshaler__ConvertToManagedWithoutUnboxing, IUnknown *pNative);
73
74 static FCDECL1(StringObject*, UriMarshaler__GetRawUriFromNative, ABI::Windows::Foundation::IUriRuntimeClass* pIUriRC);
75 static FCDECL2(IUnknown*, UriMarshaler__CreateNativeUriInstance, __in_ecount(strLen) CLR_CHAR* pRawUriObj, UINT strLen);
76
77 static FCDECL1(MethodDesc *, GetDelegateInvokeMethod, DelegateObject *pThisUNSAFE);
78 static FCDECL2(IInspectable *, GetWinRTFactoryReturnValue, Object *pThisUNSAFE, PCODE pCtorEntry);
79 static FCDECL2(IInspectable *, GetOuterInspectable, Object *pThisUNSAFE, MethodDesc *pCtorMD);
80#ifdef MDA_SUPPORTED
81 static FCDECL2(ExceptionObject*,TriggerExceptionSwallowedMDA, ExceptionObject* pExceptionUNSAFE, PCODE pManagedTarget);
82#endif // MDA_SUPPORTED
83#endif // FEATURE_COMINTEROP
84
85 static FCDECL0(void, SetLastError );
86 static FCDECL0(void, ClearLastError );
87 static FCDECL1(FC_BOOL_RET, IsQCall, NDirectMethodDesc* pNMD);
88 static FCDECL1(void, InitDeclaringType, NDirectMethodDesc* pMND);
89 static FCDECL1(void*, GetNDirectTarget, NDirectMethodDesc* pNMD);
90 static FCDECL2(void*, GetDelegateTarget, DelegateObject *pThisUNSAFE, UINT_PTR *ppStubArg);
91
92
93 static FCDECL2(void, ThrowInteropParamException, UINT resID, UINT paramIdx);
94 static FCDECL1(Object*, GetHRExceptionObject, HRESULT hr);
95
96#ifdef FEATURE_COMINTEROP
97 static FCDECL4(Object*, GetCOMHRExceptionObject, HRESULT hr, MethodDesc *pMD, Object *unsafe_pThis, CLR_BOOL fForWinRT);
98#endif // FEATURE_COMINTEROP
99
100 static FCDECL3(void*, CreateCustomMarshalerHelper, MethodDesc* pMD, mdToken paramToken, TypeHandle hndManagedType);
101
102 static FCDECL3(void, FmtClassUpdateNativeInternal, Object* pObjUNSAFE, BYTE* pbNative, OBJECTREF *ppCleanupWorkListOnStack);
103 static FCDECL2(void, FmtClassUpdateCLRInternal, Object* pObjUNSAFE, BYTE* pbNative);
104 static FCDECL2(void, LayoutDestroyNativeInternal, BYTE* pbNative, MethodTable* pMT);
105 static FCDECL1(Object*, AllocateInternal, EnregisteredTypeHandle typeHnd);
106 static FCDECL3(void, MarshalToUnmanagedVaListInternal, va_list va, DWORD cbVaListSize, const VARARGS* pArgIterator);
107 static FCDECL2(void, MarshalToManagedVaListInternal, va_list va, VARARGS* pArgIterator);
108 static FCDECL0(void*, GetStubContext);
109 static FCDECL2(void, LogPinnedArgument, MethodDesc *localDesc, Object *nativeArg);
110#ifdef _TARGET_64BIT_
111 static FCDECL0(void*, GetStubContextAddr);
112#endif // _TARGET_64BIT_
113 static FCDECL1(DWORD, CalcVaListSize, VARARGS *varargs);
114 static FCDECL3(void, ValidateObject, Object *pObjUNSAFE, MethodDesc *pMD, Object *pThisUNSAFE);
115 static FCDECL3(void, ValidateByref, void *pByref, MethodDesc *pMD, Object *pThisUNSAFE);
116
117#ifdef FEATURE_COMINTEROP
118 //-------------------------------------------------------
119 // Helper for the MDA RaceOnRCWCleanup
120 //-------------------------------------------------------
121 static FCDECL1(void, StubRegisterRCW, Object *unsafe_pThis);
122 static FCDECL1(void, StubUnregisterRCW, Object *unsafe_pThis);
123 static FCDECL1(Object*, GetWinRTFactoryObject, MethodDesc *pCMD);
124#endif // FEATURE_COMINTEROP
125
126#ifdef MDA_SUPPORTED
127 static FCDECL0(void, TriggerGCForMDA);
128 static FCDECL1(void, CheckCollectedDelegateMDA, LPVOID pEntryThunk);
129#endif // MDA_SUPPORTED
130
131#ifdef PROFILING_SUPPORTED
132 //-------------------------------------------------------
133 // Profiler helper
134 //-------------------------------------------------------
135 static FCDECL3(SIZE_T, ProfilerBeginTransitionCallback, SIZE_T pSecretParam, Thread* pThread, Object* unsafe_pThis);
136 static FCDECL2(void, ProfilerEndTransitionCallback, MethodDesc* pRealMD, Thread* pThread);
137#endif
138
139#ifdef FEATURE_ARRAYSTUB_AS_IL
140 static FCDECL2(void, ArrayTypeCheck, Object*, PtrArray*);
141#endif
142
143#ifdef FEATURE_COMINTEROP
144 static FCDECL2(StringObject *, WinRTTypeNameConverter__ConvertToWinRTTypeName, ReflectClassBaseObject *pTypeUNSAFE, CLR_BOOL *pbIsWinRTPrimitive);
145 static FCDECL2(ReflectClassBaseObject *, WinRTTypeNameConverter__GetTypeFromWinRTTypeName, StringObject *pWinRTTypeNameUNSAFE, CLR_BOOL *pbIsPrimitive);
146#endif // FEATURE_COMINTEROP
147
148#ifdef FEATURE_MULTICASTSTUB_AS_IL
149 static FCDECL2(void, MulticastDebuggerTraceHelper, Object*, INT32);
150#endif
151};
152
153#endif // __STUBHELPERS_h__
154