summaryrefslogtreecommitdiff
path: root/src/vm/qcall.h
blob: ccf4c06997ce3e465e76fa7e081fb1d6d89c0ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// QCall.H



#ifndef __QCall_h__
#define __QCall_h__

#include "clr_std/type_traits"

//
// QCALLS
//

// QCalls are internal calls from managed code in mscorlib.dll to unmanaged code in mscorwks.dll. QCalls are very much like 
// a normal P/Invoke from mscorlib.dll to mscorwks.dll.
//
// Unlike FCalls, QCalls will marshal all arguments as unmanaged types like a normal P/Invoke. QCall also switch to preemptive
// GC mode like a normal P/Invoke. These two features should make QCalls easier to write reliably compared to FCalls.
// QCalls are not prone to GC holes and GC starvation bugs that are common with FCalls.
//
// QCalls perform better compared to FCalls w/ HelperMethodFrame. The QCall overhead is about 1.4x less compared to 
// FCall w/ HelperMethodFrame overhead on x86. The performance is about the same on x64. However, the implementation 
// of P/Invoke marshaling on x64 is not tuned for performance yet. The QCalls should become significantly faster compared 
// to FCalls w/ HelperMethodFrame on x64 as we do performance tuning of P/Invoke marshaling on x64.
//
//
// The preferred type of QCall arguments is primitive types that efficiently handled by the P/Invoke marshaler (INT32, LPCWSTR, BOOL).
// (Notice that BOOL is the correct boolean flavor for QCall arguments. CLR_BOOL is the correct boolean flavor for FCall arguments.)
//
// The pointers to common unmanaged EE structures should be wrapped into helper handle types. This is to make the managed implementation
// type safe and avoid falling into unsafe C# everywhere. See the AssemblyHandle below for a good example.
//
// There is a way to pass raw object references in and out of QCalls. It is done by wrapping a pointer to 
// a local variable in a handle. It is intentionally cumbersome and should be avoided if reasonably possible.  
// See the StringHandleOnStack in the example below. String arguments will get marshaled in as LPCWSTR. 
// Returning objects, especially strings, from QCalls is the only common pattern 
// where returning the raw objects (as an OUT argument) is widely acceptable.
//
//
// QCall example - managed part (do not replicate the comments into your actual QCall implementation):
// ---------------------------------------------------------------------------------------------------
//
// class Foo {
//
//  // All QCalls should have the following DllImport and SuppressUnmanagedCodeSecurity attributes
//  [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
//  [SuppressUnmanagedCodeSecurity]
//  // QCalls should always be static extern.
//  private static extern bool Bar(int flags, string inString, StringHandleOnStack retString);
//
//  // Many QCalls have a thin managed wrapper around them to expose them to the world in more meaningful way.
//  public string Bar(int flags)
//  {
//      string retString = null;
//
//      // The strings are returned from QCalls by taking address
//      // of a local variable using JitHelpers.GetStringHandleOnStack method 
//      if (!Bar(flags, this.Id, JitHelpers.GetStringHandleOnStack(ref retString)))
//          FatalError();
//
//      return retString;
//  }
//
//  Every QCall produces a couple of bogus FXCop warnings currently. Just add them to the FXCop exlusion list for now.
//
//
// QCall example - unmanaged part (do not replicate the comments into your actual QCall implementation):
// -----------------------------------------------------------------------------------------------------
//
// The entrypoints of all QCalls has to be registered in tables in vm\ecall.cpp using QCFuncEntry macro, 
// For example: QCFuncElement("Bar", FooNative::Bar)
//
// class FooNative {
// public:
//      // All QCalls should be static and should be tagged with QCALLTYPE
//      static
//      BOOL QCALLTYPE Bar(int flags, LPCWSTR wszString, QCall::StringHandleOnStack retString);
// };
//
// BOOL QCALLTYPE FooNative::Bar(int flags, LPCWSTR wszString, QCall::StringHandleOnStack retString)
// {
//      // All QCalls should have QCALL_CONTRACT. It is alias for THROWS; GC_TRIGGERS; MODE_PREEMPTIVE; SO_TOLERANT.
//      QCALL_CONTRACT;
//
//      // Optionally, use QCALL_CHECK instead and the expanded form of the contract if you want to specify preconditions:
//      // CONTRACTL { 
//      //     QCALL_CHECK; 
//      //     PRECONDITION(wszString != NULL);
//      // } CONTRACTL_END;
//        
//      // The only line between QCALL_CONTRACT and BEGIN_QCALL
//      // should be the return value declaration if there is one.
//      BOOL retVal = FALSE;
//
//      // The body has to be enclosed in BEGIN_QCALL/END_QCALL macro. It is necessary to make the exception handling work.
//      BEGIN_QCALL;
//
//      // Validate arguments if necessary and throw exceptions like anywhere else in the EE. There is no convention currently 
//      // on whether the argument validation should be done in managed or unmanaged code.
//      if (flags != 0)
//          COMPlusThrow(kArgumentException, L"InvalidFlags");
//
//      // No need to worry about GC moving strings passed into QCall. Marshaling pins them for us.
//      printf("%S", wszString);
//
//      // This is the most efficient way to return strings back to managed code. No need to use StringBuilder.
//      retString.Set(L"Hello");
//
//      // You can not return from inside of BEGIN_QCALL/END_QCALL. The return value has to be passed out in helper variable.
//      retVal = TRUE;
//
//      END_QCALL;
//
//      return retVal;
// }


#ifdef PLATFORM_UNIX
#define QCALLTYPE __cdecl
#else // PLATFORM_UNIX
#define QCALLTYPE __stdcall
#endif // !PLATFORM_UNIX

#define BEGIN_QCALL                      \
    INSTALL_MANAGED_EXCEPTION_DISPATCHER \
    INSTALL_UNWIND_AND_CONTINUE_HANDLER

#define END_QCALL                         \
    UNINSTALL_UNWIND_AND_CONTINUE_HANDLER \
    UNINSTALL_MANAGED_EXCEPTION_DISPATCHER

#define BEGIN_QCALL_SO_TOLERANT          \
    INSTALL_MANAGED_EXCEPTION_DISPATCHER \
    INSTALL_UNWIND_AND_CONTINUE_HANDLER_NO_PROBE

#define END_QCALL_SO_TOLERANT                      \
    UNINSTALL_UNWIND_AND_CONTINUE_HANDLER_NO_PROBE \
    UNINSTALL_MANAGED_EXCEPTION_DISPATCHER


#define QCALL_CHECK             \
    THROWS;                     \
    GC_TRIGGERS;                \
    MODE_PREEMPTIVE;            \
    SO_TOLERANT;                \

#define QCALL_CONTRACT CONTRACTL { QCALL_CHECK; } CONTRACTL_END;

//
// Scope class for QCall helper methods and types
// 
class QCall
{
public:

    //
    // Helper types to aid marshaling of QCall arguments in type-safe manner
    // 
    // The C/C++ compiler has to treat these types as POD (plain old data) to generate
    // a calling convention compatible with P/Invoke marshaling. This means that:
    // NONE OF THESE HELPER TYPES CAN HAVE A CONSTRUCTOR OR DESTRUCTOR!
    // THESE HELPER TYPES CAN NOT BE IMPLEMENTED USING INHERITANCE OR TEMPLATES!
    //

    //
    // StringHandleOnStack is used for managed strings
    //
    struct StringHandleOnStack
    {
        StringObject ** m_ppStringObject;

#ifndef DACCESS_COMPILE
        //
        // Helpers for returning managed string from QCall
        //

        // Raw setter - note that you need to be in cooperative mode
        void Set(STRINGREF s)
        {
            CONTRACTL
            {
                NOTHROW;
                GC_NOTRIGGER;
                MODE_COOPERATIVE;
                SO_TOLERANT;
            }
            CONTRACTL_END;

            // The space for the return value has to be on the stack
            _ASSERTE(Thread::IsAddressInCurrentStack(m_ppStringObject));

            *m_ppStringObject = STRINGREFToObject(s);
        }

        void Set(const SString& value);
        void Set(LPCWSTR pwzValue);
        void Set(LPCUTF8 pszValue);
#endif // !DACCESS_COMPILE
    };

    //
    // ObjectHandleOnStack type is used for managed objects
    //
    struct ObjectHandleOnStack
    {
        Object ** m_ppObject;

#ifndef DACCESS_COMPILE
        //
        // Helpers for returning common managed types from QCall
        //
        void Set(OBJECTREF o)
        {
            LIMITED_METHOD_CONTRACT;

            // The space for the return value has to be on the stack
            _ASSERTE(Thread::IsAddressInCurrentStack(m_ppObject));

            *m_ppObject = OBJECTREFToObject(o);
        }

        void SetByteArray(const BYTE * p, COUNT_T length);
        void SetIntPtrArray(const PVOID * p, COUNT_T length);
        void SetGuidArray(const GUID * p, COUNT_T length);

       // Do not add operator overloads to convert this object into a stack reference to a specific object type
       // such as OBJECTREF *. While such things are correct, our debug checking logic is unable to verify that
       // the object reference is actually protected from access and therefore will assert.
       // See bug 254159 for details.

#endif // !DACCESS_COMPILE
    };

    //
    // StackCrawlMarkHandle is used for passing StackCrawlMark into QCalls
    //
    struct StackCrawlMarkHandle
    {
        StackCrawlMark * m_pMark;

        operator StackCrawlMark * ()
        {
            LIMITED_METHOD_CONTRACT;
            return m_pMark;
        }
    };

    // AppDomainHandle is used for passing AppDomains into QCalls via System.AppDomainHandle
    struct AppDomainHandle
    {
        AppDomain *m_pAppDomain;

        operator AppDomain *()
        {
            LIMITED_METHOD_CONTRACT;
#ifdef _DEBUG
            VerifyDomainHandle();
#endif // _DEBUG
            return m_pAppDomain;
        }

        AppDomain *operator->() const
        {
            LIMITED_METHOD_CONTRACT;
#ifdef _DEBUG
            VerifyDomainHandle();
#endif // _DEBUG
            return m_pAppDomain;
        }

    private:
#ifdef _DEBUG
        void VerifyDomainHandle() const;
#endif // _DEBUG
    };

    struct AssemblyHandle
    {
        DomainAssembly * m_pAssembly;

        operator DomainAssembly * ()
        {
            LIMITED_METHOD_CONTRACT;
            return m_pAssembly;
        }

        DomainAssembly * operator->() const
        {
            LIMITED_METHOD_CONTRACT;
            return m_pAssembly;
        }
    };

    struct ModuleHandle
    {
        Module * m_pModule;

        operator Module * ()
        {
            LIMITED_METHOD_CONTRACT;
            return m_pModule;
        }

        Module * operator->() const
        {
            LIMITED_METHOD_CONTRACT;
            return m_pModule;
        }
    };

    struct LoaderAllocatorHandle
    {
        LoaderAllocator * m_pLoaderAllocator;

        operator LoaderAllocator * ()
        {
            LIMITED_METHOD_CONTRACT;
            return m_pLoaderAllocator;
        }

        LoaderAllocator * operator -> () const
        {
            LIMITED_METHOD_CONTRACT;
            return m_pLoaderAllocator;
        }

        static LoaderAllocatorHandle From(LoaderAllocator * pLoaderAllocator)
        {
            LoaderAllocatorHandle h;
            h.m_pLoaderAllocator = pLoaderAllocator;
            return h;
        }
    };

    // The lifetime management between managed and native Thread objects is broken. There is a resurrection 
    // race where one can get a dangling pointer to the unmanaged Thread object. Once this race is fixed 
    // we may need to revisit how the unmanaged thread handles are passed around.
    struct ThreadHandle
    {
        Thread * m_pThread;

        operator Thread * ()
        {
            LIMITED_METHOD_CONTRACT;
            return m_pThread;
        }

        Thread * operator->() const
        {
            LIMITED_METHOD_CONTRACT;
            return m_pThread;
        }
    };
};

typedef void* EnregisteredTypeHandle;

#endif //__QCall_h__