Skip to content

Commit 441a977

Browse files
committed
Extend the Unix hosting API
This change modifies the Unix hosting API so that the hosting app can create as many managed delegates as it needs and execute them as many times it wants. The new API contains separate functions to initialize and shutdown CoreCLR and a function to create a delegate. The current ExecuteAssembly function behavior stays unmodified for now to ensure that dnx that uses that API and that pulls the binary libcoreclr is not broken. After the dnx is updated to use the new coreclr_create_delegate API, I'll remove the ExecuteAssembly. Also done: 1) Added support for comments and skipping empty lines in the mscorwks_unixexports.src. 2) Restructured the mscorwks_unixexports.src 3) Added coreclr_execute_assembly to the unixinterface.cpp / exports 4) Modified coreruncommon.cpp to use the new hosting API
1 parent bff51f7 commit 441a977

File tree

7 files changed

+292
-95
lines changed

7 files changed

+292
-95
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function(generate_exports_file inputFilename outputFilename)
121121
add_custom_command(
122122
OUTPUT ${outputFilename}
123123
COMMAND ${AWK} -f ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT} ${inputFilename} >${outputFilename}
124-
DEPENDS ${inputFilename}
124+
DEPENDS ${inputFilename} ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT}
125125
COMMENT "Generating exports file ${outputFilename}"
126126
)
127127
set_source_files_properties(${outputFilename}

generateexportedsymbols.awk

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
# Remove the CR character in case the sources are mapped from
33
# a Windows share and contain CRLF line endings
44
gsub(/\r/,"", $0);
5-
print "_" $0;
5+
6+
# Skip empty lines and comment lines starting with semicolon
7+
if (NF && !match($0, /^[:space:]*;/))
8+
{
9+
print "_" $0;
10+
}
611
}

generateversionscript.awk

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ BEGIN {
66
# Remove the CR character in case the sources are mapped from
77
# a Windows share and contain CRLF line endings
88
gsub(/\r/,"", $0);
9-
print " " $0 ";";
9+
10+
# Skip empty lines and comment lines starting with semicolon
11+
if (NF && !match($0, /^[:space:]*;/))
12+
{
13+
print " " $0 ";";
14+
}
1015
}
1116
END {
1217
print " local: *;"

src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp

+77-44
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,31 @@ static const char * const coreClrDll = "libcoreclr.dylib";
2424
static const char * const coreClrDll = "libcoreclr.so";
2525
#endif
2626

27-
// Windows types used by the ExecuteAssembly function
28-
typedef unsigned int DWORD;
29-
typedef const char16_t* LPCWSTR;
30-
typedef const char* LPCSTR;
31-
typedef int32_t HRESULT;
32-
33-
#define SUCCEEDED(Status) ((HRESULT)(Status) >= 0)
34-
35-
// Prototype of the ExecuteAssembly function from the libcoreclr.do
36-
typedef HRESULT (*ExecuteAssemblyFunction)(
37-
LPCSTR exePath,
38-
LPCSTR coreClrPath,
39-
LPCSTR appDomainFriendlyName,
40-
int propertyCount,
41-
LPCSTR* propertyKeys,
42-
LPCSTR* propertyValues,
43-
int argc,
44-
LPCSTR* argv,
45-
LPCSTR managedAssemblyPath,
46-
LPCSTR entryPointAssemblyName,
47-
LPCSTR entryPointTypeName,
48-
LPCSTR entryPointMethodsName,
49-
DWORD* exitCode);
27+
#define SUCCEEDED(Status) ((Status) >= 0)
28+
29+
// Prototype of the coreclr_initialize function from the libcoreclr.so
30+
typedef int (*InitializeCoreCLRFunction)(
31+
const char* exePath,
32+
const char* appDomainFriendlyName,
33+
int propertyCount,
34+
const char** propertyKeys,
35+
const char** propertyValues,
36+
void** hostHandle,
37+
unsigned int* domainId);
38+
39+
// Prototype of the coreclr_shutdown function from the libcoreclr.so
40+
typedef int (*ShutdownCoreCLRFunction)(
41+
void* hostHandle,
42+
unsigned int domainId);
43+
44+
// Prototype of the coreclr_execute_assembly function from the libcoreclr.so
45+
typedef int (*ExecuteAssemblyFunction)(
46+
void* hostHandle,
47+
unsigned int domainId,
48+
int argc,
49+
const char** argv,
50+
const char* managedAssemblyPath,
51+
unsigned int* exitCode);
5052

5153
bool GetAbsolutePath(const char* path, std::string& absolutePath)
5254
{
@@ -233,8 +235,23 @@ int ExecuteManagedAssembly(
233235
void* coreclrLib = dlopen(coreClrDllPath.c_str(), RTLD_NOW | RTLD_LOCAL);
234236
if (coreclrLib != nullptr)
235237
{
236-
ExecuteAssemblyFunction executeAssembly = (ExecuteAssemblyFunction)dlsym(coreclrLib, "ExecuteAssembly");
237-
if (executeAssembly != nullptr)
238+
InitializeCoreCLRFunction initializeCoreCLR = (InitializeCoreCLRFunction)dlsym(coreclrLib, "coreclr_initialize");
239+
ExecuteAssemblyFunction executeAssembly = (ExecuteAssemblyFunction)dlsym(coreclrLib, "coreclr_execute_assembly");
240+
ShutdownCoreCLRFunction shutdownCoreCLR = (ShutdownCoreCLRFunction)dlsym(coreclrLib, "coreclr_shutdown");
241+
242+
if (initializeCoreCLR == nullptr)
243+
{
244+
fprintf(stderr, "Function coreclr_initialize not found in the libcoreclr.so\n");
245+
}
246+
else if (executeAssembly == nullptr)
247+
{
248+
fprintf(stderr, "Function coreclr_execute_assembly not found in the libcoreclr.so\n");
249+
}
250+
else if (shutdownCoreCLR == nullptr)
251+
{
252+
fprintf(stderr, "Function coreclr_shutdown not found in the libcoreclr.so\n");
253+
}
254+
else
238255
{
239256
// Allowed property names:
240257
// APPBASE
@@ -272,30 +289,46 @@ int ExecuteManagedAssembly(
272289
"UseLatestBehaviorWhenTFMNotSpecified"
273290
};
274291

275-
HRESULT st = executeAssembly(
276-
currentExeAbsolutePath,
277-
coreClrDllPath.c_str(),
278-
"unixcorerun",
279-
sizeof(propertyKeys) / sizeof(propertyKeys[0]),
280-
propertyKeys,
281-
propertyValues,
282-
managedAssemblyArgc,
283-
managedAssemblyArgv,
284-
managedAssemblyAbsolutePath,
285-
NULL,
286-
NULL,
287-
NULL,
288-
(DWORD*)&exitCode);
292+
void* hostHandle;
293+
unsigned int domainId;
294+
295+
int st = initializeCoreCLR(
296+
currentExeAbsolutePath,
297+
"unixcorerun",
298+
sizeof(propertyKeys) / sizeof(propertyKeys[0]),
299+
propertyKeys,
300+
propertyValues,
301+
&hostHandle,
302+
&domainId);
289303

290304
if (!SUCCEEDED(st))
291305
{
292-
fprintf(stderr, "ExecuteAssembly failed - status: 0x%08x\n", st);
306+
fprintf(stderr, "coreclr_initialize failed - status: 0x%08x\n", st);
293307
exitCode = -1;
294308
}
295-
}
296-
else
297-
{
298-
fprintf(stderr, "Function ExecuteAssembly not found in the libcoreclr.so\n");
309+
else
310+
{
311+
st = executeAssembly(
312+
hostHandle,
313+
domainId,
314+
managedAssemblyArgc,
315+
managedAssemblyArgv,
316+
managedAssemblyAbsolutePath,
317+
(unsigned int*)&exitCode);
318+
319+
if (!SUCCEEDED(st))
320+
{
321+
fprintf(stderr, "coreclr_execute_assembly failed - status: 0x%08x\n", st);
322+
exitCode = -1;
323+
}
324+
325+
st = shutdownCoreCLR(hostHandle, domainId);
326+
if (!SUCCEEDED(st))
327+
{
328+
fprintf(stderr, "coreclr_shutdown failed - status: 0x%08x\n", st);
329+
exitCode = -1;
330+
}
331+
}
299332
}
300333

301334
if (dlclose(coreclrLib) != 0)

src/dlls/mscoree/mscorwks_unixexports.src

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1+
; Unix hosting API
2+
coreclr_create_delegate
3+
coreclr_execute_assembly
4+
coreclr_initialize
5+
coreclr_shutdown
6+
7+
; Obsolete Unix hosting API, to be removed
8+
ExecuteAssembly
9+
10+
; PAL initialization and module registration
11+
PAL_InitializeCoreCLR
12+
PAL_RegisterModule
13+
PAL_UnregisterModule
14+
15+
; Functions exported by the coreclr
16+
CoreDllMain
17+
DllMain
18+
GetCLRRuntimeHost
19+
20+
; Functions used by CoreFX
21+
EnsureOpenSslInitialized
22+
ForkAndExecProcess
23+
24+
; Win32 API and other PAL functions used by the mscorlib
125
CloseHandle
226
CoCreateGuid
327
CopyFileW
4-
CoreDllMain
528
CoTaskMemAlloc
629
CoTaskMemFree
730
CreateDirectoryW
@@ -10,19 +33,14 @@ CreateFileW
1033
CreateMutexW
1134
CreateSemaphoreW
1235
DeleteFileW
13-
DllMain
1436
DuplicateHandle
15-
EnsureOpenSslInitialized
16-
ExecuteAssembly
1737
FindClose
1838
FindFirstFileW
1939
FindNextFileW
2040
FlushFileBuffers
21-
ForkAndExecProcess
2241
FormatMessageW
2342
FreeEnvironmentStringsW
2443
GetACP
25-
GetCLRRuntimeHost
2644
GetConsoleCP
2745
GetConsoleOutputCP
2846
GetCurrentDirectoryW
@@ -53,10 +71,7 @@ MultiByteToWideChar
5371
OpenEventW
5472
OpenMutexW
5573
OpenSemaphoreW
56-
PAL_InitializeCoreCLR
5774
PAL_Random
58-
PAL_RegisterModule
59-
PAL_UnregisterModule
6075
QueryPerformanceCounter
6176
QueryPerformanceFrequency
6277
RaiseException

0 commit comments

Comments
 (0)