From c03f1ad6802aa5321bea5e24525af918b3569517 Mon Sep 17 00:00:00 2001 From: vsadov Date: Tue, 9 Jun 2020 15:04:45 -0700 Subject: [PATCH] shim coreclr policy between singlefile and standalone hosts --- .../cli/apphost/static/hostfxr_resolver_t.cpp | 46 ---------- .../apphost/static/hostpolicy_resolver.cpp | 18 ++-- src/installer/corehost/cli/hostpolicy.h | 2 +- .../corehost/cli/hostpolicy/coreclr.cpp | 68 +++------------ .../cli/hostpolicy/standalone/CMakeLists.txt | 42 ++++++++-- .../standalone/coreclr_resolver_t.cpp | 34 ++++++++ .../cli/hostpolicy/static/CMakeLists.txt | 2 + .../hostpolicy/static/coreclr_resolver_t.cpp | 84 +++++++++++++++++++ src/installer/corehost/coreclr_resolver_t.h | 61 ++++++++++++++ 9 files changed, 234 insertions(+), 123 deletions(-) create mode 100644 src/installer/corehost/cli/hostpolicy/standalone/coreclr_resolver_t.cpp create mode 100644 src/installer/corehost/cli/hostpolicy/static/coreclr_resolver_t.cpp create mode 100644 src/installer/corehost/coreclr_resolver_t.h diff --git a/src/installer/corehost/cli/apphost/static/hostfxr_resolver_t.cpp b/src/installer/corehost/cli/apphost/static/hostfxr_resolver_t.cpp index c6ef9429df84a..2c7e2b87a5f6d 100644 --- a/src/installer/corehost/cli/apphost/static/hostfxr_resolver_t.cpp +++ b/src/installer/corehost/cli/apphost/static/hostfxr_resolver_t.cpp @@ -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); @@ -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")); diff --git a/src/installer/corehost/cli/apphost/static/hostpolicy_resolver.cpp b/src/installer/corehost/cli/apphost/static/hostpolicy_resolver.cpp index 0b2a35639a2e1..210fbe65113a2 100644 --- a/src/installer/corehost/cli/apphost/static/hostpolicy_resolver.cpp +++ b/src/installer/corehost/cli/apphost/static/hostpolicy_resolver.cpp @@ -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); } @@ -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; } diff --git a/src/installer/corehost/cli/hostpolicy.h b/src/installer/corehost/cli/hostpolicy.h index 9ff2a4a34ea00..05f78fe32a025 100644 --- a/src/installer/corehost/cli/hostpolicy.h +++ b/src/installer/corehost/cli/hostpolicy.h @@ -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__ diff --git a/src/installer/corehost/cli/hostpolicy/coreclr.cpp b/src/installer/corehost/cli/hostpolicy/coreclr.cpp index 78a2021de05d3..e0d5822d324d8 100644 --- a/src/installer/corehost/cli/hostpolicy/coreclr.cpp +++ b/src/installer/corehost/cli/hostpolicy/coreclr.cpp @@ -5,46 +5,12 @@ #include #include "coreclr.h" +#include "coreclr_resolver_t.h" #include #include -// 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; @@ -52,25 +18,15 @@ namespace 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(pal::get_symbol(g_coreclr, "coreclr_initialize")); - coreclr_shutdown = reinterpret_cast(pal::get_symbol(g_coreclr, "coreclr_shutdown_2")); - coreclr_execute_assembly = reinterpret_cast(pal::get_symbol(g_coreclr, "coreclr_execute_assembly")); - coreclr_create_delegate = reinterpret_cast(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; } @@ -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; @@ -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, @@ -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, @@ -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 lock{ _shutdown_lock }; diff --git a/src/installer/corehost/cli/hostpolicy/standalone/CMakeLists.txt b/src/installer/corehost/cli/hostpolicy/standalone/CMakeLists.txt index d6631543f81b5..40c94ccd18269 100644 --- a/src/installer/corehost/cli/hostpolicy/standalone/CMakeLists.txt +++ b/src/installer/corehost/cli/hostpolicy/standalone/CMakeLists.txt @@ -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) @@ -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) diff --git a/src/installer/corehost/cli/hostpolicy/standalone/coreclr_resolver_t.cpp b/src/installer/corehost/cli/hostpolicy/standalone/coreclr_resolver_t.cpp new file mode 100644 index 0000000000000..8a6f17107a635 --- /dev/null +++ b/src/installer/corehost/cli/hostpolicy/standalone/coreclr_resolver_t.cpp @@ -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 +#include +#include +#include "coreclr_resolver_t.h" +#include +#include + +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(pal::get_symbol(g_coreclr, "coreclr_initialize")); + coreclr_resolver_contract.coreclr_shutdown = reinterpret_cast(pal::get_symbol(g_coreclr, "coreclr_shutdown_2")); + coreclr_resolver_contract.coreclr_execute_assembly = reinterpret_cast(pal::get_symbol(g_coreclr, "coreclr_execute_assembly")); + coreclr_resolver_contract.coreclr_create_delegate = reinterpret_cast(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; +} diff --git a/src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt b/src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt index 6845e369bdac3..85ddcae97e33e 100644 --- a/src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt +++ b/src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt @@ -24,6 +24,7 @@ set(SOURCES ../../bundle/file_entry.cpp ../../bundle/manifest.cpp ../../bundle/runner.cpp + ./coreclr_resolver_t.cpp ) set(HEADERS @@ -40,6 +41,7 @@ set(HEADERS ../../bundle/file_entry.h ../../bundle/manifest.h ../../bundle/runner.h + ../../../coreclr_resolver_t.h ) set(SKIP_VERSIONING 1) diff --git a/src/installer/corehost/cli/hostpolicy/static/coreclr_resolver_t.cpp b/src/installer/corehost/cli/hostpolicy/static/coreclr_resolver_t.cpp new file mode 100644 index 0000000000000..fe0e57bec6e1f --- /dev/null +++ b/src/installer/corehost/cli/hostpolicy/static/coreclr_resolver_t.cpp @@ -0,0 +1,84 @@ +// 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 +#include +#include +#include "coreclr_resolver_t.h" +#include +#include + +extern "C" +{ + 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_2( + 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); +} + + +#if defined(_WIN32) + +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(pal::get_symbol(g_coreclr, "coreclr_initialize")); + coreclr_resolver_contract.coreclr_shutdown = reinterpret_cast(pal::get_symbol(g_coreclr, "coreclr_shutdown_2")); + coreclr_resolver_contract.coreclr_execute_assembly = reinterpret_cast(pal::get_symbol(g_coreclr, "coreclr_execute_assembly")); + coreclr_resolver_contract.coreclr_create_delegate = reinterpret_cast(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; +} + +#else + +bool coreclr_resolver_t::resolve_coreclr(const pal::string_t& libcoreclr_path, coreclr_resolver_contract_t& coreclr_resolver_contract) +{ + coreclr_resolver_contract.coreclr_initialize = reinterpret_cast(coreclr_initialize); + coreclr_resolver_contract.coreclr_shutdown = reinterpret_cast(coreclr_shutdown_2); + coreclr_resolver_contract.coreclr_execute_assembly = reinterpret_cast(coreclr_execute_assembly); + coreclr_resolver_contract.coreclr_create_delegate = reinterpret_cast(coreclr_create_delegate); + + return true; +} + +#endif diff --git a/src/installer/corehost/coreclr_resolver_t.h b/src/installer/corehost/coreclr_resolver_t.h new file mode 100644 index 0000000000000..d40f9a94bd46e --- /dev/null +++ b/src/installer/corehost/coreclr_resolver_t.h @@ -0,0 +1,61 @@ +// 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. + +#ifndef __CORECLR_RESOLVER_T_H__ +#define __CORECLR_RESOLVER_T_H__ + +#include "pal.h" +#include "error_codes.h" + +using host_handle_t = void*; + +// 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, + host_handle_t* hostHandle, + unsigned int* domainId); + +// Prototype of the coreclr_shutdown function from coreclr.dll +using coreclr_shutdown_fn = pal::hresult_t(STDMETHODCALLTYPE*)( + 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*)( + 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*)( + host_handle_t hostHandle, + unsigned int domainId, + const char* entryPointAssemblyName, + const char* entryPointTypeName, + const char* entryPointMethodName, + void** delegate); + +struct coreclr_resolver_contract_t +{ + coreclr_shutdown_fn coreclr_shutdown; + coreclr_initialize_fn coreclr_initialize; + coreclr_execute_assembly_fn coreclr_execute_assembly; + coreclr_create_delegate_fn coreclr_create_delegate; +}; + +class coreclr_resolver_t +{ + public: + static bool resolve_coreclr(const pal::string_t& libcoreclr_path, coreclr_resolver_contract_t& coreclr_resolver_contract); +}; + +#endif // __CORECLR_RESOLVER_T_H__