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: CORPRIV.H
6//
7// ===========================================================================
8
9#ifndef _CORPRIV_H_
10#define _CORPRIV_H_
11#if _MSC_VER >= 1000
12#pragma once
13#endif // _MSC_VER >= 1000
14
15// %%Includes: ---------------------------------------------------------------
16// avoid taking DLL import hit on intra-DLL calls
17#define NODLLIMPORT
18#include <daccess.h>
19#include "cor.h"
20#include "corimage.h"
21#include "metadata.h"
22#include <sstring.h>
23#include "peinformation.h"
24//
25
26interface IAssemblyName;
27
28// PE images loaded through the runtime.
29typedef struct _dummyCOR { BYTE b; } *HCORMODULE;
30
31class UTSemReadWrite;
32
33// Helper function to get a pointer to the Dispenser interface.
34STDAPI MetaDataGetDispenser( // Return HRESULT
35 REFCLSID rclsid, // The class to desired.
36 REFIID riid, // Interface wanted on class factory.
37 LPVOID FAR *ppv); // Return interface pointer here.
38
39// Helper function to check whether policy allows accessing the file
40STDAPI RuntimeCheckLocationAccess(LPCWSTR wszLocation);
41STDAPI RuntimeIsNativeImageOptedOut(IAssemblyName* pAssemblyDef);
42
43LocaleID RuntimeGetFileSystemLocale();
44
45BOOL RuntimeFileNotFound(HRESULT hr);
46
47// Helper function to get an Internal interface with an in-memory metadata section
48STDAPI GetMetaDataInternalInterface(
49 LPVOID pData, // [IN] in memory metadata section
50 ULONG cbData, // [IN] size of the metadata section
51 DWORD flags, // [IN] CorOpenFlags
52 REFIID riid, // [IN] desired interface
53 void **ppv); // [OUT] returned interface
54
55// Helper function to get an internal scopeless interface given a scope.
56STDAPI GetMetaDataInternalInterfaceFromPublic(
57 IUnknown *pv, // [IN] Given interface
58 REFIID riid, // [IN] desired interface
59 void **ppv); // [OUT] returned interface
60
61// Helper function to get an internal scopeless interface given a scope.
62STDAPI GetMetaDataPublicInterfaceFromInternal(
63 void *pv, // [IN] Given interface
64 REFIID riid, // [IN] desired interface
65 void **ppv); // [OUT] returned interface
66
67// Converts an internal MD import API into the read/write version of this API.
68// This could support edit and continue, or modification of the metadata at
69// runtime (say for profiling).
70STDAPI ConvertMDInternalImport( // S_OK or error.
71 IMDInternalImport *pIMD, // [IN] The metadata to be updated.
72 IMDInternalImport **ppIMD); // [OUT] Put RW interface here.
73
74STDAPI GetAssemblyMDInternalImport( // Return code.
75 LPCWSTR szFileName, // [IN] The scope to open.
76 REFIID riid, // [IN] The interface desired.
77 IUnknown **ppIUnk); // [OUT] Return interface on success.
78
79HRESULT GetAssemblyMDInternalImportFromImage(
80 HCORMODULE hImage, //[IN] pointer to module handle to get the metadata from.
81 REFIID riid, //[IN] The interface desired.
82 IUnknown **ppIUnk); //[OUT] Return Interface on success.
83
84STDAPI GetAssemblyMDInternalImportByStream( // Return code.
85 IStream *pIStream, // [IN] The IStream for the file
86 UINT64 AssemblyId, // [IN] Unique Id for the assembly
87 REFIID riid, // [IN] The interface desired.
88 IUnknown **ppIUnk); // [OUT] Return interface on success.
89
90
91enum MDInternalImportFlags
92{
93 MDInternalImport_Default = 0,
94 MDInternalImport_NoCache = 1, // Do not share/cached the results of opening the image
95#ifdef FEATURE_PREJIT
96 MDInternalImport_TrustedNativeImage = 2, // The image is a native image, and so its format can be trusted
97 MDInternalImport_ILMetaData = 4, // Open the IL metadata, even if this is a native image
98 MDInternalImport_TrustedNativeImage_and_IL = MDInternalImport_TrustedNativeImage | MDInternalImport_ILMetaData,
99 MDInternalImport_NativeImageInstall = 0x100, // The image is a native image that is being installed into NIC
100#endif
101 MDInternalImport_CheckLongPath =8, // also check long version of the path
102 MDInternalImport_CheckShortPath =0x10, // also check long version of the path
103 MDInternalImport_OnlyLookInCache =0x20, // Only look in the cache. (If the cache does not have the image already loaded, return NULL)
104}; // enum MDInternalImportFlags
105
106
107
108STDAPI GetAssemblyMDInternalImportEx( // Return code.
109 LPCWSTR szFileName, // [IN] The scope to open.
110 REFIID riid, // [IN] The interface desired.
111 MDInternalImportFlags flags, // [in] Flags to control opening the assembly
112 IUnknown **ppIUnk, // [OUT] Return interface on success.
113 HANDLE hFile = INVALID_HANDLE_VALUE);
114
115STDAPI GetAssemblyMDInternalImportByStreamEx( // Return code.
116 IStream *pIStream, // [IN] The IStream for the file
117 UINT64 AssemblyId, // [IN] Unique Id for the assembly
118 REFIID riid, // [IN] The interface desired.
119 MDInternalImportFlags flags, // [in] Flags to control opening the assembly
120 IUnknown **ppIUnk); // [OUT] Return interface on success.
121
122
123// Returns part of the "Zap string" which describes the properties of a native image
124
125__success(SUCCEEDED(return))
126STDAPI GetNativeImageDescription(
127 __in_z LPCWSTR wzCustomString, // [IN] Custom string of the native image
128 DWORD dwConfigMask, // [IN] Config mask of the native image
129 __out_ecount_part_opt(*pdwLength,*pdwLength) LPWSTR pwzZapInfo,// [OUT] The description string. Can be NULL to find the size of buffer to allocate
130 LPDWORD pdwLength); // [IN/OUT] Length of the pwzZapInfo buffer on IN.
131 // Number of WCHARs (including termintating NULL) on OUT
132
133
134class CQuickBytes;
135
136
137// predefined constant for parent token for global functions
138#define COR_GLOBAL_PARENT_TOKEN TokenFromRid(1, mdtTypeDef)
139
140
141
142//////////////////////////////////////////////////////////////////////////
143//
144//////////////////////////////////////////////////////////////////////////
145
146// %%Interfaces: -------------------------------------------------------------
147
148// interface IMetaDataHelper
149
150// {AD93D71D-E1F2-11d1-9409-0000F8083460}
151EXTERN_GUID(IID_IMetaDataHelper, 0xad93d71d, 0xe1f2, 0x11d1, 0x94, 0x9, 0x0, 0x0, 0xf8, 0x8, 0x34, 0x60);
152
153#undef INTERFACE
154#define INTERFACE IMetaDataHelper
155DECLARE_INTERFACE_(IMetaDataHelper, IUnknown)
156{
157 // helper functions
158 // This function is exposing the ability to translate signature from a given
159 // source scope to a given target scope.
160 //
161 STDMETHOD(TranslateSigWithScope)(
162 IMetaDataAssemblyImport *pAssemImport, // [IN] importing assembly interface
163 const void *pbHashValue, // [IN] Hash Blob for Assembly.
164 ULONG cbHashValue, // [IN] Count of bytes.
165 IMetaDataImport *import, // [IN] importing interface
166 PCCOR_SIGNATURE pbSigBlob, // [IN] signature in the importing scope
167 ULONG cbSigBlob, // [IN] count of bytes of signature
168 IMetaDataAssemblyEmit *pAssemEmit, // [IN] emit assembly interface
169 IMetaDataEmit *emit, // [IN] emit interface
170 PCOR_SIGNATURE pvTranslatedSig, // [OUT] buffer to hold translated signature
171 ULONG cbTranslatedSigMax,
172 ULONG *pcbTranslatedSig) PURE;// [OUT] count of bytes in the translated signature
173
174 STDMETHOD(GetMetadata)(
175 ULONG ulSelect, // [IN] Selector.
176 void **ppData) PURE; // [OUT] Put pointer to data here.
177
178 STDMETHOD_(IUnknown *, GetCachedInternalInterface)(BOOL fWithLock) PURE; // S_OK or error
179 STDMETHOD(SetCachedInternalInterface)(IUnknown * pUnk) PURE; // S_OK or error
180 STDMETHOD_(UTSemReadWrite*, GetReaderWriterLock)() PURE; // return the reader writer lock
181 STDMETHOD(SetReaderWriterLock)(UTSemReadWrite * pSem) PURE;
182}; // IMetaDataHelper
183
184
185EXTERN_GUID(IID_IMetaDataEmitHelper, 0x5c240ae4, 0x1e09, 0x11d3, 0x94, 0x24, 0x0, 0x0, 0xf8, 0x8, 0x34, 0x60);
186
187#undef INTERFACE
188#define INTERFACE IMetaDataEmitHelper
189DECLARE_INTERFACE_(IMetaDataEmitHelper, IUnknown)
190{
191 // emit helper functions
192 STDMETHOD(DefineMethodSemanticsHelper)(
193 mdToken tkAssociation, // [IN] property or event token
194 DWORD dwFlags, // [IN] semantics
195 mdMethodDef md) PURE; // [IN] method to associated with
196
197 STDMETHOD(SetFieldLayoutHelper)( // Return hresult.
198 mdFieldDef fd, // [IN] field to associate the layout info
199 ULONG ulOffset) PURE; // [IN] the offset for the field
200
201 STDMETHOD(DefineEventHelper) (
202 mdTypeDef td, // [IN] the class/interface on which the event is being defined
203 LPCWSTR szEvent, // [IN] Name of the event
204 DWORD dwEventFlags, // [IN] CorEventAttr
205 mdToken tkEventType, // [IN] a reference (mdTypeRef or mdTypeRef) to the Event class
206 mdEvent *pmdEvent) PURE; // [OUT] output event token
207
208 STDMETHOD(AddDeclarativeSecurityHelper) (
209 mdToken tk, // [IN] Parent token (typedef/methoddef)
210 DWORD dwAction, // [IN] Security action (CorDeclSecurity)
211 void const *pValue, // [IN] Permission set blob
212 DWORD cbValue, // [IN] Byte count of permission set blob
213 mdPermission*pmdPermission) PURE; // [OUT] Output permission token
214
215 STDMETHOD(SetResolutionScopeHelper)( // Return hresult.
216 mdTypeRef tr, // [IN] TypeRef record to update
217 mdToken rs) PURE; // [IN] new ResolutionScope
218
219 STDMETHOD(SetManifestResourceOffsetHelper)( // Return hresult.
220 mdManifestResource mr, // [IN] The manifest token
221 ULONG ulOffset) PURE; // [IN] new offset
222
223 STDMETHOD(SetTypeParent)( // Return hresult.
224 mdTypeDef td, // [IN] Type definition
225 mdToken tkExtends) PURE; // [IN] parent type
226
227 STDMETHOD(AddInterfaceImpl)( // Return hresult.
228 mdTypeDef td, // [IN] Type definition
229 mdToken tkInterface) PURE; // [IN] interface type
230
231}; // IMetaDataEmitHelper
232
233//////////////////////////////////////////////////////////////////////////////
234// enum CorElementTypeZapSig defines some additional internal ELEMENT_TYPE's
235// values that are only used by ZapSig signatures.
236//////////////////////////////////////////////////////////////////////////////
237typedef enum CorElementTypeZapSig
238{
239 // ZapSig encoding for ELEMENT_TYPE_VAR and ELEMENT_TYPE_MVAR. It is always followed
240 // by the RID of a GenericParam token, encoded as a compressed integer.
241 ELEMENT_TYPE_VAR_ZAPSIG = 0x3b,
242
243 // ZapSig encoding for an array MethodTable to allow it to remain such after decoding
244 // (rather than being transformed into the TypeHandle representing that array)
245 //
246 // The element is always followed by ELEMENT_TYPE_SZARRAY or ELEMENT_TYPE_ARRAY
247 ELEMENT_TYPE_NATIVE_ARRAY_TEMPLATE_ZAPSIG = 0x3c,
248
249 // ZapSig encoding for native value types in IL stubs. IL stub signatures may contain
250 // ELEMENT_TYPE_INTERNAL followed by ParamTypeDesc with ELEMENT_TYPE_VALUETYPE element
251 // type. It acts like a modifier to the underlying structure making it look like its
252 // unmanaged view (size determined by unmanaged layout, blittable, no GC pointers).
253 //
254 // ELEMENT_TYPE_NATIVE_VALUETYPE_ZAPSIG is used when encoding such types to NGEN images.
255 // The signature looks like this: ET_NATIVE_VALUETYPE_ZAPSIG ET_VALUETYPE <token>.
256 // See code:ZapSig.GetSignatureForTypeHandle and code:SigPointer.GetTypeHandleThrowing
257 // where the encoding/decoding takes place.
258 ELEMENT_TYPE_NATIVE_VALUETYPE_ZAPSIG = 0x3d,
259
260 ELEMENT_TYPE_CANON_ZAPSIG = 0x3e, // zapsig encoding for [mscorlib]System.__Canon
261 ELEMENT_TYPE_MODULE_ZAPSIG = 0x3f, // zapsig encoding for external module id#
262
263} CorElementTypeZapSig;
264
265typedef enum CorCallingConventionInternal
266{
267 // IL stub signatures containing types that need to be restored have the highest
268 // bit of the calling convention set.
269 IMAGE_CEE_CS_CALLCONV_NEEDSRESTORE = 0x80,
270
271} CorCallingConventionInternal;
272
273//////////////////////////////////////////////////////////////////////////
274// Obsoleted ELEMENT_TYPE values which are not supported anymore.
275// They are not part of CLI ECMA spec, they were only experimental before v1.0 RTM.
276// They are needed for indexing arrays initialized using file:corTypeInfo.h
277// 0x17 ... VALUEARRAY <type> <bound>
278// 0x1a ... CPU native floating-point type
279//////////////////////////////////////////////////////////////////////////
280#define ELEMENT_TYPE_VALUEARRAY_UNSUPPORTED ((CorElementType) 0x17)
281#define ELEMENT_TYPE_R_UNSUPPORTED ((CorElementType) 0x1a)
282
283// Use this guid in the SetOption if Reflection.Emit wants to control size of the initially allocated
284// MetaData. See values: code:CorMetaDataInitialSize.
285//
286// {2675b6bf-f504-4cb4-a4d5-084eea770ddc}
287EXTERN_GUID(MetaDataInitialSize, 0x2675b6bf, 0xf504, 0x4cb4, 0xa4, 0xd5, 0x08, 0x4e, 0xea, 0x77, 0x0d, 0xdc);
288
289// Allowed values for code:MetaDataInitialSize option.
290typedef enum CorMetaDataInitialSize
291{
292 MDInitialSizeDefault = 0,
293 MDInitialSizeMinimal = 1
294} CorMetaDataInitialSize;
295
296// Internal extension of open flags code:CorOpenFlags
297typedef enum CorOpenFlagsInternal
298{
299#ifdef FEATURE_METADATA_LOAD_TRUSTED_IMAGES
300 // Flag code:ofTrustedImage is used by mscordbi.dll, therefore defined in file:CorPriv.h
301 ofTrustedImage = ofReserved3 // We trust this PE file (we are willing to do a LoadLibrary on it).
302 // It is optional and only an (VM) optimization - typically for NGEN images
303 // opened by debugger.
304#endif
305} CorOpenFlagsInternal;
306
307#ifdef FEATURE_METADATA_LOAD_TRUSTED_IMAGES
308#define IsOfTrustedImage(x) ((x) & ofTrustedImage)
309#endif
310
311// %%Classes: ----------------------------------------------------------------
312#ifndef lengthof
313#define lengthof(rg) (sizeof(rg)/sizeof(rg[0]))
314#endif
315
316#define COR_MODULE_CLASS "<Module>"
317#define COR_WMODULE_CLASS W("<Module>")
318
319STDAPI RuntimeOpenImage(LPCWSTR pszFileName, HCORMODULE* hHandle);
320STDAPI RuntimeOpenImageInternal(LPCWSTR pszFileName, HCORMODULE* hHandle,
321 DWORD *pdwLength, MDInternalImportFlags flags, HANDLE hFile = INVALID_HANDLE_VALUE);
322STDAPI RuntimeOpenImageByStream(IStream* pIStream, UINT64 AssemblyId, DWORD dwModuleId,
323 HCORMODULE* hHandle, DWORD *pdwLength, MDInternalImportFlags flags);
324
325void RuntimeAddRefHandle(HCORMODULE hHandle);
326STDAPI RuntimeReleaseHandle(HCORMODULE hHandle);
327STDAPI RuntimeGetImageBase(HCORMODULE hHandle, LPVOID* base, BOOL bMapped, COUNT_T* dwSize);
328STDAPI RuntimeGetImageKind(HCORMODULE hHandle, DWORD* pdwKind, DWORD* pdwMachine);
329STDAPI RuntimeOSHandle(HCORMODULE hHandle, HMODULE* hModule);
330STDAPI RuntimeGetAssemblyStrongNameHashForModule(HCORMODULE hModule,
331 IMetaDataImport *pMDimport,
332 BYTE *pbSNHash,
333 DWORD *pcbSNHash);
334STDAPI RuntimeGetMDInternalImport(HCORMODULE hHandle,
335 MDInternalImportFlags flags,
336 IMDInternalImport** ppMDImport);
337
338FORCEINLINE
339void ReleaseHCorModule(HCORMODULE hModule)
340{
341 HRESULT hr = RuntimeReleaseHandle(hModule);
342 _ASSERTE(SUCCEEDED(hr));
343}
344
345typedef Wrapper<HCORMODULE, DoNothing<HCORMODULE>, ReleaseHCorModule, (UINT_PTR) NULL> HCORMODULEHolder;
346
347
348// ===========================================================================
349// ISNAssemblySignature (similar to IAssemblySignature in V1)
350//
351// This is a private interface that allows querying of the strong name
352// signature.
353// This can be used for (strong-named) assemblies added to the GAC as
354// a unique identifier.
355//
356
357// {848845BC-0C4A-42e3-8915-DC850112443D}
358EXTERN_GUID(IID_ISNAssemblySignature, 0x848845BC, 0x0C4A, 0x42e3, 0x89, 0x15, 0xDC, 0x85, 0x01, 0x12, 0x44, 0x3D);
359
360#undef INTERFACE
361#define INTERFACE ISNAssemblySignature
362DECLARE_INTERFACE_(ISNAssemblySignature, IUnknown)
363{
364 // Returns the strong-name signature if the assembly is strong-name-signed
365 // Returns the MVID if the assembly is delay-signed.
366 // Fails if the assembly is not signed at all.
367 STDMETHOD(GetSNAssemblySignature) (
368 BYTE *pbSig, // [IN, OUT] Buffer to write signature
369 DWORD *pcbSig // [IN, OUT] Size of buffer, bytes written
370 ) PURE;
371};
372
373//-------------------------------------
374//--- ICeeGenInternal
375//-------------------------------------
376// {9fd3c7af-dc4e-4b9b-be22-9cf8cc577489}
377EXTERN_GUID(IID_ICeeGenInternal, 0x9fd3c7af, 0xdc4e, 0x4b9b, 0xbe, 0x22, 0x9c, 0xf8, 0xcc, 0x57, 0x74, 0x89);
378
379#undef INTERFACE
380#define INTERFACE ICeeGenInternal
381DECLARE_INTERFACE_(ICeeGenInternal, IUnknown)
382{
383 STDMETHOD (SetInitialGrowth) (DWORD growth) PURE;
384};
385
386//
387// IGetIMDInternalImport
388//
389// Private interface exposed by
390// AssemblyMDInternalImport - gives us access to the internally stored IMDInternalImport*.
391//
392// RegMeta, WinMDImport - supports the internal GetMetaDataInternalInterfaceFromPublic() "api".
393//
394// {92B2FEF9-F7F5-420d-AD42-AECEEE10A1EF}
395EXTERN_GUID(IID_IGetIMDInternalImport, 0x92b2fef9, 0xf7f5, 0x420d, 0xad, 0x42, 0xae, 0xce, 0xee, 0x10, 0xa1, 0xef);
396#undef INTERFACE
397#define INTERFACE IGetIMDInternalImport
398DECLARE_INTERFACE_(IGetIMDInternalImport, IUnknown)
399{
400 STDMETHOD(GetIMDInternalImport) (
401 IMDInternalImport ** ppIMDInternalImport // [OUT] Buffer to receive IMDInternalImport*
402 ) PURE;
403};
404
405// ===========================================================================
406#ifdef FEATURE_PREJIT
407// ===========================================================================
408
409// Use the default JIT compiler
410#define DEFAULT_NGEN_COMPILER_DLL_NAME W("clrjit.dll")
411
412#ifndef DACCESS_COMPILE
413
414/* --------------------------------------------------------------------------- *
415 * NGen logger
416 * --------------------------------------------------------------------------- */
417 #include "mscorsvc.h"
418
419struct ICorSvcLogger;
420class SvcLogger
421{
422public:
423
424 SvcLogger();
425 ~SvcLogger();
426 void ReleaseLogger();
427 void SetSvcLogger(ICorSvcLogger *pCorSvcLoggerArg);
428 BOOL HasSvcLogger();
429 ICorSvcLogger* GetSvcLogger();
430 void Printf(const CHAR *format, ...);
431 void SvcPrintf(const CHAR *format, ...);
432 void Printf(const WCHAR *format, ...);
433 void Printf(CorSvcLogLevel logLevel, const WCHAR *format, ...);
434 void SvcPrintf(const WCHAR *format, ...);
435 void Log(const WCHAR *message, CorSvcLogLevel logLevel = LogLevel_Warning);
436 //Need to add this to allocate StackSString, as we don't want static class
437
438private:
439
440 void LogHelper(SString s, CorSvcLogLevel logLevel = LogLevel_Success);
441 //instantiations that need VM services like contracts in dllmain.
442 void CheckInit();
443
444 StackSString* pss;
445 ICorSvcLogger *pCorSvcLogger;
446}; // class SvcLogger
447
448SvcLogger *GetSvcLogger();
449BOOL HasSvcLogger();
450#endif // #ifndef DACCESS_COMPILE
451
452// ===========================================================================
453#endif // #ifdef FEATURE_PREJIT
454// ===========================================================================
455
456struct CORCOMPILE_ASSEMBLY_SIGNATURE;
457struct CORCOMPILE_VERSION_INFO;
458struct CORCOMPILE_DEPENDENCY;
459typedef GUID CORCOMPILE_NGEN_SIGNATURE;
460
461
462//**********************************************************************
463// Internal versions of shim functions for use by the CLR.
464
465STDAPI GetCORSystemDirectoryInternaL(
466 SString& pBuffer
467 );
468
469//LONGPATH:TODO: Remove this once Desktop usage has been removed
470STDAPI GetCORSystemDirectoryInternal(
471 __out_ecount_part_opt(cchBuffer, *pdwLength) LPWSTR pBuffer,
472 DWORD cchBuffer,
473 __out_opt DWORD* pdwLength
474 );
475
476STDAPI GetCORVersionInternal(
477 __out_ecount_z_opt(cchBuffer) LPWSTR pBuffer,
478 DWORD cchBuffer,
479 __out DWORD *pdwLength);
480
481#endif // _CORPRIV_H_
482// EOF =======================================================================
483
484