diff --git a/src/coreclr/hosts/corerun/CMakeLists.txt b/src/coreclr/hosts/corerun/CMakeLists.txt index b59a96d2e156cc..87175bf590a542 100644 --- a/src/coreclr/hosts/corerun/CMakeLists.txt +++ b/src/coreclr/hosts/corerun/CMakeLists.txt @@ -44,9 +44,11 @@ else(CLR_CMAKE_HOST_WIN32) endif() # Static linking if (CLR_CMAKE_TARGET_ARCH_WASM) + target_sources(corerun PRIVATE corerun.wasm.cpp) target_link_libraries(corerun PRIVATE coreclr_static - clrinterpreter) + System.Native-Static + System.Native.TimeZoneData) # linker options for NodeJs, link in JavaScript helper, access to local filesystem if (CLR_CMAKE_TARGET_BROWSER) target_compile_options(corerun PRIVATE -fwasm-exceptions) @@ -54,7 +56,6 @@ else(CLR_CMAKE_HOST_WIN32) -fwasm-exceptions -sEXPORTED_RUNTIME_METHODS=FS -sEXIT_RUNTIME=1 - -sWASM_BIGINT=1 -sINITIAL_MEMORY=134217728 -sSTACK_SIZE=5MB -lnoderawfs.js diff --git a/src/coreclr/hosts/corerun/corerun.cpp b/src/coreclr/hosts/corerun/corerun.cpp index 4bbb2cbf846303..2b8105f3b3081a 100644 --- a/src/coreclr/hosts/corerun/corerun.cpp +++ b/src/coreclr/hosts/corerun/corerun.cpp @@ -9,6 +9,10 @@ #include "corerun.hpp" #include "dotenv.hpp" +#ifdef TARGET_WASM +#include "corerun.wasm.hpp" +#endif + #include using char_t = pal::char_t; @@ -486,6 +490,11 @@ static int run(const configuration& config) coreclr_set_error_writer_func(log_error_info); } +#ifdef TARGET_WASM + // install the pinvoke override callback to resolve p/invokes to statically linked libraries + wasm_add_pinvoke_override(); +#endif + int result; result = coreclr_init_func( exe_path_utf8.c_str(), diff --git a/src/coreclr/hosts/corerun/corerun.wasm.cpp b/src/coreclr/hosts/corerun/corerun.wasm.cpp new file mode 100644 index 00000000000000..6e383db9206a12 --- /dev/null +++ b/src/coreclr/hosts/corerun/corerun.wasm.cpp @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include + +#define _In_z_ +#define _In_ +#include "pinvokeoverride.h" + +extern "C" const void* SystemResolveDllImport(const char* name); + +// pinvoke_override: +// Check if given function belongs to one of statically linked libraries and return a pointer if found. +const void* pinvoke_override(const char* library_name, const char* entry_point_name) +{ + // This function is only called with the library name specified for a p/invoke, not any variations. + // It must handle exact matches to the names specified. See Interop.Libraries.cs for each platform. + if (strcmp(library_name, "libSystem.Native") == 0) + { + return SystemResolveDllImport(entry_point_name); + } + + return nullptr; +} + +void wasm_add_pinvoke_override() +{ + PInvokeOverride::SetPInvokeOverride(pinvoke_override, PInvokeOverride::Source::RuntimeConfiguration); +} diff --git a/src/coreclr/hosts/corerun/corerun.wasm.hpp b/src/coreclr/hosts/corerun/corerun.wasm.hpp new file mode 100644 index 00000000000000..302ef58542ac83 --- /dev/null +++ b/src/coreclr/hosts/corerun/corerun.wasm.hpp @@ -0,0 +1,9 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#ifndef __CORERUN_WASM_HPP__ +#define __CORERUN_WASM_HPP__ + +void wasm_add_pinvoke_override(); + +#endif // __CORERUN_WASM_HPP__ diff --git a/src/coreclr/hosts/corewasmrun/CMakeLists.txt b/src/coreclr/hosts/corewasmrun/CMakeLists.txt index 6360cce0ba7ece..df0e158e0a815a 100644 --- a/src/coreclr/hosts/corewasmrun/CMakeLists.txt +++ b/src/coreclr/hosts/corewasmrun/CMakeLists.txt @@ -4,6 +4,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) add_executable_clr(corewasmrun corewasmrun.cpp + ../corerun/corerun.wasm.cpp ) set(_WASM_PRELOAD_DIR "${CMAKE_INSTALL_PREFIX}/IL") @@ -11,18 +12,23 @@ if (EXISTS "${_WASM_PRELOAD_DIR}") set(_WASM_PRELOAD_FILE --preload-file ${_WASM_PRELOAD_DIR}@/) endif (EXISTS "${_WASM_PRELOAD_DIR}") +target_include_directories(corewasmrun PRIVATE + ../corerun +) + target_compile_options(corewasmrun PRIVATE -fwasm-exceptions) target_link_options(corewasmrun PRIVATE -fwasm-exceptions -sEXIT_RUNTIME=1 - -sWASM_BIGINT=1 -sINITIAL_MEMORY=134217728 + -sSTACK_SIZE=5MB -sFORCE_FILESYSTEM=1 ${_WASM_PRELOAD_FILE} -Wl,-error-limit=0) - target_link_libraries(corewasmrun PRIVATE - coreclr_static) + coreclr_static + System.Native-Static + System.Native.TimeZoneData) # The corerun version for wasm is being installed in its own directory # because it is an executable that comes with an index.html and we diff --git a/src/coreclr/hosts/corewasmrun/corewasmrun.cpp b/src/coreclr/hosts/corewasmrun/corewasmrun.cpp index 2fff454fc0200b..f36ec38fc853a4 100644 --- a/src/coreclr/hosts/corewasmrun/corewasmrun.cpp +++ b/src/coreclr/hosts/corewasmrun/corewasmrun.cpp @@ -4,6 +4,9 @@ #include #include +#include + +#include "corerun.wasm.hpp" static void log_error_info(const char* line) { @@ -16,14 +19,26 @@ static unsigned int CurrentAppDomainId; static int run() { - const char* exe_path = ""; + const char* exe_path = "/"; const char* app_domain_name = "corewasmrun"; const char* entry_assembly = "ManagedAssembly.dll"; + // Set base initialization properties. + std::vector propertyKeys; + std::vector propertyValues; + + propertyKeys.push_back("TRUSTED_PLATFORM_ASSEMBLIES"); + propertyValues.push_back("/HelloWorld.dll:/System.Private.CoreLib.dll:/System.Runtime.dll:/System.Console.dll:/System.Threading.dll:/System.Runtime.InteropServices.dll"); + propertyKeys.push_back("NATIVE_DLL_SEARCH_DIRECTORIES"); + propertyValues.push_back("/:.:"); + coreclr_set_error_writer(log_error_info); - printf("call coreclr_initialize\n"); - int retval = coreclr_initialize(exe_path, app_domain_name, 0, nullptr, nullptr, &CurrentClrInstance, &CurrentAppDomainId); + wasm_add_pinvoke_override(); + + printf("BEGIN: call coreclr_initialize\n"); + int retval = coreclr_initialize(exe_path, app_domain_name, 1, propertyKeys.data(), propertyValues.data(), &CurrentClrInstance, &CurrentAppDomainId); + printf("END: call coreclr_initialize\n"); if (retval < 0) { @@ -35,8 +50,26 @@ static int run() printf("coreclr_initialize succeeded - retval: 0x%08x\n", retval); } - // coreclr_execute_assembly(); - // coreclr_shutdown(); + int exit_code; + printf("BEGIN: call coreclr_execute_assembly\n"); + retval = coreclr_execute_assembly(CurrentClrInstance, CurrentAppDomainId, 0, nullptr, "HelloWorld.dll", (uint32_t*)&exit_code); + printf("END: call coreclr_execute_assembly\n"); + + if (retval < 0) + { + std::fprintf(stderr, "coreclr_execute_assembly failed - Error: 0x%08x\n", retval); + return -1; + } + + int latched_exit_code = 0; + printf("BEGIN: call coreclr_shutdown_2\n"); + retval = coreclr_shutdown_2(CurrentClrInstance, CurrentAppDomainId, &latched_exit_code); + printf("END: call coreclr_shutdown_2\n"); + if (retval < 0) + { + std::fprintf(stderr, "coreclr_shutdown_2 failed - Error: 0x%08x\n", retval); + exit_code = -1; + } return retval; } diff --git a/src/coreclr/runtime.proj b/src/coreclr/runtime.proj index 8ceadf42e0d456..f0bef340c0d06e 100644 --- a/src/coreclr/runtime.proj +++ b/src/coreclr/runtime.proj @@ -6,12 +6,14 @@ true <_IcuDir Condition="'$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)' != '' and '$(TargetsBrowser)' == 'true'">$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)/runtimes/$(TargetOS)-$(TargetArchitecture)$(_RuntimeVariant)/native + <_TzdDir Condition="'$(PkgSystem_Runtime_TimeZoneData)' != ''">$([MSBuild]::NormalizePath('$(PkgSystem_Runtime_TimeZoneData)', 'contentFiles', 'any', 'any', 'data')) + @@ -53,6 +55,7 @@ <_CoreClrBuildArg Include="-cmakeargs "-DCLR_DOTNET_HOST_PATH=$(DOTNET_HOST_PATH)"" /> <_CoreClrBuildArg Condition="'$(HasCdacBuildTool)' == 'true'" Include="-cmakeargs "-DCDAC_BUILD_TOOL_BINARY_PATH=$(RuntimeBinDir)cdac-build-tool\cdac-build-tool.dll"" /> <_CoreClrBuildArg Condition="'$(_IcuDir)' != ''" Include="-cmakeargs "-DCMAKE_ICU_DIR=$(_IcuDir)"" /> + <_CoreClrBuildArg Condition="'$(_TzdDir)' != ''" Include="-cmakeargs "-DCMAKE_TZD_DIR=$(_TzdDir)"" /> <_CoreClrBuildArg Condition="'$(FeatureXplatEventSource)' == 'false'" Include="-cmakeargs "-DFEATURE_EVENTSOURCE_XPLAT=0"" /> diff --git a/src/coreclr/vm/peimage.inl b/src/coreclr/vm/peimage.inl index 592a84792646be..b59895f49b6964 100644 --- a/src/coreclr/vm/peimage.inl +++ b/src/coreclr/vm/peimage.inl @@ -165,7 +165,11 @@ inline BOOL PEImage::HasLoadedLayout() { LIMITED_METHOD_CONTRACT; SUPPORTS_DAC; +#ifdef PEIMAGE_FLAT_LAYOUT_ONLY + return m_pLayouts[IMAGE_FLAT]!=NULL; +#else return m_pLayouts[IMAGE_LOADED]!=NULL; +#endif } inline PTR_PEImageLayout PEImage::GetLoadedLayout() diff --git a/src/native/libs/System.Native/CMakeLists.txt b/src/native/libs/System.Native/CMakeLists.txt index c746d141d55838..c823ff31ea92aa 100644 --- a/src/native/libs/System.Native/CMakeLists.txt +++ b/src/native/libs/System.Native/CMakeLists.txt @@ -135,7 +135,7 @@ if (GEN_SHARED_LIB) install_with_stripped_symbols (System.Native PROGRAMS .) endif () -if (NOT GEN_SHARED_LIB AND NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI) +if (NOT GEN_SHARED_LIB AND NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_TARGET_ANDROID) set(NATIVE_SOURCES ${NATIVE_SOURCES} entrypoints.c) endif() diff --git a/src/native/libs/System.Native/entrypoints.c b/src/native/libs/System.Native/entrypoints.c index 5a99098e61b02f..d5121676bbf642 100644 --- a/src/native/libs/System.Native/entrypoints.c +++ b/src/native/libs/System.Native/entrypoints.c @@ -134,8 +134,10 @@ static const Entry s_sysNative[] = DllImportEntry(SystemNative_GetSpaceInfoForMountPoint) DllImportEntry(SystemNative_GetFileSystemTypeNameForMountPoint) DllImportEntry(SystemNative_GetAllMountPoints) +#if !defined(TARGET_WASM) && !defined(TARGET_WASI) DllImportEntry(SystemNative_ReadEvents) DllImportEntry(SystemNative_CreateNetworkChangeListenerSocket) +#endif // !defined(TARGET_WASM) && !defined(TARGET_WASI) DllImportEntry(SystemNative_GetHostEntryForName) DllImportEntry(SystemNative_FreeHostEntry) DllImportEntry(SystemNative_GetNameInfo)