This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathcompile.cpp
8171 lines (6670 loc) · 263 KB
/
compile.cpp
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// ===========================================================================
// File: compile.cpp
//
//
// Support for zap compiler and zap files
// ===========================================================================
#include "common.h"
#ifdef FEATURE_PREJIT
#include <corcompile.h>
#include "assemblyspec.hpp"
#include "compile.h"
#include "excep.h"
#include "field.h"
#include "security.h"
#include "eeconfig.h"
#include "zapsig.h"
#include "gcrefmap.h"
#ifndef FEATURE_CORECLR
#include "corsym.h"
#endif // FEATURE_CORECLR
#include "virtualcallstub.h"
#include "typeparse.h"
#include "typestring.h"
#include "constrainedexecutionregion.h"
#include "dllimport.h"
#include "comdelegate.h"
#include "stringarraylist.h"
#ifdef FEATURE_COMINTEROP
#include "clrtocomcall.h"
#include "comtoclrcall.h"
#include "winrttypenameconverter.h"
#endif // FEATURE_COMINTEROP
#include "dllimportcallback.h"
#include "caparser.h"
#include "sigbuilder.h"
#include "cgensys.h"
#include "peimagelayout.inl"
#if defined(FEATURE_HOSTED_BINDER) && defined(FEATURE_APPX_BINDER)
#include "appxutil.h"
#include "clrprivbinderappx.h"
#include "clrprivtypecachewinrt.h"
#endif // defined(FEATURE_HOSTED_BINDER) && defined(FEATURE_APPX_BINDER)
#ifdef FEATURE_COMINTEROP
#include "clrprivbinderwinrt.h"
#include "winrthelpers.h"
#endif
#ifdef CROSSGEN_COMPILE
#include "crossgenroresolvenamespace.h"
#endif
#include <cvinfo.h>
#ifdef MDIL
#include <mdil.h>
#endif
#include "tritonstress.h"
#ifdef CROSSGEN_COMPILE
CompilationDomain * theDomain;
#endif
VerboseLevel g_CorCompileVerboseLevel = CORCOMPILE_NO_LOG;
//
// CEECompileInfo implements most of ICorCompileInfo
//
HRESULT CEECompileInfo::Startup( BOOL fForceDebug,
BOOL fForceProfiling,
BOOL fForceInstrument)
{
SystemDomain::SetCompilationOverrides(fForceDebug,
fForceProfiling,
fForceInstrument);
HRESULT hr = S_OK;
m_fCachingOfInliningHintsEnabled = TRUE;
m_fGeneratingNgenPDB = FALSE;
_ASSERTE(!g_fEEStarted && !g_fEEInit && "You cannot run the EE inside an NGEN compilation process");
if (!g_fEEStarted && !g_fEEInit)
{
#ifdef CROSSGEN_COMPILE
GetSystemInfo(&g_SystemInfo);
theDomain = new CompilationDomain(fForceDebug,
fForceProfiling,
fForceInstrument);
#endif
// When NGEN'ing this call may execute EE code, e.g. the managed code to set up
// the SharedDomain.
hr = InitializeEE(COINITEE_DEFAULT);
}
//
// JIT interface expects to be called with
// preemptive GC enabled
//
if (SUCCEEDED(hr)) {
#ifdef _DEBUG
Thread *pThread = GetThread();
_ASSERTE(pThread);
#endif
GCX_PREEMP_NO_DTOR();
}
return hr;
}
HRESULT CEECompileInfo::CreateDomain(ICorCompilationDomain **ppDomain,
IMetaDataAssemblyEmit *pEmitter,
BOOL fForceDebug,
BOOL fForceProfiling,
BOOL fForceInstrument,
BOOL fForceFulltrustDomain
#ifdef MDIL
, MDILCompilationFlags mdilCompilationFlags
#endif
)
{
STANDARD_VM_CONTRACT;
COOPERATIVE_TRANSITION_BEGIN();
#ifndef CROSSGEN_COMPILE
AppDomainCreationHolder<CompilationDomain> pCompilationDomain;
pCompilationDomain.Assign(new CompilationDomain(fForceDebug,
fForceProfiling,
fForceInstrument));
#else
CompilationDomain * pCompilationDomain = theDomain;
#endif
{
SystemDomain::LockHolder lh;
pCompilationDomain->Init(
#ifdef MDIL
mdilCompilationFlags
#endif
);
}
if (pEmitter)
pCompilationDomain->SetDependencyEmitter(pEmitter);
#if defined(FEATURE_HOSTED_BINDER) && defined(FEATURE_APPX_BINDER)
if (AppX::IsAppXProcess())
{
HRESULT hr = S_OK;
ReleaseHolder<ICLRPrivBinder> pBinderInterface;
CLRPrivBinderAppX * pBinder = CLRPrivBinderAppX::GetOrCreateBinder();
IfFailThrow(pBinder->QueryInterface(IID_ICLRPrivBinder, &pBinderInterface));
pCompilationDomain->SetLoadContextHostBinder(pBinderInterface);
}
#endif // defined(FEATURE_HOSTED_BINDER) && defined(FEATURE_APPX_BINDER)
#ifdef DEBUGGING_SUPPORTED
// Notify the debugger here, before the thread transitions into the
// AD to finish the setup, and before any assemblies are loaded into it.
SystemDomain::PublishAppDomainAndInformDebugger(pCompilationDomain);
#endif // DEBUGGING_SUPPORTED
pCompilationDomain->LoadSystemAssemblies();
pCompilationDomain->SetupSharedStatics();
*ppDomain = static_cast<ICorCompilationDomain*>(pCompilationDomain);
{
GCX_COOP();
ENTER_DOMAIN_PTR(pCompilationDomain,ADV_COMPILATION)
{
#ifdef FEATURE_CORECLR
if (fForceFulltrustDomain)
((ApplicationSecurityDescriptor *)pCompilationDomain->GetSecurityDescriptor())->SetGrantedPermissionSet(NULL, NULL, 0xFFFFFFFF);
#endif
#ifndef CROSSGEN_COMPILE
#ifndef FEATURE_CORECLR
pCompilationDomain->InitializeHashing(NULL);
#endif // FEATURE_CORECLR
#endif
pCompilationDomain->InitializeDomainContext(TRUE, NULL, NULL);
#ifndef CROSSGEN_COMPILE
#ifdef FEATURE_CORECLR
if (!NingenEnabled())
{
APPDOMAINREF adRef = (APPDOMAINREF)pCompilationDomain->GetExposedObject();
GCPROTECT_BEGIN(adRef);
MethodDescCallSite initializeSecurity(METHOD__APP_DOMAIN__INITIALIZE_DOMAIN_SECURITY);
ARG_SLOT args[] =
{
ObjToArgSlot(adRef),
ObjToArgSlot(NULL),
ObjToArgSlot(NULL),
ObjToArgSlot(NULL),
static_cast<ARG_SLOT>(FALSE)
};
initializeSecurity.Call(args);
GCPROTECT_END();
}
#endif //FEATURE_CORECLR
#endif
{
GCX_PREEMP();
// We load assemblies as domain-bound (However, they're compiled as domain neutral)
#ifdef FEATURE_LOADER_OPTIMIZATION
#ifdef FEATURE_FUSION
if (NingenEnabled())
{
pCompilationDomain->SetSharePolicy(AppDomain::SHARE_POLICY_NEVER);
}
else
{
pCompilationDomain->SetupLoaderOptimization(AppDomain::SHARE_POLICY_NEVER);
}
#else //FEATURE_FUSION
pCompilationDomain->SetSharePolicy(AppDomain::SHARE_POLICY_NEVER);
#endif //FEATURE_FUSION
#endif // FEATURE_LOADER_OPTIMIZATION
#ifdef FEATURE_FUSION
CorCompileConfigFlags flags = PEFile::GetNativeImageConfigFlags(pCompilationDomain->m_fForceDebug,
pCompilationDomain->m_fForceProfiling,
pCompilationDomain->m_fForceInstrument);
FusionBind::SetApplicationContextDWORDProperty(GetAppDomain()->GetFusionContext(),
ACTAG_ZAP_CONFIG_FLAGS, flags);
#endif //FEATURE_FUSION
}
pCompilationDomain->SetFriendlyName(W("Compilation Domain"));
if (!NingenEnabled())
{
Security::SetDefaultAppDomainProperty(pCompilationDomain->GetSecurityDescriptor());
pCompilationDomain->GetSecurityDescriptor()->FinishInitialization();
}
SystemDomain::System()->LoadDomain(pCompilationDomain);
#ifndef CROSSGEN_COMPILE
pCompilationDomain.DoneCreating();
#endif
}
END_DOMAIN_TRANSITION;
}
COOPERATIVE_TRANSITION_END();
return S_OK;
}
HRESULT CEECompileInfo::DestroyDomain(ICorCompilationDomain *pDomain)
{
STANDARD_VM_CONTRACT;
#ifndef CROSSGEN_COMPILE
COOPERATIVE_TRANSITION_BEGIN();
GCX_COOP();
CompilationDomain *pCompilationDomain = (CompilationDomain *) pDomain;
// DDB 175659: Make sure that canCallNeedsRestore() returns FALSE during compilation
// domain shutdown.
pCompilationDomain->setCannotCallNeedsRestore();
pCompilationDomain->Unload(TRUE);
COOPERATIVE_TRANSITION_END();
#endif
return S_OK;
}
HRESULT MakeCrossDomainCallbackWorker(
CROSS_DOMAIN_CALLBACK pfnCallback,
LPVOID pArgs)
{
STATIC_CONTRACT_MODE_COOPERATIVE;
STATIC_CONTRACT_SO_INTOLERANT;
HRESULT hrRetVal = E_UNEXPECTED;
BEGIN_SO_TOLERANT_CODE(GetThread());
hrRetVal = pfnCallback(pArgs);
END_SO_TOLERANT_CODE;
return hrRetVal;
}
HRESULT CEECompileInfo::MakeCrossDomainCallback(
ICorCompilationDomain* pDomain,
CROSS_DOMAIN_CALLBACK pfnCallback,
LPVOID pArgs)
{
STANDARD_VM_CONTRACT;
HRESULT hrRetVal = E_UNEXPECTED;
COOPERATIVE_TRANSITION_BEGIN();
{
// Switch to cooperative mode to switch appdomains
GCX_COOP();
ENTER_DOMAIN_PTR((CompilationDomain*)pDomain,ADV_COMPILATION)
{
//
// Switch to preemptive mode on before calling back into
// the zapper
//
GCX_PREEMP();
hrRetVal = MakeCrossDomainCallbackWorker(pfnCallback, pArgs);
}
END_DOMAIN_TRANSITION;
}
COOPERATIVE_TRANSITION_END();
return hrRetVal;
}
#ifdef TRITON_STRESS_NEED_IMPL
int LogToSvcLogger(LPCWSTR format, ...)
{
STANDARD_VM_CONTRACT;
StackSString s;
va_list args;
va_start(args, format);
s.VPrintf(format, args);
va_end(args);
GetSvcLogger()->Printf(W("%s"), s.GetUnicode());
return 0;
}
#endif
HRESULT CEECompileInfo::LoadAssemblyByPath(
LPCWSTR wzPath,
// Normally this is FALSE, but crossgen /CreatePDB sets this to TRUE, so it can
// explicitly load an NI by path
BOOL fExplicitBindToNativeImage,
CORINFO_ASSEMBLY_HANDLE *pHandle)
{
STANDARD_VM_CONTRACT;
HRESULT hr = S_OK;
COOPERATIVE_TRANSITION_BEGIN();
Assembly * pAssembly;
HRESULT hrProcessLibraryBitnessMismatch = S_OK;
bool verifyingImageIsAssembly = false;
// We don't want to do a LoadFrom, since they do not work with ngen. Instead,
// read the metadata from the file and do a bind based on that.
EX_TRY
{
// Pre-open the image so we can grab some metadata to help initialize the
// binder's AssemblySpec, which we'll use later to load the assembly for real.
PEImageHolder pImage;
#if defined(CROSSGEN_COMPILE) && !defined(FEATURE_CORECLR)
// If the path is not absolute, look for the assembly on platform path list first
if (wcschr(wzPath, '\\') == NULL || wcschr(wzPath, ':') == NULL || wcschr(wzPath, '/') == NULL)
{
CompilationDomain::FindImage(wzPath,
fExplicitBindToNativeImage ? MDInternalImport_NoCache : MDInternalImport_Default, &pImage);
}
#endif
if (pImage == NULL)
{
pImage = PEImage::OpenImage(
wzPath,
// If we're explicitly binding to an NGEN image, we do not want the cache
// this PEImage for use later, as pointers that need fixup (e.g.,
// Module::m_pModuleSecurityDescriptor) will not be valid for use later.
// Normal caching is done when we open it "for real" further down when we
// call LoadDomainAssembly().
fExplicitBindToNativeImage ? MDInternalImport_NoCache : MDInternalImport_Default);
}
#if defined(FEATURE_WINDOWSPHONE)
verifyingImageIsAssembly = true;
#endif // FEATURE_WINDOWSPHONE
if (fExplicitBindToNativeImage && !pImage->HasReadyToRunHeader())
{
pImage->VerifyIsNIAssembly();
}
else
{
pImage->VerifyIsAssembly();
}
verifyingImageIsAssembly = false;
// Check to make sure the bitness of the assembly matches the bitness of the process
// we will be loading it into and store the result. If a COR_IMAGE_ERROR gets thrown
// by LoadAssembly then we can blame it on bitness mismatch. We do the check here
// and not in the CATCH to distinguish between the COR_IMAGE_ERROR that can be thrown by
// VerifyIsAssembly (not necessarily a bitness mismatch) and that from LoadAssembly
#ifdef _WIN64
if (pImage->Has32BitNTHeaders())
{
hrProcessLibraryBitnessMismatch = PEFMT_E_32BIT;
}
#else
if (!pImage->Has32BitNTHeaders())
{
hrProcessLibraryBitnessMismatch = PEFMT_E_64BIT;
}
#endif
AssemblySpec spec;
spec.InitializeSpec(TokenFromRid(1, mdtAssembly), pImage->GetMDImport(), NULL, FALSE);
if (spec.IsMscorlib())
{
pAssembly = SystemDomain::System()->SystemAssembly();
}
else
{
AppDomain * pDomain = AppDomain::GetCurrentDomain();
PEAssemblyHolder pAssemblyHolder;
BOOL isWinRT = FALSE;
#ifdef FEATURE_COMINTEROP
isWinRT = spec.IsContentType_WindowsRuntime();
if (isWinRT)
{
LPCSTR szNameSpace;
LPCSTR szTypeName;
// It does not make sense to pass the file name to recieve fake type name for empty WinMDs, because we would use the name
// for binding in next call to BindAssemblySpec which would fail for fake WinRT type name
// We will throw/return the error instead and the caller will recognize it and react to it by not creating the ngen image -
// see code:Zapper::ComputeDependenciesInCurrentDomain
IfFailThrow(::GetFirstWinRTTypeDef(pImage->GetMDImport(), &szNameSpace, &szTypeName, NULL, NULL));
spec.SetWindowsRuntimeType(szNameSpace, szTypeName);
}
#endif //FEATURE_COMINTEROP
// If there is a host binder then use it to bind the assembly.
if (pDomain->HasLoadContextHostBinder() || isWinRT)
{
pAssemblyHolder = pDomain->BindAssemblySpec(&spec, TRUE, FALSE);
}
else
{
#ifdef FEATURE_FUSION
SafeComHolder<IBindResult> pNativeFusionAssembly;
SafeComHolder<IFusionBindLog> pFusionLog;
SafeComHolder<IAssembly> pFusionAssembly;
IfFailThrow(ExplicitBind(wzPath, pDomain->GetFusionContext(), EXPLICITBIND_FLAGS_EXE,
NULL, &pFusionAssembly, &pNativeFusionAssembly, &pFusionLog));
pAssemblyHolder = PEAssembly::Open(pFusionAssembly, pNativeFusionAssembly, pFusionLog, FALSE, FALSE);
#else //FEATURE_FUSION
//ExplicitBind
CoreBindResult bindResult;
spec.SetCodeBase(pImage->GetPath());
spec.Bind(
pDomain,
TRUE, // fThrowOnFileNotFound
&bindResult,
// fNgenExplicitBind: Generally during NGEN / MDIL compilation, this is
// TRUE, meaning "I am NGEN, and I am doing an explicit bind to the IL
// image, so don't infer the NI and try to open it, because I already
// have it open". But if we're executing crossgen /CreatePDB, this should
// be FALSE so that downstream code doesn't assume we're explicitly
// trying to bind to an IL image (we're actually explicitly trying to
// open an NI).
!fExplicitBindToNativeImage,
// fExplicitBindToNativeImage: Most callers want this FALSE; but crossgen
// /CreatePDB explicitly specifies NI names to open, and cannot assume
// that IL assemblies will be available.
fExplicitBindToNativeImage
);
pAssemblyHolder = PEAssembly::Open(&bindResult,FALSE,FALSE);
#endif //FEATURE_FUSION
}
// Now load assembly into domain.
DomainAssembly * pDomainAssembly = pDomain->LoadDomainAssembly(&spec, pAssemblyHolder, FILE_LOAD_BEGIN);
#ifndef FEATURE_APPX_BINDER
if (spec.CanUseWithBindingCache() && pDomainAssembly->CanUseWithBindingCache())
pDomain->AddAssemblyToCache(&spec, pDomainAssembly);
#endif
#if defined(CROSSGEN_COMPILE) && !defined(FEATURE_CORECLR)
pDomain->ToCompilationDomain()->ComputeAssemblyHardBindList(pAssemblyHolder->GetPersistentMDImport());
#endif
#ifdef MDIL
// MDIL is generated as a special mode of ngen.exe or coregen.exe; normally these two utilities
// would not generate anything if a native image for the requested assembly already exists.
// Of course, this is not the desired behavior when generating MDIL - it should always work.
// We need to prevent loading of the native image when we are generating MDIL.
if (!GetAppDomain()->IsMDILCompilationDomain())
#endif
{
// Mark the assembly before it gets fully loaded and NGen image dependencies are verified. This is necessary
// to allow skipping compilation if there is NGen image already.
pDomainAssembly->GetFile()->SetSafeToHardBindTo();
}
pAssembly = pDomain->LoadAssembly(&spec, pAssemblyHolder, FILE_LOADED);
// Add a dependency to the current assembly. This is done to match the behavior
// of LoadAssemblyFusion, so that the same native image is generated whether we
// ngen install by file name or by assembly name.
pDomain->ToCompilationDomain()->AddDependency(&spec, pAssemblyHolder);
}
#ifdef MDIL
if (GetAppDomain()->IsMDILCompilationDomain())
TritonStressStartup(LogToSvcLogger);
#endif
// Kind of a workaround - if we could have loaded this assembly via normal load,
*pHandle = CORINFO_ASSEMBLY_HANDLE(pAssembly);
}
EX_CATCH_HRESULT(hr);
if (verifyingImageIsAssembly && hr != S_OK)
{
hr = NGEN_E_FILE_NOT_ASSEMBLY;
}
else if ( hrProcessLibraryBitnessMismatch != S_OK && ( hr == COR_E_BADIMAGEFORMAT || hr == HRESULT_FROM_WIN32(ERROR_BAD_EXE_FORMAT) ) )
{
hr = hrProcessLibraryBitnessMismatch;
}
COOPERATIVE_TRANSITION_END();
return hr;
}
#ifdef FEATURE_FUSION
// Simple helper that factors out code common to LoadAssemblyByIAssemblyName and
// LoadAssemblyByName
static HRESULT LoadAssemblyByIAssemblyNameWorker(
IAssemblyName *pAssemblyName,
CORINFO_ASSEMBLY_HANDLE *pHandle)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
SO_INTOLERANT;
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END;
Assembly *pAssembly;
AssemblySpec spec;
spec.InitializeSpec(pAssemblyName, NULL, FALSE);
if (spec.IsMscorlib())
{
pAssembly = SystemDomain::System()->SystemAssembly();
}
else
{
DomainAssembly * pDomainAssembly = spec.LoadDomainAssembly(FILE_LOAD_BEGIN);
// Mark the assembly before it gets fully loaded and NGen image dependencies are verified. This is necessary
// to allow skipping compilation if there is NGen image already.
pDomainAssembly->GetFile()->SetSafeToHardBindTo();
pAssembly = spec.LoadAssembly(FILE_LOADED);
}
#ifdef MDIL
if (GetAppDomain()->IsMDILCompilationDomain())
TritonStressStartup(LogToSvcLogger);
#endif
//
// Return the module handle
//
*pHandle = CORINFO_ASSEMBLY_HANDLE(pAssembly);
return S_OK;
}
HRESULT CEECompileInfo::LoadAssemblyByName(
LPCWSTR wzName,
CORINFO_ASSEMBLY_HANDLE *pHandle)
{
STANDARD_VM_CONTRACT;
HRESULT hr = S_OK;
COOPERATIVE_TRANSITION_BEGIN();
EX_TRY
{
ReleaseHolder<IAssemblyName> pAssemblyName;
IfFailThrow(CreateAssemblyNameObject(&pAssemblyName, wzName, CANOF_PARSE_DISPLAY_NAME, NULL));
IfFailThrow(LoadAssemblyByIAssemblyNameWorker(pAssemblyName, pHandle));
}
EX_CATCH_HRESULT(hr);
COOPERATIVE_TRANSITION_END();
return hr;
}
HRESULT CEECompileInfo::LoadAssemblyRef(
IMDInternalImport *pAssemblyImport,
mdAssemblyRef ref,
CORINFO_ASSEMBLY_HANDLE *pHandle,
IAssemblyName **refAssemblyName /*=NULL*/)
{
STANDARD_VM_CONTRACT;
HRESULT hr = S_OK;
ReleaseHolder<IAssemblyName> pAssemblyName;
COOPERATIVE_TRANSITION_BEGIN();
EX_TRY
{
Assembly *pAssembly;
if (refAssemblyName)
*refAssemblyName = NULL;
AssemblySpec spec;
spec.InitializeSpec(ref, pAssemblyImport, NULL, FALSE);
if (spec.HasBindableIdentity())
{
if (refAssemblyName)
{
IfFailThrow(spec.CreateFusionName(&pAssemblyName));
}
pAssembly = spec.LoadAssembly(FILE_LOADED);
//
// Return the module handle
//
*pHandle = CORINFO_ASSEMBLY_HANDLE(pAssembly);
}
else
{ // Cannot load assembly refs with non-unique id.
hr = S_FALSE;
}
}
EX_CATCH_HRESULT(hr);
COOPERATIVE_TRANSITION_END();
if (refAssemblyName != NULL && pAssemblyName != NULL)
{
*refAssemblyName = pAssemblyName.Extract();
}
return hr;
}
HRESULT CEECompileInfo::LoadAssemblyByIAssemblyName(
IAssemblyName *pAssemblyName,
CORINFO_ASSEMBLY_HANDLE *pHandle
)
{
STANDARD_VM_CONTRACT;
HRESULT hr = S_OK;
COOPERATIVE_TRANSITION_BEGIN();
EX_TRY
{
IfFailThrow(LoadAssemblyByIAssemblyNameWorker(pAssemblyName, pHandle));
}
EX_CATCH_HRESULT(hr);
COOPERATIVE_TRANSITION_END();
return hr;
}
#endif //FEATURE_FUSION
#ifdef FEATURE_COMINTEROP
HRESULT CEECompileInfo::LoadTypeRefWinRT(
IMDInternalImport *pAssemblyImport,
mdTypeRef ref,
CORINFO_ASSEMBLY_HANDLE *pHandle)
{
STANDARD_VM_CONTRACT;
HRESULT hr = S_OK;
ReleaseHolder<IAssemblyName> pAssemblyName;
COOPERATIVE_TRANSITION_BEGIN();
EX_TRY
{
Assembly *pAssembly;
mdToken tkResolutionScope;
pAssemblyImport->GetResolutionScopeOfTypeRef(ref, &tkResolutionScope);
if(TypeFromToken(tkResolutionScope) == mdtAssemblyRef)
{
DWORD dwAssemblyRefFlags;
IfFailThrow(pAssemblyImport->GetAssemblyRefProps(tkResolutionScope, NULL, NULL,
NULL, NULL,
NULL, NULL, &dwAssemblyRefFlags));
if (IsAfContentType_WindowsRuntime(dwAssemblyRefFlags))
{
LPCSTR psznamespace;
LPCSTR pszname;
pAssemblyImport->GetNameOfTypeRef(ref, &psznamespace, &pszname);
AssemblySpec spec;
spec.InitializeSpec(tkResolutionScope, pAssemblyImport, NULL, FALSE);
spec.SetWindowsRuntimeType(psznamespace, pszname);
_ASSERTE(spec.HasBindableIdentity());
pAssembly = spec.LoadAssembly(FILE_LOADED);
//
// Return the module handle
//
*pHandle = CORINFO_ASSEMBLY_HANDLE(pAssembly);
}
else
{
hr = S_FALSE;
}
}
else
{
hr = S_FALSE;
}
}
EX_CATCH_HRESULT(hr);
COOPERATIVE_TRANSITION_END();
return hr;
}
#endif
BOOL CEECompileInfo::IsInCurrentVersionBubble(CORINFO_MODULE_HANDLE hModule)
{
WRAPPER_NO_CONTRACT;
return ((Module*)hModule)->IsInCurrentVersionBubble();
}
HRESULT CEECompileInfo::LoadAssemblyModule(
CORINFO_ASSEMBLY_HANDLE assembly,
mdFile file,
CORINFO_MODULE_HANDLE *pHandle)
{
STANDARD_VM_CONTRACT;
COOPERATIVE_TRANSITION_BEGIN();
Assembly *pAssembly = (Assembly*) assembly;
Module *pModule = pAssembly->GetManifestModule()->LoadModule(GetAppDomain(), file, TRUE)->GetModule();
//
// Return the module handle
//
*pHandle = CORINFO_MODULE_HANDLE(pModule);
COOPERATIVE_TRANSITION_END();
return S_OK;
}
#ifndef FEATURE_CORECLR
BOOL CEECompileInfo::SupportsAutoNGen(CORINFO_ASSEMBLY_HANDLE assembly)
{
STANDARD_VM_CONTRACT;
Assembly *pAssembly = (Assembly*) assembly;
return pAssembly->SupportsAutoNGen();
}
HRESULT CEECompileInfo::SetCachedSigningLevel(HANDLE hNI, HANDLE *pModules, COUNT_T nModules)
{
STANDARD_VM_CONTRACT;
HRESULT hr = S_OK;
HMODULE hKernel32 = WszLoadLibrary(W("kernel32.dll"));
typedef BOOL (WINAPI *SetCachedSigningLevel_t)
(__in_ecount(Count) PHANDLE SourceFiles, __in ULONG Count, __in ULONG Flags, __in HANDLE TargetFile);
SetCachedSigningLevel_t SetCachedSigningLevel
= (SetCachedSigningLevel_t)GetProcAddress(hKernel32, "SetCachedSigningLevel");
if (SetCachedSigningLevel == NULL)
{
return S_OK;
}
StackSArray<PEImage*> images;
PEImage::GetAll(images);
StackSArray<HANDLE> handles;
for (StackSArray<PEImage*>::Iterator i = images.Begin(), end = images.End(); i != end; i++)
{
if (!(*i)->IsFile())
{
continue;
}
HANDLE hFile = (*i)->GetFileHandleLocking();
handles.Append(hFile);
}
IfFailGo(SetCachedSigningLevel(handles.GetElements(), handles.GetCount(), 0, hNI));
for (COUNT_T i = 0; i < nModules; i++)
{
if (!SetCachedSigningLevel(handles.GetElements(), handles.GetCount(), 0, pModules[i]))
{
hr = HRESULT_FROM_WIN32(GetLastError());
_ASSERTE(FAILED(hr));
goto ErrExit;
}
}
ErrExit:
return hr;
}
#endif
BOOL CEECompileInfo::CheckAssemblyZap(
CORINFO_ASSEMBLY_HANDLE assembly,
__out_ecount_opt(*cAssemblyManifestModulePath)
LPWSTR assemblyManifestModulePath,
LPDWORD cAssemblyManifestModulePath)
{
STANDARD_VM_CONTRACT;
BOOL result = FALSE;
COOPERATIVE_TRANSITION_BEGIN();
Assembly *pAssembly = (Assembly*) assembly;
if (pAssembly->GetManifestFile()->HasNativeImage())
{
PEImage *pImage = pAssembly->GetManifestFile()->GetPersistentNativeImage();
if (assemblyManifestModulePath != NULL)
{
DWORD length = pImage->GetPath().GetCount();
if (length > *cAssemblyManifestModulePath)
{
length = *cAssemblyManifestModulePath - 1;
wcsncpy_s(assemblyManifestModulePath, *cAssemblyManifestModulePath, pImage->GetPath(), length);
assemblyManifestModulePath[length] = 0;
}
else
wcscpy_s(assemblyManifestModulePath, *cAssemblyManifestModulePath, pImage->GetPath());
}
result = TRUE;
}
COOPERATIVE_TRANSITION_END();
return result;
}
#ifdef MDIL
DWORD CEECompileInfo::GetMdilModuleSecurityFlags(
CORINFO_ASSEMBLY_HANDLE assembly)
{
STANDARD_VM_CONTRACT;
Assembly *pAssembly = (Assembly*) assembly;
ModuleSecurityDescriptor *pMSD = ModuleSecurityDescriptor::GetModuleSecurityDescriptor(pAssembly);
MDILHeader::Flags securityFlags = MDILHeader::MdilModuleSecurityDescriptorFlags_None;
// Is Microsoft Platform
if (pMSD->IsMicrosoftPlatform())
securityFlags = (MDILHeader::Flags)(securityFlags | MDILHeader::MdilModuleSecurityDescriptorFlags_IsMicrosoftPlatform);
// Is every method and type in the assembly transparent
if (pMSD->IsAllTransparent())
securityFlags = (MDILHeader::Flags)(securityFlags | MDILHeader::MdilModuleSecurityDescriptorFlags_IsAllTransparent);
// Is every method and type introduced by the assembly critical
if (pMSD->IsAllCritical())
securityFlags = (MDILHeader::Flags)(securityFlags | MDILHeader::MdilModuleSecurityDescriptorFlags_IsAllCritical);
// Combined with IsAllCritical - is every method and type introduced by the assembly safe critical
if (pMSD->IsTreatAsSafe())
securityFlags = (MDILHeader::Flags)(securityFlags | MDILHeader::MdilModuleSecurityDescriptorFlags_IsTreatAsSafe);
// Does the assembly not care about transparency, and wants the CLR to take care of making sure everything
// is annotated properly in the assembly.
if (pMSD->IsOpportunisticallyCritical())
securityFlags = (MDILHeader::Flags)(securityFlags | MDILHeader::MdilModuleSecurityDescriptorFlags_IsOpportunisticallyCritical);
// Partial trust assemblies are forced all-transparent under some conditions. This
// tells us whether that is true for this particular assembly.
if (pMSD->IsAllTransparentDueToPartialTrust())
securityFlags = (MDILHeader::Flags)(securityFlags | MDILHeader::MdilModuleSecurityDescriptorFlags_TransparentDueToPartialTrust);
#ifdef FEATURE_APTCA
if (pMSD->IsAPTCA())
#endif
securityFlags = (MDILHeader::Flags)(securityFlags | MDILHeader::MdilModuleSecurityDescriptorFlags_IsAPTCA);
#ifndef FEATURE_CORECLR
// Can fully trusted transparent code bypass verification
if (pMSD->CanTransparentCodeSkipVerification())
securityFlags = (MDILHeader::Flags)(securityFlags | MDILHeader::MdilModuleSecurityDescriptorFlags_SkipFullTrustVerification);
#endif // !FEATURE_CORECLR
return (DWORD)securityFlags;
}
BOOL CEECompileInfo::CompilerRelaxationNoStringInterningPermitted(
CORINFO_ASSEMBLY_HANDLE assembly)
{
STANDARD_VM_CONTRACT;
Assembly *pAssembly = (Assembly*) assembly;
return pAssembly->GetManifestModule()->IsNoStringInterning();
}
BOOL CEECompileInfo::RuntimeCompatibilityWrapExceptions(
CORINFO_ASSEMBLY_HANDLE assembly)
{
STANDARD_VM_CONTRACT;
Assembly *pAssembly = (Assembly*) assembly;
return pAssembly->GetManifestModule()->IsRuntimeWrapExceptions();
}