summaryrefslogtreecommitdiff
path: root/src/inc/fusionbind.h
blob: 8628d3f9cec101fc99eb2280f9acc0eb16b7bfe3 (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
// 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.



/*============================================================
**
** Header:  FusionBind.hpp
**
** Purpose: Implements FusionBind (loader domain) architecture
**
**
===========================================================*/
#ifndef _FUSIONBIND_H
#define _FUSIONBIND_H

#ifndef FEATURE_FUSION
#error FEATURE_FUSION is not enabled, please do not include fusionbind.h
#endif

#include <fusion.h>
#include <fusionpriv.h>
#include "metadata.h"
#include "fusionsink.h"
#include "utilcode.h"
#include "loaderheap.h"
#include "fusionsetup.h"
#include "sstring.h"
#include "ex.h"
#ifdef PAL_STDCPP_COMPAT
#include <type_traits>
#else
#include "clr_std/type_traits"
#endif

#include "binderngen.h"
#include "clrprivbinding.h"

class FusionBind
{
public:

    //****************************************************************************************
    //

    static HRESULT GetVersion(__out_ecount(*pdwVersion) LPWSTR pVersion, __inout DWORD* pdwVersion);
  
    
    //****************************************************************************************
    //
    // Creates a fusion context for the application domain. All ApplicationContext properties
    // must be set in the AppDomain store prior to this call. Any changes or additions to the
    // AppDomain store are ignored.
    static HRESULT CreateFusionContext(LPCWSTR pzName, IApplicationContext** ppFusionContext);


    //****************************************************************************************
    //
    // Loads an environmental value into the fusion context
    static HRESULT AddEnvironmentProperty(__in LPCWSTR variable, 
                                          __in LPCWSTR pProperty, 
                                          IApplicationContext* pFusionContext);

    //****************************************************************************************
    //
    static HRESULT SetupFusionContext(LPCWSTR szAppBase,
                                      LPCWSTR szPrivateBin,
                                      IApplicationContext** ppFusionContext);

    // Starts remote load of an assembly. The thread is parked on 
    // an event waiting for fusion to report success or failure.
    static HRESULT RemoteLoad(IApplicationContext * pFusionContext, 
                              FusionSink* pSink, 
                              IAssemblyName *pName, 
                              IAssembly *pParentAssembly,
                              LPCWSTR pCodeBase,
                              IAssembly** ppIAssembly,
                              IHostAssembly** ppIHostAssembly,
                              IBindResult** ppNativeFusionAssembly,
                              BOOL fForIntrospectionOnly,
                              BOOL fSuppressSecurityChecks);

    static HRESULT RemoteLoadModule(IApplicationContext * pFusionContext, 
                                    IAssemblyModuleImport* pModule, 
                                    FusionSink *pSink,
                                    IAssemblyModuleImport** pResult);

    static BOOL VerifyBindingStringW(LPCWSTR pwStr) {
        WRAPPER_NO_CONTRACT;
        if (wcschr(pwStr, '\\') ||
            wcschr(pwStr, '/') ||
            wcschr(pwStr, ':'))
            return FALSE;

        return TRUE;
    }

    static HRESULT VerifyBindingString(LPCSTR pName) {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            INJECT_FAULT(return E_OUTOFMEMORY;);
        }
        CONTRACTL_END;

        DWORD dwStrLen = WszMultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pName, -1, NULL, NULL);
        CQuickBytes qb;
        LPWSTR pwStr = (LPWSTR) qb.AllocNoThrow(dwStrLen*sizeof(WCHAR));
        if (!pwStr)
            return E_OUTOFMEMORY;
        
        if(!WszMultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, pName, -1, pwStr, dwStrLen))
            return HRESULT_FROM_GetLastError();

        if (VerifyBindingStringW(pwStr))
            return S_OK;
        else
            return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
    }

    static void GetAssemblyManifestModulePath(IAssembly *pFusionAssembly, SString &result)
    {
        CONTRACTL
        {
            THROWS;
            INJECT_FAULT(ThrowOutOfMemory());
        }
        CONTRACTL_END;

        DWORD dwSize = 0;
        LPWSTR buffer = NULL;
        COUNT_T allocation = result.GetUnicodeAllocation();
        if (allocation > 0) {
            // pass in the buffer if we got one
            dwSize = allocation + 1;
            buffer = result.OpenUnicodeBuffer(allocation);
        }
        HRESULT hr = pFusionAssembly->GetManifestModulePath(buffer, &dwSize);
        if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
        {
            if (buffer != NULL) 
                result.CloseBuffer(0);
            buffer = result.OpenUnicodeBuffer(dwSize-1);
            hr = pFusionAssembly->GetManifestModulePath(buffer, &dwSize);
        }
        if (buffer != NULL)
            result.CloseBuffer((SUCCEEDED(hr) && dwSize >= 1) ? (dwSize-1) : 0);
        IfFailThrow(hr);
    }

    static SString& GetAssemblyNameDisplayName(
        IAssemblyName *pName,
        SString &result,
        DWORD flags = 0 /* default */)
    {
        CONTRACTL
        {
            GC_NOTRIGGER;
            THROWS;
            INJECT_FAULT(ThrowOutOfMemory());
        }
        CONTRACTL_END;

        DWORD dwSize = 0;
        LPWSTR buffer = NULL;
        COUNT_T allocation = result.GetUnicodeAllocation();
        if (allocation > 0)
        {
            // pass in the buffer if we got one
            dwSize = allocation + 1;
            buffer = result.OpenUnicodeBuffer(allocation);
        }

        HRESULT hr = pName->GetDisplayName(buffer, &dwSize, flags);
        if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
        {
            if (buffer != NULL) 
                result.CloseBuffer(0);
            buffer = result.OpenUnicodeBuffer(dwSize-1);
            hr = pName->GetDisplayName(buffer, &dwSize, flags);
        }

        if (buffer != NULL)
        {
            result.CloseBuffer((SUCCEEDED(hr) && dwSize >= 1) ? (dwSize-1) : 0);
        }

        IfFailThrow(hr);
        return result;
    }

    static BOOL GetAssemblyNameStringProperty(IAssemblyName *pName, DWORD property, SString &result)
    {
        CONTRACTL
        {
            THROWS;
            INJECT_FAULT(ThrowOutOfMemory());
        }
        CONTRACTL_END;

        DWORD dwSize = 0;
        LPWSTR buffer = NULL;
        COUNT_T allocation = result.GetUnicodeAllocation();
        if (allocation > 0) {
            // pass in the buffer if we got one
            dwSize = (allocation + 1) * sizeof(WCHAR);
            buffer = result.OpenUnicodeBuffer(allocation);
        }
        HRESULT hr = pName->GetProperty(property, (LPVOID)buffer, &dwSize);
        if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
        {
            if (buffer != NULL) 
                result.CloseBuffer(0);
            buffer = result.OpenUnicodeBuffer(dwSize/sizeof(WCHAR) - 1);
            hr = pName->GetProperty(property, (LPVOID)buffer, &dwSize);
        }
        if (buffer != NULL)
            result.CloseBuffer((SUCCEEDED(hr) && dwSize >= sizeof(WCHAR)) ? (dwSize/sizeof(WCHAR)-1) : 0);
        if (hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
        {
            return FALSE;
        }
        IfFailThrow(hr);
              
        return TRUE;
    }

    static BOOL GetApplicationContextStringProperty(IApplicationContext *pContext, 
                                                    LPCWSTR property, SString &result)
    {
        CONTRACTL
        {
            THROWS;
            INJECT_FAULT(ThrowOutOfMemory());
        }
        CONTRACTL_END;

        DWORD dwSize = 0;
        LPWSTR buffer = NULL;
        COUNT_T allocation = result.GetUnicodeAllocation();
        if (allocation > 0) {
            // pass in the buffer if we got one
            dwSize = (allocation + 1) * sizeof(WCHAR);
            buffer = result.OpenUnicodeBuffer(allocation);
        }
        HRESULT hr = pContext->Get(property, (LPVOID)buffer, &dwSize, 0);
        if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
        {
            if (buffer != NULL) 
                result.CloseBuffer(0);
            buffer = result.OpenUnicodeBuffer(dwSize/sizeof(WCHAR) - 1);
            hr = pContext->Get(property, (LPVOID)buffer, &dwSize, 0);
        }
        if (buffer != NULL)
            result.CloseBuffer((SUCCEEDED(hr) && dwSize >= sizeof(WCHAR)) ? (dwSize/sizeof(WCHAR)-1) : 0);
        if (hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
        {
            return FALSE;
        }
        IfFailThrow(hr);

        return TRUE;
    }

    static BOOL GetApplicationContextDWORDProperty(IApplicationContext *pContext, 
                                                   LPCWSTR property, DWORD *result)
    {
        CONTRACTL
        {
            THROWS;
            INJECT_FAULT(return E_OUTOFMEMORY;);
        }
        CONTRACTL_END;

        DWORD dwSize = sizeof(DWORD);
        HRESULT hr = pContext->Get(property, result, &dwSize, 0);
        if (hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
            return FALSE;

        IfFailThrow(hr);
        
        return TRUE;
    }

    static void SetApplicationContextStringProperty(IApplicationContext *pContext, LPCWSTR property, 
                                                    SString &value)
    {
        CONTRACTL
        {
            THROWS;
            INJECT_FAULT(ThrowOutOfMemory());
        }
        CONTRACTL_END;

        IfFailThrow(pContext->Set(property, (void *) value.GetUnicode(), 
                                  (value.GetCount()+1)*sizeof(WCHAR), 0));
    }

    static void SetApplicationContextDWORDProperty(IApplicationContext *pContext, LPCWSTR property, 
                                                   DWORD value)
    {
        CONTRACTL
        {
            THROWS;
            INJECT_FAULT(ThrowOutOfMemory());
        }
        CONTRACTL_END;

        IfFailThrow(pContext->Set(property, &value, sizeof(value), 0));
    }
};

#endif