Skip to content

Commit c03f1ad

Browse files
committed
shim coreclr policy between singlefile and standalone hosts
1 parent 2dfb8c9 commit c03f1ad

File tree

9 files changed

+234
-123
lines changed

9 files changed

+234
-123
lines changed

src/installer/corehost/cli/apphost/static/hostfxr_resolver_t.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,6 @@ extern "C"
1515
hostfxr_error_writer_fn HOSTFXR_CALLTYPE hostfxr_set_error_writer(hostfxr_error_writer_fn error_writer);
1616
}
1717

18-
extern "C"
19-
{
20-
using host_handle_t = void*;
21-
using domain_id_t = std::uint32_t;
22-
23-
pal::hresult_t STDMETHODCALLTYPE coreclr_initialize(
24-
const char* exePath,
25-
const char* appDomainFriendlyName,
26-
int propertyCount,
27-
const char** propertyKeys,
28-
const char** propertyValues,
29-
host_handle_t* hostHandle,
30-
unsigned int* domainId);
31-
32-
pal::hresult_t STDMETHODCALLTYPE coreclr_shutdown(
33-
host_handle_t hostHandle,
34-
unsigned int domainId,
35-
int* latchedExitCode);
36-
37-
pal::hresult_t STDMETHODCALLTYPE coreclr_execute_assembly(
38-
host_handle_t hostHandle,
39-
unsigned int domainId,
40-
int argc,
41-
const char** argv,
42-
const char* managedAssemblyPath,
43-
unsigned int* exitCode);
44-
45-
pal::hresult_t STDMETHODCALLTYPE coreclr_create_delegate(
46-
host_handle_t hostHandle,
47-
unsigned int domainId,
48-
const char* entryPointAssemblyName,
49-
const char* entryPointTypeName,
50-
const char* entryPointMethodName,
51-
void** delegate);
52-
}
53-
5418
hostfxr_main_bundle_startupinfo_fn hostfxr_resolver_t::resolve_main_bundle_startupinfo()
5519
{
5620
assert(m_hostfxr_dll == nullptr);
@@ -78,16 +42,6 @@ hostfxr_main_fn hostfxr_resolver_t::resolve_main_v1()
7842

7943
hostfxr_resolver_t::hostfxr_resolver_t(const pal::string_t& app_root)
8044
{
81-
#if !defined(_WIN32)
82-
// TODO: WIP this is just to make coreclr stuff "used"
83-
// to see how linker handles this.
84-
if (app_root.length() == 100000)
85-
{
86-
coreclr_initialize(nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr);
87-
coreclr_execute_assembly(0, 0, 0, nullptr, nullptr, nullptr);
88-
}
89-
#endif
90-
9145
if (app_root.length() == 0)
9246
{
9347
trace::info(_X("Application root path is empty. This shouldn't happen"));

src/installer/corehost/cli/apphost/static/hostpolicy_resolver.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern "C"
1414
int HOSTPOLICY_CALLTYPE corehost_load(const host_interface_t* init);
1515
int HOSTPOLICY_CALLTYPE corehost_unload();
1616
corehost_error_writer_fn HOSTPOLICY_CALLTYPE corehost_set_error_writer(corehost_error_writer_fn error_writer);
17-
int HOSTPOLICY_CALLTYPE corehost_initialize(const corehost_initialize_request_t *init_request, int32_t options, /*out*/ corehost_context_contract *context_contract);
17+
int HOSTPOLICY_CALLTYPE corehost_initialize(const corehost_initialize_request_t *init_request, uint32_t options, /*out*/ corehost_context_contract *context_contract);
1818
int HOSTPOLICY_CALLTYPE corehost_main(const int argc, const pal::char_t* argv[]);
1919
int HOSTPOLICY_CALLTYPE corehost_main_with_output_buffer(const int argc, const pal::char_t* argv[], pal::char_t buffer[], int32_t buffer_size, int32_t* required_buffer_size);
2020
}
@@ -24,20 +24,16 @@ int hostpolicy_resolver::load(
2424
pal::dll_t* dll,
2525
hostpolicy_contract_t &hostpolicy_contract)
2626
{
27-
static hostpolicy_contract_t contract;
28-
2927
trace::info(_X("Using internal hostpolicy"));
3028

31-
contract.load = corehost_load;
32-
contract.unload = corehost_unload;
33-
contract.set_error_writer = corehost_set_error_writer;
34-
contract.initialize = corehost_initialize;
35-
contract.corehost_main = corehost_main;
36-
contract.corehost_main_with_output_buffer = corehost_main_with_output_buffer;
29+
hostpolicy_contract.load = corehost_load;
30+
hostpolicy_contract.unload = corehost_unload;
31+
hostpolicy_contract.set_error_writer = corehost_set_error_writer;
32+
hostpolicy_contract.initialize = corehost_initialize;
33+
hostpolicy_contract.corehost_main = corehost_main;
34+
hostpolicy_contract.corehost_main_with_output_buffer = corehost_main_with_output_buffer;
3735

38-
hostpolicy_contract = contract;
3936
*dll = nullptr;
40-
4137
return StatusCode::Success;
4238
}
4339

src/installer/corehost/cli/hostpolicy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ typedef corehost_error_writer_fn(HOSTPOLICY_CALLTYPE *corehost_set_error_writer_
3535

3636
typedef int(HOSTPOLICY_CALLTYPE *corehost_initialize_fn)(
3737
const corehost_initialize_request_t *init_request,
38-
int32_t options,
38+
uint32_t options,
3939
corehost_context_contract *handle);
4040

4141
#endif //__HOSTPOLICY_H__

src/installer/corehost/cli/hostpolicy/coreclr.cpp

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,28 @@
55
#include <cassert>
66

77
#include "coreclr.h"
8+
#include "coreclr_resolver_t.h"
89
#include <utils.h>
910
#include <error_codes.h>
1011

11-
// Prototype of the coreclr_initialize function from coreclr.dll
12-
using coreclr_initialize_fn = pal::hresult_t(STDMETHODCALLTYPE *)(
13-
const char* exePath,
14-
const char* appDomainFriendlyName,
15-
int propertyCount,
16-
const char** propertyKeys,
17-
const char** propertyValues,
18-
coreclr_t::host_handle_t* hostHandle,
19-
unsigned int* domainId);
20-
21-
// Prototype of the coreclr_shutdown function from coreclr.dll
22-
using coreclr_shutdown_fn = pal::hresult_t(STDMETHODCALLTYPE *)(
23-
coreclr_t::host_handle_t hostHandle,
24-
unsigned int domainId,
25-
int* latchedExitCode);
26-
27-
// Prototype of the coreclr_execute_assembly function from coreclr.dll
28-
using coreclr_execute_assembly_fn = pal::hresult_t(STDMETHODCALLTYPE *)(
29-
coreclr_t::host_handle_t hostHandle,
30-
unsigned int domainId,
31-
int argc,
32-
const char** argv,
33-
const char* managedAssemblyPath,
34-
unsigned int* exitCode);
35-
36-
// Prototype of the coreclr_create_delegate function from coreclr.dll
37-
using coreclr_create_delegate_fn = pal::hresult_t(STDMETHODCALLTYPE *)(
38-
coreclr_t::host_handle_t hostHandle,
39-
unsigned int domainId,
40-
const char* entryPointAssemblyName,
41-
const char* entryPointTypeName,
42-
const char* entryPointMethodName,
43-
void** delegate);
44-
4512
namespace
4613
{
47-
pal::dll_t g_coreclr = nullptr;
4814
coreclr_shutdown_fn coreclr_shutdown = nullptr;
4915
coreclr_initialize_fn coreclr_initialize = nullptr;
5016
coreclr_execute_assembly_fn coreclr_execute_assembly = nullptr;
5117
coreclr_create_delegate_fn coreclr_create_delegate = nullptr;
5218

5319
bool coreclr_bind(const pal::string_t& libcoreclr_path)
5420
{
55-
assert(g_coreclr == nullptr);
56-
57-
pal::string_t coreclr_dll_path(libcoreclr_path);
58-
append_path(&coreclr_dll_path, LIBCORECLR_NAME);
59-
60-
if (!pal::load_library(&coreclr_dll_path, &g_coreclr))
61-
{
62-
return false;
63-
}
21+
assert(coreclr_initialize == nullptr);
6422

65-
coreclr_initialize = reinterpret_cast<coreclr_initialize_fn>(pal::get_symbol(g_coreclr, "coreclr_initialize"));
66-
coreclr_shutdown = reinterpret_cast<coreclr_shutdown_fn>(pal::get_symbol(g_coreclr, "coreclr_shutdown_2"));
67-
coreclr_execute_assembly = reinterpret_cast<coreclr_execute_assembly_fn>(pal::get_symbol(g_coreclr, "coreclr_execute_assembly"));
68-
coreclr_create_delegate = reinterpret_cast<coreclr_create_delegate_fn>(pal::get_symbol(g_coreclr, "coreclr_create_delegate"));
23+
coreclr_resolver_contract_t contract;
24+
coreclr_resolver_t::resolve_coreclr(libcoreclr_path, contract);
6925

70-
assert(coreclr_initialize != nullptr
71-
&& coreclr_shutdown != nullptr
72-
&& coreclr_execute_assembly != nullptr
73-
&& coreclr_create_delegate != nullptr);
26+
coreclr_initialize = contract.coreclr_initialize;
27+
coreclr_shutdown = contract.coreclr_shutdown;
28+
coreclr_execute_assembly = contract.coreclr_execute_assembly;
29+
coreclr_create_delegate = contract.coreclr_create_delegate;
7430

7531
return true;
7632
}
@@ -89,7 +45,7 @@ pal::hresult_t coreclr_t::create(
8945
return StatusCode::CoreClrBindFailure;
9046
}
9147

92-
assert(g_coreclr != nullptr && coreclr_initialize != nullptr);
48+
assert(coreclr_initialize != nullptr);
9349

9450
host_handle_t host_handle;
9551
domain_id_t domain_id;
@@ -140,7 +96,7 @@ pal::hresult_t coreclr_t::execute_assembly(
14096
const char* managed_assembly_path,
14197
unsigned int* exit_code)
14298
{
143-
assert(g_coreclr != nullptr && coreclr_execute_assembly != nullptr);
99+
assert(coreclr_execute_assembly != nullptr);
144100

145101
return coreclr_execute_assembly(
146102
_host_handle,
@@ -157,7 +113,7 @@ pal::hresult_t coreclr_t::create_delegate(
157113
const char* entryPointMethodName,
158114
void** delegate)
159115
{
160-
assert(g_coreclr != nullptr && coreclr_execute_assembly != nullptr);
116+
assert(coreclr_execute_assembly != nullptr);
161117

162118
return coreclr_create_delegate(
163119
_host_handle,
@@ -170,7 +126,7 @@ pal::hresult_t coreclr_t::create_delegate(
170126

171127
pal::hresult_t coreclr_t::shutdown(int* latchedExitCode)
172128
{
173-
assert(g_coreclr != nullptr && coreclr_shutdown != nullptr);
129+
assert(coreclr_shutdown != nullptr);
174130

175131
std::lock_guard<std::mutex> lock{ _shutdown_lock };
176132

src/installer/corehost/cli/hostpolicy/standalone/CMakeLists.txt

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,45 @@ project(hostpolicy)
66

77
set(DOTNET_PROJECT_NAME "hostpolicy")
88

9-
# CMake does not recommend using globbing since it messes with the freshness checks
10-
11-
# Can't call add_library() without source files. Create an empty .c file,
12-
# then link with the static library just recently built.
13-
if (NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp")
14-
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp" "")
15-
endif ()
9+
# Include directories
10+
include_directories(../../fxr)
11+
include_directories(../../json)
1612

13+
# CMake does not recommend using globbing since it messes with the freshness checks
1714
set(SOURCES
18-
${CMAKE_CURRENT_BINARY_DIR}/empty.cpp
15+
../args.cpp
16+
../breadcrumbs.cpp
17+
../coreclr.cpp
18+
../deps_resolver.cpp
19+
../hostpolicy_context.cpp
20+
../hostpolicy.cpp
21+
../hostpolicy_init.cpp
22+
../../bundle/dir_utils.cpp
23+
../../bundle/extractor.cpp
24+
../../bundle/file_entry.cpp
25+
../../bundle/manifest.cpp
26+
../../bundle/runner.cpp
27+
./coreclr_resolver_t.cpp
1928
)
2029

2130
set(HEADERS
31+
../args.h
32+
../breadcrumbs.h
33+
../coreclr.h
34+
../deps_resolver.h
35+
../hostpolicy_context.h
36+
../hostpolicy_init.h
37+
../../hostpolicy.h
38+
../../corehost_context_contract.h
39+
../../bundle/dir_utils.h
40+
../../bundle/extractor.h
41+
../../bundle/file_entry.h
42+
../../bundle/manifest.h
43+
../../bundle/runner.h
44+
../../../coreclr_resolver_t.h
2245
)
2346

47+
2448
if(CLR_CMAKE_TARGET_WIN32)
2549
list(APPEND SOURCES
2650
hostpolicy.def)
@@ -56,4 +80,4 @@ if(CLR_CMAKE_HOST_UNIX)
5680
endif(CLR_CMAKE_HOST_UNIX)
5781

5882
install_with_stripped_symbols(hostpolicy TARGETS corehost)
59-
target_link_libraries(hostpolicy libhostcommon libhostpolicy_static)
83+
target_link_libraries(hostpolicy libhostcommon)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#include <cassert>
6+
#include <error_codes.h>
7+
#include <utils.h>
8+
#include "coreclr_resolver_t.h"
9+
#include <pal.h>
10+
#include <trace.h>
11+
12+
bool coreclr_resolver_t::resolve_coreclr(const pal::string_t& libcoreclr_path, coreclr_resolver_contract_t& coreclr_resolver_contract)
13+
{
14+
pal::string_t coreclr_dll_path(libcoreclr_path);
15+
append_path(&coreclr_dll_path, LIBCORECLR_NAME);
16+
17+
pal::dll_t g_coreclr = nullptr;
18+
if (!pal::load_library(&coreclr_dll_path, &g_coreclr))
19+
{
20+
return false;
21+
}
22+
23+
coreclr_resolver_contract.coreclr_initialize = reinterpret_cast<coreclr_initialize_fn>(pal::get_symbol(g_coreclr, "coreclr_initialize"));
24+
coreclr_resolver_contract.coreclr_shutdown = reinterpret_cast<coreclr_shutdown_fn>(pal::get_symbol(g_coreclr, "coreclr_shutdown_2"));
25+
coreclr_resolver_contract.coreclr_execute_assembly = reinterpret_cast<coreclr_execute_assembly_fn>(pal::get_symbol(g_coreclr, "coreclr_execute_assembly"));
26+
coreclr_resolver_contract.coreclr_create_delegate = reinterpret_cast<coreclr_create_delegate_fn>(pal::get_symbol(g_coreclr, "coreclr_create_delegate"));
27+
28+
assert(coreclr_resolver_contract.coreclr_initialize != nullptr
29+
&& coreclr_resolver_contract.coreclr_shutdown != nullptr
30+
&& coreclr_resolver_contract.coreclr_execute_assembly != nullptr
31+
&& coreclr_resolver_contract.coreclr_create_delegate != nullptr);
32+
33+
return true;
34+
}

src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ set(SOURCES
2424
../../bundle/file_entry.cpp
2525
../../bundle/manifest.cpp
2626
../../bundle/runner.cpp
27+
./coreclr_resolver_t.cpp
2728
)
2829

2930
set(HEADERS
@@ -40,6 +41,7 @@ set(HEADERS
4041
../../bundle/file_entry.h
4142
../../bundle/manifest.h
4243
../../bundle/runner.h
44+
../../../coreclr_resolver_t.h
4345
)
4446

4547
set(SKIP_VERSIONING 1)

0 commit comments

Comments
 (0)