Skip to content

Commit

Permalink
shim coreclr policy between singlefile and standalone hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
VSadov committed Jun 9, 2020
1 parent 2dfb8c9 commit c03f1ad
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 123 deletions.
46 changes: 0 additions & 46 deletions src/installer/corehost/cli/apphost/static/hostfxr_resolver_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,6 @@ extern "C"
hostfxr_error_writer_fn HOSTFXR_CALLTYPE hostfxr_set_error_writer(hostfxr_error_writer_fn error_writer);
}

extern "C"
{
using host_handle_t = void*;
using domain_id_t = std::uint32_t;

pal::hresult_t STDMETHODCALLTYPE coreclr_initialize(
const char* exePath,
const char* appDomainFriendlyName,
int propertyCount,
const char** propertyKeys,
const char** propertyValues,
host_handle_t* hostHandle,
unsigned int* domainId);

pal::hresult_t STDMETHODCALLTYPE coreclr_shutdown(
host_handle_t hostHandle,
unsigned int domainId,
int* latchedExitCode);

pal::hresult_t STDMETHODCALLTYPE coreclr_execute_assembly(
host_handle_t hostHandle,
unsigned int domainId,
int argc,
const char** argv,
const char* managedAssemblyPath,
unsigned int* exitCode);

pal::hresult_t STDMETHODCALLTYPE coreclr_create_delegate(
host_handle_t hostHandle,
unsigned int domainId,
const char* entryPointAssemblyName,
const char* entryPointTypeName,
const char* entryPointMethodName,
void** delegate);
}

hostfxr_main_bundle_startupinfo_fn hostfxr_resolver_t::resolve_main_bundle_startupinfo()
{
assert(m_hostfxr_dll == nullptr);
Expand Down Expand Up @@ -78,16 +42,6 @@ hostfxr_main_fn hostfxr_resolver_t::resolve_main_v1()

hostfxr_resolver_t::hostfxr_resolver_t(const pal::string_t& app_root)
{
#if !defined(_WIN32)
// TODO: WIP this is just to make coreclr stuff "used"
// to see how linker handles this.
if (app_root.length() == 100000)
{
coreclr_initialize(nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr);
coreclr_execute_assembly(0, 0, 0, nullptr, nullptr, nullptr);
}
#endif

if (app_root.length() == 0)
{
trace::info(_X("Application root path is empty. This shouldn't happen"));
Expand Down
18 changes: 7 additions & 11 deletions src/installer/corehost/cli/apphost/static/hostpolicy_resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern "C"
int HOSTPOLICY_CALLTYPE corehost_load(const host_interface_t* init);
int HOSTPOLICY_CALLTYPE corehost_unload();
corehost_error_writer_fn HOSTPOLICY_CALLTYPE corehost_set_error_writer(corehost_error_writer_fn error_writer);
int HOSTPOLICY_CALLTYPE corehost_initialize(const corehost_initialize_request_t *init_request, int32_t options, /*out*/ corehost_context_contract *context_contract);
int HOSTPOLICY_CALLTYPE corehost_initialize(const corehost_initialize_request_t *init_request, uint32_t options, /*out*/ corehost_context_contract *context_contract);
int HOSTPOLICY_CALLTYPE corehost_main(const int argc, const pal::char_t* argv[]);
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);
}
Expand All @@ -24,20 +24,16 @@ int hostpolicy_resolver::load(
pal::dll_t* dll,
hostpolicy_contract_t &hostpolicy_contract)
{
static hostpolicy_contract_t contract;

trace::info(_X("Using internal hostpolicy"));

contract.load = corehost_load;
contract.unload = corehost_unload;
contract.set_error_writer = corehost_set_error_writer;
contract.initialize = corehost_initialize;
contract.corehost_main = corehost_main;
contract.corehost_main_with_output_buffer = corehost_main_with_output_buffer;
hostpolicy_contract.load = corehost_load;
hostpolicy_contract.unload = corehost_unload;
hostpolicy_contract.set_error_writer = corehost_set_error_writer;
hostpolicy_contract.initialize = corehost_initialize;
hostpolicy_contract.corehost_main = corehost_main;
hostpolicy_contract.corehost_main_with_output_buffer = corehost_main_with_output_buffer;

hostpolicy_contract = contract;
*dll = nullptr;

return StatusCode::Success;
}

Expand Down
2 changes: 1 addition & 1 deletion src/installer/corehost/cli/hostpolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef corehost_error_writer_fn(HOSTPOLICY_CALLTYPE *corehost_set_error_writer_

typedef int(HOSTPOLICY_CALLTYPE *corehost_initialize_fn)(
const corehost_initialize_request_t *init_request,
int32_t options,
uint32_t options,
corehost_context_contract *handle);

#endif //__HOSTPOLICY_H__
68 changes: 12 additions & 56 deletions src/installer/corehost/cli/hostpolicy/coreclr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,28 @@
#include <cassert>

#include "coreclr.h"
#include "coreclr_resolver_t.h"
#include <utils.h>
#include <error_codes.h>

// Prototype of the coreclr_initialize function from coreclr.dll
using coreclr_initialize_fn = pal::hresult_t(STDMETHODCALLTYPE *)(
const char* exePath,
const char* appDomainFriendlyName,
int propertyCount,
const char** propertyKeys,
const char** propertyValues,
coreclr_t::host_handle_t* hostHandle,
unsigned int* domainId);

// Prototype of the coreclr_shutdown function from coreclr.dll
using coreclr_shutdown_fn = pal::hresult_t(STDMETHODCALLTYPE *)(
coreclr_t::host_handle_t hostHandle,
unsigned int domainId,
int* latchedExitCode);

// Prototype of the coreclr_execute_assembly function from coreclr.dll
using coreclr_execute_assembly_fn = pal::hresult_t(STDMETHODCALLTYPE *)(
coreclr_t::host_handle_t hostHandle,
unsigned int domainId,
int argc,
const char** argv,
const char* managedAssemblyPath,
unsigned int* exitCode);

// Prototype of the coreclr_create_delegate function from coreclr.dll
using coreclr_create_delegate_fn = pal::hresult_t(STDMETHODCALLTYPE *)(
coreclr_t::host_handle_t hostHandle,
unsigned int domainId,
const char* entryPointAssemblyName,
const char* entryPointTypeName,
const char* entryPointMethodName,
void** delegate);

namespace
{
pal::dll_t g_coreclr = nullptr;
coreclr_shutdown_fn coreclr_shutdown = nullptr;
coreclr_initialize_fn coreclr_initialize = nullptr;
coreclr_execute_assembly_fn coreclr_execute_assembly = nullptr;
coreclr_create_delegate_fn coreclr_create_delegate = nullptr;

bool coreclr_bind(const pal::string_t& libcoreclr_path)
{
assert(g_coreclr == nullptr);

pal::string_t coreclr_dll_path(libcoreclr_path);
append_path(&coreclr_dll_path, LIBCORECLR_NAME);

if (!pal::load_library(&coreclr_dll_path, &g_coreclr))
{
return false;
}
assert(coreclr_initialize == nullptr);

coreclr_initialize = reinterpret_cast<coreclr_initialize_fn>(pal::get_symbol(g_coreclr, "coreclr_initialize"));
coreclr_shutdown = reinterpret_cast<coreclr_shutdown_fn>(pal::get_symbol(g_coreclr, "coreclr_shutdown_2"));
coreclr_execute_assembly = reinterpret_cast<coreclr_execute_assembly_fn>(pal::get_symbol(g_coreclr, "coreclr_execute_assembly"));
coreclr_create_delegate = reinterpret_cast<coreclr_create_delegate_fn>(pal::get_symbol(g_coreclr, "coreclr_create_delegate"));
coreclr_resolver_contract_t contract;
coreclr_resolver_t::resolve_coreclr(libcoreclr_path, contract);

assert(coreclr_initialize != nullptr
&& coreclr_shutdown != nullptr
&& coreclr_execute_assembly != nullptr
&& coreclr_create_delegate != nullptr);
coreclr_initialize = contract.coreclr_initialize;
coreclr_shutdown = contract.coreclr_shutdown;
coreclr_execute_assembly = contract.coreclr_execute_assembly;
coreclr_create_delegate = contract.coreclr_create_delegate;

return true;
}
Expand All @@ -89,7 +45,7 @@ pal::hresult_t coreclr_t::create(
return StatusCode::CoreClrBindFailure;
}

assert(g_coreclr != nullptr && coreclr_initialize != nullptr);
assert(coreclr_initialize != nullptr);

host_handle_t host_handle;
domain_id_t domain_id;
Expand Down Expand Up @@ -140,7 +96,7 @@ pal::hresult_t coreclr_t::execute_assembly(
const char* managed_assembly_path,
unsigned int* exit_code)
{
assert(g_coreclr != nullptr && coreclr_execute_assembly != nullptr);
assert(coreclr_execute_assembly != nullptr);

return coreclr_execute_assembly(
_host_handle,
Expand All @@ -157,7 +113,7 @@ pal::hresult_t coreclr_t::create_delegate(
const char* entryPointMethodName,
void** delegate)
{
assert(g_coreclr != nullptr && coreclr_execute_assembly != nullptr);
assert(coreclr_execute_assembly != nullptr);

return coreclr_create_delegate(
_host_handle,
Expand All @@ -170,7 +126,7 @@ pal::hresult_t coreclr_t::create_delegate(

pal::hresult_t coreclr_t::shutdown(int* latchedExitCode)
{
assert(g_coreclr != nullptr && coreclr_shutdown != nullptr);
assert(coreclr_shutdown != nullptr);

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

Expand Down
42 changes: 33 additions & 9 deletions src/installer/corehost/cli/hostpolicy/standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,45 @@ project(hostpolicy)

set(DOTNET_PROJECT_NAME "hostpolicy")

# CMake does not recommend using globbing since it messes with the freshness checks

# Can't call add_library() without source files. Create an empty .c file,
# then link with the static library just recently built.
if (NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp" "")
endif ()
# Include directories
include_directories(../../fxr)
include_directories(../../json)

# CMake does not recommend using globbing since it messes with the freshness checks
set(SOURCES
${CMAKE_CURRENT_BINARY_DIR}/empty.cpp
../args.cpp
../breadcrumbs.cpp
../coreclr.cpp
../deps_resolver.cpp
../hostpolicy_context.cpp
../hostpolicy.cpp
../hostpolicy_init.cpp
../../bundle/dir_utils.cpp
../../bundle/extractor.cpp
../../bundle/file_entry.cpp
../../bundle/manifest.cpp
../../bundle/runner.cpp
./coreclr_resolver_t.cpp
)

set(HEADERS
../args.h
../breadcrumbs.h
../coreclr.h
../deps_resolver.h
../hostpolicy_context.h
../hostpolicy_init.h
../../hostpolicy.h
../../corehost_context_contract.h
../../bundle/dir_utils.h
../../bundle/extractor.h
../../bundle/file_entry.h
../../bundle/manifest.h
../../bundle/runner.h
../../../coreclr_resolver_t.h
)


if(CLR_CMAKE_TARGET_WIN32)
list(APPEND SOURCES
hostpolicy.def)
Expand Down Expand Up @@ -56,4 +80,4 @@ if(CLR_CMAKE_HOST_UNIX)
endif(CLR_CMAKE_HOST_UNIX)

install_with_stripped_symbols(hostpolicy TARGETS corehost)
target_link_libraries(hostpolicy libhostcommon libhostpolicy_static)
target_link_libraries(hostpolicy libhostcommon)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include <cassert>
#include <error_codes.h>
#include <utils.h>
#include "coreclr_resolver_t.h"
#include <pal.h>
#include <trace.h>

bool coreclr_resolver_t::resolve_coreclr(const pal::string_t& libcoreclr_path, coreclr_resolver_contract_t& coreclr_resolver_contract)
{
pal::string_t coreclr_dll_path(libcoreclr_path);
append_path(&coreclr_dll_path, LIBCORECLR_NAME);

pal::dll_t g_coreclr = nullptr;
if (!pal::load_library(&coreclr_dll_path, &g_coreclr))
{
return false;
}

coreclr_resolver_contract.coreclr_initialize = reinterpret_cast<coreclr_initialize_fn>(pal::get_symbol(g_coreclr, "coreclr_initialize"));
coreclr_resolver_contract.coreclr_shutdown = reinterpret_cast<coreclr_shutdown_fn>(pal::get_symbol(g_coreclr, "coreclr_shutdown_2"));
coreclr_resolver_contract.coreclr_execute_assembly = reinterpret_cast<coreclr_execute_assembly_fn>(pal::get_symbol(g_coreclr, "coreclr_execute_assembly"));
coreclr_resolver_contract.coreclr_create_delegate = reinterpret_cast<coreclr_create_delegate_fn>(pal::get_symbol(g_coreclr, "coreclr_create_delegate"));

assert(coreclr_resolver_contract.coreclr_initialize != nullptr
&& coreclr_resolver_contract.coreclr_shutdown != nullptr
&& coreclr_resolver_contract.coreclr_execute_assembly != nullptr
&& coreclr_resolver_contract.coreclr_create_delegate != nullptr);

return true;
}
2 changes: 2 additions & 0 deletions src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(SOURCES
../../bundle/file_entry.cpp
../../bundle/manifest.cpp
../../bundle/runner.cpp
./coreclr_resolver_t.cpp
)

set(HEADERS
Expand All @@ -40,6 +41,7 @@ set(HEADERS
../../bundle/file_entry.h
../../bundle/manifest.h
../../bundle/runner.h
../../../coreclr_resolver_t.h
)

set(SKIP_VERSIONING 1)
Expand Down
Loading

0 comments on commit c03f1ad

Please sign in to comment.