summaryrefslogtreecommitdiff
path: root/src/inc/zapper.h
blob: 26ac7e8f14a39142801fa1073e511b9bea535739 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// 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.

#ifndef ZAPPER_H_
#define ZAPPER_H_

#include <winwrap.h>
#include <windows.h>
#include <stdlib.h>
#include <objbase.h>
#include <stddef.h>
#include <float.h>
#include <limits.h>

#include "sarray.h"
#include "sstring.h"
#include "shash.h"
#include "utilcode.h"
#include "corjit.h"
#ifdef FEATURE_FUSION
#include "binderngen.h"
#endif
#include "corcompile.h"
#include "corhlprpriv.h"
#include "ngen.h"
#include "corbbtprof.h"
#include "pedecoder.h"
#include "mscorsvc.h"
#include "holderinst.h"
#include "corpriv.h"

// For side by side issues, it's best to use the exported API calls to generate a
// Zapper Object instead of creating one on your own.

STDAPI NGenCreateNGenWorker(ICorSvcWorker **pCorSvcWorker, ILocalServerLifetime *pLocalServerLifetime, ICorSvcLogger *pCorSvcLogger);
STDAPI NGenCreateZapper(HANDLE* hZapper, NGenOptions* opt);
STDAPI NGenFreeZapper(HANDLE hZapper);
STDAPI NGenTryEnumerateFusionCache(HANDLE hZapper, LPCWSTR assemblyName, bool fPrint, bool fDelete);
STDAPI_(BOOL) NGenCompile(HANDLE hZapper, LPCWSTR path);


/* --------------------------------------------------------------------------- *
 * Zapper classes
 * --------------------------------------------------------------------------- */

class ZapperOptions;
class ZapperAttributionStats;
class ZapImage;
class ZapInfo;

typedef enum CorZapLogLevel
{
        CORZAP_LOGLEVEL_ERROR,
        CORZAP_LOGLEVEL_WARNING,
        CORZAP_LOGLEVEL_SUCCESS,
        CORZAP_LOGLEVEL_INFO,
} CorZapLogLevel;

class Zapper
{
    friend class ZapImage;
    friend class ZapInfo;
    friend class ZapILMetaData;

 private:

    //
    // Interfaces
    //

    ICorDynamicInfo         *m_pEEJitInfo;
    ICorCompileInfo         *m_pEECompileInfo;
    ICorJitCompiler         *m_pJitCompiler;
    IMetaDataDispenserEx    *m_pMetaDataDispenser;
    HMODULE                  m_hJitLib;
#ifdef _TARGET_AMD64_
    HMODULE                  m_hJitLegacy;
#endif

#ifdef ALLOW_SXS_JIT_NGEN
    ICorJitCompiler         *m_alternateJit;
    HMODULE                  m_hAltJITCompiler;
#endif // ALLOW_SXS_JIT_NGEN

    //
    // Options
    //

    ZapperOptions *         m_pOpt;
    BOOL                    m_fFreeZapperOptions;

    SString                 m_exeName;      // If an EXE is specified

    bool                    m_fromDllHost;

    //
    // Current assembly info
    //

    ICorCompilationDomain  *m_pDomain;
    CORINFO_ASSEMBLY_HANDLE m_hAssembly;
    IMDInternalImport      *m_pAssemblyImport;

    SString                 m_outputPath; // Temp folder for creating the output file

    IMetaDataAssemblyEmit  *m_pAssemblyEmit;
    IMetaDataAssemblyEmit  *CreateAssemblyEmitter();

    //
    //
    // Status info
    //

    BOOL                    m_failed;
    CorInfoRegionKind       m_currentRegionKind;

    SString                 m_platformAssembliesPaths;
    SString                 m_trustedPlatformAssemblies;
    SString                 m_platformResourceRoots;
    SString                 m_appPaths;
    SString                 m_appNiPaths;
    SString                 m_platformWinmdPaths;

#if !defined(FEATURE_MERGE_JIT_AND_ENGINE)
    SString                 m_CLRJITPath;
    bool                    m_fDontLoadJit;
#endif // !defined(FEATURE_MERGE_JIT_AND_ENGINE)
#if !defined(NO_NGENPDB)
    SString                 m_DiasymreaderPath;
#endif // !defined(NO_NGENPDB)
    bool                    m_fForceFullTrust;

    SString                 m_outputFilename;

  public:

    struct assemblyDependencies
    {
        struct DependencyEntry
        {
            BSTR bstr;
            LoadHintEnum loadHint;
            NGenHintEnum ngenHint;
        };

        SArray<DependencyEntry> dependencyArray;
        NGenHintEnum ngenHint;
        SString displayName;

        assemblyDependencies()
        {
            Initialize();
            ngenHint = NGenDefault;
        }

        ~assemblyDependencies()
        {
            Cleanup();
        }

        void Reinitialize()
        {
            Cleanup();
            Initialize();
        }

        void SetNGenHint(NGenHintEnum ngenHint)
        {
            this->ngenHint = ngenHint;
        }

        NGenHintEnum GetNGenHint()
        {
            return ngenHint;
        }

        void SetDisplayName(const WCHAR *pStr)
        {
            displayName.Set(pStr);
        }

        const WCHAR *GetDisplayName()
        {
            return displayName.GetUnicode();
        }

        void Append(const WCHAR *pStr, LoadHintEnum loadHint = LoadDefault, NGenHintEnum ngenHint = NGenDefault)
        {
            // Don't append string if it's a duplicate
            for (COUNT_T i = 0; i < dependencyArray.GetCount(); i++)
            {
                if (wcscmp(dependencyArray[i].bstr, pStr) == 0)
                    return;
            }

            BSTRHolder bstr(::SysAllocString(pStr));
            DependencyEntry dependencyEntry;
            dependencyEntry.bstr = bstr.GetValue();
            dependencyEntry.loadHint = loadHint;
            dependencyEntry.ngenHint = ngenHint;

            dependencyArray.Append(dependencyEntry);
            bstr.SuppressRelease();
        }

        SAFEARRAY *GetSAFEARRAY()
        {
            SafeArrayHolder pDependencies(SafeArrayCreateVector(VT_BSTR, 0, dependencyArray.GetCount()));
            if (pDependencies.GetValue() == NULL) ThrowLastError();

            for (COUNT_T i = 0; i < dependencyArray.GetCount(); i++)
            {
                LONG indices[1];
                indices[0] = (LONG) i;
                IfFailThrow(SafeArrayPutElement(pDependencies, indices, this->dependencyArray[i].bstr));
            }

            pDependencies.SuppressRelease();
            return pDependencies.GetValue();
        }

        SAFEARRAY *GetLoadHintSAFEARRAY()
        {
            SafeArrayHolder pSettings(SafeArrayCreateVector(VT_UI4, 0, dependencyArray.GetCount()));
            if (pSettings.GetValue() == NULL) ThrowLastError();

            for (COUNT_T i = 0; i < dependencyArray.GetCount(); i++)
            {
                LONG indices[1];
                indices[0] = (LONG) i;
                IfFailThrow(SafeArrayPutElement(pSettings, indices, &this->dependencyArray[i].loadHint));
            }

            pSettings.SuppressRelease();
            return pSettings.GetValue();
        }

        SAFEARRAY *GetNGenHintSAFEARRAY()
        {
            SafeArrayHolder pSettings(SafeArrayCreateVector(VT_UI4, 0, dependencyArray.GetCount()));
            if (pSettings.GetValue() == NULL) ThrowLastError();

            for (COUNT_T i = 0; i < dependencyArray.GetCount(); i++)
            {
                LONG indices[1];
                indices[0] = (LONG) i;
                IfFailThrow(SafeArrayPutElement(pSettings, indices, &this->dependencyArray[i].ngenHint));
            }

            pSettings.SuppressRelease();
            return pSettings.GetValue();
        }

    private:
        void Initialize()
        {
            dependencyArray.SetCount(0);
            // Should we reinitialize ngenHint to the default value as well?
        }

        void Cleanup()
        {
            for (COUNT_T i = 0; i < dependencyArray.GetCount(); i++)
            {
                ::SysFreeString(dependencyArray[i].bstr);
            }
        }
    } m_assemblyDependencies;


  public:

    Zapper(ZapperOptions *pOpt);
    Zapper(NGenOptions *pOpt, bool fromDllHost = false);

    void Init(ZapperOptions *pOpt, bool fFreeZapperOptions= false);

    static Zapper *NewZapper(NGenOptions *pOpt, bool fromDllHost = false)
    {
        CONTRACTL
        {
            THROWS;
            GC_NOTRIGGER;
        }
        CONTRACTL_END;
        return new Zapper(pOpt, fromDllHost);
    }

    ~Zapper();

    // The arguments control which native image of mscorlib to use.
    // This matters for hardbinding.
    void InitEE(BOOL fForceDebug, BOOL fForceProfile, BOOL fForceInstrument);
    void LoadAndInitializeJITForNgen(LPCWSTR pwzJitName, OUT HINSTANCE* phJit, OUT ICorJitCompiler** ppICorJitCompiler);

#ifdef FEATURE_FUSION
    HRESULT TryEnumerateFusionCache(LPCWSTR assemblyName, bool fPrint, bool fDelete);
    int EnumerateFusionCache(LPCWSTR assemblyName, bool fPrint, bool fDelete,
            CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig = NULL);
    void PrintFusionCacheEntry(CorSvcLogLevel logLevel, IAssemblyName *pZapAssemblyName);
    void DeleteFusionCacheEntry(IAssemblyName *pZapAssemblyName);
    void DeleteFusionCacheEntry(LPCWSTR assemblyName, CORCOMPILE_NGEN_SIGNATURE *pNativeImageSig);

    void PrintDependencies(
            IMetaDataAssemblyImport * pAssemblyImport,
            CORCOMPILE_DEPENDENCY * pDependencies,
            COUNT_T cDependencies,
            SString &s);
    BOOL VerifyDependencies(
            IMDInternalImport * pAssemblyImport,
            CORCOMPILE_DEPENDENCY * pDependencies,
            COUNT_T cDependencies);

    void PrintAssemblyVersionInfo(IAssemblyName *pZapAssemblyName, SString &s);

    IAssemblyName *GetAssemblyFusionName(IMetaDataAssemblyImport *pImport);
    IAssemblyName *GetAssemblyRefFusionName(IMetaDataAssemblyImport *pImport,
                                            mdAssemblyRef ar);
#endif //FEATURE_FUSION

    BOOL IsAssembly(LPCWSTR path);

    void CreateDependenciesLookupDomain();
    void ComputeDependencies(LPCWSTR pAssemblyName, CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig);
    void ComputeAssemblyDependencies(CORINFO_ASSEMBLY_HANDLE hAssembly);

    void CreatePdb(BSTR pAssemblyPathOrName, BSTR pNativeImagePath, BSTR pPdbPath, BOOL pdbLines, BSTR pManagedPdbSearchPath);
    void CreatePdbInCurrentDomain(BSTR pAssemblyPathOrName, BSTR pNativeImagePath, BSTR pPdbPath, BOOL pdbLines, BSTR pManagedPdbSearchPath);

    void DefineOutputAssembly(SString& strAssemblyName, ULONG * pHashAlgId);

    HRESULT Compile(LPCWSTR path, CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig = NULL);
    void    DontUseProfileData();  
    bool    HasProfileData();     

    void CompileAssembly(CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig);
    ZapImage * CompileModule(CORINFO_MODULE_HANDLE hModule,
                             IMetaDataAssemblyEmit *pEmit);
#ifdef FEATURE_MULTIMODULE_ASSEMBLIES 
    void CompileNonManifestModules(ULONG hashAlgId, SArray<HANDLE> &hFiles);
    static void * GetMapViewOfFile(
                                HANDLE hFile,
                                DWORD * pdwFileLen);
    static void ComputeHashValue(HANDLE hFile, int hashAlg,
                                 BYTE **ppHashValue, DWORD *cbHashValue);
#endif // FEATURE_MULTIMODULE_ASSEMBLIES 
    void InstallCompiledAssembly(LPCWSTR szAssemblyName, LPCWSTR szNativeImagePath, HANDLE hFile, SArray<HANDLE> &hFiles);

    HRESULT GetExceptionHR();

    void Success(LPCWSTR format, ...);
    void Error(LPCWSTR format, ...);
    void Warning(LPCWSTR format, ...);
    void Info(LPCWSTR format, ...);
    void Print(CorZapLogLevel level, LPCWSTR format, ...);
    void Print(CorZapLogLevel level, LPCWSTR format, va_list args);
    void PrintErrorMessage(CorZapLogLevel level, Exception *ex);
    void PrintErrorMessage(CorZapLogLevel level, HRESULT hr);
    void ReportEventNGEN(WORD wType, DWORD dwEventID, LPCWSTR format, ...);

    BOOL            CheckAssemblyUpToDate(CORINFO_ASSEMBLY_HANDLE hAssembly, CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig);
    BOOL            TryToInstallFromRepository(CORINFO_ASSEMBLY_HANDLE hAssembly, CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig);
    BOOL            TryToInstallFromRepositoryDir(SString &strNativeImageDir,
                                                  SString &strSimpleName,
                                                  CORCOMPILE_NGEN_SIGNATURE *pNativeImageSig,
                                                  BOOL *pfHitMismatchedVersion,
                                                  BOOL *pfHitMismatchedDependencies,
                                                  BOOL useHardLink = FALSE);
    void            CopyAndInstallFromRepository(LPCWSTR lpszNativeImageDir, 
                                                 LPCWSTR lpszNativeImageName, 
                                                 CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig,
                                                 BOOL useHardLink = FALSE);
    void            InstallFromRepository(LPCWSTR lpszNativeImage, 
                                          CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig);

    void            CopyDirectory(LPCWSTR srcPath, LPCWSTR dstPath);
    void            CleanDirectory(LPCWSTR path);
    void            TryCleanDirectory(LPCWSTR path);
    static void     TryCleanDirectory(Zapper * pZapper);

    void            GetOutputFolder();

    void SetPlatformAssembliesPaths(LPCWSTR pwzPlatformAssembliesPaths);
    void SetTrustedPlatformAssemblies(LPCWSTR pwzTrustedPlatformAssemblies);
    void SetPlatformResourceRoots(LPCWSTR pwzPlatformResourceRoots);
    void SetAppPaths(LPCWSTR pwzAppPaths);
    void SetAppNiPaths(LPCWSTR pwzAppNiPaths);
    void SetPlatformWinmdPaths(LPCWSTR pwzPlatformWinmdPaths);
    void SetForceFullTrust(bool val);

#if !defined(FEATURE_MERGE_JIT_AND_ENGINE)
    void SetCLRJITPath(LPCWSTR pwszCLRJITPath);
    void SetDontLoadJit();
#endif // !defined(FEATURE_MERGE_JIT_AND_ENGINE)

#if !defined(NO_NGENPDB)
    void SetDiasymreaderPath(LPCWSTR pwzDiasymreaderPath);
#endif // !defined(NO_NGENPDB)

    void SetOutputFilename(LPCWSTR pwszOutputFilename);
    SString GetOutputFileName();

    void SetLegacyMode();

 private:

    void DestroyDomain();
    void CleanupAssembly();

    void CreateCompilationDomain();
    void SetContextInfo(LPCWSTR assemblyName = NULL);

    void InitializeCompilerFlags(CORCOMPILE_VERSION_INFO * pVersionInfo);

    // DomainCallback is subclassed by each method that would like to compile in a given domain
    class DomainCallback
    {
    public:
        virtual void doCallback() = NULL;
    };

    static HRESULT __stdcall GenericDomainCallback(LPVOID pvArgs);
    void InvokeDomainCallback(DomainCallback *callback);

    void CompileInCurrentDomain(__in LPCWSTR path, CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig);
    void ComputeDependenciesInCurrentDomain(LPCWSTR pAssemblyName, CORCOMPILE_NGEN_SIGNATURE * pNativeImageSig);
    void CreateDependenciesLookupDomainInCurrentDomain();

    void HangWorker(LPCWSTR hangKey, LPCWSTR insideHangKey);
};

class ZapperOptions
{
  public:
    enum StatOptions
    {
        NO_STATS            = 0,
        DEFAULT_STATS       = 1,
        FIXUP_STATS         = DEFAULT_STATS << 1,
        CALL_STATS          = FIXUP_STATS << 1,
        ATTRIB_STATS        = CALL_STATS << 1,
        ALL_STATS           = ~NO_STATS,
    };

    LPWSTR      m_zapSet;               // Add to zap string. Use to get a private scope of ngen images, for debugging/testing

    LPCWSTR     m_repositoryDir;        // Directory with prebuilt native images
    RepositoryFlags m_repositoryFlags;  // Copy the native images back to NativeImagesDir

    bool        m_autodebug;            // Use the debug setting specified by the IL module

    MethodNamesList* m_onlyMethods;     // only methods to process
    MethodNamesList* m_excludeMethods;  // excluded these methods
    mdToken          m_onlyOneMethod;   // only compile method with matching token

    bool        m_silent;               // Dont spew any text output
    bool        m_verbose;              // Spew extra text ouput
    bool        m_ignoreErrors;         // Continue in the face of errors
    unsigned    m_statOptions;          // print statisitcs on number of methods, size of code ...
    bool        m_ngenProfileImage;     // ngening with "/prof"

                                        // Which optimizations should be done
    bool        m_ignoreProfileData;    // Don't use profile data
    bool        m_noProcedureSplitting; // Don't do procedure splitting

    bool        m_fHasAnyProfileData;   // true if we successfully loaded and used 
                                        //  any profile data when compiling this assembly

    bool        m_fPartialNGen;         // Generate partial NGen images using IBC data

    bool        m_fPartialNGenSet;      // m_fPartialNGen has been set through the environment

    bool        m_fAutoNGen;            // This is an automatic NGen request

    bool        m_fRepositoryOnly;      // Install from repository only, no real NGen

    bool        m_fNGenLastRetry;       // This is retry of the compilation

    CORJIT_FLAGS m_compilerFlags;

    bool       m_legacyMode;          // true if the zapper was invoked using legacy mode

    bool        m_fNoMetaData;          // Do not copy metadata and IL to native image

    ZapperOptions();
    ~ZapperOptions();
};

struct NGenPrivateAttributesClass : public NGenPrivateAttributes
{
    NGenPrivateAttributesClass()
    {
        Flags    = 0;
        ZapStats = 0;
        DbgDir   = NULL;
    }

private:
    // Make sure that copies of this object aren't inadvertently created
    NGenPrivateAttributesClass(const NGenPrivateAttributesClass &init);
    const NGenPrivateAttributesClass &operator =(const NGenPrivateAttributesClass &rhs);

public:
    ~NGenPrivateAttributesClass()
    {
        if (DbgDir)
        {
            ::SysFreeString(DbgDir);
            DbgDir = NULL;
        }
    }
};

#endif // ZAPPER_H_