Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9bf9b7c
[wasm coreclr] Initial changes for corewasmrun host
radekdoulik Mar 26, 2025
14ed390
Make alternate signal stack calls no-op on wasm
radekdoulik Mar 26, 2025
7c3bd24
[wasm coreclr] Fix field alignment
radekdoulik May 7, 2025
d193fc5
Update conditions to use TARGET_WASM
radekdoulik May 7, 2025
4a7481d
Save WIP changes before rebase
radekdoulik May 15, 2025
4914ac3
Update after rebase
radekdoulik May 16, 2025
64b74e2
Merge remote-tracking branch 'remotes/origin/main'
radekdoulik Jun 3, 2025
477350b
[wasm][coreclr] Implement CallDescrWorkerInternal
radekdoulik May 29, 2025
45843ee
Remaining changes
radekdoulik Jun 3, 2025
feef9d1
Update after merge
radekdoulik Jun 3, 2025
11dc45b
Remove parts which are not needed anymore
radekdoulik Jun 3, 2025
27f3bcb
Make some of the CORECLR_LIBRARIES conditional
radekdoulik Jun 3, 2025
fb89f83
Remove unwanted changes from conflicts resolve
radekdoulik Jun 3, 2025
1aa0e14
Enable interpreter verbose mode on wasm only
radekdoulik Jun 3, 2025
1a50037
Make the PE image loading changes wasm specific
radekdoulik Jun 3, 2025
d776675
Remove debug prints
radekdoulik Jun 3, 2025
5c6dc22
Fix whitespace
radekdoulik Jun 3, 2025
12ed21e
Do not use unsupported syscalls
radekdoulik Jun 5, 2025
19e6949
Link statically on wasm
radekdoulik Jun 5, 2025
a9eff81
Remove unwanted changes
radekdoulik Jun 5, 2025
d083b0a
Add MONO_RUNTIME to detect when we build mono runtime
radekdoulik Jun 5, 2025
9c041fa
Remove debug prints
radekdoulik Jun 5, 2025
e825c3b
Remove runtime init test
radekdoulik Jun 5, 2025
e1f4573
Rename log files
radekdoulik Jun 5, 2025
3cdb237
Make preload conditional
radekdoulik Jun 6, 2025
58b6a76
Fix comment
radekdoulik Jun 6, 2025
9cd5621
Remove debug code
radekdoulik Jun 6, 2025
ae9b5ea
Remove old files
radekdoulik Jun 6, 2025
c711276
Remove old file
radekdoulik Jun 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/coreclr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ if (DEFINED CLR_CMAKE_ICU_DIR)
include_directories(${CLR_CMAKE_ICU_DIR}/include)
endif(DEFINED CLR_CMAKE_ICU_DIR)

if (CLR_CMAKE_TARGET_ARCH_WASM)
add_compile_options(-fwasm-exceptions)
add_link_options(-fwasm-exceptions -sEXIT_RUNTIME=1 -sUSE_OFFSET_CONVERTER=1)
endif()

#----------------------------------------------------
# Cross target Component build specific configuration
#----------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class AsmOffsets
public const int OFFSETOF__REGDISPLAY__SP = 0xba8;
public const int OFFSETOF__REGDISPLAY__ControlPC = 0xbb0;
#elif TARGET_WASM
public const int SIZEOF__REGDISPLAY = 0x3c;
public const int OFFSETOF__REGDISPLAY__SP = 0x34;
public const int OFFSETOF__REGDISPLAY__ControlPC = 0x38;
public const int SIZEOF__REGDISPLAY = 0x38;
public const int OFFSETOF__REGDISPLAY__SP = 0x30;
public const int OFFSETOF__REGDISPLAY__ControlPC = 0x34;
#endif

#if TARGET_64BIT
Expand All @@ -73,9 +73,14 @@ class AsmOffsets
public const int OFFSETOF__StackFrameIterator__m_AdjustedControlPC = 0x3d0;
#else // TARGET_64BIT
public const int OFFSETOF__REGDISPLAY__m_pCurrentContext = 0x4;
#if FEATURE_INTERPRETER
public const int SIZEOF__StackFrameIterator = 0xdc;
public const int OFFSETOF__StackFrameIterator__m_AdjustedControlPC = 0xd8;
#else
public const int SIZEOF__StackFrameIterator = 0xcc;
public const int OFFSETOF__StackFrameIterator__m_isRuntimeWrappedExceptions = 0xba;
public const int OFFSETOF__StackFrameIterator__m_AdjustedControlPC = 0xc8;
#endif
public const int OFFSETOF__StackFrameIterator__m_isRuntimeWrappedExceptions = 0xba;
#endif // TARGET_64BIT

#else // DEBUG
Expand Down Expand Up @@ -167,7 +172,7 @@ class AsmOffsets
#elif TARGET_LOONGARCH64
public const int SIZEOF__PAL_LIMITED_CONTEXT = 0x520;
#elif TARGET_WASM
public const int SIZEOF__PAL_LIMITED_CONTEXT = 0x08;
public const int SIZEOF__PAL_LIMITED_CONTEXT = 0x04;
#endif

#if TARGET_AMD64
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/clrdefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ if(CLR_CMAKE_TARGET_LINUX_MUSL)
add_definitions(-DNO_FIXED_STACK_LIMIT)
endif(CLR_CMAKE_TARGET_LINUX_MUSL)

add_definitions(-DDEBUGGING_SUPPORTED)
#if(NOT CLR_CMAKE_TARGET_ARCH_WASM)
add_definitions(-DDEBUGGING_SUPPORTED)
#endif()
add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>>:PROFILING_SUPPORTED>)
add_compile_definitions($<$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>:PROFILING_SUPPORTED_DATA>)

Expand Down Expand Up @@ -270,4 +272,5 @@ function(set_target_definitions_to_custom_os_and_arch)
if (TARGETDETAILS_ARCH STREQUAL "armel")
target_compile_definitions(${TARGETDETAILS_TARGET} PRIVATE ARM_SOFTFP)
endif()

endfunction()
8 changes: 4 additions & 4 deletions src/coreclr/clrfeatures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ if(NOT DEFINED FEATURE_DBGIPC)
endif(NOT DEFINED FEATURE_DBGIPC)

if(NOT DEFINED FEATURE_INTERPRETER)
if(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64)
if(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_WASM)
set(FEATURE_INTERPRETER $<IF:$<CONFIG:Debug,Checked>,1,0>)
else(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64)
else(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_WASM)
set(FEATURE_INTERPRETER 0)
endif(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64)
endif(CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_WASM)
endif(NOT DEFINED FEATURE_INTERPRETER)

if(NOT DEFINED FEATURE_STANDALONE_GC)
Expand All @@ -50,7 +50,7 @@ if(NOT DEFINED FEATURE_SINGLE_FILE_DIAGNOSTICS)
set(FEATURE_SINGLE_FILE_DIAGNOSTICS 1)
endif(NOT DEFINED FEATURE_SINGLE_FILE_DIAGNOSTICS)

if (CLR_CMAKE_TARGET_WIN32 OR CLR_CMAKE_TARGET_UNIX)
if ((CLR_CMAKE_TARGET_WIN32 OR CLR_CMAKE_TARGET_UNIX) AND NOT CLR_CMAKE_TARGET_ARCH_WASM)
set(FEATURE_COMWRAPPERS 1)
endif()

Expand Down
24 changes: 18 additions & 6 deletions src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ endif (CLR_CMAKE_HOST_WIN32)

add_definitions(-DFX_VER_INTERNALNAME_STR=CoreCLR.dll)

if (CLR_CMAKE_TARGET_ARCH_WASM)
set(LINK_TYPE STATIC)
else()
set(LINK_TYPE SHARED)
endif()

add_library_clr(coreclr
SHARED
${LINK_TYPE}
${CLR_SOURCES}
)

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

if (CLR_CMAKE_HOST_UNIX)
if (CLR_CMAKE_HOST_UNIX AND NOT CLR_CMAKE_TARGET_ARCH_WASM)
set(LIB_UNWINDER unwinder_wks)
endif (CLR_CMAKE_HOST_UNIX)
endif (CLR_CMAKE_HOST_UNIX AND NOT CLR_CMAKE_TARGET_ARCH_WASM)

if (NOT CLR_CMAKE_TARGET_ARCH_WASM)
set(LIB_CORDBEE cordbee_wks)
set(LIB_INTEROP interop)
set(LIB_CDAC_CONTRACT_DESCRIPTOR cdac_contract_descriptor)
endif (NOT CLR_CMAKE_TARGET_ARCH_WASM)

# IMPORTANT! Please do not rearrange the order of the libraries. The linker on Linux is
# order dependent and changing the order can result in undefined symbols in the shared
# library.
set(CORECLR_LIBRARIES
utilcode
${START_LIBRARY_GROUP} # Start group of libraries that have circular references
cordbee_wks
${LIB_CORDBEE}
debug-pal
${LIB_UNWINDER}
v3binder
Expand All @@ -95,10 +107,10 @@ set(CORECLR_LIBRARIES
utilcode
v3binder
System.Globalization.Native-Static
interop
${LIB_INTEROP}
coreclrminipal
gc_pal
cdac_contract_descriptor
${LIB_CDAC_CONTRACT_DESCRIPTOR}
)

if(CLR_CMAKE_TARGET_ARCH_AMD64)
Expand Down
12 changes: 10 additions & 2 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ bool GCToOSInterface::Initialize()
// Verify that the s_helperPage is really aligned to the g_SystemInfo.dwPageSize
assert((((size_t)g_helperPage) & (OS_PAGE_SIZE - 1)) == 0);

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

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

pRetVal = pAlignedRetVal;
#ifdef MADV_DONTDUMP
#if defined(MADV_DONTDUMP) && !defined(TARGET_BROWSER)
// Do not include reserved uncommitted memory in coredump.
if (!committing)
{
Expand Down Expand Up @@ -624,9 +628,13 @@ bool GCToOSInterface::VirtualRelease(void* address, size_t size)
// true if it has succeeded, false if it has failed
static bool VirtualCommitInner(void* address, size_t size, uint16_t node, bool newMemory)
{
#ifndef TARGET_BROWSER
bool success = mprotect(address, size, PROT_WRITE | PROT_READ) == 0;
#else
bool success = true;
#endif // !TARGET_BROWSER

#ifdef MADV_DODUMP
#if defined(MADV_DONTDUMP) && !defined(TARGET_BROWSER)
if (success && !newMemory)
{
// Include committed memory in coredump. New memory is included by default.
Expand Down
12 changes: 8 additions & 4 deletions src/coreclr/hosts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
include_directories(inc)

if(CLR_CMAKE_HOST_WIN32)
add_subdirectory(coreshim)
endif(CLR_CMAKE_HOST_WIN32)
if (CLR_CMAKE_TARGET_ARCH_WASM)
add_subdirectory(corewasmrun)
else()
if(CLR_CMAKE_HOST_WIN32)
add_subdirectory(coreshim)
endif(CLR_CMAKE_HOST_WIN32)

add_subdirectory(corerun)
add_subdirectory(corerun)
endif()
28 changes: 28 additions & 0 deletions src/coreclr/hosts/corewasmrun/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
project(corewasmrun)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

if (DEFINED CLR_CMAKE_ICU_DIR)
link_directories(${CLR_CMAKE_ICU_DIR}/lib)
endif(DEFINED CLR_CMAKE_ICU_DIR)

add_executable_clr(corewasmrun
corewasmrun.cpp
)

set(_WASM_PRELOAD_DIR "${CMAKE_CURRENT_BINARY_DIR}/../../../../../bin/coreclr/browser.wasm.Debug/IL")
if (EXISTS "${_WASM_PRELOAD_DIR}")
set(_WASM_PRELOAD_FILE --preload-file ${_WASM_PRELOAD_DIR}@/)
endif (EXISTS "${_WASM_PRELOAD_DIR}")

target_compile_options(corewasmrun PRIVATE -fwasm-exceptions)
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)

target_link_libraries(corewasmrun PRIVATE coreclr)
target_link_libraries(corewasmrun PRIVATE clrinterpreter)

target_link_libraries(corewasmrun PRIVATE icuuc)
target_link_libraries(corewasmrun PRIVATE icui18n)
target_link_libraries(corewasmrun PRIVATE icudata)

install_clr(TARGETS corewasmrun DESTINATIONS . COMPONENT hosts)
45 changes: 45 additions & 0 deletions src/coreclr/hosts/corewasmrun/corewasmrun.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <cstdio>
#include <coreclrhost.h>

static void log_error_info(const char* line)
{
std::fprintf(stderr, "log error: %s\n", line);
}

// The current CoreCLR instance details.
static void* CurrentClrInstance;
static unsigned int CurrentAppDomainId;

static int run()
{
const char* exe_path = "<coreclr-wasm>";
const char* app_domain_name = "corewasmrun";
const char* entry_assembly = "ManagedAssembly.dll";

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);

if (retval < 0)
{
std::fprintf(stderr, "coreclr_initialize failed - Error: 0x%08x\n", retval);
return -1;
}
else
{
printf("coreclr_initialize succeeded - retval: 0x%08x\n", retval);
}

// coreclr_execute_assembly();
// coreclr_shutdown();

return retval;
}

int main()
{
int retval = run();

return retval;
}
52 changes: 52 additions & 0 deletions src/coreclr/hosts/corewasmrun/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>corewasmrun</title>
</head>
<body>
<h1>corewasmrun</h1>
<pre id="log"></pre>
<script>
Module = {
preRun: [ function () {
ENV.PAL_DBG_CHANNELS="+all.all";
// ENV.PAL_DBG_CHANNELS="+all.ERROR";
}],
onExit: function (code) {
console.log("onExit, code: " + code);
},
};

const originalConsoleLog = console.log;
console.log = function(message) {
originalConsoleLog(message);
fetch('/log=corewasmrun-log.txt', {
method: 'POST',
body: ('stdout: ' + message),
headers: {
'Content-Type': 'text/plain'
}
});
const elt = document.createElement("span");
elt.textContent = message + "\n";
document.querySelector("#log").appendChild(elt);
};
const originalConsoleError = console.error;
console.error = function(message) {
originalConsoleError(message);
fetch('/log=corewasmrun-log.txt', {
method: 'POST',
body: ('stderr: ' + message),
headers: {
'Content-Type': 'text/plain'
}
});
const elt = document.createElement("span");
elt.textContent = message + "\n";
elt.style.color = "red";
document.querySelector("#log").appendChild(elt);
};
</script>
<script src="corewasmrun.js"></script>
</body>
8 changes: 6 additions & 2 deletions src/coreclr/inc/clrnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ RtlVirtualUnwind(
#define UNW_FLAG_EHANDLER 0x1 /* filter handler */
#define UNW_FLAG_UHANDLER 0x2 /* unwind handler */

PEXCEPTION_ROUTINE
PEXCEPTION_ROUTINE inline
RtlVirtualUnwind (
_In_ DWORD HandlerType,
_In_ DWORD ImageBase,
Expand All @@ -520,7 +520,11 @@ RtlVirtualUnwind (
_Out_ PVOID *HandlerData,
_Out_ PDWORD EstablisherFrame,
__inout_opt PT_KNONVOLATILE_CONTEXT_POINTERS ContextPointers
);
)
{
_ASSERTE("The function RtlVirtualUnwind is not implemented on wasm");
return nullptr;
}

FORCEINLINE
ULONG
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/inc/loaderheap.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ struct LoaderHeapEvent;
// When an interleaved LoaderHeap is constructed, this is the interleaving size
inline UINT32 GetStubCodePageSize()
{
#if defined(TARGET_ARM64) && defined(TARGET_UNIX)
#if (defined(TARGET_ARM64) && defined(TARGET_UNIX)) || defined(TARGET_WASM)
return max(16*1024u, GetOsPageSize());
#elif defined(TARGET_ARM)
return 4096; // ARM is special as the 32bit instruction set does not easily permit a 16KB offset
Expand Down
7 changes: 6 additions & 1 deletion src/coreclr/interpreter/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,13 @@ class InterpCompiler
CORINFO_METHOD_INFO* m_methodInfo;
#ifdef DEBUG
const char *m_methodName;
#ifdef __wasm__
// enable verbose output on wasm temporarily
bool m_verbose = true;
#else
bool m_verbose = false;
#endif
#endif // __wasm__
#endif // DEBUG

static int32_t InterpGetMovForType(InterpType interpType, bool signExtend);

Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/minipal/Unix/doublemapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ static const off_t MaxDoubleMappedSize = UINT_MAX;

bool VMToOSInterface::CreateDoubleMemoryMapper(void** pHandle, size_t *pMaxExecutableCodeSize)
{
#ifdef __wasm__
return false;
#endif
if (minipal_detect_rosetta())
{
// Rosetta doesn't support double mapping correctly
Expand Down
Loading