Skip to content

Commit c49f135

Browse files
committed
[wasm][coreclr] Get further in the runtime initialization
Add corewasmhost binary to test runtime initialization Fix linking of the host binary with coreclr and interpreter Allow calling interpreter with arguments and handle return value Use fake precode stubs to pass the precode type and method desc Make alternate signal stack calls no-op on wasm Implement CallDescrWorkerInternal on wasm Fix PE image loading without RTR on wasm Do not use unsupported syscalls
1 parent fcd197c commit c49f135

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+951
-52
lines changed

src/coreclr/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ if (DEFINED CLR_CMAKE_ICU_DIR)
3939
include_directories(${CLR_CMAKE_ICU_DIR}/include)
4040
endif(DEFINED CLR_CMAKE_ICU_DIR)
4141

42+
if (CLR_CMAKE_TARGET_ARCH_WASM)
43+
add_compile_options(-fwasm-exceptions)
44+
add_link_options(-fwasm-exceptions -sEXIT_RUNTIME=1 -sUSE_OFFSET_CONVERTER=1)
45+
endif()
46+
4247
#----------------------------------------------------
4348
# Cross target Component build specific configuration
4449
#----------------------------------------------------

src/coreclr/System.Private.CoreLib/src/System/Runtime/ExceptionServices/AsmOffsets.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class AsmOffsets
5151
public const int OFFSETOF__REGDISPLAY__SP = 0xba8;
5252
public const int OFFSETOF__REGDISPLAY__ControlPC = 0xbb0;
5353
#elif TARGET_WASM
54-
public const int SIZEOF__REGDISPLAY = 0x3c;
55-
public const int OFFSETOF__REGDISPLAY__SP = 0x34;
56-
public const int OFFSETOF__REGDISPLAY__ControlPC = 0x38;
54+
public const int SIZEOF__REGDISPLAY = 0x38;
55+
public const int OFFSETOF__REGDISPLAY__SP = 0x30;
56+
public const int OFFSETOF__REGDISPLAY__ControlPC = 0x34;
5757
#endif
5858

5959
#if TARGET_64BIT
@@ -73,9 +73,14 @@ class AsmOffsets
7373
public const int OFFSETOF__StackFrameIterator__m_AdjustedControlPC = 0x3d0;
7474
#else // TARGET_64BIT
7575
public const int OFFSETOF__REGDISPLAY__m_pCurrentContext = 0x4;
76+
#if FEATURE_INTERPRETER
77+
public const int SIZEOF__StackFrameIterator = 0xdc;
78+
public const int OFFSETOF__StackFrameIterator__m_AdjustedControlPC = 0xd8;
79+
#else
7680
public const int SIZEOF__StackFrameIterator = 0xcc;
77-
public const int OFFSETOF__StackFrameIterator__m_isRuntimeWrappedExceptions = 0xba;
7881
public const int OFFSETOF__StackFrameIterator__m_AdjustedControlPC = 0xc8;
82+
#endif
83+
public const int OFFSETOF__StackFrameIterator__m_isRuntimeWrappedExceptions = 0xba;
7984
#endif // TARGET_64BIT
8085

8186
#else // DEBUG
@@ -167,7 +172,7 @@ class AsmOffsets
167172
#elif TARGET_LOONGARCH64
168173
public const int SIZEOF__PAL_LIMITED_CONTEXT = 0x520;
169174
#elif TARGET_WASM
170-
public const int SIZEOF__PAL_LIMITED_CONTEXT = 0x08;
175+
public const int SIZEOF__PAL_LIMITED_CONTEXT = 0x04;
171176
#endif
172177

173178
#if TARGET_AMD64

src/coreclr/clrdefinitions.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ if(CLR_CMAKE_TARGET_LINUX_MUSL)
3535
add_definitions(-DNO_FIXED_STACK_LIMIT)
3636
endif(CLR_CMAKE_TARGET_LINUX_MUSL)
3737

38-
add_definitions(-DDEBUGGING_SUPPORTED)
38+
#if(NOT CLR_CMAKE_TARGET_ARCH_WASM)
39+
add_definitions(-DDEBUGGING_SUPPORTED)
40+
#endif()
3941
add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>>:PROFILING_SUPPORTED>)
4042
add_compile_definitions($<$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>:PROFILING_SUPPORTED_DATA>)
4143

@@ -270,4 +272,5 @@ function(set_target_definitions_to_custom_os_and_arch)
270272
if (TARGETDETAILS_ARCH STREQUAL "armel")
271273
target_compile_definitions(${TARGETDETAILS_TARGET} PRIVATE ARM_SOFTFP)
272274
endif()
275+
273276
endfunction()

src/coreclr/clrfeatures.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ if(NOT DEFINED FEATURE_DBGIPC)
3131
endif(NOT DEFINED FEATURE_DBGIPC)
3232

3333
if(NOT DEFINED FEATURE_INTERPRETER)
34-
if(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64)
34+
if(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_WASM)
3535
set(FEATURE_INTERPRETER $<IF:$<CONFIG:Debug,Checked>,1,0>)
36-
else(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64)
36+
else(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_WASM)
3737
set(FEATURE_INTERPRETER 0)
38-
endif(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64)
38+
endif(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_WASM)
3939
endif(NOT DEFINED FEATURE_INTERPRETER)
4040

4141
if(NOT DEFINED FEATURE_STANDALONE_GC)
@@ -50,7 +50,7 @@ if(NOT DEFINED FEATURE_SINGLE_FILE_DIAGNOSTICS)
5050
set(FEATURE_SINGLE_FILE_DIAGNOSTICS 1)
5151
endif(NOT DEFINED FEATURE_SINGLE_FILE_DIAGNOSTICS)
5252

53-
if (CLR_CMAKE_TARGET_WIN32 OR CLR_CMAKE_TARGET_UNIX)
53+
if ((CLR_CMAKE_TARGET_WIN32 OR CLR_CMAKE_TARGET_UNIX) AND NOT CLR_CMAKE_TARGET_ARCH_WASM)
5454
set(FEATURE_COMWRAPPERS 1)
5555
endif()
5656

src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ endif (CLR_CMAKE_HOST_WIN32)
5656

5757
add_definitions(-DFX_VER_INTERNALNAME_STR=CoreCLR.dll)
5858

59+
if (CLR_CMAKE_TARGET_ARCH_WASM)
60+
set(LINK_TYPE STATIC)
61+
else()
62+
set(LINK_TYPE SHARED)
63+
endif()
64+
5965
add_library_clr(coreclr
60-
SHARED
66+
${LINK_TYPE}
6167
${CLR_SOURCES}
6268
)
6369

@@ -70,17 +76,23 @@ add_dependencies(coreclr coreclr_exports)
7076
set_property(TARGET coreclr APPEND_STRING PROPERTY LINK_FLAGS ${EXPORTS_LINKER_OPTION})
7177
set_property(TARGET coreclr APPEND_STRING PROPERTY LINK_DEPENDS ${EXPORTS_FILE})
7278

73-
if (CLR_CMAKE_HOST_UNIX)
79+
if (CLR_CMAKE_HOST_UNIX AND NOT CLR_CMAKE_TARGET_ARCH_WASM)
7480
set(LIB_UNWINDER unwinder_wks)
75-
endif (CLR_CMAKE_HOST_UNIX)
81+
endif (CLR_CMAKE_HOST_UNIX AND NOT CLR_CMAKE_TARGET_ARCH_WASM)
82+
83+
if (NOT CLR_CMAKE_TARGET_ARCH_WASM)
84+
set(LIB_CORDBEE cordbee_wks)
85+
set(LIB_INTEROP interop)
86+
set(LIB_CDAC_CONTRACT_DESCRIPTOR cdac_contract_descriptor)
87+
endif (NOT CLR_CMAKE_TARGET_ARCH_WASM)
7688

7789
# IMPORTANT! Please do not rearrange the order of the libraries. The linker on Linux is
7890
# order dependent and changing the order can result in undefined symbols in the shared
7991
# library.
8092
set(CORECLR_LIBRARIES
8193
utilcode
8294
${START_LIBRARY_GROUP} # Start group of libraries that have circular references
83-
cordbee_wks
95+
${LIB_CORDBEE}
8496
debug-pal
8597
${LIB_UNWINDER}
8698
v3binder
@@ -95,10 +107,10 @@ set(CORECLR_LIBRARIES
95107
utilcode
96108
v3binder
97109
System.Globalization.Native-Static
98-
interop
110+
${LIB_INTEROP}
99111
coreclrminipal
100112
gc_pal
101-
cdac_contract_descriptor
113+
${LIB_CDAC_CONTRACT_DESCRIPTOR}
102114
)
103115

104116
if(CLR_CMAKE_TARGET_ARCH_AMD64)

src/coreclr/gc/unix/gcenv.unix.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ bool GCToOSInterface::Initialize()
244244
// Verify that the s_helperPage is really aligned to the g_SystemInfo.dwPageSize
245245
assert((((size_t)g_helperPage) & (OS_PAGE_SIZE - 1)) == 0);
246246

247+
#ifndef TARGET_BROWSER
247248
// Locking the page ensures that it stays in memory during the two mprotect
248249
// calls in the FlushProcessWriteBuffers below. If the page was unmapped between
249250
// those calls, they would not have the expected effect of generating IPI.
@@ -253,6 +254,9 @@ bool GCToOSInterface::Initialize()
253254
{
254255
return false;
255256
}
257+
#else
258+
int status;
259+
#endif // !TARGET_BROWSER
256260

257261
status = pthread_mutex_init(&g_flushProcessWriteBuffersMutex, NULL);
258262
if (status != 0)
@@ -576,7 +580,7 @@ static void* VirtualReserveInner(size_t size, size_t alignment, uint32_t flags,
576580
}
577581

578582
pRetVal = pAlignedRetVal;
579-
#ifdef MADV_DONTDUMP
583+
#if defined(MADV_DONTDUMP) && !defined(TARGET_BROWSER)
580584
// Do not include reserved uncommitted memory in coredump.
581585
if (!committing)
582586
{
@@ -624,9 +628,13 @@ bool GCToOSInterface::VirtualRelease(void* address, size_t size)
624628
// true if it has succeeded, false if it has failed
625629
static bool VirtualCommitInner(void* address, size_t size, uint16_t node, bool newMemory)
626630
{
631+
#ifndef TARGET_BROWSER
627632
bool success = mprotect(address, size, PROT_WRITE | PROT_READ) == 0;
633+
#else
634+
bool success = true;
635+
#endif // !TARGET_BROWSER
628636

629-
#ifdef MADV_DODUMP
637+
#if defined(MADV_DONTDUMP) && !defined(TARGET_BROWSER)
630638
if (success && !newMemory)
631639
{
632640
// Include committed memory in coredump. New memory is included by default.

src/coreclr/hosts/CMakeLists.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
include_directories(inc)
22

3-
if(CLR_CMAKE_HOST_WIN32)
4-
add_subdirectory(coreshim)
5-
endif(CLR_CMAKE_HOST_WIN32)
3+
if (CLR_CMAKE_TARGET_ARCH_WASM)
4+
add_subdirectory(corewasmrun)
5+
else()
6+
if(CLR_CMAKE_HOST_WIN32)
7+
add_subdirectory(coreshim)
8+
endif(CLR_CMAKE_HOST_WIN32)
69

7-
add_subdirectory(corerun)
10+
add_subdirectory(corerun)
11+
endif()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
project(corewasmrun)
2+
3+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
4+
5+
if (DEFINED CLR_CMAKE_ICU_DIR)
6+
link_directories(${CLR_CMAKE_ICU_DIR}/lib)
7+
endif(DEFINED CLR_CMAKE_ICU_DIR)
8+
9+
add_executable_clr(corewasmrun
10+
corewasmrun.cpp
11+
)
12+
13+
set(_WASM_PRELOAD_DIR "${CMAKE_CURRENT_BINARY_DIR}/../../../../../bin/coreclr/browser.wasm.Debug/IL")
14+
if (EXISTS "${_WASM_PRELOAD_DIR}")
15+
set(_WASM_PRELOAD_FILE --preload-file ${_WASM_PRELOAD_DIR}@/)
16+
endif (EXISTS "${_WASM_PRELOAD_DIR}")
17+
18+
target_compile_options(corewasmrun PRIVATE -fwasm-exceptions)
19+
target_link_options(corewasmrun PRIVATE -fwasm-exceptions -sEXIT_RUNTIME=1 -sUSE_OFFSET_CONVERTER=1 -sINITIAL_MEMORY=134217728 -sFORCE_FILESYSTEM=1 ${_WASM_PRELOAD_FILE} -Wl,-error-limit=0)
20+
21+
target_link_libraries(corewasmrun PRIVATE coreclr)
22+
target_link_libraries(corewasmrun PRIVATE clrinterpreter)
23+
24+
target_link_libraries(corewasmrun PRIVATE icuuc)
25+
target_link_libraries(corewasmrun PRIVATE icui18n)
26+
target_link_libraries(corewasmrun PRIVATE icudata)
27+
28+
install_clr(TARGETS corewasmrun DESTINATIONS . COMPONENT hosts)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <cstdio>
2+
#include <coreclrhost.h>
3+
4+
static void log_error_info(const char* line)
5+
{
6+
std::fprintf(stderr, "log error: %s\n", line);
7+
}
8+
9+
// The current CoreCLR instance details.
10+
static void* CurrentClrInstance;
11+
static unsigned int CurrentAppDomainId;
12+
13+
static int run()
14+
{
15+
const char* exe_path = "<coreclr-wasm>";
16+
const char* app_domain_name = "corewasmrun";
17+
const char* entry_assembly = "ManagedAssembly.dll";
18+
19+
coreclr_set_error_writer(log_error_info);
20+
21+
printf("call coreclr_initialize\n");
22+
int retval = coreclr_initialize(exe_path, app_domain_name, 0, nullptr, nullptr, &CurrentClrInstance, &CurrentAppDomainId);
23+
24+
if (retval < 0)
25+
{
26+
std::fprintf(stderr, "coreclr_initialize failed - Error: 0x%08x\n", retval);
27+
return -1;
28+
}
29+
else
30+
{
31+
printf("coreclr_initialize succeeded - retval: 0x%08x\n", retval);
32+
}
33+
34+
// coreclr_execute_assembly();
35+
// coreclr_shutdown();
36+
37+
return retval;
38+
}
39+
40+
int main()
41+
{
42+
int retval = run();
43+
44+
return retval;
45+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>corewasmrun</title>
6+
</head>
7+
<body>
8+
<h1>corewasmrun</h1>
9+
<pre id="log"></pre>
10+
<script>
11+
Module = {
12+
preRun: [ function () {
13+
ENV.PAL_DBG_CHANNELS="+all.all";
14+
// ENV.PAL_DBG_CHANNELS="+all.ERROR";
15+
}],
16+
onExit: function (code) {
17+
console.log("onExit, code: " + code);
18+
},
19+
};
20+
21+
const originalConsoleLog = console.log;
22+
console.log = function(message) {
23+
originalConsoleLog(message);
24+
fetch('/log=corewasmrun-log.txt', {
25+
method: 'POST',
26+
body: ('stdout: ' + message),
27+
headers: {
28+
'Content-Type': 'text/plain'
29+
}
30+
});
31+
const elt = document.createElement("span");
32+
elt.textContent = message + "\n";
33+
document.querySelector("#log").appendChild(elt);
34+
};
35+
const originalConsoleError = console.error;
36+
console.error = function(message) {
37+
originalConsoleError(message);
38+
fetch('/log=corewasmrun-log.txt', {
39+
method: 'POST',
40+
body: ('stderr: ' + message),
41+
headers: {
42+
'Content-Type': 'text/plain'
43+
}
44+
});
45+
const elt = document.createElement("span");
46+
elt.textContent = message + "\n";
47+
elt.style.color = "red";
48+
document.querySelector("#log").appendChild(elt);
49+
};
50+
</script>
51+
<script src="corewasmrun.js"></script>
52+
</body>

0 commit comments

Comments
 (0)