summaryrefslogtreecommitdiff
path: root/src/vm/clrprivbinderutil.cpp
blob: dc534b3b677a4d310e1eb97f37b747dbb5c78c5c (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
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//

//
// Contains helper types for assembly binding host infrastructure.

#include "common.h"

#include "utilcode.h"
#include "strsafe.h"

#include "clrprivbinderutil.h"

inline
LPWSTR CopyStringThrowing(
    LPCWSTR wszString)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    NewArrayHolder<WCHAR> wszDup = NULL;
    if (wszString != NULL)
    {
        size_t wszLen = wcslen(wszString);
        wszDup = new WCHAR[wszLen + 1];
        IfFailThrow(StringCchCopy(wszDup, wszLen + 1, wszString));
    }
    wszDup.SuppressRelease();

    return wszDup;
}


namespace CLRPrivBinderUtil
{
#ifndef CLR_STANDALONE_BINDER
#ifdef FEATURE_FUSION
    //-----------------------------------------------------------------------------------------------------------------
    CLRPrivAssemblyBindResultWrapper::CLRPrivAssemblyBindResultWrapper(
        IAssemblyName *pIAssemblyName,
        PCWSTR wzAssemblyPath,
        IILFingerprintFactory *pILFingerprintFactory
        ) :
        m_wzAssemblyPath(DuplicateStringThrowing(wzAssemblyPath)),
        m_pIAssemblyName(clr::SafeAddRef(pIAssemblyName)),
        m_bIBindResultNISet(false),
        m_pIBindResultNI(nullptr),
        m_pIILFingerprint(nullptr),
        m_pILFingerprintFactory(clr::SafeAddRef(pILFingerprintFactory)),
        m_lock(CrstLeafLock)
    {
        STANDARD_VM_CONTRACT;
        VALIDATE_ARG_THROW(pIAssemblyName != nullptr && wzAssemblyPath != nullptr);
    }

    //-----------------------------------------------------------------------------------------------------------------
    CLRPrivAssemblyBindResultWrapper::~CLRPrivAssemblyBindResultWrapper()
    {
        clr::SafeRelease(m_pIAssemblyName);
        clr::SafeRelease(m_pIILFingerprint);
        clr::SafeRelease(m_pIBindResultNI);
    }

    //=================================================================================================================
    // IBindResult methods

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetAssemblyNameDef(
        /*out*/ IAssemblyName **ppIAssemblyNameDef)
    {
        LIMITED_METHOD_CONTRACT;
        
        VALIDATE_ARG_RET(ppIAssemblyNameDef != nullptr);
        *ppIAssemblyNameDef = clr::SafeAddRef(m_pIAssemblyName);
        return S_OK;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetNextAssemblyModuleName(
        /*in*/      DWORD   dwNIndex,
        __inout_ecount(*pdwCCModuleName)    LPWSTR  pwzModuleName,
        /*in, out, annotation("__inout")*/  LPDWORD pdwCCModuleName)
    {
        STANDARD_BIND_CONTRACT;
        _ASSERTE(!("E_NOTIMPL: " __FUNCTION__));
        return E_NOTIMPL;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetAssemblyLocation(
        /*out*/ IAssemblyLocation **ppIAssemblyLocation)
    {
        STANDARD_BIND_CONTRACT;
        VALIDATE_ARG_RET(ppIAssemblyLocation != nullptr);
        return this->QueryInterface(ppIAssemblyLocation);
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetNativeImage(
        /*out*/ IBindResult **ppIBindResultNI,
        /*out*/ BOOL         *pfIBindResultNIProbed)
    {
        LIMITED_METHOD_CONTRACT;

        // m_bIBindResultNISet must always be read *before* m_pIBindResultNI
        bool bIBindResultNISet = m_bIBindResultNISet;

        if (pfIBindResultNIProbed != nullptr)
            *pfIBindResultNIProbed = bIBindResultNISet;

        if (bIBindResultNISet && ppIBindResultNI != nullptr)
            *ppIBindResultNI = clr::SafeAddRef(m_pIBindResultNI);

        return S_OK;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::SetNativeImage(
        /*in*/  IBindResult  *pIBindResultNI,
        /*out*/ IBindResult **ppIBindResultNIFinal)
    {
        STANDARD_BIND_CONTRACT;
        HRESULT hr = S_OK;

        EX_TRY
        {
            // Native Binder needs S_FALSE returned if it loses the race.
            hr = S_FALSE;

            if (!m_bIBindResultNISet)
            {
                CrstHolder lock(&m_lock);
                if (!m_bIBindResultNISet)
                {
                    m_pIBindResultNI = clr::SafeAddRef(pIBindResultNI);
                    m_bIBindResultNISet = true;

                    // Won the race!
                    hr = S_OK;
                }
            }
        }
        EX_CATCH_HRESULT(hr);

        if (ppIBindResultNIFinal != nullptr)
            *ppIBindResultNIFinal = clr::SafeAddRef(m_pIBindResultNI);

        return hr;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::IsEqual(
        /*in*/ IUnknown *pIUnk)
    {
        STANDARD_BIND_CONTRACT;
        HRESULT hr = S_OK;

        VALIDATE_ARG_RET(pIUnk != nullptr);

        ReleaseHolder<IBindResult> pIBindResult;

        hr = pIUnk->QueryInterface(__uuidof(IBindResult), (void **)&pIBindResult);
        if (SUCCEEDED(hr))
        {
            hr = pIBindResult == static_cast<IBindResult*>(this) ? S_OK : S_FALSE;
        }
        else if (hr == E_NOINTERFACE)
        {
            hr = S_FALSE;
        }

        return hr;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetNextAssemblyNameRef(
        /*in*/  DWORD           dwNIndex,
        /*out*/ IAssemblyName **ppIAssemblyNameRef)
    {
        STANDARD_BIND_CONTRACT;
        _ASSERTE(!("E_UNEXPECTED: " __FUNCTION__));
        return E_UNEXPECTED;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetNextDependentAssembly(
        /*in*/  DWORD      dwNIndex,
        /*out*/ IUnknown **ppIUnknownAssembly)
    {
        STANDARD_BIND_CONTRACT;
        _ASSERTE(!("E_UNEXPECTED: " __FUNCTION__));
        return E_UNEXPECTED;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetAssemblyLocationOfILImage(
        /*out*/ IAssemblyLocation **ppAssemblyLocation)
    {
        LIMITED_METHOD_CONTRACT;
        return this->QueryInterface(ppAssemblyLocation);
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetILFingerprint(
        /*out*/ IILFingerprint **ppFingerprint)
    {
        STANDARD_BIND_CONTRACT;
        HRESULT hr = S_OK;

        VALIDATE_ARG_RET(ppFingerprint != nullptr);

        EX_TRY
        {
            *ppFingerprint = m_pIILFingerprint;
            if (*ppFingerprint == nullptr)
            {
                ReleaseHolder<IILFingerprint> pFingerprint;
                if (SUCCEEDED(hr = m_pILFingerprintFactory->GetILFingerprintForPath(GetILAssemblyPath(), &pFingerprint)))
                {
                    if (InterlockedCompareExchangeT<IILFingerprint>(&m_pIILFingerprint, pFingerprint, nullptr) == nullptr)
                    {
                        pFingerprint.SuppressRelease();
                    }
                }
            }
            *ppFingerprint = clr::SafeAddRef(m_pIILFingerprint);
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetSourceILTimestamp(
        /*out*/ FILETIME* pFileTime)
    {
        STANDARD_BIND_CONTRACT;
        HRESULT hr = S_OK;

        VALIDATE_ARG_RET(pFileTime != nullptr);

        EX_TRY
        {
            WIN32_FILE_ATTRIBUTE_DATA wfd;
            if (!WszGetFileAttributesEx(GetILAssemblyPath(), GetFileExInfoStandard, &wfd))
                ThrowLastError();
            *pFileTime = wfd.ftLastWriteTime;
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetSourceILSize(
        /*out*/ DWORD* pSize)
    {
        STANDARD_BIND_CONTRACT;
        HRESULT hr = S_OK;

        VALIDATE_ARG_RET(pSize != nullptr);

        EX_TRY
        {
            WIN32_FILE_ATTRIBUTE_DATA wfd;
            if (!WszGetFileAttributesEx(GetILAssemblyPath(), GetFileExInfoStandard, &wfd))
                ThrowLastError();
            if(wfd.nFileSizeHigh != 0)
                ThrowHR(COR_E_OVERFLOW);
            *pSize = wfd.nFileSizeLow;
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetNIInfo(
        /*out*/ INativeImageInstallInfo** pInfo)
    {
        STANDARD_BIND_CONTRACT;
        _ASSERTE(!("E_UNEXPECTED: " __FUNCTION__));
        return E_UNEXPECTED;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetFlags(
        /*out*/ DWORD * pdwFlags)
    {
        STANDARD_BIND_CONTRACT;
        PRECONDITION(CheckPointer(pdwFlags));
        if (pdwFlags == nullptr)
        {
            return E_POINTER;
        }

        // Currently, no effort is made to open assemblies and build a full IAssemblyName - this currently
        // only contains the simple name. Since AppX packages cannot be in-place updated we can be confident
        // that the binding environment will remain unchanged between NGEN and runtime. As such, return the
        // flag to indicate that the native image binder should skip self assembly definition validation.
        *pdwFlags = IBindResultFlag_AssemblyNameDefIncomplete;

        return S_OK;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetLocationType(
        /*out*/DWORD *pdwLocationType)
    {
        LIMITED_METHOD_CONTRACT;
        VALIDATE_ARG_RET(pdwLocationType != nullptr);
        
        if (pdwLocationType == nullptr)
            return E_INVALIDARG;
        *pdwLocationType = ASSEMBLY_LOCATION_PATH;
        return S_OK;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetPath(
        __inout_ecount(*pdwccAssemblyPath) LPWSTR  pwzAssemblyPath,
        /*in, annotation("__inout")*/      LPDWORD pdwccAssemblyPath)
    {
        STANDARD_BIND_CONTRACT;
        HRESULT hr = S_OK;

        VALIDATE_ARG_RET(pdwccAssemblyPath != nullptr);

        EX_TRY
        {
            DWORD cchILAssemblyPath = static_cast<DWORD>(wcslen(GetILAssemblyPath())) + 1;
            if (pwzAssemblyPath != nullptr && cchILAssemblyPath <= *pdwccAssemblyPath)
            {
                IfFailThrow(StringCchCopy(pwzAssemblyPath, *pdwccAssemblyPath, GetILAssemblyPath()));
                *pdwccAssemblyPath = cchILAssemblyPath;
                hr = S_OK;
            }
            else
            {
                *pdwccAssemblyPath = cchILAssemblyPath;
                hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
            }
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT CLRPrivAssemblyBindResultWrapper::GetHostID(
        /*out*/ UINT64 *puiHostID)
    {
        STANDARD_BIND_CONTRACT;
        _ASSERTE(!("E_UNEXPECTED: " __FUNCTION__));
        return E_UNEXPECTED;
    }
#endif //FEATURE_FUSION
#endif //!CLR_STANDALONE_BINDER

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT VerifyBind(
        IAssemblyName *pRefAssemblyName,
        ICLRPrivAssemblyInfo *pDefAssemblyInfo)
    {
        STANDARD_BIND_CONTRACT;

        HRESULT hr = S_OK;
        VALIDATE_PTR_RET(pRefAssemblyName);
        VALIDATE_PTR_RET(pDefAssemblyInfo);

        AssemblyIdentity refIdentity;
        IfFailRet(refIdentity.Initialize(pRefAssemblyName));

        AssemblyIdentity defIdentity;
        IfFailRet(defIdentity.Initialize(pDefAssemblyInfo));

        return VerifyBind(refIdentity, defIdentity);
    }

    //-----------------------------------------------------------------------------------------------------------------
    HRESULT VerifyBind(
        CLRPrivBinderUtil::AssemblyIdentity const & refIdentity,
        CLRPrivBinderUtil::AssemblyIdentity const & defIdentity)
    {
        LIMITED_METHOD_CONTRACT;

        //
        // Compare versions. Success conditions are the same as those in Silverlight:
        //  1. Reference identity has no version.
        //  2. Both identities have versions, and ref.version <= def.version.
        //
        // Since the default value of AssemblyVersion is 0.0.0.0, then if the
        // ref has no value set then the comparison will use 0.0.0.0, which will
        // always compare as true to the version contained in the def.
        //

        if (defIdentity.Version < refIdentity.Version)
        {   // Bound assembly has a lower version number than the reference.
            return CLR_E_BIND_ASSEMBLY_VERSION_TOO_LOW;
        }

        //
        // Compare public key tokens. Success conditions are:
        //  1. Reference identity has no PKT.
        //  2. Both identities have identical PKT values.
        //

        if (refIdentity.KeyToken.GetSize() != 0 &&          // Ref without PKT always passes.
            refIdentity.KeyToken != defIdentity.KeyToken)   // Otherwise Def must have matching PKT.
        {
            return CLR_E_BIND_ASSEMBLY_PUBLIC_KEY_MISMATCH;
        }

        return S_OK;
    }

    //---------------------------------------------------------------------------------------------
    CLRPrivResourcePathImpl::CLRPrivResourcePathImpl(LPCWSTR wzPath)
        : m_wzPath(CopyStringThrowing(wzPath))
    { STANDARD_VM_CONTRACT; }

    //---------------------------------------------------------------------------------------------
    HRESULT CLRPrivResourcePathImpl::GetPath(
        DWORD cchBuffer,
        LPDWORD pcchBuffer,
        __inout_ecount_part(cchBuffer, *pcchBuffer) LPWSTR wzBuffer)
    {
        LIMITED_METHOD_CONTRACT;
        HRESULT hr = S_OK;

        if (pcchBuffer == nullptr)
            IfFailRet(E_INVALIDARG);

        *pcchBuffer = (DWORD)wcslen(m_wzPath);

        if (wzBuffer != nullptr)
        {
            if (FAILED(StringCchCopy(wzBuffer, cchBuffer, m_wzPath)))
                IfFailRet(HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER));
        }
            
        return hr;
    }

    //---------------------------------------------------------------------------------------------
    CLRPrivResourceStreamImpl::CLRPrivResourceStreamImpl(IStream * pStream)
        : m_pStream(pStream)
    {
        LIMITED_METHOD_CONTRACT;
        pStream->AddRef();
    }

    //---------------------------------------------------------------------------------------------
    HRESULT CLRPrivResourceStreamImpl::GetStream(
        REFIID riid,
        LPVOID * ppvStream)
    {
        LIMITED_METHOD_CONTRACT;
        return m_pStream->QueryInterface(riid, ppvStream);
    }

    //---------------------------------------------------------------------------------------------
    HRESULT AssemblyVersion::Initialize(
        IAssemblyName * pAssemblyName)
    {
        WRAPPER_NO_CONTRACT;
        HRESULT hr = pAssemblyName->GetVersion(&dwMajorMinor, &dwBuildRevision);
        if (hr == FUSION_E_INVALID_NAME)
        {
            hr = S_FALSE;
        }
        return hr;
    }

    //---------------------------------------------------------------------------------------------
    HRESULT AssemblyVersion::Initialize(
        ICLRPrivAssemblyInfo * pAssemblyInfo)
    {
        WRAPPER_NO_CONTRACT;
        return pAssemblyInfo->GetAssemblyVersion(&wMajor, &wMinor, &wBuild, &wRevision);
    }

    //---------------------------------------------------------------------------------------------
    HRESULT PublicKey::Initialize(
        ICLRPrivAssemblyInfo * pAssemblyInfo)
    {
        LIMITED_METHOD_CONTRACT;
        HRESULT hr = S_OK;
        
        VALIDATE_PTR_RET(pAssemblyInfo);

        Uninitialize();

        DWORD cbKeyDef = 0;
        hr = pAssemblyInfo->GetAssemblyPublicKey(cbKeyDef, &cbKeyDef, nullptr);

        if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
        {
            if (cbKeyDef != 0)
            {
                NewArrayHolder<BYTE> pbKeyDef = new (nothrow) BYTE[cbKeyDef];
                IfNullRet(pbKeyDef);

                if (SUCCEEDED(hr = pAssemblyInfo->GetAssemblyPublicKey(cbKeyDef, &cbKeyDef, pbKeyDef)))
                {
                    m_key = pbKeyDef.Extract();
                    m_key_owned = true;
                    m_size = cbKeyDef;
                }
            }
        }

        return hr;
    }

    //---------------------------------------------------------------------------------------------
    HRESULT PublicKeyToken::Initialize(
        BYTE * pbKeyToken,
        DWORD cbKeyToken)
    {
        LIMITED_METHOD_CONTRACT;

        VALIDATE_CONDITION((pbKeyToken == nullptr) == (cbKeyToken == 0), return E_INVALIDARG);
        VALIDATE_ARG_RET(cbKeyToken == 0 || cbKeyToken == PUBLIC_KEY_TOKEN_LEN1);

        m_cbKeyToken = cbKeyToken;

        if (pbKeyToken != nullptr)
        {
            memcpy(m_rbKeyToken, pbKeyToken, PUBLIC_KEY_TOKEN_LEN1);
        }
        else
        {
            memset(m_rbKeyToken, 0, PUBLIC_KEY_TOKEN_LEN1);
        }

        return S_OK;
    }

    //---------------------------------------------------------------------------------------------
    HRESULT PublicKeyToken::Initialize(
        PublicKey const & pk)
    {
        LIMITED_METHOD_CONTRACT;

        StrongNameBufferHolder<BYTE>            pbKeyToken;
        DWORD                                   cbKeyToken;

        if (!StrongNameTokenFromPublicKey(const_cast<BYTE*>(pk.GetKey()), pk.GetSize(), &pbKeyToken, &cbKeyToken))
        {
            return static_cast<HRESULT>(StrongNameErrorInfo());
        }

        return Initialize(pbKeyToken, cbKeyToken);
    }

    //=====================================================================================================================
    HRESULT PublicKeyToken::Initialize(
        IAssemblyName * pName)
    {
        LIMITED_METHOD_CONTRACT;

        HRESULT hr = S_OK;

        DWORD cbKeyToken = sizeof(m_rbKeyToken);
        hr = pName->GetProperty(ASM_NAME_PUBLIC_KEY_TOKEN, m_rbKeyToken, &cbKeyToken);
        if (SUCCEEDED(hr))
        {
            m_cbKeyToken = cbKeyToken;
        }

        if (hr == FUSION_E_INVALID_NAME)
        {
            hr = S_FALSE;
        }

        return hr;
    }

    //=====================================================================================================================
    HRESULT PublicKeyToken::Initialize(
        ICLRPrivAssemblyInfo * pName)
    {
        LIMITED_METHOD_CONTRACT;

        HRESULT hr = S_OK;

        PublicKey pk;
        IfFailRet(pk.Initialize(pName));

        if (hr == S_OK) // Can return S_FALSE if no public key/token defined.
        {
            hr = Initialize(pk);
        }

        return hr;
    }

    //=====================================================================================================================
    bool operator==(
        PublicKeyToken const & lhs,
        PublicKeyToken const & rhs)
    {
        LIMITED_METHOD_CONTRACT;

        // Sizes must match
        if (lhs.GetSize() != rhs.GetSize())
        {
            return false;
        }

        // Empty PKT values are considered to be equal.
        if (lhs.GetSize() == 0)
        {
            return true;
        }

        // Compare values.
        return memcmp(lhs.GetToken(), rhs.GetToken(), lhs.GetSize()) == 0;
    }

    //=====================================================================================================================
    HRESULT AssemblyIdentity::Initialize(
        LPCWSTR wzName)
    {
        LIMITED_METHOD_CONTRACT;
        return StringCchCopy(Name, sizeof(Name) / sizeof(Name[0]), wzName);
    }

    //=====================================================================================================================
    HRESULT AssemblyIdentity::Initialize(
        ICLRPrivAssemblyInfo * pAssemblyInfo)
    {
        STANDARD_BIND_CONTRACT;
        HRESULT hr = S_OK;

        DWORD cchName = sizeof(Name) / sizeof(Name[0]);
        IfFailRet(pAssemblyInfo->GetAssemblyName(cchName, &cchName, Name));
        IfFailRet(Version.Initialize(pAssemblyInfo));
        IfFailRet(KeyToken.Initialize(pAssemblyInfo));

        return hr;
    }

    //=====================================================================================================================
    HRESULT AssemblyIdentity::Initialize(
        IAssemblyName * pAssemblyName)
    {
        STANDARD_BIND_CONTRACT;
        HRESULT hr = S_OK;

        DWORD cchName = sizeof(Name) / sizeof(Name[0]);
        IfFailRet(pAssemblyName->GetName(&cchName, Name));
        IfFailRet(Version.Initialize(pAssemblyName));
        IfFailRet(KeyToken.Initialize(pAssemblyName));

        return hr;
    }

#ifdef FEATURE_FUSION
#ifndef CLR_STANDALONE_BINDER
    //=====================================================================================================================
    HRESULT AssemblyIdentity::Initialize(
        AssemblySpec * pSpec)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            FORBID_FAULT;
            MODE_ANY;
            CAN_TAKE_LOCK;
        }
        CONTRACTL_END

        HRESULT hr = S_OK;

        if (0 == WszMultiByteToWideChar(
            CP_UTF8, 0 /*flags*/, pSpec->GetName(), -1, Name, (int) (sizeof(Name) / sizeof(Name[0]))))
        {
            return HRESULT_FROM_GetLastError();
        }

        AssemblyMetaDataInternal * pAMDI = pSpec->GetContext();
        if (pAMDI != nullptr)
        {
            Version.wMajor = pAMDI->usMajorVersion;
            Version.wMinor = pAMDI->usMinorVersion;
            Version.wBuild = pAMDI->usBuildNumber;
            Version.wRevision = pAMDI->usRevisionNumber;
        }

        if (pSpec->HasPublicKeyToken())
        {
            PBYTE pbKey;
            DWORD cbKey;
            pSpec->GetPublicKeyToken(&pbKey, &cbKey);
            IfFailRet(KeyToken.Initialize(pbKey, cbKey));
        }

        return hr;
    }
#endif //!CLR_STANDALONE_BINDER
#endif

    
    //=====================================================================================================================
    // Destroys list of strings (code:WStringList).
    void 
    WStringList_Delete(
        WStringList * pList)
    {
        LIMITED_METHOD_CONTRACT;
        
        if (pList != nullptr)
        {
            for (WStringListElem * pElem = pList->RemoveHead(); pElem != nullptr; pElem = pList->RemoveHead())
            {
                // Delete the string
                delete [] pElem->GetValue();
                delete pElem;
            }
            
            delete pList;
        }
    }


////////////////////////////////////////////////////////////////////////////////////////////////////
///// ----------------------------- Direct calls to VM  -------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////
#if !defined(CLR_STANDALONE_BINDER) && defined(FEATURE_APPX_BINDER)
    ICLRPrivAssembly* RaiseAssemblyResolveEvent(IAssemblyName *pAssemblyName, ICLRPrivAssembly* pRequestingAssembly)
    {
        CONTRACT(ICLRPrivAssembly*)
        {
            THROWS;
            GC_TRIGGERS;
            MODE_ANY;
            PRECONDITION(AppX::IsAppXProcess());
            PRECONDITION(AppDomain::GetCurrentDomain()->IsDefaultDomain());
            POSTCONDITION(CheckPointer(RETVAL, NULL_OK));
            INJECT_FAULT(COMPlusThrowOM(););
        }
        CONTRACT_END;

        BinderMethodID methodId;

        methodId = METHOD__APP_DOMAIN__ON_ASSEMBLY_RESOLVE;  // post-bind execution event (the classic V1.0 event)

        // Elevate threads allowed loading level.  This allows the host to load an assembly even in a restricted
        // condition.  Note, however, that this exposes us to possible recursion failures, if the host tries to
        // load the assemblies currently being loaded.  (Such cases would then throw an exception.)

        OVERRIDE_LOAD_LEVEL_LIMIT(FILE_ACTIVE);
        OVERRIDE_TYPE_LOAD_LEVEL_LIMIT(CLASS_LOADED);

        DomainAssembly* pDomainAssembly = AppDomain::GetCurrentDomain()->FindAssembly(pRequestingAssembly);

        GCX_COOP();

        Assembly* pAssembly = NULL;

        struct _gc {
            OBJECTREF AppDomainRef;
            OBJECTREF AssemblyRef;
            STRINGREF str;
        } gc;
        ZeroMemory(&gc, sizeof(gc));

        SString ssAssemblyName;
        FusionBind::GetAssemblyNameDisplayName(pAssemblyName, ssAssemblyName);

        GCPROTECT_BEGIN(gc);
        if ((gc.AppDomainRef = GetAppDomain()->GetRawExposedObject()) != NULL)
        {
            gc.AssemblyRef = pDomainAssembly->GetExposedAssemblyObject();

            MethodDescCallSite onAssemblyResolve(methodId, &gc.AppDomainRef);

            gc.str = StringObject::NewString(ssAssemblyName.GetUnicode());
            ARG_SLOT args[3] = {
                ObjToArgSlot(gc.AppDomainRef),
                ObjToArgSlot(gc.AssemblyRef),
                ObjToArgSlot(gc.str)
            };
            ASSEMBLYREF ResultingAssemblyRef = (ASSEMBLYREF) onAssemblyResolve.Call_RetOBJECTREF(args);
            if (ResultingAssemblyRef != NULL)
            {
                pAssembly = ResultingAssemblyRef->GetAssembly();
            }
        }
        GCPROTECT_END();

        if (pAssembly != NULL)
        {
            if (pAssembly->IsIntrospectionOnly())
            {
                // Cannot return an introspection assembly from an execution callback or vice-versa
                COMPlusThrow(kFileLoadException, IDS_CLASSLOAD_ASSEMBLY_RESOLVE_RETURNED_INTROSPECTION );
            }
            if (pAssembly->IsCollectible())
            {
                COMPlusThrow(kNotSupportedException, W("NotSupported_CollectibleAssemblyResolve"));
            }

            // Check that the public key token matches the one specified in the spec
            // MatchPublicKeys throws as appropriate.
            
            StackScratchBuffer ssBuffer;
            AssemblySpec spec;
            IfFailThrow(spec.Init(ssAssemblyName.GetUTF8(ssBuffer)));
            spec.MatchPublicKeys(pAssembly);

        }

        if (pAssembly == nullptr)
            ThrowHR(COR_E_FILENOTFOUND);

        RETURN  pAssembly->GetManifestFile()->GetHostAssembly();
    }

    BOOL CompareHostBinderSpecs(AssemblySpec * a1, AssemblySpec * a2)
    {
        WRAPPER_NO_CONTRACT;
        return a1->CompareEx(a2, AssemblySpec::ASC_Default);
    }
#endif //!CLR_STANDALONE_BINDER && FEATURE_APPX
} // namespace CLRPrivBinderUtil