Skip to content

Commit a3bdddc

Browse files
authored
Make HelloWorld run on wasm with core[wasm]run (#119640)
1 parent 1f50a94 commit a3bdddc

File tree

10 files changed

+107
-11
lines changed

10 files changed

+107
-11
lines changed

src/coreclr/hosts/corerun/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ else(CLR_CMAKE_HOST_WIN32)
4444
endif()
4545
# Static linking
4646
if (CLR_CMAKE_TARGET_ARCH_WASM)
47+
target_sources(corerun PRIVATE corerun.wasm.cpp)
4748
target_link_libraries(corerun PRIVATE
4849
coreclr_static
49-
clrinterpreter)
50+
System.Native-Static
51+
System.Native.TimeZoneData)
5052
# linker options for NodeJs, link in JavaScript helper, access to local filesystem
5153
if (CLR_CMAKE_TARGET_BROWSER)
5254
target_compile_options(corerun PRIVATE -fwasm-exceptions)
5355
target_link_options(corerun PRIVATE
5456
-fwasm-exceptions
5557
-sEXPORTED_RUNTIME_METHODS=FS
5658
-sEXIT_RUNTIME=1
57-
-sWASM_BIGINT=1
5859
-sINITIAL_MEMORY=134217728
5960
-sSTACK_SIZE=5MB
6061
-lnoderawfs.js

src/coreclr/hosts/corerun/corerun.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include "corerun.hpp"
1010
#include "dotenv.hpp"
1111

12+
#ifdef TARGET_WASM
13+
#include "corerun.wasm.hpp"
14+
#endif
15+
1216
#include <fstream>
1317

1418
using char_t = pal::char_t;
@@ -486,6 +490,11 @@ static int run(const configuration& config)
486490
coreclr_set_error_writer_func(log_error_info);
487491
}
488492

493+
#ifdef TARGET_WASM
494+
// install the pinvoke override callback to resolve p/invokes to statically linked libraries
495+
wasm_add_pinvoke_override();
496+
#endif
497+
489498
int result;
490499
result = coreclr_init_func(
491500
exe_path_utf8.c_str(),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
4+
#include <string.h>
5+
6+
#define _In_z_
7+
#define _In_
8+
#include "pinvokeoverride.h"
9+
10+
extern "C" const void* SystemResolveDllImport(const char* name);
11+
12+
// pinvoke_override:
13+
// Check if given function belongs to one of statically linked libraries and return a pointer if found.
14+
const void* pinvoke_override(const char* library_name, const char* entry_point_name)
15+
{
16+
// This function is only called with the library name specified for a p/invoke, not any variations.
17+
// It must handle exact matches to the names specified. See Interop.Libraries.cs for each platform.
18+
if (strcmp(library_name, "libSystem.Native") == 0)
19+
{
20+
return SystemResolveDllImport(entry_point_name);
21+
}
22+
23+
return nullptr;
24+
}
25+
26+
void wasm_add_pinvoke_override()
27+
{
28+
PInvokeOverride::SetPInvokeOverride(pinvoke_override, PInvokeOverride::Source::RuntimeConfiguration);
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
4+
#ifndef __CORERUN_WASM_HPP__
5+
#define __CORERUN_WASM_HPP__
6+
7+
void wasm_add_pinvoke_override();
8+
9+
#endif // __CORERUN_WASM_HPP__

src/coreclr/hosts/corewasmrun/CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,31 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
44

55
add_executable_clr(corewasmrun
66
corewasmrun.cpp
7+
../corerun/corerun.wasm.cpp
78
)
89

910
set(_WASM_PRELOAD_DIR "${CMAKE_INSTALL_PREFIX}/IL")
1011
if (EXISTS "${_WASM_PRELOAD_DIR}")
1112
set(_WASM_PRELOAD_FILE --preload-file ${_WASM_PRELOAD_DIR}@/)
1213
endif (EXISTS "${_WASM_PRELOAD_DIR}")
1314

15+
target_include_directories(corewasmrun PRIVATE
16+
../corerun
17+
)
18+
1419
target_compile_options(corewasmrun PRIVATE -fwasm-exceptions)
1520
target_link_options(corewasmrun PRIVATE
1621
-fwasm-exceptions
1722
-sEXIT_RUNTIME=1
18-
-sWASM_BIGINT=1
1923
-sINITIAL_MEMORY=134217728
24+
-sSTACK_SIZE=5MB
2025
-sFORCE_FILESYSTEM=1
2126
${_WASM_PRELOAD_FILE}
2227
-Wl,-error-limit=0)
23-
2428
target_link_libraries(corewasmrun PRIVATE
25-
coreclr_static)
29+
coreclr_static
30+
System.Native-Static
31+
System.Native.TimeZoneData)
2632

2733
# The corerun version for wasm is being installed in its own directory
2834
# because it is an executable that comes with an index.html and we

src/coreclr/hosts/corewasmrun/corewasmrun.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
#include <cstdio>
66
#include <coreclrhost.h>
7+
#include <vector>
8+
9+
#include "corerun.wasm.hpp"
710

811
static void log_error_info(const char* line)
912
{
@@ -16,14 +19,26 @@ static unsigned int CurrentAppDomainId;
1619

1720
static int run()
1821
{
19-
const char* exe_path = "<coreclr-wasm>";
22+
const char* exe_path = "/";
2023
const char* app_domain_name = "corewasmrun";
2124
const char* entry_assembly = "ManagedAssembly.dll";
2225

26+
// Set base initialization properties.
27+
std::vector<const char*> propertyKeys;
28+
std::vector<const char*> propertyValues;
29+
30+
propertyKeys.push_back("TRUSTED_PLATFORM_ASSEMBLIES");
31+
propertyValues.push_back("/HelloWorld.dll:/System.Private.CoreLib.dll:/System.Runtime.dll:/System.Console.dll:/System.Threading.dll:/System.Runtime.InteropServices.dll");
32+
propertyKeys.push_back("NATIVE_DLL_SEARCH_DIRECTORIES");
33+
propertyValues.push_back("/:.:");
34+
2335
coreclr_set_error_writer(log_error_info);
2436

25-
printf("call coreclr_initialize\n");
26-
int retval = coreclr_initialize(exe_path, app_domain_name, 0, nullptr, nullptr, &CurrentClrInstance, &CurrentAppDomainId);
37+
wasm_add_pinvoke_override();
38+
39+
printf("BEGIN: call coreclr_initialize\n");
40+
int retval = coreclr_initialize(exe_path, app_domain_name, 1, propertyKeys.data(), propertyValues.data(), &CurrentClrInstance, &CurrentAppDomainId);
41+
printf("END: call coreclr_initialize\n");
2742

2843
if (retval < 0)
2944
{
@@ -35,8 +50,26 @@ static int run()
3550
printf("coreclr_initialize succeeded - retval: 0x%08x\n", retval);
3651
}
3752

38-
// coreclr_execute_assembly();
39-
// coreclr_shutdown();
53+
int exit_code;
54+
printf("BEGIN: call coreclr_execute_assembly\n");
55+
retval = coreclr_execute_assembly(CurrentClrInstance, CurrentAppDomainId, 0, nullptr, "HelloWorld.dll", (uint32_t*)&exit_code);
56+
printf("END: call coreclr_execute_assembly\n");
57+
58+
if (retval < 0)
59+
{
60+
std::fprintf(stderr, "coreclr_execute_assembly failed - Error: 0x%08x\n", retval);
61+
return -1;
62+
}
63+
64+
int latched_exit_code = 0;
65+
printf("BEGIN: call coreclr_shutdown_2\n");
66+
retval = coreclr_shutdown_2(CurrentClrInstance, CurrentAppDomainId, &latched_exit_code);
67+
printf("END: call coreclr_shutdown_2\n");
68+
if (retval < 0)
69+
{
70+
std::fprintf(stderr, "coreclr_shutdown_2 failed - Error: 0x%08x\n", retval);
71+
exit_code = -1;
72+
}
4073

4174
return retval;
4275
}

src/coreclr/runtime.proj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
<HasCdacBuildTool Condition="'$(ClrFullNativeBuild)' == 'true' or '$(ClrRuntimeSubset)' == 'true' or '$(ClrDebugSubset)' == 'true' or '$(ClrCrossComponentsSubset)' == 'true'">true</HasCdacBuildTool>
77

88
<_IcuDir Condition="'$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)' != '' and '$(TargetsBrowser)' == 'true'">$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)/runtimes/$(TargetOS)-$(TargetArchitecture)$(_RuntimeVariant)/native</_IcuDir>
9+
<_TzdDir Condition="'$(PkgSystem_Runtime_TimeZoneData)' != ''">$([MSBuild]::NormalizePath('$(PkgSystem_Runtime_TimeZoneData)', 'contentFiles', 'any', 'any', 'data'))</_TzdDir>
910
</PropertyGroup>
1011

1112
<ItemGroup>
1213
<ProjectReference Include="runtime-prereqs.proj" GlobalPropertiesToRemove="$(NativeBuildPartitionPropertiesToRemove)" />
1314
<ProjectReference Condition="'$(HasCdacBuildTool)' == 'true'" Include="tools\cdac-build-tool\cdac-build-tool.csproj" ReferenceOutputAssembly="false" GlobalPropertiesToRemove="$(NativeBuildPartitionPropertiesToRemove)" />
1415
<PackageReference Condition="'$(TargetsWasm)' == 'true'" Include="Microsoft.NETCore.Runtime.ICU.Transport" PrivateAssets="all" Version="$(MicrosoftNETCoreRuntimeICUTransportVersion)" GeneratePathProperty="true" />
16+
<PackageReference Condition="'$(TargetsWasm)' == 'true'" Include="System.Runtime.TimeZoneData" PrivateAssets="all" Version="$(SystemRuntimeTimeZoneDataVersion)" GeneratePathProperty="true" />
1517
</ItemGroup>
1618

1719
<Import Project="$(RepositoryEngineeringDir)nativepgo.targets" />
@@ -53,6 +55,7 @@
5355
<_CoreClrBuildArg Include="-cmakeargs &quot;-DCLR_DOTNET_HOST_PATH=$(DOTNET_HOST_PATH)&quot;" />
5456
<_CoreClrBuildArg Condition="'$(HasCdacBuildTool)' == 'true'" Include="-cmakeargs &quot;-DCDAC_BUILD_TOOL_BINARY_PATH=$(RuntimeBinDir)cdac-build-tool\cdac-build-tool.dll&quot;" />
5557
<_CoreClrBuildArg Condition="'$(_IcuDir)' != ''" Include="-cmakeargs &quot;-DCMAKE_ICU_DIR=$(_IcuDir)&quot;" />
58+
<_CoreClrBuildArg Condition="'$(_TzdDir)' != ''" Include="-cmakeargs &quot;-DCMAKE_TZD_DIR=$(_TzdDir)&quot;" />
5659
<_CoreClrBuildArg Condition="'$(FeatureXplatEventSource)' == 'false'" Include="-cmakeargs &quot;-DFEATURE_EVENTSOURCE_XPLAT=0&quot;" />
5760
</ItemGroup>
5861

src/coreclr/vm/peimage.inl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ inline BOOL PEImage::HasLoadedLayout()
165165
{
166166
LIMITED_METHOD_CONTRACT;
167167
SUPPORTS_DAC;
168+
#ifdef PEIMAGE_FLAT_LAYOUT_ONLY
169+
return m_pLayouts[IMAGE_FLAT]!=NULL;
170+
#else
168171
return m_pLayouts[IMAGE_LOADED]!=NULL;
172+
#endif
169173
}
170174

171175
inline PTR_PEImageLayout PEImage::GetLoadedLayout()

src/native/libs/System.Native/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ if (GEN_SHARED_LIB)
135135
install_with_stripped_symbols (System.Native PROGRAMS .)
136136
endif ()
137137

138-
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)
138+
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)
139139
set(NATIVE_SOURCES ${NATIVE_SOURCES} entrypoints.c)
140140
endif()
141141

src/native/libs/System.Native/entrypoints.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ static const Entry s_sysNative[] =
134134
DllImportEntry(SystemNative_GetSpaceInfoForMountPoint)
135135
DllImportEntry(SystemNative_GetFileSystemTypeNameForMountPoint)
136136
DllImportEntry(SystemNative_GetAllMountPoints)
137+
#if !defined(TARGET_WASM) && !defined(TARGET_WASI)
137138
DllImportEntry(SystemNative_ReadEvents)
138139
DllImportEntry(SystemNative_CreateNetworkChangeListenerSocket)
140+
#endif // !defined(TARGET_WASM) && !defined(TARGET_WASI)
139141
DllImportEntry(SystemNative_GetHostEntryForName)
140142
DllImportEntry(SystemNative_FreeHostEntry)
141143
DllImportEntry(SystemNative_GetNameInfo)

0 commit comments

Comments
 (0)