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
|
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// ============================================================
//
// ApplicationContext.cpp
//
//
// Implements the ApplicationContext class
//
// ============================================================
#ifndef FEATURE_CORESYSTEM
#define DISABLE_BINDER_DEBUG_LOGGING
#endif
#include "applicationcontext.hpp"
#include "stringarraylist.h"
#include "loadcontext.hpp"
#include "propertymap.hpp"
#include "failurecache.hpp"
#include "assemblyidentitycache.hpp"
#ifdef FEATURE_VERSIONING_LOG
#include "debuglog.hpp"
#endif // FEATURE_VERSIONING_LOG
#include "utils.hpp"
#include "variables.hpp"
#include "ex.h"
namespace BINDER_SPACE
{
namespace
{
void CopyIntoBuffer(/* in */ SBuffer *pPropertyValue,
/* in */ LPVOID pvValue,
/* in */ DWORD cbValue)
{
_ASSERTE(pPropertyValue != NULL);
BYTE *pRawBuffer = pPropertyValue->OpenRawBuffer(cbValue);
memcpy(pRawBuffer, pvValue, cbValue);
pPropertyValue->CloseRawBuffer();
}
const void *GetRawBuffer(SBuffer *pPropertyValue)
{
_ASSERTE(pPropertyValue != NULL);
// SBuffer provides const void *() operator
const void *pPropertyRawBuffer = *pPropertyValue;
_ASSERTE(pPropertyRawBuffer != NULL);
_ASSERTE(pPropertyRawBuffer != pPropertyValue);
return pPropertyRawBuffer;
}
HRESULT CheckRequiredBufferSize(/* in */ SBuffer *pPropertyValue,
/* in */ LPVOID pvValue,
/* in, out */ LPDWORD pcbValue)
{
_ASSERTE(pPropertyValue != NULL);
HRESULT hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
DWORD cbPropertySize = static_cast<DWORD>(pPropertyValue->GetSize());
if (pcbValue == NULL)
{
hr = E_INVALIDARG;
}
else if ((cbPropertySize <= *pcbValue) && (pvValue != NULL))
{
*pcbValue = cbPropertySize;
hr = S_OK;
}
else
{
*pcbValue = cbPropertySize;
}
return hr;
}
HRESULT CopyTextPropertyIntoBuffer(/* in */ SBuffer *pPropertyValue,
/* out */ LPWSTR wzPropertyBuffer,
/* in, out */ DWORD *pdwPropertyBufferSize)
{
HRESULT hr = S_OK;
void *pvValue = static_cast<void *>(wzPropertyBuffer);
DWORD cbValue = *pdwPropertyBufferSize * sizeof(WCHAR);
if ((hr = CheckRequiredBufferSize(pPropertyValue, pvValue, &cbValue)) == S_OK)
{
memcpy(pvValue, GetRawBuffer(pPropertyValue), cbValue);
}
// Adjust byte size to character count
_ASSERTE(cbValue % sizeof(WCHAR) == 0);
*pdwPropertyBufferSize = cbValue / sizeof(WCHAR);
return hr;
}
BOOL EndsWithPathSeparator(/* in */ PathString &path)
{
SString winDirSeparor(SString::Literal, W("\\"));
SString unixDirSeparor(SString::Literal, W("/"));
return (path.EndsWith(winDirSeparor) || path.EndsWith(unixDirSeparor));
}
};
STDMETHODIMP ApplicationContext::QueryInterface(REFIID riid,
void **ppv)
{
HRESULT hr = S_OK;
if (ppv == NULL)
{
hr = E_POINTER;
}
else
{
if (IsEqualIID(riid, IID_IUnknown))
{
AddRef();
*ppv = static_cast<IUnknown *>(this);
}
else
{
*ppv = NULL;
hr = E_NOINTERFACE;
}
}
return hr;
}
STDMETHODIMP_(ULONG) ApplicationContext::AddRef()
{
return InterlockedIncrement(&m_cRef);
}
STDMETHODIMP_(ULONG) ApplicationContext::Release()
{
ULONG ulRef = InterlockedDecrement(&m_cRef);
if (ulRef == 0)
{
delete this;
}
return ulRef;
}
ApplicationContext::ApplicationContext()
{
m_cRef = 1;
m_dwAppDomainId = 0;
m_pExecutionContext = NULL;
m_pInspectionContext = NULL;
m_pFailureCache = NULL;
m_contextCS = NULL;
m_pTrustedPlatformAssemblyMap = nullptr;
m_pFileNameHash = nullptr;
}
ApplicationContext::~ApplicationContext()
{
SAFE_RELEASE(m_pExecutionContext);
SAFE_RELEASE(m_pInspectionContext);
SAFE_DELETE(m_pFailureCache);
if (m_contextCS != NULL)
{
ClrDeleteCriticalSection(m_contextCS);
}
if (m_pTrustedPlatformAssemblyMap != nullptr)
{
delete m_pTrustedPlatformAssemblyMap;
}
if (m_pFileNameHash != nullptr)
{
delete m_pFileNameHash;
}
}
HRESULT ApplicationContext::Init()
{
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("ApplicationContext::Init"));
BINDER_LOG_POINTER(W("this"), this);
ReleaseHolder<ExecutionContext> pExecutionContext;
ReleaseHolder<InspectionContext> pInspectionContext;
PropertyMap *pPropertyMap = NULL;
FailureCache *pFailureCache = NULL;
// Allocate context objects
SAFE_NEW(pExecutionContext, ExecutionContext);
SAFE_NEW(pInspectionContext, InspectionContext);
SAFE_NEW(pFailureCache, FailureCache);
m_contextCS = ClrCreateCriticalSection(
CrstFusionAppCtx,
CRST_REENTRANCY);
if (!m_contextCS)
{
SAFE_DELETE(pPropertyMap);
SAFE_DELETE(pFailureCache);
hr = E_OUTOFMEMORY;
}
else
{
m_pExecutionContext = pExecutionContext.Extract();
m_pInspectionContext = pInspectionContext.Extract();
m_pFailureCache = pFailureCache;
}
#if defined(FEATURE_HOST_ASSEMBLY_RESOLVER)
m_fCanExplicitlyBindToNativeImages = false;
#endif // defined(FEATURE_HOST_ASSEMBLY_RESOLVER)
Exit:
BINDER_LOG_LEAVE_HR(W("ApplicationContext::Init"), hr);
return hr;
}
HRESULT GetNextPath(SString& paths, SString::Iterator& startPos, SString& outPath)
{
HRESULT hr = S_OK;
bool wrappedWithQuotes = false;
// Skip any leading spaces or path separators
while (paths.Skip(startPos, W(' ')) || paths.Skip(startPos, PATH_SEPARATOR_CHAR_W)) {}
if (startPos == paths.End())
{
// No more paths in the string and we just skipped over some white space
outPath.Set(W(""));
return S_FALSE;
}
// Support paths being wrapped with quotations
if (paths.Skip(startPos, W('\"')))
{
wrappedWithQuotes = true;
}
SString::Iterator iEnd = startPos; // Where current path ends
SString::Iterator iNext; // Where next path starts
if (wrappedWithQuotes)
{
if (paths.Find(iEnd, W('\"')))
{
iNext = iEnd;
// Find where the next path starts - there should be a path separator right after the closing quotation mark
if (paths.Find(iNext, PATH_SEPARATOR_CHAR_W))
{
iNext++;
}
else
{
iNext = paths.End();
}
}
else
{
// There was no terminating quotation mark - that's bad
GO_WITH_HRESULT(E_INVALIDARG);
}
}
else if (paths.Find(iEnd, PATH_SEPARATOR_CHAR_W))
{
iNext = iEnd + 1;
}
else
{
iNext = iEnd = paths.End();
}
// Skip any trailing spaces
while (iEnd[-1] == W(' '))
{
iEnd--;
}
_ASSERTE(startPos < iEnd);
outPath.Set(paths, startPos, iEnd);
startPos = iNext;
Exit:
return hr;
}
HRESULT ApplicationContext::SetupBindingPaths(SString &sTrustedPlatformAssemblies,
SString &sPlatformResourceRoots,
SString &sAppPaths,
SString &sAppNiPaths,
BOOL fAcquireLock)
{
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("ApplicationContext::SetupBindingPaths"));
BINDER_LOG_POINTER(W("this"), this);
#ifndef CROSSGEN_COMPILE
CRITSEC_Holder contextLock(fAcquireLock ? GetCriticalSectionCookie() : NULL);
#endif
if (m_pTrustedPlatformAssemblyMap != nullptr)
{
#if defined(BINDER_DEBUG_LOG)
BINDER_LOG(W("ApplicationContext::SetupBindingPaths: Binding paths already setup"));
#endif // BINDER_LOG_STRING
GO_WITH_HRESULT(S_OK);
}
//
// Parse TrustedPlatformAssemblies
//
m_pTrustedPlatformAssemblyMap = new SimpleNameToFileNameMap();
m_pFileNameHash = new TpaFileNameHash();
sTrustedPlatformAssemblies.Normalize();
for (SString::Iterator i = sTrustedPlatformAssemblies.Begin(); i != sTrustedPlatformAssemblies.End(); )
{
SString fileName;
HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextPath(sTrustedPlatformAssemblies, i, fileName));
if (pathResult == S_FALSE)
{
break;
}
// Find the beginning of the simple name
SString::Iterator iSimpleNameStart = fileName.End();
if (!fileName.FindBack(iSimpleNameStart, DIRECTORY_SEPARATOR_CHAR_W))
{
// Couldn't find a directory separator. File must have been specified as a relative path. Not allowed.
GO_WITH_HRESULT(E_INVALIDARG);
}
if (iSimpleNameStart == fileName.End())
{
GO_WITH_HRESULT(E_INVALIDARG);
}
// Advance past the directory separator to the first character of the file name
iSimpleNameStart++;
SString simpleName;
bool isNativeImage = false;
// GCC complains if we create SStrings inline as part of a function call
SString sNiDll(W(".ni.dll"));
SString sNiExe(W(".ni.exe"));
SString sNiWinmd(W(".ni.winmd"));
SString sDll(W(".dll"));
SString sExe(W(".exe"));
SString sWinmd(W(".winmd"));
if (fileName.EndsWithCaseInsensitive(sNiDll) ||
fileName.EndsWithCaseInsensitive(sNiExe))
{
simpleName.Set(fileName, iSimpleNameStart, fileName.End() - 7);
isNativeImage = true;
}
else if (fileName.EndsWithCaseInsensitive(sNiWinmd))
{
simpleName.Set(fileName, iSimpleNameStart, fileName.End() - 9);
isNativeImage = true;
}
else if (fileName.EndsWithCaseInsensitive(sDll) ||
fileName.EndsWithCaseInsensitive(sExe))
{
simpleName.Set(fileName, iSimpleNameStart, fileName.End() - 4);
}
else if (fileName.EndsWithCaseInsensitive(sWinmd))
{
simpleName.Set(fileName, iSimpleNameStart, fileName.End() - 6);
}
else
{
// Invalid filename
GO_WITH_HRESULT(E_INVALIDARG);
}
const SimpleNameToFileNameMapEntry *pExistingEntry = m_pTrustedPlatformAssemblyMap->LookupPtr(simpleName.GetUnicode());
if (pExistingEntry != nullptr)
{
//
// We want to store only the first entry matching a simple name we encounter.
// The exception is if we first store an IL reference and later in the string
// we encounter a native image. Since we don't touch IL in the presence of
// native images, we replace the IL entry with the NI.
//
if (pExistingEntry->m_wszILFileName != nullptr && !isNativeImage ||
pExistingEntry->m_wszNIFileName != nullptr && isNativeImage)
{
BINDER_LOG_STRING(W("ApplicationContext::SetupBindingPaths: Skipping TPA entry because of already existing IL/NI entry for short name "), fileName.GetUnicode());
continue;
}
}
LPWSTR wszSimpleName = nullptr;
if (pExistingEntry == nullptr)
{
wszSimpleName = new WCHAR[simpleName.GetCount() + 1];
if (wszSimpleName == nullptr)
{
GO_WITH_HRESULT(E_OUTOFMEMORY);
}
wcscpy_s(wszSimpleName, simpleName.GetCount() + 1, simpleName.GetUnicode());
}
else
{
wszSimpleName = pExistingEntry->m_wszSimpleName;
}
LPWSTR wszFileName = new WCHAR[fileName.GetCount() + 1];
if (wszFileName == nullptr)
{
GO_WITH_HRESULT(E_OUTOFMEMORY);
}
wcscpy_s(wszFileName, fileName.GetCount() + 1, fileName.GetUnicode());
SimpleNameToFileNameMapEntry mapEntry;
mapEntry.m_wszSimpleName = wszSimpleName;
if (isNativeImage)
{
mapEntry.m_wszNIFileName = wszFileName;
mapEntry.m_wszILFileName = pExistingEntry == nullptr ? nullptr : pExistingEntry->m_wszILFileName;
}
else
{
mapEntry.m_wszILFileName = wszFileName;
mapEntry.m_wszNIFileName = pExistingEntry == nullptr ? nullptr : pExistingEntry->m_wszNIFileName;
}
m_pTrustedPlatformAssemblyMap->AddOrReplace(mapEntry);
FileNameMapEntry fileNameExistenceEntry;
fileNameExistenceEntry.m_wszFileName = wszFileName;
m_pFileNameHash->AddOrReplace(fileNameExistenceEntry);
BINDER_LOG_STRING(W("ApplicationContext::SetupBindingPaths: Added TPA entry"), wszFileName);
}
//
// Parse PlatformResourceRoots
//
sPlatformResourceRoots.Normalize();
for (SString::Iterator i = sPlatformResourceRoots.Begin(); i != sPlatformResourceRoots.End(); )
{
SString pathName;
HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextPath(sPlatformResourceRoots, i, pathName));
if (pathResult == S_FALSE)
{
break;
}
m_platformResourceRoots.Append(pathName);
BINDER_LOG_STRING(W("ApplicationContext::SetupBindingPaths: Added resource root"), pathName);
}
//
// Parse AppPaths
//
sAppPaths.Normalize();
for (SString::Iterator i = sAppPaths.Begin(); i != sAppPaths.End(); )
{
SString pathName;
HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextPath(sAppPaths, i, pathName));
if (pathResult == S_FALSE)
{
break;
}
m_appPaths.Append(pathName);
BINDER_LOG_STRING(W("ApplicationContext::SetupBindingPaths: Added App Path"), pathName);
}
//
// Parse AppNiPaths
//
sAppNiPaths.Normalize();
for (SString::Iterator i = sAppNiPaths.Begin(); i != sAppNiPaths.End(); )
{
SString pathName;
HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextPath(sAppNiPaths, i, pathName));
if (pathResult == S_FALSE)
{
break;
}
m_appNiPaths.Append(pathName);
BINDER_LOG_STRING(W("ApplicationContext::SetupBindingPaths: Added App NI Path"), pathName);
}
Exit:
BINDER_LOG_LEAVE_HR(W("ApplicationContext::SetupBindingPaths"), hr);
return hr;
}
HRESULT ApplicationContext::GetAssemblyIdentity(LPCSTR szTextualIdentity,
AssemblyIdentityUTF8 **ppAssemblyIdentity)
{
HRESULT hr = S_OK;
BINDER_LOG_ENTER(W("ApplicationContext::GetAssemblyIdentity"));
BINDER_LOG_POINTER(W("this"), this);
_ASSERTE(szTextualIdentity != NULL);
_ASSERTE(ppAssemblyIdentity != NULL);
CRITSEC_Holder contextLock(GetCriticalSectionCookie());
AssemblyIdentityUTF8 *pAssemblyIdentity = m_assemblyIdentityCache.Lookup(szTextualIdentity);
if (pAssemblyIdentity == NULL)
{
NewHolder<AssemblyIdentityUTF8> pNewAssemblyIdentity;
SString sTextualIdentity;
SAFE_NEW(pNewAssemblyIdentity, AssemblyIdentityUTF8);
sTextualIdentity.SetUTF8(szTextualIdentity);
BOOL fWindowsPhone7 = false;
#ifdef FEATURE_LEGACYNETCF
fWindowsPhone7 = RuntimeIsLegacyNetCF(GetAppDomainId());
#endif // FEATURE_LEGACYNETCF
IF_FAIL_GO(TextualIdentityParser::Parse(sTextualIdentity, pNewAssemblyIdentity, fWindowsPhone7));
IF_FAIL_GO(m_assemblyIdentityCache.Add(szTextualIdentity, pNewAssemblyIdentity));
pNewAssemblyIdentity->PopulateUTF8Fields();
pAssemblyIdentity = pNewAssemblyIdentity.Extract();
}
*ppAssemblyIdentity = pAssemblyIdentity;
Exit:
BINDER_LOG_LEAVE_HR(W("ApplicationContext::GetAssemblyIdentity"), hr);
return hr;
}
bool ApplicationContext::IsTpaListProvided()
{
return m_pTrustedPlatformAssemblyMap != nullptr;
}
};
|