-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
CoreShim.cpp
415 lines (345 loc) · 11 KB
/
CoreShim.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "CoreShim.h"
#include <set>
#include <sstream>
#include <vector>
#include <mutex>
namespace
{
template<typename CT>
struct PathBuffer
{
PathBuffer()
: DefBuffer{}
, Buf{ DefBuffer }
, Len{ ARRAY_SIZE(DefBuffer) }
{ }
void SetLength(_In_ DWORD len)
{
if (len > Len)
{
Buf = BigBuffer.data();
Len = static_cast<DWORD>(BigBuffer.size());
}
}
void ExpandBuffer(_In_ DWORD factor = 2)
{
SetLength(Len * factor);
}
operator DWORD()
{
return Len;
}
operator CT *()
{
return Buf;
}
CT DefBuffer[MAX_PATH];
std::vector<CT> BigBuffer;
CT *Buf;
DWORD Len;
};
std::wstring GetExePath()
{
PathBuffer<WCHAR> buffer;
DWORD len = ::GetModuleFileNameW(nullptr, buffer, buffer);
while (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
buffer.ExpandBuffer();
len = ::GetModuleFileNameW(nullptr, buffer, buffer);
}
return std::wstring{ buffer.Buf, buffer.Buf + len };
}
bool TryGetEnvVar(_In_z_ const WCHAR* env, _Inout_ std::wstring& value)
{
DWORD len = ::GetEnvironmentVariableW(env, nullptr, 0);
if (len == 0)
return false;
PathBuffer<WCHAR> buffer;
buffer.SetLength(len);
(void)::GetEnvironmentVariableW(env, buffer, buffer);
value = static_cast<WCHAR *>(buffer.Buf);
return true;
}
std::wstring GetEnvVar(_In_z_ const WCHAR *env)
{
std::wstring value;
if (!TryGetEnvVar(env, value))
{
throw __HRESULT_FROM_WIN32(ERROR_ENVVAR_NOT_FOUND);
}
return value;
}
std::string ConvertWideToUtf8(_In_ const std::wstring &wide)
{
// [TODO] Properly convert to UTF-8
std::string narrow;
for (WCHAR p : wide)
narrow.push_back(static_cast<CHAR>(p));
return narrow;
}
coreclr *s_CoreClrInstance;
}
namespace Utility
{
HRESULT TryGetEnvVar(_In_z_ const WCHAR *env, _Inout_ std::wstring &envVar)
{
try
{
envVar = GetEnvVar(env);
}
catch (HRESULT hr)
{
return hr;
}
return S_OK;
}
HRESULT GetCoreShimDirectory(_Inout_ std::wstring &dir)
{
HMODULE hModule;
BOOL res = ::GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&TryGetEnvVar),
&hModule);
if (res == FALSE)
return HRESULT_FROM_WIN32(::GetLastError());
std::wstring path;
size_t dwModuleFileName = MAX_PATH / 2;
do
{
path.resize(dwModuleFileName * 2);
dwModuleFileName = GetModuleFileNameW(hModule, (LPWSTR)path.data(), static_cast<DWORD>(path.size()));
} while (dwModuleFileName == path.size());
if (dwModuleFileName == 0)
return HRESULT_FROM_WIN32(::GetLastError());
size_t idx = path.find_last_of(W('\\'));
if (idx == std::wstring::npos)
return E_UNEXPECTED;
path.resize(idx + 1);
dir = std::move(path);
return S_OK;
}
HRESULT GetCoreShimDirectory(_Inout_ std::string &dir)
{
HRESULT hr;
std::wstring dir_wide;
RETURN_IF_FAILED(GetCoreShimDirectory(dir_wide));
dir = ConvertWideToUtf8(dir_wide);
return S_OK;
}
}
bool TryLoadHostPolicy(const WCHAR* hostPolicyPath)
{
const WCHAR *hostpolicyName = W("hostpolicy.dll");
HMODULE hMod = ::GetModuleHandleW(hostpolicyName);
if (hMod != nullptr)
{
return true;
}
// Check if a hostpolicy exists and if it does, load it.
if (INVALID_FILE_ATTRIBUTES != ::GetFileAttributesW(hostPolicyPath))
{
hMod = ::LoadLibraryExW(hostPolicyPath, nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (hMod == nullptr)
return false;
// Initialize the hostpolicy mock to a default state
using Set_corehost_resolve_component_dependencies_Values_fn = void(__cdecl *)(
int returnValue,
const WCHAR *assemblyPaths,
const WCHAR *nativeSearchPaths,
const WCHAR *resourceSearchPaths);
auto set_comp_depend_values = (Set_corehost_resolve_component_dependencies_Values_fn)
::GetProcAddress(hMod, "Set_corehost_resolve_component_dependencies_Values");
assert(set_comp_depend_values != nullptr);
set_comp_depend_values(0, W(""), W(""), W(""));
}
return true;
}
HRESULT coreclr::GetCoreClrInstance(_Outptr_ coreclr **instance, _In_opt_z_ const WCHAR *path)
{
if (s_CoreClrInstance != nullptr)
{
*instance = s_CoreClrInstance;
return S_FALSE;
}
const wchar_t* mockHostPolicyEnvVar = W("MOCK_HOSTPOLICY");
std::wstring hostPolicyPath;
if (TryGetEnvVar(mockHostPolicyEnvVar, hostPolicyPath))
{
if (!TryLoadHostPolicy(hostPolicyPath.c_str()))
{
return E_UNEXPECTED;
}
}
try
{
std::wstring pathLocal;
if (path == nullptr)
{
pathLocal = GetEnvVar(W("CORE_ROOT"));
}
else
{
pathLocal = { path };
}
pathLocal.append(W("\\coreclr.dll"));
AutoModule hmod = ::LoadLibraryExW(pathLocal.c_str() , nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (hmod == nullptr)
return HRESULT_FROM_WIN32(::GetLastError());
s_CoreClrInstance = new coreclr{ std::move(hmod) };
}
catch (HRESULT hr)
{
return hr;
}
catch (const std::bad_alloc&)
{
return E_OUTOFMEMORY;
}
*instance = s_CoreClrInstance;
return S_OK;
}
HRESULT coreclr::CreateTpaList(_Inout_ std::string &tpaList, _In_opt_z_ const WCHAR *dir)
{
assert(tpaList.empty());
// Represents priority order
static const WCHAR * const tpaExtensions[] =
{
W(".ni.dll"),
W(".dll"),
W(".ni.exe"),
W(".exe"),
};
try
{
std::wstring w_dirLocal;
if (dir == nullptr)
{
w_dirLocal = GetEnvVar(W("CORE_ROOT"));
}
else
{
w_dirLocal = { dir };
}
std::string dirLocal = ConvertWideToUtf8(w_dirLocal);
w_dirLocal.append(W("\\*"));
std::set<std::wstring> addedAssemblies;
std::stringstream tpaStream;
// Walk the directory for each extension separately so assembly types
// are discovered in priority order - see above.
for (int extIndex = 0; extIndex < ARRAY_SIZE(tpaExtensions); extIndex++)
{
const WCHAR* ext = tpaExtensions[extIndex];
size_t extLength = ::wcslen(ext);
WIN32_FIND_DATAW ffd;
AutoFindFile sh = ::FindFirstFileW(w_dirLocal.c_str(), &ffd);
if (sh == nullptr)
break;
// For all entries in the directory
do
{
// Only examine non-directory entries
if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
std::wstring filename{ ffd.cFileName };
// Check if the extension matches
int extPos = static_cast<int>(filename.length() - extLength);
if ((extPos <= 0) || (filename.compare(extPos, extLength, ext) != 0))
{
continue;
}
std::wstring filenameWithoutExt{ filename.substr(0, extPos) };
// Only one type of a particular assembly instance should be inserted
// See extension list above.
if (addedAssemblies.find(filenameWithoutExt) == std::end(addedAssemblies))
{
addedAssemblies.insert(std::move(filenameWithoutExt));
std::string filename_utf8 = ConvertWideToUtf8(filename);
tpaStream << dirLocal << "\\" << filename_utf8 << ";";
}
}
} while (::FindNextFileW(sh, &ffd) != FALSE);
}
tpaList = tpaStream.str();
}
catch (HRESULT hr)
{
return hr;
}
catch (const std::bad_alloc&)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
coreclr::coreclr(_Inout_ AutoModule hmod)
: _hmod{ std::move(hmod) }
, _attached{ false }
, _clrInst{ nullptr }
, _appDomainId{ std::numeric_limits<uint32_t>::max() }
{
_initialize = (decltype(_initialize))::GetProcAddress(_hmod, "coreclr_initialize");
assert(_initialize != nullptr);
_create_delegate = (decltype(_create_delegate))::GetProcAddress(_hmod, "coreclr_create_delegate");
assert(_create_delegate != nullptr);
_shutdown = (decltype(_shutdown))::GetProcAddress(_hmod, "coreclr_shutdown");
assert(_shutdown != nullptr);
}
coreclr::~coreclr()
{
if (_clrInst != nullptr && !_attached)
{
HRESULT hr = _shutdown(_clrInst, _appDomainId);
assert(SUCCEEDED(hr));
(void)hr;
}
}
HRESULT coreclr::Initialize(
_In_ int propertyCount,
_In_reads_(propertyCount) const char **keys,
_In_reads_(propertyCount) const char **values,
_In_opt_z_ const char *appDomainName)
{
if (_clrInst != nullptr)
return __HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS);
if (appDomainName == nullptr)
appDomainName = "CoreShim";
HRESULT hr;
// Check if this is hosted scenario - launched via CoreRun.exe
HMODULE mod = ::GetModuleHandleW(W("CoreRun.exe"));
if (mod != NULL)
{
using GetCurrentClrDetailsFunc = HRESULT(__cdecl *)(void **clrInstance, unsigned int *appDomainId);
auto getCurrentClrDetails = (GetCurrentClrDetailsFunc)::GetProcAddress(mod, "GetCurrentClrDetails");
RETURN_IF_FAILED(getCurrentClrDetails(&_clrInst, &_appDomainId));
if (_clrInst != nullptr)
{
_attached = true;
return S_OK;
}
}
try
{
const std::wstring exePathW = GetExePath();
const std::string exePath = ConvertWideToUtf8(exePathW);
RETURN_IF_FAILED(_initialize(exePath.c_str(), appDomainName, propertyCount, keys, values, &_clrInst, &_appDomainId));
}
catch (const std::bad_alloc&)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
HRESULT coreclr::CreateDelegate(
_In_z_ const char *assembly,
_In_z_ const char *type,
_In_z_ const char *method,
_Out_ void **del)
{
if (_clrInst == nullptr)
return E_NOT_VALID_STATE;
HRESULT hr;
RETURN_IF_FAILED(_create_delegate(_clrInst, _appDomainId, assembly, type, method, del));
return S_OK;
}