-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathexports.cpp
More file actions
515 lines (439 loc) · 16.5 KB
/
exports.cpp
File metadata and controls
515 lines (439 loc) · 16.5 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//*****************************************************************************
// exports.cpp
//
// Implementation for the interface exposed by coreclr
//
//*****************************************************************************
#include "stdafx.h"
#include <utilcode.h>
#include <corhost.h>
#include <configuration.h>
#include "../../vm/ceemain.h"
#if defined(TARGET_UNIX) && (defined(FEATURE_GDBJIT) || defined(FEATURE_PERFMAP))
#include "../../vm/gdbjithelpers.h"
#endif // TARGET_UNIX && (FEATURE_GDBJIT || FEATURE_PERFMAP)
#include "bundle.h"
#include "pinvokeoverride.h"
#include <hostinformation.h>
#include <corehost/host_runtime_contract.h>
#define ASSERTE_ALL_BUILDS(expr) _ASSERTE_ALL_BUILDS((expr))
#ifdef TARGET_UNIX
#define NO_HOSTING_API_RETURN_ADDRESS ((void*)UINTPTR_MAX)
void* g_hostingApiReturnAddress = NO_HOSTING_API_RETURN_ADDRESS;
class HostingApiFrameHolder
{
public:
HostingApiFrameHolder(void* returnAddress)
{
g_hostingApiReturnAddress = returnAddress;
}
~HostingApiFrameHolder()
{
g_hostingApiReturnAddress = NO_HOSTING_API_RETURN_ADDRESS;
}
};
#endif // TARGET_UNIX
// Holder for const wide strings
typedef NewArrayHolder<const WCHAR> ConstWStringHolder;
// Holder for array of wide strings
class ConstWStringArrayHolder : public NewArrayHolder<LPCWSTR>
{
int m_cElements;
public:
ConstWStringArrayHolder() :
NewArrayHolder<LPCWSTR>(),
m_cElements(0)
{
}
void Set(LPCWSTR* value, int cElements)
{
NewArrayHolder<LPCWSTR>::operator=(value);
m_cElements = cElements;
}
~ConstWStringArrayHolder()
{
for (int i = 0; i < m_cElements; i++)
{
delete [] this->m_value[i];
}
}
};
// Convert 8 bit string to unicode
static LPCWSTR StringToUnicode(LPCSTR str)
{
int length = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
ASSERTE_ALL_BUILDS(length != 0);
LPWSTR result = new (nothrow) WCHAR[length];
ASSERTE_ALL_BUILDS(result != NULL);
length = MultiByteToWideChar(CP_UTF8, 0, str, -1, result, length);
ASSERTE_ALL_BUILDS(length != 0);
return result;
}
// Convert 8 bit string array to unicode string array
static LPCWSTR* StringArrayToUnicode(int argc, LPCSTR* argv)
{
LPCWSTR* argvW = nullptr;
if (argc > 0)
{
argvW = new (nothrow) LPCWSTR[argc];
ASSERTE_ALL_BUILDS(argvW != 0);
for (int i = 0; i < argc; i++)
{
argvW[i] = StringToUnicode(argv[i]);
}
}
return argvW;
}
static void InitializeStartupFlags(STARTUP_FLAGS* startupFlagsRef)
{
STARTUP_FLAGS startupFlags = static_cast<STARTUP_FLAGS>(0);
if (Configuration::GetKnobBooleanValue(W("System.GC.Concurrent"), CLRConfig::UNSUPPORTED_gcConcurrent))
{
startupFlags = static_cast<STARTUP_FLAGS>(startupFlags | STARTUP_CONCURRENT_GC);
}
if (Configuration::GetKnobBooleanValue(W("System.GC.Server"), CLRConfig::UNSUPPORTED_gcServer))
{
startupFlags = static_cast<STARTUP_FLAGS>(startupFlags | STARTUP_SERVER_GC);
}
if (Configuration::GetKnobBooleanValue(W("System.GC.RetainVM"), CLRConfig::UNSUPPORTED_GCRetainVM))
{
startupFlags = static_cast<STARTUP_FLAGS>(startupFlags | STARTUP_HOARD_GC_VM);
}
*startupFlagsRef = startupFlags;
}
static void ConvertConfigPropertiesToUnicode(
const char** propertyKeys,
const char** propertyValues,
int propertyCount,
LPCWSTR** propertyKeysWRef,
LPCWSTR** propertyValuesWRef,
BundleProbeFn** bundleProbe,
PInvokeOverrideFn** pinvokeOverride,
host_runtime_contract** hostContract)
{
LPCWSTR* propertyKeysW = new (nothrow) LPCWSTR[propertyCount];
ASSERTE_ALL_BUILDS(propertyKeysW != nullptr);
LPCWSTR* propertyValuesW = new (nothrow) LPCWSTR[propertyCount];
ASSERTE_ALL_BUILDS(propertyValuesW != nullptr);
for (int propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
{
propertyKeysW[propertyIndex] = StringToUnicode(propertyKeys[propertyIndex]);
propertyValuesW[propertyIndex] = StringToUnicode(propertyValues[propertyIndex]);
if (strcmp(propertyKeys[propertyIndex], HOST_PROPERTY_BUNDLE_PROBE) == 0)
{
// If this application is a single-file bundle, the bundle-probe callback
// is passed in as the value of "BUNDLE_PROBE" property (encoded as a string).
// The function in HOST_RUNTIME_CONTRACT is given priority over this property,
// so we only set the bundle probe if it has not already been set.
if (*bundleProbe == nullptr)
*bundleProbe = (BundleProbeFn*)u16_strtoui64(propertyValuesW[propertyIndex], nullptr, 0);
}
else if (strcmp(propertyKeys[propertyIndex], HOST_PROPERTY_PINVOKE_OVERRIDE) == 0)
{
// If host provides a PInvoke override (typically in a single-file bundle),
// the override callback is passed in as the value of "PINVOKE_OVERRIDE" property (encoded as a string).
// The function in HOST_RUNTIME_CONTRACT is given priority over this property,
// so we only set the p/invoke override if it has not already been set.
if (*pinvokeOverride == nullptr)
*pinvokeOverride = (PInvokeOverrideFn*)u16_strtoui64(propertyValuesW[propertyIndex], nullptr, 0);
}
else if (strcmp(propertyKeys[propertyIndex], HOST_PROPERTY_RUNTIME_CONTRACT) == 0)
{
// Host contract is passed in as the value of HOST_RUNTIME_CONTRACT property (encoded as a string).
host_runtime_contract* hostContractLocal = (host_runtime_contract*)u16_strtoui64(propertyValuesW[propertyIndex], nullptr, 0);
*hostContract = hostContractLocal;
// Functions in HOST_RUNTIME_CONTRACT have priority over the individual properties
// for callbacks, so we set them as long as the contract has a non-null function.
if (hostContractLocal->bundle_probe != nullptr)
*bundleProbe = hostContractLocal->bundle_probe;
if (hostContractLocal->pinvoke_override != nullptr)
*pinvokeOverride = hostContractLocal->pinvoke_override;
}
}
*propertyKeysWRef = propertyKeysW;
*propertyValuesWRef = propertyValuesW;
}
coreclr_error_writer_callback_fn g_errorWriter = nullptr;
//
// Set callback for writing error logging
//
// Parameters:
// errorWriter - callback that will be called for each line of the error info
// - passing in NULL removes a callback that was previously set
//
// Returns:
// S_OK
//
extern "C"
DLLEXPORT
int coreclr_set_error_writer(coreclr_error_writer_callback_fn error_writer)
{
g_errorWriter = error_writer;
return S_OK;
}
#if defined(TARGET_UNIX) && (defined(FEATURE_GDBJIT) || defined(FEATURE_PERFMAP))
GetInfoForMethodDelegate getInfoForMethodDelegate = NULL;
extern "C" int coreclr_create_delegate(void*, unsigned int, const char*, const char*, const char*, void**);
#endif // TARGET_UNIX && (FEATURE_GDBJIT || FEATURE_PERFMAP)
//
// Initialize the CoreCLR. Creates and starts CoreCLR host and creates an app domain
//
// Parameters:
// exePath - Absolute path of the executable that invoked the ExecuteAssembly (the native host application)
// appDomainFriendlyName - Friendly name of the app domain that will be created to execute the assembly
// propertyCount - Number of properties (elements of the following two arguments)
// propertyKeys - Keys of properties of the app domain
// propertyValues - Values of properties of the app domain
// hostHandle - Output parameter, handle of the created host
// domainId - Output parameter, id of the created app domain
//
// Returns:
// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
//
extern "C"
NOINLINE
DLLEXPORT
int coreclr_initialize(
const char* exePath,
const char* appDomainFriendlyName,
int propertyCount,
const char** propertyKeys,
const char** propertyValues,
void** hostHandle,
unsigned int* domainId)
{
HRESULT hr;
LPCWSTR* propertyKeysW;
LPCWSTR* propertyValuesW;
BundleProbeFn* bundleProbe = nullptr;
PInvokeOverrideFn* pinvokeOverride = nullptr;
host_runtime_contract* hostContract = nullptr;
#ifdef TARGET_UNIX
HostingApiFrameHolder apiFrameHolder(_ReturnAddress());
#endif
ConvertConfigPropertiesToUnicode(
propertyKeys,
propertyValues,
propertyCount,
&propertyKeysW,
&propertyValuesW,
&bundleProbe,
&pinvokeOverride,
&hostContract);
#ifdef TARGET_UNIX
DWORD error = PAL_InitializeCoreCLR(exePath, g_coreclr_embedded);
hr = HRESULT_FROM_WIN32(error);
// If PAL initialization failed, then we should return right away and avoid
// calling any other APIs because they can end up calling into the PAL layer again.
if (FAILED(hr))
{
return hr;
}
#endif
if (hostContract != nullptr)
{
HostInformation::SetContract(hostContract);
}
if (pinvokeOverride != nullptr)
{
PInvokeOverride::SetPInvokeOverride(pinvokeOverride, PInvokeOverride::Source::RuntimeConfiguration);
}
ReleaseHolder<ICLRRuntimeHost4> host;
hr = CorHost2::CreateObject(IID_ICLRRuntimeHost4, (void**)&host);
IfFailRet(hr);
if (bundleProbe != nullptr)
{
static Bundle bundle(exePath, bundleProbe);
Bundle::AppBundle = &bundle;
}
// This will take ownership of propertyKeysWTemp and propertyValuesWTemp
Configuration::InitializeConfigurationKnobs(propertyCount, propertyKeysW, propertyValuesW);
#ifdef TARGET_UNIX
if (Configuration::GetKnobBooleanValue(W("System.Runtime.CrashReportBeforeSignalChaining"), CLRConfig::INTERNAL_CrashReportBeforeSignalChaining))
{
PAL_EnableCrashReportBeforeSignalChaining();
}
#endif
STARTUP_FLAGS startupFlags;
InitializeStartupFlags(&startupFlags);
hr = host->SetStartupFlags(startupFlags);
IfFailRet(hr);
hr = host->Start();
IfFailRet(hr);
ConstWStringHolder appDomainFriendlyNameW = StringToUnicode(appDomainFriendlyName);
hr = host->CreateAppDomainWithManager(
appDomainFriendlyNameW,
0,
NULL, // Name of the assembly that contains the AppDomainManager implementation
NULL, // The AppDomainManager implementation type name
propertyCount,
propertyKeysW,
propertyValuesW,
(DWORD *)domainId);
if (SUCCEEDED(hr))
{
host.SuppressRelease();
*hostHandle = host;
#if defined(TARGET_UNIX) && (defined(FEATURE_GDBJIT) || defined(FEATURE_PERFMAP))
HRESULT createDelegateResult;
createDelegateResult = coreclr_create_delegate(*hostHandle,
*domainId,
"System.Private.CoreLib",
"System.Diagnostics.StackTrace",
"GetInfoForMethod",
(void**)&getInfoForMethodDelegate);
#if defined(_DEBUG)
if (!SUCCEEDED(createDelegateResult))
{
fprintf(stderr,
"Can't create delegate for 'System.Diagnostics.StackTrace.GetInfoForMethod' "
"method - status: 0x%08x\n", createDelegateResult);
}
#endif // _DEBUG
#endif
}
return hr;
}
//
// Shutdown CoreCLR. It unloads the app domain and stops the CoreCLR host.
//
// Parameters:
// hostHandle - Handle of the host
// domainId - Id of the domain
//
// Returns:
// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
//
extern "C"
DLLEXPORT
int coreclr_shutdown(
void* hostHandle,
unsigned int domainId)
{
ReleaseHolder<ICLRRuntimeHost4> host(reinterpret_cast<ICLRRuntimeHost4*>(hostHandle));
HRESULT hr = host->UnloadAppDomain(domainId, true); // Wait until done
IfFailRet(hr);
hr = host->Stop();
#ifdef TARGET_UNIX
PAL_Shutdown();
#endif
return hr;
}
//
// Shutdown CoreCLR. It unloads the app domain and stops the CoreCLR host.
//
// Parameters:
// hostHandle - Handle of the host
// domainId - Id of the domain
// latchedExitCode - Latched exit code after domain unloaded
//
// Returns:
// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
//
extern "C"
DLLEXPORT
int coreclr_shutdown_2(
void* hostHandle,
unsigned int domainId,
int* latchedExitCode)
{
ReleaseHolder<ICLRRuntimeHost4> host(reinterpret_cast<ICLRRuntimeHost4*>(hostHandle));
HRESULT hr = host->UnloadAppDomain2(domainId, true, latchedExitCode); // Wait until done
IfFailRet(hr);
hr = host->Stop();
#ifdef TARGET_UNIX
PAL_Shutdown();
#endif
return hr;
}
//
// Create a native callable function pointer for a managed method.
//
// Parameters:
// hostHandle - Handle of the host
// domainId - Id of the domain
// entryPointAssemblyName - Name of the assembly which holds the custom entry point
// entryPointTypeName - Name of the type which holds the custom entry point
// entryPointMethodName - Name of the method which is the custom entry point
// delegate - Output parameter, the function stores a native callable function pointer to the delegate at the specified address
//
// Returns:
// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
//
extern "C"
DLLEXPORT
int coreclr_create_delegate(
void* hostHandle,
unsigned int domainId,
const char* entryPointAssemblyName,
const char* entryPointTypeName,
const char* entryPointMethodName,
void** delegate)
{
ICLRRuntimeHost4* host = reinterpret_cast<ICLRRuntimeHost4*>(hostHandle);
ConstWStringHolder entryPointAssemblyNameW = StringToUnicode(entryPointAssemblyName);
ConstWStringHolder entryPointTypeNameW = StringToUnicode(entryPointTypeName);
ConstWStringHolder entryPointMethodNameW = StringToUnicode(entryPointMethodName);
HRESULT hr = host->CreateDelegate(
domainId,
entryPointAssemblyNameW,
entryPointTypeNameW,
entryPointMethodNameW,
(INT_PTR*)delegate);
return hr;
}
//
// Execute a managed assembly with given arguments
//
// Parameters:
// hostHandle - Handle of the host
// domainId - Id of the domain
// argc - Number of arguments passed to the executed assembly
// argv - Array of arguments passed to the executed assembly
// managedAssemblyPath - Path of the managed assembly to execute (or NULL if using a custom entrypoint).
// exitCode - Exit code returned by the executed assembly
//
// Returns:
// HRESULT indicating status of the operation. S_OK if the assembly was successfully executed
//
extern "C"
NOINLINE
DLLEXPORT
int coreclr_execute_assembly(
void* hostHandle,
unsigned int domainId,
int argc,
const char** argv,
const char* managedAssemblyPath,
unsigned int* exitCode)
{
if (exitCode == NULL)
{
return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
}
*exitCode = -1;
#ifdef TARGET_UNIX
HostingApiFrameHolder apiFrameHolder(_ReturnAddress());
#endif
ICLRRuntimeHost4* host = reinterpret_cast<ICLRRuntimeHost4*>(hostHandle);
ConstWStringArrayHolder argvW;
argvW.Set(StringArrayToUnicode(argc, argv), argc);
ConstWStringHolder managedAssemblyPathW = StringToUnicode(managedAssemblyPath);
HRESULT hr = host->ExecuteAssembly(domainId, managedAssemblyPathW, argc, argvW, (DWORD *)exitCode);
IfFailRet(hr);
return hr;
}
void LogErrorToHost(const char* format, ...)
{
if (g_errorWriter != NULL)
{
char messageBuffer[1024];
va_list args;
va_start(args, format);
_vsnprintf_s(messageBuffer, ARRAY_SIZE(messageBuffer), _TRUNCATE, format, args);
g_errorWriter(messageBuffer);
va_end(args);
}
}