-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
assemblybindercommon.cpp
1335 lines (1122 loc) · 50.4 KB
/
assemblybindercommon.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.
// ============================================================
//
// AssemblyBinder.cpp
//
//
// Implements the AssemblyBinder class
//
// ============================================================
#include "common.h"
#include "assemblybindercommon.hpp"
#include "assemblyname.hpp"
#include "assembly.hpp"
#include "applicationcontext.hpp"
#include "assemblyhashtraits.hpp"
#include "bindertracing.h"
#include "bindresult.inl"
#include "failurecache.hpp"
#include "utils.hpp"
#include "stringarraylist.h"
#include "configuration.h"
#if !defined(DACCESS_COMPILE)
#include "defaultassemblybinder.h"
// Helper function in the VM, invoked by the Binder, to invoke the host assembly resolver
extern HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToBindWithin,
BINDER_SPACE::AssemblyName *pAssemblyName,
DefaultAssemblyBinder *pDefaultBinder,
AssemblyBinder *pBinder,
BINDER_SPACE::Assembly **ppLoadedAssembly);
#endif // !defined(DACCESS_COMPILE)
STDAPI BinderAcquirePEImage(LPCTSTR szAssemblyPath,
PEImage** ppPEImage,
BundleFileLocation bundleFileLocation);
namespace BINDER_SPACE
{
namespace
{
//
// This defines the assembly equivalence relation
//
bool IsCompatibleAssemblyVersion(/* in */ AssemblyName *pRequestedName,
/* in */ AssemblyName *pFoundName)
{
AssemblyVersion *pRequestedVersion = pRequestedName->GetVersion();
AssemblyVersion *pFoundVersion = pFoundName->GetVersion();
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
return true;
}
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
return false;
}
if (pRequestedVersion->GetMajor() < pFoundVersion->GetMajor())
{
// The requested version is less than the found version
return true;
}
if (!pRequestedVersion->HasMinor())
{
return true;
}
if (!pFoundVersion->HasMinor() || pRequestedVersion->GetMinor() > pFoundVersion->GetMinor())
{
return false;
}
if (pRequestedVersion->GetMinor() < pFoundVersion->GetMinor())
{
return true;
}
if (!pRequestedVersion->HasBuild())
{
return true;
}
if (!pFoundVersion->HasBuild() || pRequestedVersion->GetBuild() > pFoundVersion->GetBuild())
{
return false;
}
if (pRequestedVersion->GetBuild() < pFoundVersion->GetBuild())
{
return true;
}
if (!pRequestedVersion->HasRevision())
{
return true;
}
if (!pFoundVersion->HasRevision() || pRequestedVersion->GetRevision() > pFoundVersion->GetRevision())
{
return false;
}
return true;
}
HRESULT CreateImageAssembly(PEImage *pPEImage,
BindResult *pBindResult)
{
HRESULT hr = S_OK;
ReleaseHolder<Assembly> pAssembly;
SAFE_NEW(pAssembly, Assembly);
IF_FAIL_GO(pAssembly->Init(pPEImage, /* fIsInTPA */ FALSE ));
pBindResult->SetResult(pAssembly);
Exit:
return hr;
}
};
HRESULT AssemblyBinderCommon::TranslatePEToArchitectureType(DWORD *pdwPAFlags, PEKIND *PeKind)
{
HRESULT hr = S_OK;
_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.
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)
*PeKind = peMSIL;
}
else if (CLRPeKind & pe32Plus)
{
// 64-bit
if (CLRPeKind & pe32BitRequired)
{
// Invalid
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
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
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT));
}
}
}
Exit:
return hr;
}
HRESULT AssemblyBinderCommon::BindAssembly(/* in */ AssemblyBinder *pBinder,
/* in */ AssemblyName *pAssemblyName,
/* in */ bool excludeAppPaths,
/* out */ Assembly **ppAssembly)
{
HRESULT hr = S_OK;
LONG kContextVersion = 0;
BindResult bindResult;
ApplicationContext* pApplicationContext = pBinder->GetAppContext();
// Tracing happens outside the binder lock to avoid calling into managed code within the lock
BinderTracing::ResolutionAttemptedOperation tracer{pAssemblyName, pBinder, 0 /*managedALC*/, hr};
Retry:
{
// Lock the binding application context
CRITSEC_Holder contextLock(pApplicationContext->GetCriticalSectionCookie());
_ASSERTE(pAssemblyName != NULL);
IF_FAIL_GO(BindByName(pApplicationContext,
pAssemblyName,
false, // skipFailureCaching
false, // skipVersionCompatibilityCheck
excludeAppPaths,
&bindResult));
// Remember the post-bind version
kContextVersion = pApplicationContext->GetVersion();
} // lock(pApplicationContext)
Exit:
tracer.TraceBindResult(bindResult);
if (bindResult.HaveResult())
{
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.GetAssembly(TRUE /* fAddRef */);
}
}
return hr;
}
/* static */
HRESULT AssemblyBinderCommon::BindToSystem(SString &systemDirectory,
Assembly **ppSystemAssembly)
{
HRESULT hr = S_OK;
_ASSERTE(ppSystemAssembly != NULL);
ReleaseHolder<Assembly> pSystemAssembly;
// System.Private.CoreLib.dll is expected to be found at one of the following locations:
// * Non-single-file app: In systemDirectory, beside coreclr.dll
// * Framework-dependent single-file app: In systemDirectory, beside coreclr.dll
// * Self-contained single-file app: Within the single-file bundle.
//
// CoreLib path (sCoreLib):
// * Absolute path when looking for a file on disk
// * Bundle-relative path when looking within the single-file bundle.
StackSString sCoreLibName(CoreLibName_IL_W);
StackSString sCoreLib;
BinderTracing::PathSource pathSource = BinderTracing::PathSource::Bundle;
BundleFileLocation bundleFileLocation = Bundle::ProbeAppBundle(sCoreLibName, /*pathIsBundleRelative */ true);
if (!bundleFileLocation.IsValid())
{
pathSource = BinderTracing::PathSource::ApplicationAssemblies;
}
sCoreLib.Set(systemDirectory);
CombinePath(sCoreLib, sCoreLibName, sCoreLib);
hr = AssemblyBinderCommon::GetAssembly(sCoreLib,
TRUE /* fIsInTPA */,
&pSystemAssembly,
bundleFileLocation);
BinderTracing::PathProbed(sCoreLib, pathSource, hr);
if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
// Try to find corelib in the TPA
StackSString sCoreLibSimpleName(CoreLibName_W);
StackSString sTrustedPlatformAssemblies = Configuration::GetKnobStringValue(W("TRUSTED_PLATFORM_ASSEMBLIES"));
sTrustedPlatformAssemblies.Normalize();
bool found = false;
for (SString::Iterator i = sTrustedPlatformAssemblies.Begin(); i != sTrustedPlatformAssemblies.End(); )
{
SString fileName;
SString simpleName;
bool isNativeImage = false;
HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextTPAPath(sTrustedPlatformAssemblies, i, /*dllOnly*/ true, fileName, simpleName, isNativeImage));
if (pathResult == S_FALSE)
{
break;
}
if (simpleName.EqualsCaseInsensitive(sCoreLibSimpleName))
{
sCoreLib = fileName;
found = true;
break;
}
}
if (!found)
{
GO_WITH_HRESULT(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
}
hr = AssemblyBinderCommon::GetAssembly(sCoreLib,
TRUE /* fIsInTPA */,
&pSystemAssembly,
bundleFileLocation);
BinderTracing::PathProbed(sCoreLib, BinderTracing::PathSource::ApplicationAssemblies, hr);
}
IF_FAIL_GO(hr);
*ppSystemAssembly = pSystemAssembly.Extract();
Exit:
return hr;
}
/* static */
HRESULT AssemblyBinderCommon::BindToSystemSatellite(SString& systemDirectory,
SString& simpleName,
SString& cultureName,
Assembly** ppSystemAssembly)
{
HRESULT hr = S_OK;
_ASSERTE(ppSystemAssembly != NULL);
// Satellite assembly's relative path
StackSString relativePath;
// append culture name
if (!cultureName.IsEmpty())
{
CombinePath(relativePath, cultureName, relativePath);
}
// append satellite assembly's simple name
CombinePath(relativePath, simpleName, relativePath);
// append extension
relativePath.Append(W(".dll"));
// Satellite assembly's path:
// * Absolute path when looking for a file on disk
// * Bundle-relative path when looking within the single-file bundle.
StackSString sCoreLibSatellite;
BinderTracing::PathSource pathSource = BinderTracing::PathSource::Bundle;
BundleFileLocation bundleFileLocation = Bundle::ProbeAppBundle(relativePath, /*pathIsBundleRelative */ true);
if (!bundleFileLocation.IsValid())
{
sCoreLibSatellite.Set(systemDirectory);
pathSource = BinderTracing::PathSource::ApplicationAssemblies;
}
CombinePath(sCoreLibSatellite, relativePath, sCoreLibSatellite);
ReleaseHolder<Assembly> pSystemAssembly;
IF_FAIL_GO(AssemblyBinderCommon::GetAssembly(sCoreLibSatellite,
TRUE /* fIsInTPA */,
&pSystemAssembly,
bundleFileLocation));
BinderTracing::PathProbed(sCoreLibSatellite, pathSource, hr);
*ppSystemAssembly = pSystemAssembly.Extract();
Exit:
return hr;
}
/* static */
HRESULT AssemblyBinderCommon::BindByName(ApplicationContext *pApplicationContext,
AssemblyName *pAssemblyName,
bool skipFailureCaching,
bool skipVersionCompatibilityCheck,
bool excludeAppPaths,
BindResult *pBindResult)
{
HRESULT hr = S_OK;
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)) && skipFailureCaching)
{
// 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 (!IsValidArchitecture(pAssemblyName->GetArchitecture()))
{
// Assembly reference contains wrong architecture
IF_FAIL_GO(FUSION_E_INVALID_NAME);
}
IF_FAIL_GO(BindLocked(pApplicationContext,
pAssemblyName,
skipVersionCompatibilityCheck,
excludeAppPaths,
pBindResult));
if (!pBindResult->HaveResult())
{
// Behavior rules are clueless now
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
}
Exit:
if (FAILED(hr))
{
if (skipFailureCaching)
{
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:
return hr;
}
/* static */
HRESULT AssemblyBinderCommon::BindLocked(ApplicationContext *pApplicationContext,
AssemblyName *pAssemblyName,
bool skipVersionCompatibilityCheck,
bool excludeAppPaths,
BindResult *pBindResult)
{
HRESULT hr = S_OK;
bool isTpaListProvided = pApplicationContext->IsTpaListProvided();
Assembly *pAssembly = NULL;
hr = FindInExecutionContext(pApplicationContext, pAssemblyName, &pAssembly);
// Add the attempt to the bind result on failure / not found. On success, it will be added after the version check.
if (FAILED(hr) || pAssembly == NULL)
pBindResult->SetAttemptResult(hr, pAssembly, /*isInContext*/ true);
IF_FAIL_GO(hr);
if (pAssembly != NULL)
{
if (!skipVersionCompatibilityCheck)
{
// Can't give higher version than already bound
bool isCompatible = IsCompatibleAssemblyVersion(pAssemblyName, pAssembly->GetAssemblyName());
hr = isCompatible ? S_OK : FUSION_E_APP_DOMAIN_LOCKED;
pBindResult->SetAttemptResult(hr, pAssembly, /*isInContext*/ true);
// TPA binder returns FUSION_E_REF_DEF_MISMATCH for incompatible version
if (hr == FUSION_E_APP_DOMAIN_LOCKED && isTpaListProvided)
hr = FUSION_E_REF_DEF_MISMATCH;
}
else
{
pBindResult->SetAttemptResult(hr, pAssembly, /*isInContext*/ true);
}
IF_FAIL_GO(hr);
pBindResult->SetResult(pAssembly, /*isInContext*/ true);
}
else
if (isTpaListProvided)
{
// BindByTpaList handles setting attempt results on the bind result
hr = BindByTpaList(pApplicationContext,
pAssemblyName,
excludeAppPaths,
pBindResult);
if (SUCCEEDED(hr) && pBindResult->HaveResult())
{
bool isCompatible = IsCompatibleAssemblyVersion(pAssemblyName, pBindResult->GetAssemblyName());
hr = isCompatible ? S_OK : FUSION_E_APP_DOMAIN_LOCKED;
pBindResult->SetAttemptResult(hr, pBindResult->GetAssembly());
// TPA binder returns FUSION_E_REF_DEF_MISMATCH for incompatible version
if (hr == FUSION_E_APP_DOMAIN_LOCKED && isTpaListProvided)
hr = FUSION_E_REF_DEF_MISMATCH;
}
if (FAILED(hr))
{
pBindResult->SetNoResult();
}
IF_FAIL_GO(hr);
}
Exit:
return hr;
}
/* static */
HRESULT AssemblyBinderCommon::FindInExecutionContext(ApplicationContext *pApplicationContext,
AssemblyName *pAssemblyName,
Assembly **ppAssembly)
{
_ASSERTE(pApplicationContext != NULL);
_ASSERTE(pAssemblyName != NULL);
_ASSERTE(ppAssembly != NULL);
ExecutionContext *pExecutionContext = pApplicationContext->GetExecutionContext();
Assembly *pAssembly = pExecutionContext->Lookup(pAssemblyName);
// Set any found context entry. It is up to the caller to check the returned HRESULT
// for errors due to validation
*ppAssembly = pAssembly;
if (pAssembly == NULL)
return S_FALSE;
AssemblyName *pContextName = pAssembly->GetAssemblyName();
if (pAssemblyName->GetIsDefinition() &&
(pContextName->GetArchitecture() != pAssemblyName->GetArchitecture()))
{
return FUSION_E_APP_DOMAIN_LOCKED;
}
return S_OK;
}
//
// Tests whether a candidate assembly's name matches the requested.
// This does not do a version check. The binder applies version policy
// further up the stack once it gets a successful bind.
//
BOOL TestCandidateRefMatchesDef(AssemblyName *pRequestedAssemblyName,
AssemblyName *pBoundAssemblyName,
BOOL tpaListAssembly)
{
DWORD dwIncludeFlags = AssemblyName::INCLUDE_DEFAULT;
if (!tpaListAssembly)
{
if (pRequestedAssemblyName->IsNeutralCulture())
{
dwIncludeFlags |= AssemblyName::EXCLUDE_CULTURE;
}
}
if (pRequestedAssemblyName->GetArchitecture() != peNone)
{
dwIncludeFlags |= AssemblyName::INCLUDE_ARCHITECTURE;
}
return pBoundAssemblyName->Equals(pRequestedAssemblyName, dwIncludeFlags);
}
namespace
{
HRESULT BindSatelliteResourceFromBundle(
AssemblyName* pRequestedAssemblyName,
SString &relativePath,
BindResult* pBindResult)
{
HRESULT hr = S_OK;
BundleFileLocation bundleFileLocation = Bundle::ProbeAppBundle(relativePath, /* pathIsBundleRelative */ true);
if (!bundleFileLocation.IsValid())
{
return hr;
}
ReleaseHolder<Assembly> pAssembly;
hr = AssemblyBinderCommon::GetAssembly(relativePath,
FALSE /* fIsInTPA */,
&pAssembly,
bundleFileLocation);
BinderTracing::PathProbed(relativePath, BinderTracing::PathSource::Bundle, hr);
// Missing files are okay and expected when probing
if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
return S_OK;
}
pBindResult->SetAttemptResult(hr, pAssembly);
if (FAILED(hr))
return hr;
AssemblyName* pBoundAssemblyName = pAssembly->GetAssemblyName();
if (TestCandidateRefMatchesDef(pRequestedAssemblyName, pBoundAssemblyName, false /*tpaListAssembly*/))
{
pBindResult->SetResult(pAssembly);
hr = S_OK;
}
else
{
hr = FUSION_E_REF_DEF_MISMATCH;
}
pBindResult->SetAttemptResult(hr, pAssembly);
return hr;
}
HRESULT BindSatelliteResourceByProbingPaths(
const StringArrayList *pResourceRoots,
AssemblyName *pRequestedAssemblyName,
SString &relativePath,
BindResult *pBindResult,
BinderTracing::PathSource pathSource)
{
HRESULT hr = S_OK;
for (UINT i = 0; i < pResourceRoots->GetCount(); i++)
{
ReleaseHolder<Assembly> pAssembly;
SString &wszBindingPath = (*pResourceRoots)[i];
SString fileName(wszBindingPath);
CombinePath(fileName, relativePath, fileName);
hr = AssemblyBinderCommon::GetAssembly(fileName,
FALSE /* fIsInTPA */,
&pAssembly);
BinderTracing::PathProbed(fileName, pathSource, hr);
// Missing files are okay and expected when probing
if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
continue;
}
pBindResult->SetAttemptResult(hr, pAssembly);
if (FAILED(hr))
return hr;
AssemblyName *pBoundAssemblyName = pAssembly->GetAssemblyName();
if (TestCandidateRefMatchesDef(pRequestedAssemblyName, pBoundAssemblyName, false /*tpaListAssembly*/))
{
pBindResult->SetResult(pAssembly);
hr = S_OK;
}
else
{
hr = FUSION_E_REF_DEF_MISMATCH;
}
pBindResult->SetAttemptResult(hr, pAssembly);
return hr;
}
// Up-stack expects S_OK when we don't find any candidate assemblies and no fatal error occurred (ie, no S_FALSE)
return S_OK;
}
HRESULT BindSatelliteResource(
ApplicationContext* pApplicationContext,
AssemblyName* pRequestedAssemblyName,
BindResult* pBindResult)
{
// Satellite resource probing strategy is to look:
// * First within the single-file bundle
// * Then under each of the Platform Resource Roots
// * Then under each of the App Paths.
//
// During each search, if we find a platform resource file with matching file name, but whose ref-def didn't match,
// fall back to application resource lookup to handle case where a user creates resources with the same
// names as platform ones.
HRESULT hr = S_OK;
const SString& simpleNameRef = pRequestedAssemblyName->GetSimpleName();
SString& cultureRef = pRequestedAssemblyName->GetCulture();
_ASSERTE(!pRequestedAssemblyName->IsNeutralCulture());
ReleaseHolder<Assembly> pAssembly;
SString fileName;
CombinePath(fileName, cultureRef, fileName);
CombinePath(fileName, simpleNameRef, fileName);
fileName.Append(W(".dll"));
hr = BindSatelliteResourceFromBundle(pRequestedAssemblyName, fileName, pBindResult);
if (pBindResult->HaveResult() || FAILED(hr))
{
return hr;
}
hr = BindSatelliteResourceByProbingPaths(pApplicationContext->GetPlatformResourceRoots(),
pRequestedAssemblyName,
fileName,
pBindResult,
BinderTracing::PathSource::PlatformResourceRoots);
if (pBindResult->HaveResult() || FAILED(hr))
{
return hr;
}
hr = BindSatelliteResourceByProbingPaths(pApplicationContext->GetAppPaths(),
pRequestedAssemblyName,
fileName,
pBindResult,
BinderTracing::PathSource::AppPaths);
return hr;
}
HRESULT BindAssemblyByProbingPaths(
const StringArrayList *pBindingPaths,
AssemblyName *pRequestedAssemblyName,
Assembly **ppAssembly)
{
const SString &simpleName = pRequestedAssemblyName->GetSimpleName();
BinderTracing::PathSource pathSource = BinderTracing::PathSource::AppPaths;
// Loop through the binding paths looking for a matching assembly
for (DWORD i = 0; i < pBindingPaths->GetCount(); i++)
{
HRESULT hr;
ReleaseHolder<Assembly> pAssembly;
LPCWSTR wszBindingPath = (*pBindingPaths)[i];
PathString fileNameWithoutExtension(wszBindingPath);
CombinePath(fileNameWithoutExtension, simpleName, fileNameWithoutExtension);
// Look for a matching dll first
PathString fileName(fileNameWithoutExtension);
fileName.Append(W(".dll"));
hr = AssemblyBinderCommon::GetAssembly(fileName,
FALSE, // fIsInTPA
&pAssembly);
BinderTracing::PathProbed(fileName, pathSource, hr);
if (FAILED(hr))
{
fileName.Set(fileNameWithoutExtension);
fileName.Append(W(".exe"));
hr = AssemblyBinderCommon::GetAssembly(fileName,
FALSE, // fIsInTPA
&pAssembly);
BinderTracing::PathProbed(fileName, pathSource, hr);
}
// Since we're probing, file not founds are ok and we should just try another
// probing path
if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
continue;
}
// Set any found assembly. It is up to the caller to check the returned HRESULT for errors due to validation
*ppAssembly = pAssembly.Extract();
if (FAILED(hr))
return hr;
// We found a candidate.
//
// Below this point, we either establish that the ref-def matches, or
// we fail the bind.
// Compare requested AssemblyName with that from the candidate assembly
if (!TestCandidateRefMatchesDef(pRequestedAssemblyName, pAssembly->GetAssemblyName(), false /*tpaListAssembly*/))
return FUSION_E_REF_DEF_MISMATCH;
return S_OK;
}
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
}
}
/*
* BindByTpaList is the entry-point for the custom binding algorithm in CoreCLR.
*
* The search for assemblies will proceed in the following order:
*
* If this application is a single-file bundle, the meta-data contained in the bundle
* will be probed to find the requested assembly. If the assembly is not found,
* The list of platform assemblies (TPAs) are considered next.
*
* Platform assemblies are specified as a list of files. This list is the only set of
* assemblies that we will load as platform. They can be specified as IL or NIs.
*
* Resources for platform assemblies are located by probing starting at the Platform Resource Roots,
* a set of folders configured by the host.
*
* If a requested assembly identity cannot be found in the TPA list or the resource roots,
* it is considered an application assembly. We probe for application assemblies in the
* AppPaths, a list of paths containing IL files and satellite resource folders.
*
*/
/* static */
HRESULT AssemblyBinderCommon::BindByTpaList(ApplicationContext *pApplicationContext,
AssemblyName *pRequestedAssemblyName,
bool excludeAppPaths,
BindResult *pBindResult)
{
HRESULT hr = S_OK;
bool fPartialMatchOnTpa = false;
if (!pRequestedAssemblyName->IsNeutralCulture())
{
IF_FAIL_GO(BindSatelliteResource(pApplicationContext, pRequestedAssemblyName, pBindResult));
}
else
{
ReleaseHolder<Assembly> pTPAAssembly;
const SString& simpleName = pRequestedAssemblyName->GetSimpleName();
// Is assembly in the bundle?
// Single-file bundle contents take precedence over TPA.
// The list of bundled assemblies is contained in the bundle manifest, and NOT in the TPA.
// Therefore the bundle is first probed using the assembly's simple name.
// If found, the assembly is loaded from the bundle.
if (Bundle::AppIsBundle())
{
// Search Assembly.ni.dll, then Assembly.dll
// The Assembly.ni.dll paths are rare, and intended for supporting managed C++ R2R assemblies.
const WCHAR* const candidates[] = { W(".ni.dll"), W(".dll") };
// Loop through the binding paths looking for a matching assembly
for (int i = 0; i < 2; i++)
{
SString assemblyFileName(simpleName);
assemblyFileName.Append(candidates[i]);
SString assemblyFilePath(Bundle::AppBundle->BasePath());
assemblyFilePath.Append(assemblyFileName);
BundleFileLocation bundleFileLocation = Bundle::ProbeAppBundle(assemblyFileName, /* pathIsBundleRelative */ true);
if (bundleFileLocation.IsValid())
{
hr = GetAssembly(assemblyFilePath,
TRUE, // fIsInTPA
&pTPAAssembly,
bundleFileLocation);
BinderTracing::PathProbed(assemblyFilePath, BinderTracing::PathSource::Bundle, hr);
if (hr != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
// Any other error is fatal
IF_FAIL_GO(hr);
if (TestCandidateRefMatchesDef(pRequestedAssemblyName, pTPAAssembly->GetAssemblyName(), true /*tpaListAssembly*/))
{
// We have found the requested assembly match in the bundle with validation of the full-qualified name.
// Bind to it.
pBindResult->SetResult(pTPAAssembly);
GO_WITH_HRESULT(S_OK);
}
}
}
}
}
// Is assembly on TPA list?
SimpleNameToFileNameMap * tpaMap = pApplicationContext->GetTpaList();
const SimpleNameToFileNameMapEntry *pTpaEntry = tpaMap->LookupPtr(simpleName.GetUnicode());
if (pTpaEntry != nullptr)
{
if (pTpaEntry->m_wszNIFileName != nullptr)
{
SString fileName(pTpaEntry->m_wszNIFileName);
hr = GetAssembly(fileName,
TRUE, // fIsInTPA
&pTPAAssembly);
BinderTracing::PathProbed(fileName, BinderTracing::PathSource::ApplicationAssemblies, hr);
}
else
{
_ASSERTE(pTpaEntry->m_wszILFileName != nullptr);
SString fileName(pTpaEntry->m_wszILFileName);
hr = GetAssembly(fileName,
TRUE, // fIsInTPA
&pTPAAssembly);
BinderTracing::PathProbed(fileName, BinderTracing::PathSource::ApplicationAssemblies, hr);
}
pBindResult->SetAttemptResult(hr, pTPAAssembly);
// On file not found, simply fall back to app path probing
if (hr != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
// Any other error is fatal
IF_FAIL_GO(hr);
if (TestCandidateRefMatchesDef(pRequestedAssemblyName, pTPAAssembly->GetAssemblyName(), true /*tpaListAssembly*/))
{
// We have found the requested assembly match on TPA with validation of the full-qualified name. Bind to it.
pBindResult->SetResult(pTPAAssembly);
pBindResult->SetAttemptResult(S_OK, pTPAAssembly);
GO_WITH_HRESULT(S_OK);
}
else
{
// We found the assembly on TPA but it didn't match the RequestedAssembly assembly-name. In this case, lets proceed to see if we find the requested
// assembly in the App paths.
pBindResult->SetAttemptResult(FUSION_E_REF_DEF_MISMATCH, pTPAAssembly);
fPartialMatchOnTpa = true;
}
}
// We either didn't find a candidate, or the ref-def failed. Either way; fall back to app path probing.
}
if (!excludeAppPaths)
{
// Probe AppPaths
ReleaseHolder<Assembly> pAssembly;
hr = BindAssemblyByProbingPaths(pApplicationContext->GetAppPaths(),
pRequestedAssemblyName,
&pAssembly);
pBindResult->SetAttemptResult(hr, pAssembly);
if (hr != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
IF_FAIL_GO(hr);
// At this point, we have found an assembly with the expected name in the App paths. If this was also found on TPA,
// make sure that the app assembly has the same fullname (excluding version) as the TPA version. If it does, then
// we should bind to the TPA assembly. If it does not, then bind to the app assembly since it has a different fullname than the
// TPA assembly.
if (fPartialMatchOnTpa)
{
if (TestCandidateRefMatchesDef(pAssembly->GetAssemblyName(), pTPAAssembly->GetAssemblyName(), true /*tpaListAssembly*/))
{
// Fullname (SimpleName+Culture+PKT) matched for TPA and app assembly - so bind to TPA instance.
pBindResult->SetResult(pTPAAssembly);
pBindResult->SetAttemptResult(hr, pTPAAssembly);
GO_WITH_HRESULT(S_OK);
}
else
{
// Fullname (SimpleName+Culture+PKT) did not match for TPA and app assembly - so bind to app instance.
pBindResult->SetResult(pAssembly);
GO_WITH_HRESULT(S_OK);
}
}
else
{
// We didn't see this assembly on TPA - so simply bind to the app instance.
pBindResult->SetResult(pAssembly);
GO_WITH_HRESULT(S_OK);
}
}
}
}
// Couldn't find a matching assembly in any of the probing paths
// Return S_FALSE here. BindByName will interpret a successful HRESULT
// and lack of BindResult as a failure to find a matching assembly.
hr = S_FALSE;
Exit:
return hr;
}
/* static */
HRESULT AssemblyBinderCommon::GetAssembly(SString &assemblyPath,
BOOL fIsInTPA,
Assembly **ppAssembly,
BundleFileLocation bundleFileLocation)
{