This repository has been 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
/
assemblybinder.cpp
1981 lines (1671 loc) · 78.1 KB
/
assemblybinder.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
// 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.
// ============================================================
//
// AssemblyBinder.cpp
//
//
// Implements the AssemblyBinder class
//
// ============================================================
#include "assemblybinder.hpp"
#include "assemblyname.hpp"
#include "assembly.hpp"
#include "applicationcontext.hpp"
#include "loadcontext.hpp"
#include "bindresult.inl"
#include "failurecache.hpp"
#ifdef FEATURE_VERSIONING_LOG
#include "bindinglog.hpp"
#endif // FEATURE_VERSIONING_LOG
#include "utils.hpp"
#include "variables.hpp"
#include "stringarraylist.h"
#include "strongname.h"
#ifdef FEATURE_VERSIONING_LOG
#include "../dlls/mscorrc/fusres.h"
#endif // FEATURE_VERSIONING_LOG
#define APP_DOMAIN_LOCKED_INSPECTION_ONLY 0x01
#define APP_DOMAIN_LOCKED_UNLOCKED 0x02
#define APP_DOMAIN_LOCKED_CONTEXT 0x04
#define BIND_BEHAVIOR_STATIC 0
#define BIND_BEHAVIOR_ORDER_INDEPENDENT 1
#define BIND_BEHAVIOR_BEST_MATCH 2
#ifndef IMAGE_FILE_MACHINE_ARM64
#define IMAGE_FILE_MACHINE_ARM64 0xAA64 // ARM64 Little-Endian
#endif
BOOL IsCompilationProcess();
#if !defined(DACCESS_COMPILE) && !defined(CROSSGEN_COMPILE)
#include "clrprivbindercoreclr.h"
#include "clrprivbinderassemblyloadcontext.h"
// Helper function in the VM, invoked by the Binder, to invoke the host assembly resolver
extern HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToBindWithin,
IAssemblyName *pIAssemblyName, CLRPrivBinderCoreCLR *pTPABinder,
BINDER_SPACE::AssemblyName *pAssemblyName, ICLRPrivAssembly **ppLoadedAssembly);
// Helper to check if we have a host assembly resolver set
extern BOOL RuntimeCanUseAppPathAssemblyResolver(DWORD adid);
#endif // !defined(DACCESS_COMPILE) && !defined(CROSSGEN_COMPILE)
namespace BINDER_SPACE
{
typedef enum
{
kVersionIgnore,
kVersionExact,
kVersionServiceRollForward,
kVersionFeatureRollForward,
kVersionFeatureExact,
kVersionFeatureHighest,
kVersionFeatureLowestHigher,
kVersionFeatureHighestLower
} VersionMatchMode;
namespace
{
BOOL fAssemblyBinderInitialized = FALSE;
//
// This defines the assembly equivalence relation
//
HRESULT IsValidAssemblyVersion(/* in */ AssemblyName *pRequestedName,
/* in */ AssemblyName *pFoundName,
/* in */ ApplicationContext *pApplicationContext)
{
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("IsValidAssemblyVersion"));
AssemblyVersion *pRequestedVersion = pRequestedName->GetVersion();
AssemblyVersion *pFoundVersion = pFoundName->GetVersion();
do
{
if (!pRequestedVersion->HasMajor())
{
// An unspecified requested version component matches any value for the same component in the found version,
// regardless of lesser-order version components
break;
}
if (!pFoundVersion->HasMajor() || pRequestedVersion->GetMajor() > pFoundVersion->GetMajor())
{
// - A specific requested version component does not match an unspecified value for the same component in
// the found version, regardless of lesser-order version components
// - Or, the requested version is greater than the found version
hr = FUSION_E_APP_DOMAIN_LOCKED;
break;
}
if (pRequestedVersion->GetMajor() < pFoundVersion->GetMajor())
{
// The requested version is less than the found version
break;
}
if (!pRequestedVersion->HasMinor())
{
break;
}
if (!pFoundVersion->HasMinor() || pRequestedVersion->GetMinor() > pFoundVersion->GetMinor())
{
hr = FUSION_E_APP_DOMAIN_LOCKED;
break;
}
if (pRequestedVersion->GetMinor() < pFoundVersion->GetMinor())
{
break;
}
if (!pRequestedVersion->HasBuild())
{
break;
}
if (!pFoundVersion->HasBuild() || pRequestedVersion->GetBuild() > pFoundVersion->GetBuild())
{
hr = FUSION_E_APP_DOMAIN_LOCKED;
break;
}
if (pRequestedVersion->GetBuild() < pFoundVersion->GetBuild())
{
break;
}
if (!pRequestedVersion->HasRevision())
{
break;
}
if (!pFoundVersion->HasRevision() || pRequestedVersion->GetRevision() > pFoundVersion->GetRevision())
{
hr = FUSION_E_APP_DOMAIN_LOCKED;
break;
}
} while (false);
if (pApplicationContext->IsTpaListProvided() && hr == FUSION_E_APP_DOMAIN_LOCKED)
{
// For our new binding models, use a more descriptive error code than APP_DOMAIN_LOCKED for bind
// failures.
hr = FUSION_E_REF_DEF_MISMATCH;
}
BINDER_LOG_LEAVE_HR(W("IsValidAssemblyVersion"), hr)
return hr;
}
HRESULT URLToFullPath(PathString &assemblyPath)
{
HRESULT hr = S_OK;
SString::Iterator pos = assemblyPath.Begin();
if (assemblyPath.MatchCaseInsensitive(pos, g_BinderVariables->httpURLPrefix))
{
// HTTP downloads are unsupported
hr = FUSION_E_CODE_DOWNLOAD_DISABLED;
}
else
{
SString fullAssemblyPath;
WCHAR *pwzFullAssemblyPath = fullAssemblyPath.OpenUnicodeBuffer(MAX_LONGPATH);
DWORD dwCCFullAssemblyPath = MAX_LONGPATH + 1; // SString allocates extra byte for null.
MutateUrlToPath(assemblyPath);
dwCCFullAssemblyPath = WszGetFullPathName(assemblyPath.GetUnicode(),
dwCCFullAssemblyPath,
pwzFullAssemblyPath,
NULL);
if (dwCCFullAssemblyPath > MAX_LONGPATH)
{
fullAssemblyPath.CloseBuffer();
pwzFullAssemblyPath = fullAssemblyPath.OpenUnicodeBuffer(dwCCFullAssemblyPath - 1);
dwCCFullAssemblyPath = WszGetFullPathName(assemblyPath.GetUnicode(),
dwCCFullAssemblyPath,
pwzFullAssemblyPath,
NULL);
}
fullAssemblyPath.CloseBuffer(dwCCFullAssemblyPath);
if (dwCCFullAssemblyPath == 0)
{
hr = HRESULT_FROM_GetLastError();
}
else
{
assemblyPath.Set(fullAssemblyPath);
}
}
return hr;
}
#ifdef FEATURE_VERSIONING_LOG
//
// This function outputs the current binding result
// and flushes the bind log.
//
HRESULT LogBindResult(ApplicationContext *pApplicationContext,
HRESULT hrLog,
BindResult *pBindResult)
{
HRESULT hr = S_OK;
BindingLog *pBindingLog = pApplicationContext->GetBindingLog();
if (!pBindingLog->CanLog())
{
// For non-logging, return the bind result
hr = hrLog;
goto Exit;
}
IF_FAIL_GO(pBindingLog->LogHR(hrLog));
if ((hrLog == S_OK) && pBindResult->HaveResult())
{
IF_FAIL_GO(pBindingLog->LogResult(pBindResult));
}
IF_FAIL_GO(pBindingLog->Flush());
// For failure-free logging, return the bind result
hr = hrLog;
Exit:
// SilverLight does not propagate binding log; therefore kill the information here.
pApplicationContext->ClearBindingLog();
return hr;
}
inline UINT GetLockedContextEntry(BOOL fInspectionOnly)
{
if (fInspectionOnly)
return ID_FUSLOG_BINDING_LOCKED_ASSEMBLY_INS_CONTEXT;
else
return ID_FUSLOG_BINDING_LOCKED_ASSEMBLY_EXE_CONTEXT;
}
inline UINT GetLockedEntry(BOOL fInspectionOnly)
{
if (fInspectionOnly)
return ID_FUSLOG_BINDING_LOCKED_MT_INS_LOCKED_ENTRY;
else
return ID_FUSLOG_BINDING_LOCKED_MT_EXE_LOCKED_ENTRY;
}
inline UINT GetLocalizedEntry(BOOL fInspectionOnly)
{
if (fInspectionOnly)
return ID_FUSLOG_BINDING_LOCKED_MT_INS_LOCALI_ENTRY;
else
return ID_FUSLOG_BINDING_LOCKED_MT_EXE_LOCALI_ENTRY;
}
inline UINT GetCBaseEntry(BOOL fInspectionOnly)
{
if (fInspectionOnly)
return ID_FUSLOG_BINDING_LOCKED_MT_INS_CBASE_ENTRY;
else
return ID_FUSLOG_BINDING_LOCKED_MT_EXE_CBASE_ENTRY;
}
HRESULT LogAppDomainLocked(ApplicationContext *pApplicationContext,
DWORD dwLockedReason,
AssemblyName *pAssemblyName = NULL)
{
HRESULT hr = S_OK;
BindingLog *pBindingLog = pApplicationContext->GetBindingLog();
if (pBindingLog->CanLog())
{
PathString info;
PathString format;
BOOL fInspectionOnly = FALSE;
if ((dwLockedReason & APP_DOMAIN_LOCKED_INSPECTION_ONLY) != 0)
{
dwLockedReason &= ~APP_DOMAIN_LOCKED_INSPECTION_ONLY;
fInspectionOnly = TRUE;
}
switch (dwLockedReason)
{
case APP_DOMAIN_LOCKED_UNLOCKED:
{
IF_FAIL_GO(info.
LoadResourceAndReturnHR(CCompRC::Debugging,
ID_FUSLOG_BINDING_LOCKED_UNLOCKED));
}
break;
case APP_DOMAIN_LOCKED_CONTEXT:
{
PathString displayName;
_ASSERTE(pAssemblyName != NULL);
IF_FAIL_GO(format.
LoadResourceAndReturnHR(CCompRC::Debugging,
GetLockedContextEntry(fInspectionOnly)));
pAssemblyName->GetDisplayName(displayName,
AssemblyName::INCLUDE_VERSION |
AssemblyName::INCLUDE_ARCHITECTURE);
info.Printf(format.GetUnicode(), displayName.GetUnicode());
}
break;
default:
_ASSERTE(0);
IF_FAIL_GO(E_INVALIDARG);
break;
}
IF_FAIL_GO(pBindingLog->Log(info));
}
Exit:
return hr;
}
HRESULT LogBindBehavior(ApplicationContext *pApplicationContext,
DWORD dwBindBehavior)
{
HRESULT hr = S_OK;
BindingLog *pBindingLog = pApplicationContext->GetBindingLog();
if (pBindingLog->CanLog())
{
PathString bindBehavior;
UINT uiBindBehavior = 0;
switch (dwBindBehavior)
{
case BIND_BEHAVIOR_STATIC:
uiBindBehavior = ID_FUSLOG_BINDING_BEHAVIOR_STATIC;
break;
case BIND_BEHAVIOR_ORDER_INDEPENDENT:
uiBindBehavior = ID_FUSLOG_BINDING_BEHAVIOR_ORDER_INDEPENDENT;
break;
case BIND_BEHAVIOR_BEST_MATCH:
uiBindBehavior = ID_FUSLOG_BINDING_BEHAVIOR_BEST_MATCH;
break;
default:
_ASSERTE(0);
IF_FAIL_GO(E_INVALIDARG);
break;
}
IF_FAIL_GO(bindBehavior.LoadResourceAndReturnHR(CCompRC::Debugging, uiBindBehavior));
IF_FAIL_GO(pBindingLog->Log(bindBehavior.GetUnicode()));
}
Exit:
return hr;
}
HRESULT LogAssemblyNameWhereRef(ApplicationContext *pApplicationContext,
Assembly *pAssembly)
{
HRESULT hr = S_OK;
BindingLog *pBindingLog = pApplicationContext->GetBindingLog();
if (pBindingLog->CanLog())
{
PathString info;
IF_FAIL_GO(info.
LoadResourceAndReturnHR(CCompRC::Debugging, ID_FUSLOG_BINDING_LOG_WHERE_REF_NAME));
IF_FAIL_GO(pBindingLog->LogAssemblyName(info.GetUnicode(),
pAssembly->GetAssemblyName()));
}
Exit:
return hr;
}
HRESULT LogConfigurationError(ApplicationContext *pApplicationContext,
AssemblyName *pCulturedManifestName,
AssemblyName *pLocalPathAssemblyName)
{
HRESULT hr = S_OK;
BindingLog *pBindingLog = pApplicationContext->GetBindingLog();
if (pBindingLog->CanLog())
{
PathString tmp;
PathString culturedManifestDisplayName;
PathString localPathDisplayName;
PathString info;
IF_FAIL_GO(tmp.
LoadResourceAndReturnHR(CCompRC::Debugging, ID_FUSLOG_BINDING_LOG_ERRONOUS_MANIFEST_ENTRY));
pCulturedManifestName->GetDisplayName(culturedManifestDisplayName,
AssemblyName::INCLUDE_VERSION |
AssemblyName::INCLUDE_ARCHITECTURE);
pLocalPathAssemblyName->GetDisplayName(localPathDisplayName,
AssemblyName::INCLUDE_VERSION |
AssemblyName::INCLUDE_ARCHITECTURE);
info.Printf(tmp.GetUnicode(),
culturedManifestDisplayName.GetUnicode(),
localPathDisplayName.GetUnicode());
IF_FAIL_GO(pBindingLog->Log(info.GetUnicode()));
}
Exit:
return hr;
}
HRESULT LogPathAttempt(ApplicationContext *pApplicationContext,
PathString &assemblyPath)
{
HRESULT hr = S_OK;
BindingLog *pBindingLog = pApplicationContext->GetBindingLog();
if (pBindingLog->CanLog())
{
PathString tmp;
PathString info;
IF_FAIL_GO(tmp.LoadResourceAndReturnHR(CCompRC::Debugging,
ID_FUSLOG_BINDING_LOG_PATH_ATTEMPT));
info.Printf(tmp.GetUnicode(), assemblyPath.GetUnicode());
IF_FAIL_GO(pBindingLog->Log(info));
}
Exit:
return hr;
}
#endif // FEATURE_VERSIONING_LOG
#ifndef CROSSGEN_COMPILE
HRESULT CreateImageAssembly(IMDInternalImport *pIMetaDataAssemblyImport,
PEKIND PeKind,
PEImage *pPEImage,
PEImage *pNativePEImage,
BOOL fInspectionOnly,
BindResult *pBindResult)
{
HRESULT hr = S_OK;
ReleaseHolder<Assembly> pAssembly;
PathString asesmblyPath;
SAFE_NEW(pAssembly, Assembly);
IF_FAIL_GO(pAssembly->Init(pIMetaDataAssemblyImport,
PeKind,
pPEImage,
pNativePEImage,
asesmblyPath,
fInspectionOnly,
FALSE /* fIsInGAC */));
pBindResult->SetResult(pAssembly);
pBindResult->SetIsFirstRequest(TRUE);
Exit:
return hr;
}
#endif // !CROSSGEN_COMPILE
};
/* static */
HRESULT AssemblyBinder::Startup()
{
HRESULT hr = S_OK;
if (!BINDER_SPACE::fAssemblyBinderInitialized)
{
g_BinderVariables = new Variables();
IF_FAIL_GO(g_BinderVariables->Init());
// Setup Debug log
BINDER_LOG_STARTUP();
// We're done
BINDER_SPACE::fAssemblyBinderInitialized = TRUE;
}
Exit:
return hr;
}
HRESULT AssemblyBinder::TranslatePEToArchitectureType(DWORD *pdwPAFlags, PEKIND *PeKind)
{
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("TranslatePEToArchitectureType"))
_ASSERTE(pdwPAFlags != NULL);
_ASSERTE(PeKind != NULL);
CorPEKind CLRPeKind = (CorPEKind) pdwPAFlags[0];
DWORD dwImageType = pdwPAFlags[1];
*PeKind = peNone;
if(CLRPeKind == peNot)
{
// Not a PE. Shouldn't ever get here.
BINDER_LOG(W("Not a PE!"));
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT));
}
else
{
if ((CLRPeKind & peILonly) && !(CLRPeKind & pe32Plus) &&
!(CLRPeKind & pe32BitRequired) && dwImageType == IMAGE_FILE_MACHINE_I386)
{
// Processor-agnostic (MSIL)
BINDER_LOG(W("Processor-agnostic (MSIL)"));
*PeKind = peMSIL;
}
else if (CLRPeKind & pe32Plus)
{
// 64-bit
if (CLRPeKind & pe32BitRequired)
{
// Invalid
BINDER_LOG(W("CLRPeKind & pe32BitRequired is true"));
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT));
}
// Regardless of whether ILONLY is set or not, the architecture
// is the machine type.
if(dwImageType == IMAGE_FILE_MACHINE_ARM64)
*PeKind = peARM64;
else if (dwImageType == IMAGE_FILE_MACHINE_AMD64)
*PeKind = peAMD64;
else
{
// We don't support other architectures
BINDER_LOG(W("Unknown architecture"));
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT));
}
}
else
{
// 32-bit, non-agnostic
if(dwImageType == IMAGE_FILE_MACHINE_I386)
*PeKind = peI386;
else if(dwImageType == IMAGE_FILE_MACHINE_ARMNT)
*PeKind = peARM;
else
{
// Not supported
BINDER_LOG(W("32-bit, non-agnostic"));
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT));
}
}
}
Exit:
BINDER_LOG_LEAVE_HR(W("TranslatePEToArchitectureType"), hr);
return hr;
}
// See code:BINDER_SPACE::AssemblyBinder::GetAssembly for info on fNgenExplicitBind
// and fExplicitBindToNativeImage, and see code:CEECompileInfo::LoadAssemblyByPath
// for an example of how they're used.
HRESULT AssemblyBinder::BindAssembly(/* in */ ApplicationContext *pApplicationContext,
/* in */ AssemblyName *pAssemblyName,
/* in */ LPCWSTR szCodeBase,
/* in */ PEAssembly *pParentAssembly,
/* in */ BOOL fNgenExplicitBind,
/* in */ BOOL fExplicitBindToNativeImage,
/* in */ bool excludeAppPaths,
/* out */ Assembly **ppAssembly)
{
HRESULT hr = S_OK;
LONG kContextVersion = 0;
BindResult bindResult;
BINDER_LOG_ENTER(W("AssemblyBinder::BindAssembly"));
#ifndef CROSSGEN_COMPILE
Retry:
{
// Lock the binding application context
CRITSEC_Holder contextLock(pApplicationContext->GetCriticalSectionCookie());
#endif
if (szCodeBase == NULL)
{
#ifdef FEATURE_VERSIONING_LOG
// Log bind
IF_FAIL_GO(BindingLog::CreateInContext(pApplicationContext,
pAssemblyName,
pParentAssembly));
#endif // FEATURE_VERSIONING_LOG
hr = BindByName(pApplicationContext,
pAssemblyName,
BIND_CACHE_FAILURES,
excludeAppPaths,
&bindResult);
IF_FAIL_GO(hr);
}
else
{
PathString assemblyPath(szCodeBase);
#ifdef FEATURE_VERSIONING_LOG
// Log bind
IF_FAIL_GO(BindingLog::CreateInContext(pApplicationContext,
assemblyPath,
pParentAssembly));
#endif // FEATURE_VERSIONING_LOG
// Convert URL to full path and block HTTP downloads
IF_FAIL_GO(URLToFullPath(assemblyPath));
BOOL fDoNgenExplicitBind = fNgenExplicitBind;
// Only use explicit ngen binding in the new coreclr path-based binding model
if(!pApplicationContext->IsTpaListProvided())
{
fDoNgenExplicitBind = FALSE;
}
IF_FAIL_GO(BindWhereRef(pApplicationContext,
assemblyPath,
fDoNgenExplicitBind,
fExplicitBindToNativeImage,
excludeAppPaths,
&bindResult));
}
// Remember the post-bind version
kContextVersion = pApplicationContext->GetVersion();
Exit:
#ifdef FEATURE_VERSIONING_LOG
hr = LogBindResult(pApplicationContext, hr, &bindResult);
#else // FEATURE_VERSIONING_LOG
;
#endif // FEATURE_VERSIONING_LOG
#ifndef CROSSGEN_COMPILE
} // lock(pApplicationContext)
#endif
if (bindResult.HaveResult())
{
#ifndef CROSSGEN_COMPILE
BindResult hostBindResult;
hr = RegisterAndGetHostChosen(pApplicationContext,
kContextVersion,
&bindResult,
&hostBindResult);
if (hr == S_FALSE)
{
// Another bind interfered. We need to retry the entire bind.
// This by design loops as long as needed because by construction we eventually
// will succeed or fail the bind.
bindResult.Reset();
goto Retry;
}
else if (hr == S_OK)
{
*ppAssembly = hostBindResult.GetAsAssembly(TRUE /* fAddRef */);
}
#else // CROSSGEN_COMPILE
*ppAssembly = bindResult.GetAsAssembly(TRUE /* fAddRef */);
#endif // CROSSGEN_COMPILE
}
BINDER_LOG_LEAVE_HR(W("AssemblyBinder::BindAssembly"), hr);
return hr;
}
/* static */
HRESULT AssemblyBinder::BindToSystem(SString &systemDirectory,
Assembly **ppSystemAssembly,
bool fBindToNativeImage)
{
_ASSERTE(BINDER_SPACE::fAssemblyBinderInitialized == TRUE);
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("AssemblyBinder:BindToSystem"));
_ASSERTE(ppSystemAssembly != NULL);
StackSString sCoreLibDir(systemDirectory);
ReleaseHolder<Assembly> pSystemAssembly;
if(!sCoreLibDir.EndsWith(DIRECTORY_SEPARATOR_CHAR_W))
{
sCoreLibDir.Append(DIRECTORY_SEPARATOR_CHAR_W);
}
StackSString sCoreLib;
// At run-time, System.Private.CoreLib.dll is expected to be the NI image.
sCoreLib = sCoreLibDir;
sCoreLib.Append(CoreLibName_IL_W);
BOOL fExplicitBindToNativeImage = (fBindToNativeImage == true)? TRUE:FALSE;
#ifdef FEATURE_NI_BIND_FALLBACK
// Some non-Windows platforms do not automatically generate the NI image as CoreLib.dll.
// If those platforms also do not support automatic fallback from NI to IL, bind as IL.
fExplicitBindToNativeImage = FALSE;
#endif // FEATURE_NI_BIND_FALLBACK
IF_FAIL_GO(AssemblyBinder::GetAssembly(sCoreLib,
FALSE /* fInspectionOnly */,
TRUE /* fIsInGAC */,
fExplicitBindToNativeImage,
&pSystemAssembly));
*ppSystemAssembly = pSystemAssembly.Extract();
Exit:
BINDER_LOG_LEAVE_HR(W("AssemblyBinder::BindToSystem"), hr);
return hr;
}
/* static */
HRESULT AssemblyBinder::BindToSystemSatellite(SString &systemDirectory,
SString &simpleName,
SString &cultureName,
Assembly **ppSystemAssembly)
{
_ASSERTE(BINDER_SPACE::fAssemblyBinderInitialized == TRUE);
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("AssemblyBinder:BindToSystemSatellite"));
_ASSERTE(ppSystemAssembly != NULL);
StackSString sMscorlibSatellite(systemDirectory);
ReleaseHolder<Assembly> pSystemAssembly;
// append culture name
if (!cultureName.IsEmpty())
{
CombinePath(sMscorlibSatellite, cultureName, sMscorlibSatellite);
}
// append satellite assembly's simple name
CombinePath(sMscorlibSatellite, simpleName, sMscorlibSatellite);
// append extension
sMscorlibSatellite.Append(W(".dll"));
IF_FAIL_GO(AssemblyBinder::GetAssembly(sMscorlibSatellite,
FALSE /* fInspectionOnly */,
TRUE /* fIsInGAC */,
FALSE /* fExplicitBindToNativeImage */,
&pSystemAssembly));
*ppSystemAssembly = pSystemAssembly.Extract();
Exit:
BINDER_LOG_LEAVE_HR(W("AssemblyBinder::BindToSystemSatellite"), hr);
return hr;
}
/* static */
HRESULT AssemblyBinder::GetAssemblyFromImage(PEImage *pPEImage,
PEImage *pNativePEImage,
Assembly **ppAssembly)
{
_ASSERTE(BINDER_SPACE::fAssemblyBinderInitialized == TRUE);
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("AssemblyBinder::GetAssemblyFromImage"));
_ASSERTE(pPEImage != NULL);
_ASSERTE(ppAssembly != NULL);
ReleaseHolder<Assembly> pAssembly;
ReleaseHolder<IMDInternalImport> pIMetaDataAssemblyImport;
DWORD dwPAFlags[2];
PEKIND PeKind = peNone;
SAFE_NEW(pAssembly, Assembly);
if(pNativePEImage)
{
IF_FAIL_GO(BinderAcquireImport(pNativePEImage, &pIMetaDataAssemblyImport, dwPAFlags, TRUE));
}
else
{
IF_FAIL_GO(BinderAcquireImport(pPEImage, &pIMetaDataAssemblyImport, dwPAFlags, FALSE));
}
IF_FAIL_GO(TranslatePEToArchitectureType(dwPAFlags, &PeKind));
IF_FAIL_GO(pAssembly->Init(pIMetaDataAssemblyImport,
PeKind,
pPEImage,
pNativePEImage,
g_BinderVariables->emptyString,
FALSE /* fInspectionOnly */,
FALSE /* fIsInGAC */));
// TODO: Is this correct?
pAssembly->SetIsByteArray(TRUE);
*ppAssembly = pAssembly.Extract();
Exit:
BINDER_LOG_LEAVE_HR(W("AssemblyBinder::GetAssemblyFromImage"), hr);
return hr;
}
#ifndef CROSSGEN_COMPILE
/* static */
HRESULT AssemblyBinder::PreBindByteArray(ApplicationContext *pApplicationContext,
PEImage *pPEImage,
BOOL fInspectionOnly)
{
_ASSERTE(BINDER_SPACE::fAssemblyBinderInitialized == TRUE);
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("AssemblyBinder::PreBindByteArray"));
ReleaseHolder<AssemblyName> pAssemblyName;
BOOL fNeedHostRegister = FALSE;
LONG kContextVersion = 0;
ReleaseHolder<IMDInternalImport> pIMetaDataAssemblyImport;
DWORD dwPAFlags[2];
PEKIND PeKind = peNone;
BindResult bindResult;
// Prepare binding data
SAFE_NEW(pAssemblyName, AssemblyName);
IF_FAIL_GO(BinderAcquireImport(pPEImage, &pIMetaDataAssemblyImport, dwPAFlags, FALSE));
IF_FAIL_GO(TranslatePEToArchitectureType(dwPAFlags, &PeKind));
IF_FAIL_GO(pAssemblyName->Init(pIMetaDataAssemblyImport, PeKind));
pAssemblyName->SetIsDefinition(TRUE);
// Validate architecture
if (!fInspectionOnly && !Assembly::IsValidArchitecture(pAssemblyName->GetArchitecture()))
{
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT));
}
// Attempt the actual bind (eventually more than once)
Retry:
{
// Lock the application context
CRITSEC_Holder contextLock(pApplicationContext->GetCriticalSectionCookie());
// Attempt uncached bind and register stream if possible
if (!fInspectionOnly &&
((hr = BindByName(pApplicationContext,
pAssemblyName,
BIND_CACHE_FAILURES | BIND_CACHE_RERUN_BIND,
false, // excludeAppPaths
&bindResult)) == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
)
{
if ((fInspectionOnly && !bindResult.HaveResult()) ||
(bindResult.GetRetargetedAssemblyName() == NULL))
{
IF_FAIL_GO(CreateImageAssembly(pIMetaDataAssemblyImport,
PeKind,
pPEImage,
NULL,
fInspectionOnly,
&bindResult));
if (fInspectionOnly)
{
// For inspection-only, we do not share the map.
IF_FAIL_GO(Register(pApplicationContext, fInspectionOnly, &bindResult));
}
else
{
// Remember the post-bind version of the context
kContextVersion = pApplicationContext->GetVersion();
fNeedHostRegister = TRUE;
}
}
}
} // lock(pApplicationContext)
if (fNeedHostRegister)
{
BindResult hostBindResult;
// This has to happen outside the binder lock as it can cause new binds
IF_FAIL_GO(RegisterAndGetHostChosen(pApplicationContext,
kContextVersion,
&bindResult,
&hostBindResult));
if (hr == S_FALSE)
{
// Another bind interfered. We need to retry entire bind.
// This by design loops as long as needed because by construction we eventually
// will succeed or fail the bind.
bindResult.Reset();
goto Retry;
}
}
// Ignore bind errors here because we need to attempt by-name load to get log entry.
GO_WITH_HRESULT(S_OK);
Exit:
BINDER_LOG_LEAVE_HR(W("AssemblyBinder::PreBindByteArray"), hr);
return hr;
}
#endif //CROSSGEN_COMPILE
/* static */
HRESULT AssemblyBinder::BindByName(ApplicationContext *pApplicationContext,
AssemblyName *pAssemblyName,
DWORD dwBindFlags,
bool excludeAppPaths,
BindResult *pBindResult)
{
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("AssemblyBinder::BindByName"));
PathString assemblyDisplayName;
// Look for already cached binding failure (ignore PA, every PA will lock the context)
pAssemblyName->GetDisplayName(assemblyDisplayName,
AssemblyName::INCLUDE_VERSION);
hr = pApplicationContext->GetFailureCache()->Lookup(assemblyDisplayName);
if (FAILED(hr))
{
if ((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) && RerunBind(dwBindFlags))
{
// Ignore pre-existing transient bind error (re-bind will succeed)
pApplicationContext->GetFailureCache()->Remove(assemblyDisplayName);
}
goto LogExit;
}
else if (hr == S_FALSE)
{
// workaround: Special case for byte arrays. Rerun the bind to create binding log.
pAssemblyName->SetIsDefinition(TRUE);
hr = S_OK;
}
if (!Assembly::IsValidArchitecture(pAssemblyName->GetArchitecture()))
{
// Assembly reference contains wrong architecture
IF_FAIL_GO(FUSION_E_INVALID_NAME);
}
IF_FAIL_GO(BindLocked(pApplicationContext,
pAssemblyName,
dwBindFlags,
excludeAppPaths,
pBindResult));
if (!pBindResult->HaveResult())
{
// Behavior rules are clueless now
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
}
Exit:
if (FAILED(hr) && CacheBindFailures(dwBindFlags))
{
if (RerunBind(dwBindFlags))
{
if (hr != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
// Cache non-transient bind error for byte-array
hr = S_FALSE;
}
else
{
// Ignore transient bind error (re-bind will succeed)
goto LogExit;
}
}
hr = pApplicationContext->AddToFailureCache(assemblyDisplayName, hr);
}
LogExit:
BINDER_LOG_LEAVE_HR(W("AssemblyBinder::BindByName"), hr);
return hr;
}