Add a fourth parameter to the DEFINE_DACVAR macro that is the actual fully qualified...
[platform/upstream/coreclr.git] / src / inc / corhost.h
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
4 //
5 //
6
7 //
8 //*****************************************************************************
9 // CorHost.h
10 //
11 // Class factories are used by the pluming in COM to activate new objects.
12 // This module contains the class factory code to instantiate the debugger
13 // objects described in <cordb.h>.
14 //
15 //*****************************************************************************
16
17 #ifndef __CorHost__h__
18 #define __CorHost__h__
19
20
21 #include "windows.h" // worth to include before mscoree.h so we are guaranteed to pick few definitions
22 #ifdef CreateSemaphore
23 #undef CreateSemaphore
24 #endif
25
26 #include "mscoree.h"
27
28 #include "clrinternal.h"
29
30 #include "ivehandler.h"
31 #include "ivalidator.h"
32 #ifdef FEATURE_FUSION
33 #include "fusion.h"
34 #endif
35 #include "holder.h"
36
37 #if defined(FEATURE_HOSTED_BINDER)
38 #include "clrprivhosting.h"
39 #endif
40
41 #ifdef FEATURE_COMINTEROP
42 #include "activation.h" // WinRT activation.
43 #endif
44
45 class DangerousNonHostedSpinLock;
46
47 #define INVALID_STACK_BASE_MARKER_FOR_CHECK_STATE 2
48
49 #ifndef _WIN64
50 #if (_WIN32_WINNT < 0x0500)
51 typedef VOID (__stdcall *WAITORTIMERCALLBACK)(PVOID, BOOL);
52 #endif
53 #endif
54
55 class AppDomain;
56 class Assembly;
57
58 #if !defined(FEATURE_CORECLR)
59 class CorThreadpool : public ICorThreadpool
60 {
61 public:
62     HRESULT STDMETHODCALLTYPE  CorRegisterWaitForSingleObject(PHANDLE phNewWaitObject,
63                                                               HANDLE hWaitObject,
64                                                               WAITORTIMERCALLBACK Callback,
65                                                               PVOID Context,
66                                                               ULONG timeout,
67                                                               BOOL  executeOnlyOnce,
68                                                               BOOL* pResult);
69
70     HRESULT STDMETHODCALLTYPE  CorBindIoCompletionCallback(HANDLE fileHandle, LPOVERLAPPED_COMPLETION_ROUTINE callback);
71
72     HRESULT STDMETHODCALLTYPE  CorUnregisterWait(HANDLE hWaitObject,HANDLE CompletionEvent, BOOL* pResult);
73
74     HRESULT STDMETHODCALLTYPE  CorQueueUserWorkItem(LPTHREAD_START_ROUTINE Function,PVOID Context,BOOL executeOnlyOnce, BOOL* pResult );
75
76     HRESULT STDMETHODCALLTYPE  CorCallOrQueueUserWorkItem(LPTHREAD_START_ROUTINE Function,PVOID Context,BOOL* pResult );
77
78     HRESULT STDMETHODCALLTYPE  CorCreateTimer(PHANDLE phNewTimer,
79                                               WAITORTIMERCALLBACK Callback,
80                                               PVOID Parameter,
81                                               DWORD DueTime,
82                                               DWORD Period,
83                                               BOOL* pResult);
84
85     HRESULT STDMETHODCALLTYPE  CorDeleteTimer(HANDLE Timer, HANDLE CompletionEvent, BOOL* pResult);
86
87     HRESULT STDMETHODCALLTYPE  CorChangeTimer(HANDLE Timer,ULONG DueTime,ULONG Period, BOOL* pResult);
88
89     HRESULT STDMETHODCALLTYPE CorSetMaxThreads(DWORD MaxWorkerThreads,
90                                                DWORD MaxIOCompletionThreads);
91
92     HRESULT STDMETHODCALLTYPE CorGetMaxThreads(DWORD *MaxWorkerThreads,
93                                                DWORD *MaxIOCompletionThreads);
94
95     HRESULT STDMETHODCALLTYPE CorGetAvailableThreads(DWORD *AvailableWorkerThreads,
96                                                   DWORD *AvailableIOCompletionThreads);
97 };
98
99 class CorGCHost : public IGCHost2
100 {
101 public:
102     // IGCHost
103     STDMETHODIMP STDMETHODCALLTYPE SetGCStartupLimits(
104         DWORD SegmentSize,
105         DWORD MaxGen0Size);
106
107     STDMETHODIMP STDMETHODCALLTYPE Collect(
108         LONG Generation);
109
110     STDMETHODIMP STDMETHODCALLTYPE GetStats(
111         COR_GC_STATS *pStats);
112
113     STDMETHODIMP STDMETHODCALLTYPE GetThreadStats(
114         DWORD *pFiberCookie,
115         COR_GC_THREAD_STATS *pStats);
116
117     STDMETHODIMP STDMETHODCALLTYPE SetVirtualMemLimit(
118         SIZE_T sztMaxVirtualMemMB);
119
120     // IGCHost2
121     STDMETHODIMP STDMETHODCALLTYPE SetGCStartupLimitsEx(
122         SIZE_T SegmentSize,
123         SIZE_T MaxGen0Size);
124
125 private:
126
127     HRESULT _SetGCSegmentSize(SIZE_T SegmentSize);
128     HRESULT _SetGCMaxGen0Size(SIZE_T MaxGen0Size);
129 };
130
131 class CorConfiguration : public ICorConfiguration
132 {
133 public:
134     virtual HRESULT STDMETHODCALLTYPE SetGCThreadControl(
135         /* [in] */ IGCThreadControl __RPC_FAR *pGCThreadControl);
136
137     virtual HRESULT STDMETHODCALLTYPE SetGCHostControl(
138         /* [in] */ IGCHostControl __RPC_FAR *pGCHostControl);
139
140     virtual HRESULT STDMETHODCALLTYPE SetDebuggerThreadControl(
141         /* [in] */ IDebuggerThreadControl __RPC_FAR *pDebuggerThreadControl);
142
143     virtual HRESULT STDMETHODCALLTYPE AddDebuggerSpecialThread(
144         /* [in] */ DWORD dwSpecialThreadId);
145
146     // This mechanism isn't thread-safe with respect to reference counting, because
147     // the runtime will use the cached pointer without adding extra refcounts to protect
148     // itself.  So if one thread calls GetGCThreadControl & another thread calls
149     // ICorHost::SetGCThreadControl, we have a race.
150     static IGCThreadControl *GetGCThreadControl()
151     {
152         LIMITED_METHOD_CONTRACT;
153
154         return m_CachedGCThreadControl;
155     }
156
157     static IGCHostControl *GetGCHostControl()
158     {
159         LIMITED_METHOD_CONTRACT;
160
161         return m_CachedGCHostControl;
162     }
163
164     static IDebuggerThreadControl *GetDebuggerThreadControl()
165     {
166         LIMITED_METHOD_CONTRACT;
167
168         return m_CachedDebuggerThreadControl;
169     }
170
171     static DWORD GetDebuggerSpecialThreadCount()
172     {
173         LIMITED_METHOD_CONTRACT;
174
175         return m_DSTCount;
176     }
177
178     static DWORD *GetDebuggerSpecialThreadArray()
179     {
180         LIMITED_METHOD_CONTRACT;
181
182         return m_DSTArray;
183     }
184
185     // Helper function that returns true if the thread is in the debugger special thread list
186     static BOOL IsDebuggerSpecialThread(DWORD dwThreadId);
187
188     // Helper function to update the thread list in the debugger control block
189     static HRESULT RefreshDebuggerSpecialThreadList();
190
191     // Clean up debugger thread control object, called at shutdown
192     static void CleanupDebuggerThreadControl();
193
194 private:
195     // Cache the IGCThreadControl interface until the EE is started, at which point
196     // we pass it through.
197     static IGCThreadControl *m_CachedGCThreadControl;
198     static IGCHostControl *m_CachedGCHostControl;
199     static IDebuggerThreadControl *m_CachedDebuggerThreadControl;
200
201     // Array of ID's of threads that should be considered "special" to
202     // the debugging services.
203     static DWORD *m_DSTArray;
204     static DWORD  m_DSTArraySize;
205     static DWORD  m_DSTCount;
206 };
207
208 class CorValidator : public IValidator
209 {
210 protected:
211     CorValidator() {LIMITED_METHOD_CONTRACT;}
212
213 public:
214     STDMETHODIMP STDMETHODCALLTYPE Validate(
215             IVEHandler        *veh,
216             IUnknown          *pAppDomain,
217             unsigned long      ulFlags,
218             unsigned long      ulMaxError,
219             unsigned long      token,
220             __in_z LPWSTR             fileName,
221             BYTE               *pe,
222             unsigned long      ulSize);
223
224     STDMETHODIMP STDMETHODCALLTYPE FormatEventInfo(
225             HRESULT            hVECode,
226             VEContext          Context,
227             __out_ecount(ulMaxLength) LPWSTR             msg,
228             unsigned long      ulMaxLength,
229             SAFEARRAY         *psa);
230 };
231
232 class CLRValidator : public ICLRValidator
233 {
234 protected:
235     CLRValidator() {LIMITED_METHOD_CONTRACT;}
236
237 public:
238     STDMETHODIMP STDMETHODCALLTYPE Validate(
239             IVEHandler        *veh,
240             unsigned long      ulAppDomainId,
241             unsigned long      ulFlags,
242             unsigned long      ulMaxError,
243             unsigned long      token,
244             __in_z LPWSTR             fileName,
245             BYTE               *pe,
246             unsigned long      ulSize);
247
248     STDMETHODIMP STDMETHODCALLTYPE FormatEventInfo(
249             HRESULT            hVECode,
250             VEContext          Context,
251             __out_ecount(ulMaxLength) LPWSTR             msg,
252             unsigned long      ulMaxLength,
253             SAFEARRAY         *psa);
254 };
255
256 class CorDebuggerInfo : public IDebuggerInfo
257 {
258 public:
259     STDMETHODIMP IsDebuggerAttached(BOOL *pbAttached);
260 };
261 #endif // !defined(FEATURE_CORECLR)
262
263 class CorExecutionManager
264     : public ICLRExecutionManager
265 {
266 public:
267     CorExecutionManager();
268
269     STDMETHODIMP STDMETHODCALLTYPE Pause(DWORD dwAppDomainId, DWORD dwFlags);
270     STDMETHODIMP STDMETHODCALLTYPE Resume(DWORD dwAppDomainId);
271
272 private:
273     DWORD m_dwFlags; //flags passed to the last Pause call.
274     INT64 m_pauseStartTime;
275 };
276
277 class CorRuntimeHostBase
278 {
279 protected:
280     CorRuntimeHostBase()
281     :m_Started(FALSE),
282      m_cRef(0)
283 #ifdef FEATURE_CORECLR
284     , m_fStarted(FALSE)
285 #endif // FEATURE_CORECLR
286     {LIMITED_METHOD_CONTRACT;}
287
288     STDMETHODIMP_(ULONG) AddRef(void);
289
290     // Starts the runtime. This is equivalent to CoInitializeCor()
291     STDMETHODIMP Start();
292
293 #ifdef FEATURE_COMINTEROP
294     // Creates a domain in the runtime. The identity array is
295     // a pointer to an array TYPE containing IIdentity objects defining
296     // the security identity.
297     STDMETHODIMP CreateDomain(LPCWSTR pwzFriendlyName,   // Optional
298                               IUnknown* pIdentityArray, // Optional
299                               IUnknown ** pAppDomain);
300
301     // Returns the default domain.
302     STDMETHODIMP GetDefaultDomain(IUnknown ** pAppDomain);
303
304     // Enumerate currently existing domains.
305     STDMETHODIMP EnumDomains(HDOMAINENUM *hEnum);
306
307     // Returns S_FALSE when there are no more domains. A domain
308     // is passed out only when S_OK is returned.
309     STDMETHODIMP NextDomain(HDOMAINENUM hEnum,
310                             IUnknown** pAppDomain);
311
312     // Close the enumeration releasing resources
313     STDMETHODIMP CloseEnum(HDOMAINENUM hEnum);
314
315     STDMETHODIMP CreateDomainEx(LPCWSTR pwzFriendlyName,
316                                 IUnknown* pSetup, // Optional
317                                 IUnknown* pEvidence, // Optional
318                                 IUnknown ** pAppDomain);
319
320     // Create appdomain setup object that can be passed into CreateDomainEx
321     STDMETHODIMP CreateDomainSetup(IUnknown** pAppDomainSetup);
322
323     // Create Evidence object that can be passed into CreateDomainEx
324     STDMETHODIMP CreateEvidence(IUnknown** pEvidence);
325
326     // Unload a domain, releasing the reference will only release the
327     // the wrapper to the domain not unload the domain.
328     STDMETHODIMP UnloadDomain(IUnknown* pAppDomain);
329
330     // Returns the threads domain if there is one.
331     STDMETHODIMP CurrentDomain(IUnknown ** pAppDomain);
332 #endif // FEATURE_COMINTEROP
333
334     STDMETHODIMP MapFile(                       // Return code.
335         HANDLE     hFile,                       // [in]  Handle for file
336         HMODULE   *hMapAddress                  // [out] HINSTANCE for mapped file
337         );
338
339     STDMETHODIMP LocksHeldByLogicalThread(      // Return code.
340         DWORD *pCount                           // [out] Number of locks that the current thread holds.
341         );
342
343 protected:
344     BOOL        m_Started;              // Has START been called?
345
346     LONG        m_cRef;                 // Ref count.
347
348 #ifdef FEATURE_CORECLR
349     // This flag will be used to ensure that a CoreCLR host can invoke Start/Stop in pairs only.
350     BOOL m_fStarted; 
351     BOOL m_fAppDomainCreated; // this flag is used when an appdomain can only create a single appdomain
352 #endif // FEATURE_CORECLR
353
354     static ULONG       m_Version;              // Version of ICorRuntimeHost.
355                                         // Some functions are only available in ICLRRuntimeHost.
356                                         // Some functions are no-op in ICLRRuntimeHost.
357
358     STDMETHODIMP UnloadAppDomain(DWORD dwDomainId, BOOL fWaitUntilDone);
359
360 public:
361     static ULONG GetHostVersion()
362     {
363         LIMITED_METHOD_CONTRACT;
364         _ASSERTE (m_Version != 0);
365         return m_Version;
366     }
367
368 };
369
370 #if !defined(FEATURE_CORECLR) // simple hosting
371 class CorHost :
372     public CorRuntimeHostBase, public ICorRuntimeHost, public CorThreadpool
373     , public CorGCHost, public CorConfiguration
374     , public CorValidator, public CorDebuggerInfo
375     , public CorExecutionManager
376 {
377 public:
378     CorHost() {WRAPPER_NO_CONTRACT;}
379
380     // *** IUnknown methods ***
381     STDMETHODIMP    QueryInterface(REFIID riid, void** ppv);
382     STDMETHODIMP_(ULONG) AddRef(void)
383     {
384         WRAPPER_NO_CONTRACT;
385         STATIC_CONTRACT_SO_TOLERANT;
386         return CorRuntimeHostBase::AddRef();
387     }
388     STDMETHODIMP_(ULONG) Release(void);
389
390
391     // *** ICorRuntimeHost methods ***
392     // Returns an object for configuring the runtime prior to
393     // it starting. If the runtime has been initialized this
394     // routine returns an error. See ICorConfiguration.
395     STDMETHODIMP GetConfiguration(ICorConfiguration** pConfiguration);
396
397
398     // Starts the runtime. This is equivalent to CoInitializeCor();
399     STDMETHODIMP Start(void);
400
401     STDMETHODIMP Stop();
402
403     // Creates a domain in the runtime. The identity array is
404     // a pointer to an array TYPE containing IIdentity objects defining
405     // the security identity.
406     STDMETHODIMP CreateDomain(LPCWSTR pwzFriendlyName,   // Optional
407                               IUnknown* pIdentityArray, // Optional
408                               IUnknown ** pAppDomain)
409     {
410         WRAPPER_NO_CONTRACT;
411         STATIC_CONTRACT_SO_TOLERANT;
412         return CorRuntimeHostBase::CreateDomain(pwzFriendlyName,pIdentityArray,pAppDomain);
413     }
414
415     // Returns the default domain.
416     STDMETHODIMP GetDefaultDomain(IUnknown ** pAppDomain)
417     {
418         WRAPPER_NO_CONTRACT;
419         STATIC_CONTRACT_SO_TOLERANT;
420         return CorRuntimeHostBase::GetDefaultDomain(pAppDomain);
421     }
422
423     // Enumerate currently existing domains.
424     STDMETHODIMP EnumDomains(HDOMAINENUM *hEnum)
425     {
426         WRAPPER_NO_CONTRACT;
427         STATIC_CONTRACT_SO_TOLERANT;
428         return CorRuntimeHostBase::EnumDomains(hEnum);
429     }
430
431     // Returns S_FALSE when there are no more domains. A domain
432     // is passed out only when S_OK is returned.
433     STDMETHODIMP NextDomain(HDOMAINENUM hEnum,
434                             IUnknown** pAppDomain)
435     {
436         WRAPPER_NO_CONTRACT;
437         STATIC_CONTRACT_SO_TOLERANT;
438         return CorRuntimeHostBase::NextDomain(hEnum,pAppDomain);
439     }
440
441     // Close the enumeration releasing resources
442     STDMETHODIMP CloseEnum(HDOMAINENUM hEnum)
443     {
444         WRAPPER_NO_CONTRACT;
445         STATIC_CONTRACT_SO_TOLERANT;
446         return CorRuntimeHostBase::CloseEnum(hEnum);
447     }
448
449     STDMETHODIMP CreateDomainEx(LPCWSTR pwzFriendlyName,
450                                 IUnknown* pSetup, // Optional
451                                 IUnknown* pEvidence, // Optional
452                                 IUnknown ** pAppDomain)
453     {
454         WRAPPER_NO_CONTRACT;
455         STATIC_CONTRACT_SO_TOLERANT;
456         return CorRuntimeHostBase::CreateDomainEx(pwzFriendlyName,pSetup,pEvidence,pAppDomain);
457     }
458
459     // Create appdomain setup object that can be passed into CreateDomainEx
460     STDMETHODIMP CreateDomainSetup(IUnknown** pAppDomainSetup)
461     {
462         WRAPPER_NO_CONTRACT;
463         STATIC_CONTRACT_SO_TOLERANT;
464         return CorRuntimeHostBase::CreateDomainSetup(pAppDomainSetup);
465     }
466
467     // Create Evidence object that can be passed into CreateDomainEx
468     STDMETHODIMP CreateEvidence(IUnknown** pEvidence)
469     {
470         WRAPPER_NO_CONTRACT;
471         STATIC_CONTRACT_SO_TOLERANT;
472         return CorRuntimeHostBase::CreateEvidence(pEvidence);
473     }
474
475     // Unload a domain, releasing the reference will only release the
476     // the wrapper to the domain not unload the domain.
477     STDMETHODIMP UnloadDomain(IUnknown* pAppDomain)
478     {
479         WRAPPER_NO_CONTRACT;
480         STATIC_CONTRACT_SO_TOLERANT;
481         return CorRuntimeHostBase::UnloadDomain(pAppDomain);
482     }
483
484     // Returns the threads domain if there is one.
485     STDMETHODIMP CurrentDomain(IUnknown ** pAppDomain)
486     {
487         WRAPPER_NO_CONTRACT;
488         STATIC_CONTRACT_SO_TOLERANT;
489         return CorRuntimeHostBase::CurrentDomain(pAppDomain);
490     }
491
492     // TODO: Following 4 APIs should be move to CorHost for V1.
493     STDMETHODIMP CreateLogicalThreadState();    // Return code.
494     STDMETHODIMP DeleteLogicalThreadState();    // Return code.
495     STDMETHODIMP SwitchInLogicalThreadState(    // Return code.
496         DWORD *pFiberCookie                     // [in] Cookie that indicates the fiber to use.
497         );
498
499     STDMETHODIMP SwitchOutLogicalThreadState(   // Return code.
500         DWORD **pFiberCookie                    // [out] Cookie that indicates the fiber being switched out.
501         );
502
503     STDMETHODIMP LocksHeldByLogicalThread(      // Return code.
504         DWORD *pCount                           // [out] Number of locks that the current thread holds.
505         )
506     {
507         WRAPPER_NO_CONTRACT;
508         STATIC_CONTRACT_SO_TOLERANT;
509         return CorRuntimeHostBase::LocksHeldByLogicalThread(pCount);
510     }
511
512     // Class factory hook-up.
513     static HRESULT CreateObject(REFIID riid, void **ppUnk);
514
515     STDMETHODIMP MapFile(                       // Return code.
516         HANDLE     hFile,                       // [in]  Handle for file
517         HMODULE   *hMapAddress                  // [out] HINSTANCE for mapped file
518         )
519     {
520         WRAPPER_NO_CONTRACT;
521         STATIC_CONTRACT_SO_TOLERANT;
522         return CorRuntimeHostBase::MapFile(hFile,hMapAddress);
523     }
524 };
525 #endif // !defined(FEATURE_CORECLR)
526
527 class ConnectionNameTable;
528 typedef DPTR(class ConnectionNameTable) PTR_ConnectionNameTable;
529
530 class CrstStatic;
531
532 // Defines the precedence (in increading oder) of the two symbol reading knobs
533 enum ESymbolReadingSetBy
534 {
535     eSymbolReadingSetByDefault,
536     eSymbolReadingSetByConfig,  // EEConfig - config file, env var, etc.
537     eSymbolReadingSetByHost,    // Hosting API - highest precedence
538     eSymbolReadingSetBy_COUNT
539 };
540
541 #ifdef FEATURE_INCLUDE_ALL_INTERFACES
542 // Hash table entry to keep track <connection, name> for SQL fiber support
543 typedef DPTR(struct ConnectionNameHashEntry) PTR_ConnectionNameHashEntry;
544 struct ConnectionNameHashEntry
545 {
546     FREEHASHENTRY   entry;
547     CONNID          m_dwConnectionId;
548     PTR_WSTR        m_pwzName;
549     ICLRTask        **m_ppCLRTaskArray;
550     UINT            m_CLRTaskCount;
551 };
552
553
554 class CCLRDebugManager : public ICLRDebugManager
555 {
556 public:
557     CCLRDebugManager() {LIMITED_METHOD_CONTRACT;};
558
559     STDMETHODIMP    QueryInterface(REFIID riid, void** ppv);
560     STDMETHODIMP_(ULONG) AddRef(void)
561     {
562         LIMITED_METHOD_CONTRACT;
563         return 1;
564     }
565     STDMETHODIMP_(ULONG) Release(void);
566
567     // ICLRTDebugManager's interface
568     STDMETHODIMP BeginConnection(
569         CONNID  dwConnectionId,
570         __in_z wchar_t *szConnectionName);
571     STDMETHODIMP SetConnectionTasks(
572         DWORD id,
573         DWORD dwCount,
574         ICLRTask **ppCLRTask);
575     STDMETHODIMP EndConnection(
576         CONNID  dwConnectionId);
577
578     // Set ACL on shared section, events, and process
579     STDMETHODIMP SetDacl(PACL pacl);
580
581     // Returning the current ACL that CLR is using
582     STDMETHODIMP GetDacl(PACL *pacl);
583
584     STDMETHODIMP IsDebuggerAttached(BOOL *pbAttached);
585
586     // symbol reading policy - include file line info when getting a call stack etc.
587     STDMETHODIMP SetSymbolReadingPolicy(ESymbolReadingPolicy policy);
588
589 #ifdef DACCESS_COMPILE
590     // Expose iterators for DAC. Debugger can use this on attach to find existing Connections.
591     //
592     // Example usage:
593     //   HASHFIND h;
594     //   ConnectionNameHashEntry * pConnection = FindFirst(&h);
595     //   while(pConnection != NULL) {
596     //       DoSomething(pConnection);
597     //       pConnection = FindNext(&h);
598     //   }
599     static ConnectionNameHashEntry * FindFirst(HASHFIND * pHashfind);
600     static ConnectionNameHashEntry * FindNext(HASHFIND * pHashfind);
601 #endif
602
603     static void ProcessInit();
604     static void ProcessCleanup();
605
606     // Get the current symbol reading policy setting
607     static ESymbolReadingPolicy GetSymbolReadingPolicy()
608     {
609         return m_symbolReadingPolicy;
610     }
611
612     // Set the symbol reading policy if the setter has higher precendence than the current setting
613     static void SetSymbolReadingPolicy( ESymbolReadingPolicy policy, ESymbolReadingSetBy setBy );
614     
615 private:
616     static CrstStatic m_lockConnectionNameTable;
617     SPTR_DECL(ConnectionNameTable, m_pConnectionNameHash);
618
619     static ESymbolReadingPolicy m_symbolReadingPolicy;
620     static ESymbolReadingSetBy m_symbolReadingSetBy;
621 };
622
623 #endif // FEATURE_INCLUDE_ALL_INTERFACES
624
625 #if defined(FEATURE_INCLUDE_ALL_INTERFACES) || defined(FEATURE_WINDOWSPHONE)
626 class CCLRErrorReportingManager :
627 #ifdef FEATURE_WINDOWSPHONE
628     public ICLRErrorReportingManager2
629 #else
630     public ICLRErrorReportingManager
631 #endif // FEATURE_WINDOWSPHONE
632 {
633     friend class ClrDataAccess;
634     friend struct _DacGlobals;
635
636     SVAL_DECL(ECustomDumpFlavor, g_ECustomDumpFlavor);
637     
638 #ifdef FEATURE_WINDOWSPHONE
639     WCHAR* m_pApplicationId;
640     WCHAR* m_pInstanceId;
641     
642     class BucketParamsCache
643     {
644     private:
645         WCHAR** m_pParams;
646         DWORD const m_cMaxParams;
647     public:
648         BucketParamsCache(DWORD maxNumParams);
649         ~BucketParamsCache();
650         
651         WCHAR const* GetAt(BucketParameterIndex index);
652         HRESULT SetAt(BucketParameterIndex index, WCHAR const* val);
653     };
654     
655     BucketParamsCache* m_pBucketParamsCache;
656     
657     HRESULT CopyToDataCache(WCHAR** pTarget, WCHAR const* pSource);
658 #endif // FEATURE_WINDOWSPHONE
659
660 public:
661     CCLRErrorReportingManager();
662     ~CCLRErrorReportingManager();
663
664     STDMETHODIMP    QueryInterface(REFIID riid, void** ppv);
665     STDMETHODIMP_(ULONG) AddRef(void);
666     STDMETHODIMP_(ULONG) Release(void);
667
668     // ICLRErrorReportingManager APIs //
669     
670     // Get Watson bucket parameters for "current" exception (on calling thread).
671     STDMETHODIMP GetBucketParametersForCurrentException(BucketParameters *pParams);
672     STDMETHODIMP BeginCustomDump(   ECustomDumpFlavor dwFlavor,
673                                         DWORD dwNumItems,
674                                         CustomDumpItem items[],
675                                         DWORD dwReserved);
676     STDMETHODIMP EndCustomDump();
677     
678 #ifdef FEATURE_WINDOWSPHONE
679     // ICLRErrorReportingManager2 APIs //
680     
681     STDMETHODIMP SetApplicationData(ApplicationDataKey key, WCHAR const* pValue);
682     STDMETHODIMP SetBucketParametersForUnhandledException(BucketParameters const* pBucketParams, DWORD* pCountParams);
683     
684     // internal APIs
685     
686     // returns the application data for the specified key if available, else returns NULL.
687     WCHAR const* GetApplicationData(ApplicationDataKey key);
688     
689     // returns bucket parameter override data if available, else returns NULL.
690     WCHAR const* GetBucketParamOverride(BucketParameterIndex bucketParamId);
691 #endif // FEATURE_WINDOWSPHONE
692 };
693
694 extern CCLRErrorReportingManager g_CLRErrorReportingManager;
695 #endif // defined(FEATURE_INCLUDE_ALL_INTERFACES) || defined(FEATURE_WINDOWSPHONE)
696
697 #ifdef FEATURE_IPCMAN
698 // @TODO:: a-meicht
699 // consolidate the following class with DebuggerManager.
700 //
701 class CCLRSecurityAttributeManager
702 {
703 public:
704
705     // Set ACL on shared section, events, and process
706     STDMETHODIMP SetDACL(PACL pacl);
707
708     // Returning the current ACL that CLR is using
709     STDMETHODIMP GetDACL(PACL *pacl);
710
711     static void ProcessInit();
712     static void ProcessCleanUp();
713
714     // retrieving Host security attribute setting. If host does not set it, default to
715     // our default policy.
716     static HRESULT GetHostSecurityAttributes(SECURITY_ATTRIBUTES **ppSA);
717     static void DestroyHostSecurityAttributes(SECURITY_ATTRIBUTES *pSA);
718
719     static CrstStatic          m_hostSAMutex;
720
721 private:
722     static PACL                m_pACL;
723
724     // Security attributes cached for the current process.
725     static SECURITY_ATTRIBUTES m_hostSA;
726     static SECURITY_DESCRIPTOR m_hostSD;
727
728     static HRESULT CopyACL(PACL pAclOriginal, PACL ppAclNew);
729 };
730 #endif // FEATURE_IPCMAN
731
732 class CorHost2 :
733     public CorRuntimeHostBase
734 #ifndef FEATURE_PAL    
735     , public IPrivateManagedExceptionReporting /* This interface is for internal Watson testing only*/
736 #endif // FEATURE_PAL    
737 #ifdef FEATURE_CORECLR
738     , public ICLRRuntimeHost2
739 #else
740     , public CorThreadpool
741     , public CorGCHost
742     , public CorConfiguration
743     , public CLRValidator
744     , public CorDebuggerInfo
745     , public ICLRRuntimeHost
746 #endif // FEATURE_CORECLR
747 #if defined(FEATURE_HOSTED_BINDER) && !defined(FEATURE_CORECLR)
748     , public ICLRPrivRuntime
749 #endif
750     , public CorExecutionManager
751 {
752     friend struct _DacGlobals;
753
754 public:
755     CorHost2();
756
757     // *** IUnknown methods ***
758     STDMETHODIMP    QueryInterface(REFIID riid, void** ppv);
759     STDMETHODIMP_(ULONG) AddRef(void)
760     {
761         WRAPPER_NO_CONTRACT;
762         return CorRuntimeHostBase::AddRef();
763     }
764     STDMETHODIMP_(ULONG) Release(void);
765
766
767     // *** ICorRuntimeHost methods ***
768 #ifndef FEATURE_CORECLR
769     // Returns an object for configuring the runtime prior to
770     // it starting. If the runtime has been initialized this
771     // routine returns an error. See ICorConfiguration.
772     STDMETHODIMP GetConfiguration(ICorConfiguration** pConfiguration);
773 #endif // FEATURE_CORECLR
774
775 #ifndef FEATURE_PAL    
776     // defined in IPrivateManagedExceptionReporting interface for internal Watson testing only
777     STDMETHODIMP GetBucketParametersForCurrentException(BucketParameters *pParams);
778 #endif // FEATURE_PAL    
779
780     // Starts the runtime. This is equivalent to CoInitializeCor().
781     STDMETHODIMP Start();
782     STDMETHODIMP Stop();
783
784     STDMETHODIMP ExecuteInAppDomain(DWORD dwAppDomainId,
785                                     FExecuteInAppDomainCallback pCallback,
786                                     void * cookie);
787
788     STDMETHODIMP LocksHeldByLogicalThread(      // Return code.
789         DWORD *pCount                           // [out] Number of locks that the current thread holds.
790         )
791     {
792         WRAPPER_NO_CONTRACT;
793         return CorRuntimeHostBase::LocksHeldByLogicalThread(pCount);
794     }
795
796     // Class factory hook-up.
797     static HRESULT CreateObject(REFIID riid, void **ppUnk);
798
799     STDMETHODIMP MapFile(                       // Return code.
800         HANDLE     hFile,                       // [in]  Handle for file
801         HMODULE   *hMapAddress                  // [out] HINSTANCE for mapped file
802         )
803     {
804         WRAPPER_NO_CONTRACT;
805         return CorRuntimeHostBase::MapFile(hFile,hMapAddress);
806     }
807
808     STDMETHODIMP STDMETHODCALLTYPE SetHostControl(
809         IHostControl* pHostControl);
810
811     STDMETHODIMP STDMETHODCALLTYPE GetCLRControl(
812         ICLRControl** pCLRControl);
813
814     STDMETHODIMP UnloadAppDomain(DWORD dwDomainId, BOOL fWaitUntilDone);
815
816     STDMETHODIMP GetCurrentAppDomainId(DWORD *pdwAppDomainId);
817
818     STDMETHODIMP ExecuteApplication(LPCWSTR  pwzAppFullName,
819                                     DWORD    dwManifestPaths,
820                                     LPCWSTR  *ppwzManifestPaths,
821                                     DWORD    dwActivationData,
822                                     LPCWSTR  *ppwzActivationData,
823                                     int      *pReturnValue);
824
825     STDMETHODIMP ExecuteInDefaultAppDomain(LPCWSTR pwzAssemblyPath,
826                                            LPCWSTR pwzTypeName,
827                                            LPCWSTR pwzMethodName,
828                                            LPCWSTR pwzArgument,
829                                            DWORD   *pReturnValue);
830
831 #ifdef FEATURE_CORECLR
832     // *** ICLRRuntimeHost2 methods ***
833     STDMETHODIMP CreateAppDomainWithManager(
834         LPCWSTR wszFriendlyName,
835         DWORD  dwSecurityFlags,
836         LPCWSTR wszAppDomainManagerAssemblyName, 
837         LPCWSTR wszAppDomainManagerTypeName, 
838         int nProperties, 
839         LPCWSTR* pPropertyNames, 
840         LPCWSTR* pPropertyValues, 
841         DWORD* pAppDomainID);
842
843     STDMETHODIMP CreateDelegate(
844         DWORD appDomainID,
845         LPCWSTR wszAssemblyName,     
846         LPCWSTR wszClassName,     
847         LPCWSTR wszMethodName,
848         INT_PTR* fnPtr);
849
850     STDMETHODIMP Authenticate(ULONGLONG authKey);
851
852     STDMETHODIMP RegisterMacEHPort();
853     STDMETHODIMP SetStartupFlags(STARTUP_FLAGS flag);
854     STDMETHODIMP DllGetActivationFactory(
855         DWORD appDomainID, 
856         LPCWSTR wszTypeName,
857         IActivationFactory ** factory);
858
859     STDMETHODIMP ExecuteAssembly(
860         DWORD dwAppDomainId,
861         LPCWSTR pwzAssemblyPath,
862         int argc,
863         LPCWSTR* argv,
864         DWORD* pReturnValue);
865
866 #endif // !FEATURE_CORECLR
867
868 #if defined(FEATURE_HOSTED_BINDER) && !defined(FEATURE_CORECLR)
869     /**********************************************************************************
870      ** ICLRPrivRuntime Methods
871      **********************************************************************************/
872     STDMETHODIMP GetInterface(
873         REFCLSID rclsid,
874         REFIID   riid,
875         LPVOID * ppUnk);
876
877     STDMETHODIMP CreateAppDomain(
878         LPCWSTR pwzFriendlyName,
879         ICLRPrivBinder * pBinder,
880         LPDWORD pdwAppDomainId);
881
882     STDMETHODIMP CreateDelegate(
883         DWORD appDomainID,
884         LPCWSTR wszAssemblyName,
885         LPCWSTR wszClassName,
886         LPCWSTR wszMethodName,
887         LPVOID * ppvDelegate);
888
889     STDMETHODIMP ExecuteMain(
890         ICLRPrivBinder * pBinder,
891         int * pRetVal);
892
893 #endif // FEATURE_HOSTED_BINDER && !FEATURE_CORECLR
894
895     static IHostControl *GetHostControl ()
896     {
897         LIMITED_METHOD_CONTRACT;
898
899 #ifdef FEATURE_CORECLR
900         return NULL;
901 #else // FEATURE_CORECLR
902         return m_HostControl;
903 #endif // FEATURE_CORECLR
904     }
905
906 #ifdef FEATURE_INCLUDE_ALL_INTERFACES
907     static IHostMemoryManager *GetHostMemoryManager ()
908     {
909         LIMITED_METHOD_CONTRACT;
910
911 #ifdef FEATURE_CORECLR
912         return NULL;
913 #else // FEATURE_CORECLR
914         return m_HostMemoryManager;
915 #endif // FEATURE_CORECLR
916     }
917
918     static IHostMalloc *GetHostMalloc ()
919     {
920         LIMITED_METHOD_CONTRACT;
921
922 #ifdef FEATURE_CORECLR
923         return NULL;
924 #else // FEATURE_CORECLR
925         return m_HostMalloc;
926 #endif // FEATURE_CORECLR
927     }
928
929     static IHostTaskManager *GetHostTaskManager ()
930     {
931         LIMITED_METHOD_CONTRACT;
932
933 #ifdef FEATURE_CORECLR
934         return NULL;
935 #else // FEATURE_CORECLR
936         return m_HostTaskManager;
937 #endif // FEATURE_CORECLR
938     }
939
940     static IHostThreadpoolManager *GetHostThreadpoolManager ()
941     {
942         LIMITED_METHOD_CONTRACT;
943
944 #ifdef FEATURE_CORECLR
945         return NULL;
946 #else // FEATURE_CORECLR
947         return m_HostThreadpoolManager;
948 #endif // FEATURE_CORECLR
949     }
950
951     static IHostIoCompletionManager *GetHostIoCompletionManager ()
952     {
953         LIMITED_METHOD_CONTRACT;
954
955 #ifdef FEATURE_CORECLR
956         return NULL;
957 #else // FEATURE_CORECLR
958         return m_HostIoCompletionManager;
959 #endif // FEATURE_CORECLR
960     }
961
962     static IHostSyncManager *GetHostSyncManager ()
963     {
964         LIMITED_METHOD_CONTRACT;
965
966 #ifdef FEATURE_CORECLR
967         return NULL;
968 #else // FEATURE_CORECLR
969         return m_HostSyncManager;
970 #endif // FEATURE_CORECLR
971     }
972
973     static IHostAssemblyManager *GetHostAssemblyManager()
974     {
975         LIMITED_METHOD_CONTRACT;
976
977 #ifdef FEATURE_CORECLR
978         return NULL;
979 #else // FEATURE_CORECLR
980         return m_HostAssemblyManager;
981 #endif // FEATURE_CORECLR
982     }
983
984     static IHostGCManager *GetHostGCManager()
985     {
986         LIMITED_METHOD_CONTRACT;
987
988 #ifdef FEATURE_CORECLR
989         return NULL;
990 #else // FEATURE_CORECLR
991         return m_HostGCManager;
992 #endif // FEATURE_CORECLR
993     }
994
995     static IHostSecurityManager *GetHostSecurityManager()
996     {
997         LIMITED_METHOD_CONTRACT;
998
999 #ifdef FEATURE_CORECLR
1000         return NULL;
1001 #else // FEATURE_CORECLR
1002         return m_HostSecurityManager;
1003 #endif // FEATURE_CORECLR
1004     }
1005
1006     static IHostPolicyManager *GetHostPolicyManager ()
1007     {
1008         LIMITED_METHOD_CONTRACT;
1009
1010 #ifdef FEATURE_CORECLR
1011         return NULL;
1012 #else // FEATURE_CORECLR
1013         return m_HostPolicyManager;
1014 #endif // FEATURE_CORECLR
1015     }
1016 #endif // FEATURE_INCLUDE_ALL_INTERFACES
1017
1018 #ifdef FEATURE_LEGACYNETCF_DBG_HOST_CONTROL
1019     static IHostNetCFDebugControlManager *GetHostNetCFDebugControlManager ()
1020     {
1021         LIMITED_METHOD_CONTRACT;
1022         return m_HostNetCFDebugControlManager;
1023     }
1024 #endif
1025
1026     static int GetHostOverlappedExtensionSize()
1027     {
1028         LIMITED_METHOD_CONTRACT;
1029
1030 #ifdef FEATURE_CORECLR
1031         return 0;
1032 #else // FEATURE_CORECLR
1033         _ASSERTE (m_HostOverlappedExtensionSize != -1);
1034         return m_HostOverlappedExtensionSize;
1035 #endif // FEATURE_CORECLR
1036     }
1037
1038 #ifdef FEATURE_INCLUDE_ALL_INTERFACES
1039     static ICLRAssemblyReferenceList *GetHostDomainNeutralAsms()
1040     {
1041         LIMITED_METHOD_CONTRACT;
1042
1043 #ifdef FEATURE_CORECLR
1044         return NULL;
1045 #else // FEATURE_CORECLR
1046         return m_pHostDomainNeutralAsms;
1047 #endif // FEATURE_CORECLR
1048     }
1049 #endif // FEATURE_INCLUDE_ALL_INTERFACES
1050
1051 #ifndef FEATURE_CORECLR
1052     static HRESULT SetFlagsAndHostConfig(STARTUP_FLAGS dwStartupFlags, LPCWSTR pwzHostConfigFile, BOOL fFinalize);
1053     static LPCWSTR GetHostConfigFile();
1054
1055     static void GetDefaultAppDomainProperties(StringArrayList **ppPropertyNames, StringArrayList **ppPropertyValues);
1056 #endif // !FEATURE_CORECLR
1057
1058     static STARTUP_FLAGS GetStartupFlags();
1059
1060 #ifndef FEATURE_CORECLR
1061     static HRESULT SetPropertiesForDefaultAppDomain(DWORD nProperties,
1062                                                     __in_ecount(nProperties) LPCWSTR *pwszPropertyNames,
1063                                                     __in_ecount(nProperties) LPCWSTR *pwszPropertyValues);
1064
1065     static HRESULT SetAppDomainManagerType(LPCWSTR wszAppDomainManagerAssembly,
1066                                            LPCWSTR wszAppDomainManagerType,
1067                                            EInitializeNewDomainFlags dwInitializeDomainFlags);
1068 #endif // FEATURE_CORECLR
1069
1070     static LPCWSTR GetAppDomainManagerAsm();
1071
1072     static LPCWSTR GetAppDomainManagerType();
1073
1074     static EInitializeNewDomainFlags GetAppDomainManagerInitializeNewDomainFlags();
1075
1076     static BOOL HasAppDomainManagerInfo()
1077     {
1078         LIMITED_METHOD_CONTRACT;
1079         return GetAppDomainManagerAsm() != NULL && GetAppDomainManagerType() != NULL;
1080     }
1081
1082     static BOOL HasStarted()
1083     {
1084         return m_RefCount != 0;
1085     }
1086     
1087     static BOOL IsLoadFromBlocked(); // LoadFrom, LoadFile and Load(byte[]) are blocked in certain hosting scenarios
1088
1089 private:
1090 #ifdef FEATURE_CORECLR
1091     // This flag indicates if this instance was the first to load and start CoreCLR
1092     BOOL m_fFirstToLoadCLR;
1093
1094     // This flag indicates if the host has authenticated with us or not
1095     BOOL m_fIsHostAuthenticated;
1096
1097 #endif // FEATURE_CORECLR
1098
1099 #if defined(FEATURE_CORECLR) || defined(FEATURE_HOSTED_BINDER)
1100     // Helpers for both ICLRRuntimeHost2 and ICLRPrivRuntime
1101     HRESULT _CreateAppDomain(
1102         LPCWSTR wszFriendlyName,
1103         DWORD  dwFlags,
1104         LPCWSTR wszAppDomainManagerAssemblyName, 
1105         LPCWSTR wszAppDomainManagerTypeName, 
1106         int nProperties, 
1107         LPCWSTR* pPropertyNames, 
1108         LPCWSTR* pPropertyValues,
1109 #if defined(FEATURE_HOSTED_BINDER) && !defined(FEATURE_CORECLR)
1110         ICLRPrivBinder* pBinder,
1111 #endif
1112         DWORD* pAppDomainID);
1113
1114     HRESULT _CreateDelegate(
1115         DWORD appDomainID,
1116         LPCWSTR wszAssemblyName,     
1117         LPCWSTR wszClassName,     
1118         LPCWSTR wszMethodName,
1119         INT_PTR* fnPtr);
1120 #endif // defined(FEATURE_CORECLR) || defined(FEATURE_HOSTED_BINDER)
1121
1122 #ifdef FEATURE_HOSTED_BINDER
1123     // entrypoint helper to be wrapped in a filter to process unhandled exceptions
1124     VOID ExecuteMainInner(Assembly* pRootAssembly);
1125 #endif // FEATURE_HOSTED_BINDER
1126
1127     static LONG  m_RefCount;
1128
1129     static IHostControl *m_HostControl;
1130
1131     static LPCWSTR s_wszAppDomainManagerAsm;
1132     static LPCWSTR s_wszAppDomainManagerType;
1133     static EInitializeNewDomainFlags s_dwDomainManagerInitFlags;
1134
1135 #if !defined(FEATURE_CORECLR)
1136     static StringArrayList s_defaultDomainPropertyNames;
1137     static StringArrayList s_defaultDomainPropertyValues;
1138
1139 protected:
1140     static IHostMemoryManager *m_HostMemoryManager;
1141     static IHostMalloc *m_HostMalloc;
1142     static IHostTaskManager  *m_HostTaskManager;
1143     static IHostThreadpoolManager *m_HostThreadpoolManager;
1144     static IHostIoCompletionManager *m_HostIoCompletionManager;
1145     static IHostSyncManager *m_HostSyncManager;
1146     static IHostAssemblyManager *m_HostAssemblyManager;
1147     static IHostGCManager *m_HostGCManager;
1148     static IHostSecurityManager *m_HostSecurityManager;
1149     static IHostPolicyManager *m_HostPolicyManager;
1150     static int m_HostOverlappedExtensionSize;
1151     static ICLRAssemblyReferenceList *m_pHostDomainNeutralAsms;
1152
1153     static WCHAR m_wzHostConfigFile[_MAX_PATH];
1154
1155     static BOOL m_dwFlagsFinalized;
1156     static DangerousNonHostedSpinLock m_FlagsLock; // protects the flags and host config
1157 #endif // !defined(FEATURE_CORECLR)
1158
1159 #ifdef FEATURE_LEGACYNETCF_DBG_HOST_CONTROL
1160 protected:
1161     static IHostNetCFDebugControlManager *m_HostNetCFDebugControlManager;
1162 #endif
1163
1164     SVAL_DECL(STARTUP_FLAGS, m_dwStartupFlags);
1165 };
1166
1167 #ifdef FEATURE_INCLUDE_ALL_INTERFACES
1168 class CorHostProtectionManager : public ICLRHostProtectionManager
1169 #else // !FEATURE_INCLUDE_ALL_INTERFACES
1170 class CorHostProtectionManager
1171 #endif // FEATURE_INCLUDE_ALL_INTERFACES
1172 {
1173 private:
1174     EApiCategories m_eProtectedCategories;
1175     bool m_fEagerSerializeGrantSet;
1176     bool m_fFrozen;
1177
1178 public:
1179     CorHostProtectionManager();
1180
1181     // IUnknown methods
1182     HRESULT STDMETHODCALLTYPE QueryInterface(
1183         REFIID id,
1184         void **pInterface);
1185     ULONG STDMETHODCALLTYPE AddRef();
1186     ULONG STDMETHODCALLTYPE Release();
1187
1188     // Interface methods
1189     virtual HRESULT STDMETHODCALLTYPE SetProtectedCategories(/* [in] */ EApiCategories eFullTrustOnlyResources);
1190     virtual HRESULT STDMETHODCALLTYPE SetEagerSerializeGrantSets();
1191
1192     // Getters
1193     EApiCategories GetProtectedCategories();
1194     bool GetEagerSerializeGrantSets() const;
1195
1196     void Freeze();
1197 };
1198
1199 #ifdef FEATURE_COMINTEROP
1200 extern "C"
1201 HRESULT STDMETHODCALLTYPE DllGetActivationFactoryImpl(
1202                                                       LPCWSTR wszAssemblyName, 
1203                                                       LPCWSTR wszTypeName, 
1204                                                       LPCWSTR wszCodeBase,
1205                                                       IActivationFactory ** factory);
1206
1207 #endif // defined(FEATURE_COMINTEROP)
1208
1209 extern SIZE_T Host_SegmentSize;
1210 extern SIZE_T Host_MaxGen0Size;
1211 extern BOOL  Host_fSegmentSizeSet;
1212 extern BOOL  Host_fMaxGen0SizeSet;
1213
1214 #define PARTIAL_TRUST_VISIBLE_ASSEMBLIES_PROPERTY W("PARTIAL_TRUST_VISIBLE_ASSEMBLIES")
1215 #endif // __CorHost__h__