From 779f95072010c320adf613aff0127e5336c8c323 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Tue, 21 Jan 2025 13:51:50 +0100 Subject: [PATCH 01/63] Make configure pass for wasm and paltests component Disable few parts, some might need closer look later. --- src/coreclr/CMakeLists.txt | 2 +- src/coreclr/clrfeatures.cmake | 4 +++- src/coreclr/jit/CMakeLists.txt | 3 +++ src/coreclr/pal/src/CMakeLists.txt | 18 ++++++++++-------- src/coreclr/pal/src/configure.cmake | 3 +++ .../corehost/apphost/static/CMakeLists.txt | 4 ++-- .../System.Globalization.Native/CMakeLists.txt | 2 +- .../System.Net.Security.Native/CMakeLists.txt | 4 +++- 8 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index 16308f0dce921e..00aa359e2cad42 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -246,7 +246,7 @@ if(CLR_CMAKE_HOST_UNIX) add_subdirectory(nativeresources) endif(CLR_CMAKE_HOST_UNIX) -if(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) +if(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS AND NOT CLR_CMAKE_HOST_BROWSER) add_subdirectory(utilcode) add_subdirectory(inc) diff --git a/src/coreclr/clrfeatures.cmake b/src/coreclr/clrfeatures.cmake index 2bf7216af1b4db..eff3f9d25badb5 100644 --- a/src/coreclr/clrfeatures.cmake +++ b/src/coreclr/clrfeatures.cmake @@ -3,7 +3,9 @@ if(CLR_CMAKE_TARGET_TIZEN_LINUX) endif() if(NOT DEFINED FEATURE_EVENT_TRACE) - set(FEATURE_EVENT_TRACE 1) + if (NOT CLR_CMAKE_TARGET_BROWSER) + set(FEATURE_EVENT_TRACE 1) + endif() endif(NOT DEFINED FEATURE_EVENT_TRACE) if(NOT DEFINED FEATURE_PERFTRACING AND FEATURE_EVENT_TRACE) diff --git a/src/coreclr/jit/CMakeLists.txt b/src/coreclr/jit/CMakeLists.txt index 104a7be7f18202..cfaad57968a3c0 100644 --- a/src/coreclr/jit/CMakeLists.txt +++ b/src/coreclr/jit/CMakeLists.txt @@ -500,6 +500,9 @@ elseif(CLR_CMAKE_TARGET_ARCH_LOONGARCH64) elseif(CLR_CMAKE_TARGET_ARCH_RISCV64) set(JIT_ARCH_SOURCES ${JIT_RISCV64_SOURCES}) set(JIT_ARCH_HEADERS ${JIT_RISCV64_HEADERS}) +elseif(CLR_CMAKE_TARGET_ARCH_WASM) + set(JIT_ARCH_SOURCES ${JIT_WASM32_SOURCES}) + set(JIT_ARCH_HEADERS ${JIT_WASM32_HEADERS}) else() clr_unknown_arch() endif() diff --git a/src/coreclr/pal/src/CMakeLists.txt b/src/coreclr/pal/src/CMakeLists.txt index 07c81a0661352e..12e7e49c4bcf83 100644 --- a/src/coreclr/pal/src/CMakeLists.txt +++ b/src/coreclr/pal/src/CMakeLists.txt @@ -1,8 +1,8 @@ -if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_HAIKU) +if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_HAIKU OR CLR_CMAKE_TARGET_BROWSER) # On OSX and *BSD, we use the libunwind that's part of the OS # On Haiku, we used a special port of libunwind set(CLR_CMAKE_USE_SYSTEM_LIBUNWIND 1) -endif(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_HAIKU) +endif(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_HAIKU OR CLR_CMAKE_TARGET_BROWSER) if(NOT DEFINED ENV{ROOTFS_DIR}) include_directories(SYSTEM /usr/local/include) @@ -110,13 +110,15 @@ endif(CLR_CMAKE_TARGET_HAIKU) # turn off capability to remove unused functions (which was enabled in debug build with sanitizers) set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -Wl,--no-gc-sections") -set(ARCH_SOURCES - arch/${PAL_ARCH_SOURCES_DIR}/context2.S - arch/${PAL_ARCH_SOURCES_DIR}/debugbreak.S - arch/${PAL_ARCH_SOURCES_DIR}/exceptionhelper.S -) +if (NOT CLR_CMAKE_TARGET_BROWSER) + set(ARCH_SOURCES + arch/${PAL_ARCH_SOURCES_DIR}/context2.S + arch/${PAL_ARCH_SOURCES_DIR}/debugbreak.S + arch/${PAL_ARCH_SOURCES_DIR}/exceptionhelper.S + ) +endif() -if(NOT CLR_CMAKE_TARGET_OSX) +if(NOT CLR_CMAKE_TARGET_OSX AND NOT CLR_CMAKE_TARGET_BROWSER) list(APPEND PLATFORM_SOURCES arch/${PAL_ARCH_SOURCES_DIR}/callsignalhandlerwrapper.S arch/${PAL_ARCH_SOURCES_DIR}/signalhandlerhelper.cpp diff --git a/src/coreclr/pal/src/configure.cmake b/src/coreclr/pal/src/configure.cmake index 8dd44c550ba5db..fe664e7008d433 100644 --- a/src/coreclr/pal/src/configure.cmake +++ b/src/coreclr/pal/src/configure.cmake @@ -935,6 +935,9 @@ elseif(CLR_CMAKE_TARGET_HAIKU) # Haiku does not have ptrace. set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 0) set(HAVE_SCHED_OTHER_ASSIGNABLE 1) +elseif(CLR_CMAKE_TARGET_BROWSER) + set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 0) + set(HAVE_SCHED_OTHER_ASSIGNABLE 0) else() # Anything else is Linux if(NOT HAVE_LTTNG_TRACEPOINT_H AND FEATURE_EVENT_TRACE) unset(HAVE_LTTNG_TRACEPOINT_H CACHE) diff --git a/src/native/corehost/apphost/static/CMakeLists.txt b/src/native/corehost/apphost/static/CMakeLists.txt index 25c1d55e7360cd..b7864655f2defa 100644 --- a/src/native/corehost/apphost/static/CMakeLists.txt +++ b/src/native/corehost/apphost/static/CMakeLists.txt @@ -184,7 +184,7 @@ else() include(${CLR_SRC_NATIVE_DIR}/libs/System.IO.Compression.Native/extra_libs.cmake) append_extra_compression_libs(NATIVE_LIBS) - if (NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_HOST_ANDROID) # no gssapi on tvOS, see https://developer.apple.com/documentation/gss + if (NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_HOST_ANDROID AND NOT CLR_CMAKE_TARGET_BROWSER) # no gssapi on tvOS, see https://developer.apple.com/documentation/gss # Additional requirements for System.Net.Security.Native include(${CLR_SRC_NATIVE_DIR}/libs/System.Net.Security.Native/extra_libs.cmake) append_extra_security_libs(NATIVE_LIBS) @@ -194,7 +194,7 @@ else() include(${CLR_SRC_NATIVE_DIR}/libs/System.Native/extra_libs.cmake) append_extra_system_libs(NATIVE_LIBS) - if(NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_HOST_ANDROID) + if(NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_HOST_ANDROID AND NOT CLR_CMAKE_TARGET_BROWSER) # Additional requirements for System.Security.Cryptography.Native.OpenSsl include(${CLR_SRC_NATIVE_DIR}/libs/System.Security.Cryptography.Native/extra_libs.cmake) append_extra_cryptography_libs(NATIVE_LIBS) diff --git a/src/native/libs/System.Globalization.Native/CMakeLists.txt b/src/native/libs/System.Globalization.Native/CMakeLists.txt index 035f85a257b97c..496bdb0d33e5cd 100644 --- a/src/native/libs/System.Globalization.Native/CMakeLists.txt +++ b/src/native/libs/System.Globalization.Native/CMakeLists.txt @@ -22,7 +22,7 @@ if(CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_WASI) add_compile_options(-Wno-unknown-warning-option) # The mobile configurations in particular bring their own ICU, so skip - if (NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT DEFINED CMAKE_ICU_DIR) + if (NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT DEFINED CMAKE_ICU_DIR AND NOT CLR_CMAKE_TARGET_BROWSER) if (CLR_CMAKE_TARGET_OSX) execute_process(COMMAND brew --prefix OUTPUT_VARIABLE brew_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) set(ICU_HOMEBREW_INC_PATH "${brew_prefix}/opt/icu4c/include") diff --git a/src/native/libs/System.Net.Security.Native/CMakeLists.txt b/src/native/libs/System.Net.Security.Native/CMakeLists.txt index b0e02f41509b58..4a19c74dae73b8 100644 --- a/src/native/libs/System.Net.Security.Native/CMakeLists.txt +++ b/src/native/libs/System.Net.Security.Native/CMakeLists.txt @@ -5,7 +5,9 @@ add_compile_options(-Wno-incompatible-pointer-types-discards-qualifiers) include(${CMAKE_CURRENT_LIST_DIR}/extra_libs.cmake) set(NATIVE_LIBS_EXTRA) -append_extra_security_libs(NATIVE_LIBS_EXTRA) +if (NOT CLR_CMAKE_TARGET_BROWSER) + append_extra_security_libs(NATIVE_LIBS_EXTRA) +endif() set(NATIVEGSS_SOURCES pal_gssapi.c From b33f467e016408c4024d9fd3055655be55867888 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Fri, 24 Jan 2025 17:05:54 +0100 Subject: [PATCH 02/63] Fix compilation and linking on wasm --- src/coreclr/inc/volatile.h | 2 +- src/coreclr/pal/CMakeLists.txt | 7 +- src/coreclr/pal/inc/pal.h | 29 ++++++++ src/coreclr/pal/inc/rt/palrt.h | 8 +++ src/coreclr/pal/src/CMakeLists.txt | 10 ++- src/coreclr/pal/src/arch/wasm/stubs.cpp | 69 +++++++++++++++++++ src/coreclr/pal/src/debug/debug.cpp | 5 ++ src/coreclr/pal/src/exception/seh-unwind.cpp | 4 ++ src/coreclr/pal/src/exception/seh.cpp | 4 +- src/coreclr/pal/src/include/pal/context.h | 13 ++++ src/coreclr/pal/src/misc/sysinfo.cpp | 2 + src/coreclr/pal/src/thread/context.cpp | 8 +++ .../pal_sxs/test1/dlltest1.cpp | 5 +- .../pal_sxs/test1/dlltest2.cpp | 5 +- 14 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 src/coreclr/pal/src/arch/wasm/stubs.cpp diff --git a/src/coreclr/inc/volatile.h b/src/coreclr/inc/volatile.h index efcb25f8acd8a5..ce49c38ef7ac22 100644 --- a/src/coreclr/inc/volatile.h +++ b/src/coreclr/inc/volatile.h @@ -68,7 +68,7 @@ #error The Volatile type is currently only defined for Visual C++ and GNU C++ #endif -#if defined(__GNUC__) && !defined(HOST_X86) && !defined(HOST_AMD64) && !defined(HOST_ARM) && !defined(HOST_ARM64) && !defined(HOST_LOONGARCH64) && !defined(HOST_RISCV64) && !defined(HOST_S390X) && !defined(HOST_POWERPC64) +#if defined(__GNUC__) && !defined(HOST_X86) && !defined(HOST_AMD64) && !defined(HOST_ARM) && !defined(HOST_ARM64) && !defined(HOST_LOONGARCH64) && !defined(HOST_RISCV64) && !defined(HOST_S390X) && !defined(HOST_POWERPC64) && !defined(HOST_WASM) #error The Volatile type is currently only defined for GCC when targeting x86, AMD64, ARM, ARM64, LOONGARCH64, RISCV64, PPC64LE, or S390X CPUs #endif diff --git a/src/coreclr/pal/CMakeLists.txt b/src/coreclr/pal/CMakeLists.txt index 9213941ba6da01..1f657a6144e053 100644 --- a/src/coreclr/pal/CMakeLists.txt +++ b/src/coreclr/pal/CMakeLists.txt @@ -6,7 +6,12 @@ include_directories(${COREPAL_SOURCE_DIR}/inc) include_directories(${COREPAL_SOURCE_DIR}/src) include_directories(${COREPAL_SOURCE_DIR}/../inc) -add_compile_options(-fexceptions) +if (NOT CLR_CMAKE_TARGET_BROWSER) + add_compile_options(-fexceptions) +else() + add_compile_options(-fwasm-exceptions) + add_link_options(-fwasm-exceptions) +endif() add_subdirectory(src) add_subdirectory(tests) diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index 032b888863ef8b..12f57b2256702b 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -2470,6 +2470,33 @@ typedef struct _KNONVOLATILE_CONTEXT_POINTERS { // } KNONVOLATILE_CONTEXT_POINTERS, *PKNONVOLATILE_CONTEXT_POINTERS; +#elif defined(HOST_WASM) +#define CONTEXT_CONTROL 0 +#define CONTEXT_INTEGER 0 +#define CONTEXT_FLOATING_POINT 0 +#define CONTEXT_FULL 0 + +#define CONTEXT_XSTATE 0 + +#define CONTEXT_EXCEPTION_ACTIVE 0x8000000L +#define CONTEXT_SERVICE_ACTIVE 0x10000000L +#define CONTEXT_EXCEPTION_REQUEST 0x40000000L +#define CONTEXT_EXCEPTION_REPORTING 0x80000000L + +typedef struct _CONTEXT { + ULONG ContextFlags; + + ULONG Dr0_PAL_Undefined; + ULONG Dr1_PAL_Undefined; + ULONG Dr2_PAL_Undefined; + ULONG Dr3_PAL_Undefined; + ULONG Dr6_PAL_Undefined; + ULONG Dr7_PAL_Undefined; +} CONTEXT, *PCONTEXT, *LPCONTEXT; + +typedef struct _KNONVOLATILE_CONTEXT_POINTERS { + DWORD none; +} KNONVOLATILE_CONTEXT_POINTERS, *PKNONVOLATILE_CONTEXT_POINTERS; #else #error Unknown architecture for defining CONTEXT. @@ -2601,6 +2628,8 @@ PALIMPORT BOOL PALAPI PAL_GetUnwindInfoSize(SIZE_T baseAddress, ULONG64 ehFrameH #define PAL_CS_NATIVE_DATA_SIZE 96 #elif defined(__HAIKU__) && defined(__x86_64__) #define PAL_CS_NATIVE_DATA_SIZE 56 +#elif defined(HOST_WASM) +#define PAL_CS_NATIVE_DATA_SIZE 76 #else #error PAL_CS_NATIVE_DATA_SIZE is not defined for this architecture #endif diff --git a/src/coreclr/pal/inc/rt/palrt.h b/src/coreclr/pal/inc/rt/palrt.h index 9317654958b63b..20a936ae18f09e 100644 --- a/src/coreclr/pal/inc/rt/palrt.h +++ b/src/coreclr/pal/inc/rt/palrt.h @@ -1030,6 +1030,14 @@ typedef struct _DISPATCHER_CONTEXT { DWORD Reserved; } DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT; +#elif defined(HOST_WASM) + +typedef struct _DISPATCHER_CONTEXT { + // WASM does not build the VM or JIT at this point, + // so we only provide a dummy definition. + DWORD Reserved; +} DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT; + #else #error Unknown architecture for defining DISPATCHER_CONTEXT. diff --git a/src/coreclr/pal/src/CMakeLists.txt b/src/coreclr/pal/src/CMakeLists.txt index 12e7e49c4bcf83..e455183f32e949 100644 --- a/src/coreclr/pal/src/CMakeLists.txt +++ b/src/coreclr/pal/src/CMakeLists.txt @@ -59,6 +59,8 @@ elseif(CLR_CMAKE_HOST_ARCH_S390X) set(PAL_ARCH_SOURCES_DIR s390x) elseif(CLR_CMAKE_HOST_ARCH_POWERPC64) set(PAL_ARCH_SOURCES_DIR ppc64le) +elseif(CLR_CMAKE_HOST_ARCH_WASM) + set(PAL_ARCH_SOURCES_DIR wasm) endif() if(CLR_CMAKE_USE_SYSTEM_LIBUNWIND) @@ -118,12 +120,18 @@ if (NOT CLR_CMAKE_TARGET_BROWSER) ) endif() +if (CLR_CMAKE_TARGET_BROWSER) + set(PLATFORM_SOURCES + arch/${PAL_ARCH_SOURCES_DIR}/stubs.cpp + ) +endif() + if(NOT CLR_CMAKE_TARGET_OSX AND NOT CLR_CMAKE_TARGET_BROWSER) list(APPEND PLATFORM_SOURCES arch/${PAL_ARCH_SOURCES_DIR}/callsignalhandlerwrapper.S arch/${PAL_ARCH_SOURCES_DIR}/signalhandlerhelper.cpp ) -endif(NOT CLR_CMAKE_TARGET_OSX) +endif(NOT CLR_CMAKE_TARGET_OSX AND NOT CLR_CMAKE_TARGET_BROWSER) if(CLR_CMAKE_HOST_ARCH_ARM) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") diff --git a/src/coreclr/pal/src/arch/wasm/stubs.cpp b/src/coreclr/pal/src/arch/wasm/stubs.cpp new file mode 100644 index 00000000000000..388d321c12dc81 --- /dev/null +++ b/src/coreclr/pal/src/arch/wasm/stubs.cpp @@ -0,0 +1,69 @@ +#include "pal/dbgmsg.h" +#include "pal/signal.hpp" +SET_DEFAULT_DEBUG_CHANNEL(EXCEPT); // some headers have code with asserts, so do this first + +/* debugbreak */ + +extern "C" void +DBG_DebugBreak() +{ + _ASSERT("DebugBreak not implemented on wasm"); +} + +/* context */ + +extern "C" void +RtlCaptureContext(OUT PCONTEXT ContextRecord) +{ + _ASSERT("RtlCaptureContext not implemented on wasm"); +} + +extern "C" void +CONTEXT_CaptureContext(LPCONTEXT lpContext) +{ + _ASSERT("CONTEXT_CaptureContext not implemented on wasm"); +} + +extern "C" void ThrowExceptionFromContextInternal(CONTEXT* context, PAL_SEHException* ex) +{ + _ASSERT("ThrowExceptionFromContextInternal not implemented on wasm"); +} + +/* unwind */ + +void ExecuteHandlerOnCustomStack(int code, siginfo_t *siginfo, void *context, size_t sp, SignalHandlerWorkerReturnPoint* returnPoint) +{ + _ASSERT("ExecuteHandlerOnCustomStack not implemented on wasm"); +} + +extern "C" int unw_getcontext(int) +{ + _ASSERT("unw_getcontext not implemented on wasm"); + return 0; +} + +extern "C" int unw_init_local(int, int) +{ + _ASSERT("unw_init_local not implemented on wasm"); + return 0; +} + +extern "C" int unw_step(int) +{ + _ASSERT("unw_step not implemented on wasm"); + return 0; +} + +extern "C" int unw_is_signal_frame(int) +{ + _ASSERT("unw_is_signal_frame not implemented on wasm"); + return 0; +} + +/* threading */ + +extern "C" int pthread_setschedparam(pthread_t, int, const struct sched_param *) +{ + _ASSERT("pthread_setschedparam not implemented on wasm"); + return 0; +} diff --git a/src/coreclr/pal/src/debug/debug.cpp b/src/coreclr/pal/src/debug/debug.cpp index 8783ff48603219..f1114300d9c639 100644 --- a/src/coreclr/pal/src/debug/debug.cpp +++ b/src/coreclr/pal/src/debug/debug.cpp @@ -420,7 +420,12 @@ DebugBreak( BOOL IsInDebugBreak(void *addr) { +#if defined (__wasm__) + _ASSERT("IsInDebugBreak not implemented on wasm"); + return false; +#else return (addr >= (void *)DBG_DebugBreak) && (addr <= (void *)DBG_DebugBreak_End); +#endif } /*++ diff --git a/src/coreclr/pal/src/exception/seh-unwind.cpp b/src/coreclr/pal/src/exception/seh-unwind.cpp index 71e94f929cf33f..f890a03dccb010 100644 --- a/src/coreclr/pal/src/exception/seh-unwind.cpp +++ b/src/coreclr/pal/src/exception/seh-unwind.cpp @@ -499,6 +499,8 @@ void UnwindContextToWinContext(unw_cursor_t *cursor, CONTEXT *winContext) unw_get_reg(cursor, UNW_PPC64_R28, (unw_word_t *) &winContext->R28); unw_get_reg(cursor, UNW_PPC64_R29, (unw_word_t *) &winContext->R29); unw_get_reg(cursor, UNW_PPC64_R30, (unw_word_t *) &winContext->R30); +#elif defined(HOST_WASM) + ASSERT("UnwindContextToWinContext not implemented for WASM"); #else #error unsupported architecture #endif @@ -634,6 +636,8 @@ void GetContextPointers(unw_cursor_t *cursor, unw_context_t *unwContext, KNONVOL GetContextPointer(cursor, unwContext, UNW_PPC64_R29, (SIZE_T **)&contextPointers->R29); GetContextPointer(cursor, unwContext, UNW_PPC64_R30, (SIZE_T **)&contextPointers->R30); GetContextPointer(cursor, unwContext, UNW_PPC64_R31, (SIZE_T **)&contextPointers->R31); +#elif defined(HOST_WASM) + ASSERT("GetContextPointers not implemented for WASM"); #else #error unsupported architecture #endif diff --git a/src/coreclr/pal/src/exception/seh.cpp b/src/coreclr/pal/src/exception/seh.cpp index 02a540734001b0..bff76ad159f36d 100644 --- a/src/coreclr/pal/src/exception/seh.cpp +++ b/src/coreclr/pal/src/exception/seh.cpp @@ -304,7 +304,7 @@ PAL_ERROR SEHEnable(CPalThread *pthrCurrent) { #if HAVE_MACH_EXCEPTIONS return pthrCurrent->EnableMachExceptions(); -#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(__HAIKU__) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(__HAIKU__) || defined(__wasm__) return NO_ERROR; #else// HAVE_MACH_EXCEPTIONS #error not yet implemented @@ -329,7 +329,7 @@ PAL_ERROR SEHDisable(CPalThread *pthrCurrent) { #if HAVE_MACH_EXCEPTIONS return pthrCurrent->DisableMachExceptions(); -#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(__HAIKU__) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(__HAIKU__) || defined(__wasm__) return NO_ERROR; #else // HAVE_MACH_EXCEPTIONS #error not yet implemented diff --git a/src/coreclr/pal/src/include/pal/context.h b/src/coreclr/pal/src/include/pal/context.h index 5d515a1434e00c..b74b5ac388cd73 100644 --- a/src/coreclr/pal/src/include/pal/context.h +++ b/src/coreclr/pal/src/include/pal/context.h @@ -209,6 +209,11 @@ struct sve_context { #define MCREG_Xer(mc) ((mc).gp_regs[37]) #define MCREG_Ccr(mc) ((mc).gp_regs[38]) +#elif defined(HOST_WASM) + +#define MCREG_Sp(mc) 0 +#define MCREG_Pc(mc) 0 + #elif HAVE___GREGSET_T #ifdef HOST_64BIT @@ -1389,6 +1394,9 @@ inline static DWORD64 CONTEXTGetPC(LPCONTEXT pContext) return pContext->PSWAddr; #elif defined(HOST_POWERPC64) return pContext->Nip; +#elif defined(HOST_WASM) // wasm has no PC + _ASSERT(false); + return 0; #else return pContext->Pc; #endif @@ -1404,6 +1412,8 @@ inline static void CONTEXTSetPC(LPCONTEXT pContext, DWORD64 pc) pContext->PSWAddr = pc; #elif defined(HOST_POWERPC64) pContext->Nip = pc; +#elif defined(HOST_WASM) // wasm has no PC + _ASSERT(false); #else pContext->Pc = pc; #endif @@ -1421,6 +1431,9 @@ inline static DWORD64 CONTEXTGetFP(LPCONTEXT pContext) return pContext->R11; #elif defined(HOST_POWERPC64) return pContext->R31; +#elif defined(HOST_WASM) // wasm has no PC + _ASSERT(false); + return 0; #else return pContext->Fp; #endif diff --git a/src/coreclr/pal/src/misc/sysinfo.cpp b/src/coreclr/pal/src/misc/sysinfo.cpp index d2d5af184f2fc4..3d899eccd62dd0 100644 --- a/src/coreclr/pal/src/misc/sysinfo.cpp +++ b/src/coreclr/pal/src/misc/sysinfo.cpp @@ -219,6 +219,8 @@ GetSystemInfo( lpSystemInfo->lpMaximumApplicationAddress = (PVOID) 0xfffffd7fffe00000ul; #elif defined(__HAIKU__) lpSystemInfo->lpMaximumApplicationAddress = (PVOID) 0x7fffffe00000ul; +#elif defined(__wasm__) + lpSystemInfo->lpMaximumApplicationAddress = (PVOID) (1ul << 31); #elif defined(USERLIMIT) lpSystemInfo->lpMaximumApplicationAddress = (PVOID) USERLIMIT; #elif defined(HOST_64BIT) diff --git a/src/coreclr/pal/src/thread/context.cpp b/src/coreclr/pal/src/thread/context.cpp index ecd252936299df..6a7f1350c3a1c0 100644 --- a/src/coreclr/pal/src/thread/context.cpp +++ b/src/coreclr/pal/src/thread/context.cpp @@ -314,6 +314,11 @@ typedef int __ptrace_request; ASSIGN_REG(R29) \ ASSIGN_REG(R30) +#elif defined(HOST_WASM) +#define ASSIGN_CONTROL_REGS \ + ASSERT("WASM does not have registers"); +#define ASSIGN_INTEGER_REGS \ + ASSERT("WASM does not have registers"); #else #error "Don't know how to assign registers on this architecture" #endif @@ -2181,6 +2186,9 @@ DBG_FlushInstructionCache( #endif syscall(__NR_riscv_flush_icache, (char *)lpBaseAddress, (char *)((INT_PTR)lpBaseAddress + dwSize), 0 /* all harts */); +#elif defined(HOST_WASM) + // No need to flush the instruction cache on WebAssembly? + ASSERT("llvm.clear_cache is not supported on wasm"); #else __builtin___clear_cache((char *)lpBaseAddress, (char *)((INT_PTR)lpBaseAddress + dwSize)); #endif diff --git a/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/dlltest1.cpp b/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/dlltest1.cpp index 1ed1e1f88d1779..d05a2aa4b454e7 100644 --- a/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/dlltest1.cpp +++ b/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/dlltest1.cpp @@ -33,8 +33,9 @@ static void FailingFunction(volatile int *p) *p = 1; // Causes an access violation exception } -BOOL bTry = FALSE; -BOOL bExcept = FALSE; +// temporarily make these static to make it build, we should disable these on wasm as we don't support shared libraries +static BOOL bTry = FALSE; +static BOOL bExcept = FALSE; extern "C" int diff --git a/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/dlltest2.cpp b/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/dlltest2.cpp index 10be0de5598926..4e7e5797eebd06 100644 --- a/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/dlltest2.cpp +++ b/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/dlltest2.cpp @@ -33,8 +33,9 @@ static void FailingFunction(volatile int *p) *p = 1; // Causes an access violation exception } -BOOL bTry = FALSE; -BOOL bExcept = FALSE; +// temporarily make these static to make it build, we should disable these on wasm as we don't support shared libraries +static BOOL bTry = FALSE; +static BOOL bExcept = FALSE; extern "C" int From 812d033e360ec577287f6e77b02c9637428afc24 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Mon, 27 Jan 2025 17:25:39 +0100 Subject: [PATCH 03/63] Ignore limits and dlopen(nullptr, ...) on wasm --- src/coreclr/pal/src/init/pal.cpp | 4 ++++ src/coreclr/pal/src/loader/module.cpp | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/coreclr/pal/src/init/pal.cpp b/src/coreclr/pal/src/init/pal.cpp index 8dcf3a1aee4830..89a2869889bcc1 100644 --- a/src/coreclr/pal/src/init/pal.cpp +++ b/src/coreclr/pal/src/init/pal.cpp @@ -949,6 +949,10 @@ Return value: --*/ static BOOL INIT_IncreaseDescriptorLimit(void) { +#ifdef __wasm__ + // WebAssembly cannot set limits + return TRUE; +#endif #ifndef DONT_SET_RLIMIT_NOFILE struct rlimit rlp; int result; diff --git a/src/coreclr/pal/src/loader/module.cpp b/src/coreclr/pal/src/loader/module.cpp index fe43edcc0b97ca..ffb4da3da276fc 100644 --- a/src/coreclr/pal/src/loader/module.cpp +++ b/src/coreclr/pal/src/loader/module.cpp @@ -1016,11 +1016,13 @@ BOOL LOADInitializeModules() exe_module.self = (HMODULE)&exe_module; exe_module.dl_handle = dlopen(nullptr, RTLD_LAZY); +#if not defined(__wasm__) // wasm does not support shared libraries if (exe_module.dl_handle == nullptr) { ERROR("Executable module will be broken : dlopen(nullptr) failed\n"); return FALSE; } +#endif exe_module.lib_name = nullptr; exe_module.refcount = -1; exe_module.next = &exe_module; From d5609a13c8169b432bac0dbf9bc2db1a9c5ff4da Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Mon, 27 Jan 2025 17:27:50 +0100 Subject: [PATCH 04/63] Do not return 0 for minipal_get_current_thread_id on wasm --- src/native/minipal/thread.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/native/minipal/thread.h b/src/native/minipal/thread.h index 0ebd00415dbb19..500183673f08b6 100644 --- a/src/native/minipal/thread.h +++ b/src/native/minipal/thread.h @@ -43,9 +43,6 @@ extern "C" { */ static inline size_t minipal_get_current_thread_id(void) { -#ifdef __wasm - return 0; -#else #if defined(__GNUC__) && !defined(__clang__) && defined(__cplusplus) // gcc doesn't like _Thread_local when __cplusplus is defined. // although thread_local is C2x, which other compilers don't allow with C11. @@ -74,7 +71,6 @@ static inline size_t minipal_get_current_thread_id(void) #endif return tid; -#endif } /** From 249c63be0990d83ab81aa4b049df26a80459f0fa Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Mon, 27 Jan 2025 17:33:51 +0100 Subject: [PATCH 05/63] Add simple index.html to run paltests on wasm in browser --- src/coreclr/pal/tests/palsuite/wasm/index.html | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/coreclr/pal/tests/palsuite/wasm/index.html diff --git a/src/coreclr/pal/tests/palsuite/wasm/index.html b/src/coreclr/pal/tests/palsuite/wasm/index.html new file mode 100644 index 00000000000000..5e91590d4192e2 --- /dev/null +++ b/src/coreclr/pal/tests/palsuite/wasm/index.html @@ -0,0 +1,10 @@ + + + + + PAL Tests WASM + + +

PAL Tests WASM

+ + From 182dbab9be31a7b3e83c4b03a825dc6ba05ea434 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Tue, 28 Jan 2025 12:15:06 +0100 Subject: [PATCH 06/63] Forward console log and error to http server --- .../pal/tests/palsuite/wasm/index.html | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/coreclr/pal/tests/palsuite/wasm/index.html b/src/coreclr/pal/tests/palsuite/wasm/index.html index 5e91590d4192e2..3fd699c486edc1 100644 --- a/src/coreclr/pal/tests/palsuite/wasm/index.html +++ b/src/coreclr/pal/tests/palsuite/wasm/index.html @@ -6,5 +6,29 @@

PAL Tests WASM

+ From 1e85092e074f0f36436452dbc6ec5646d0b12631 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Tue, 28 Jan 2025 13:11:56 +0100 Subject: [PATCH 07/63] Enable debug output temporarily --- src/coreclr/pal/src/include/pal/dbgmsg.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/coreclr/pal/src/include/pal/dbgmsg.h b/src/coreclr/pal/src/include/pal/dbgmsg.h index ca8d1454de9143..f58f4b430f027d 100644 --- a/src/coreclr/pal/src/include/pal/dbgmsg.h +++ b/src/coreclr/pal/src/include/pal/dbgmsg.h @@ -259,9 +259,15 @@ extern Volatile dbg_master_switch ; static const DBG_CHANNEL_ID defdbgchan = DCI_##x /* Is debug output enabled for the given level and channel? */ +#ifdef __wasm__ +// enable all debug output temporarily, before we have a way to control it +#define DBG_ENABLED(level, channel) (output_file && \ + dbg_master_switch) +#else #define DBG_ENABLED(level, channel) (output_file && \ dbg_master_switch && \ (dbg_channel_flags[channel] & (1 << (level)))) +#endif #define TRACE \ DBG_PRINTF(DLI_TRACE,defdbgchan,TRUE) From e3a397d11de6b4e6e200e590a2bcde4513e642c3 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Tue, 28 Jan 2025 20:18:35 +0100 Subject: [PATCH 08/63] Do not create synchronization manager worker thread on wasm And also skip creating pipe to communicate with it --- src/coreclr/pal/src/init/pal.cpp | 3 ++- src/coreclr/pal/src/synchmgr/synchmanager.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/coreclr/pal/src/init/pal.cpp b/src/coreclr/pal/src/init/pal.cpp index 89a2869889bcc1..7a74159cae60b8 100644 --- a/src/coreclr/pal/src/init/pal.cpp +++ b/src/coreclr/pal/src/init/pal.cpp @@ -607,6 +607,7 @@ Initialize( } } +#ifndef __wasm__ if (flags & PAL_INITIALIZE_SYNC_THREAD) { // @@ -619,7 +620,7 @@ Initialize( goto CLEANUP13; } } - +#endif /* initialize structured exception handling stuff (signals, etc) */ if (FALSE == SEHInitialize(pThread, flags)) { diff --git a/src/coreclr/pal/src/synchmgr/synchmanager.cpp b/src/coreclr/pal/src/synchmgr/synchmanager.cpp index eaccd19e209b6e..0d1b0bc32ed1b7 100644 --- a/src/coreclr/pal/src/synchmgr/synchmanager.cpp +++ b/src/coreclr/pal/src/synchmgr/synchmanager.cpp @@ -1340,13 +1340,14 @@ namespace CorUnix goto I_exit; } +#ifndef __wasm__ if (!pSynchManager->CreateProcessPipe()) { ERROR("Unable to create process pipe \n"); palErr = ERROR_OPEN_FAILED; goto I_exit; } - +#endif s_pObjSynchMgr = pSynchManager; // Initialization was successful From c81583293ec8aefa55f02424ce54c8a277f66936 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Tue, 28 Jan 2025 20:19:46 +0100 Subject: [PATCH 09/63] Temporarily override main args on wasm to simplify testing --- src/coreclr/pal/tests/palsuite/paltests.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/coreclr/pal/tests/palsuite/paltests.cpp b/src/coreclr/pal/tests/palsuite/paltests.cpp index 4ec0a3982e3cf5..3de4be18b3fd19 100644 --- a/src/coreclr/pal/tests/palsuite/paltests.cpp +++ b/src/coreclr/pal/tests/palsuite/paltests.cpp @@ -45,6 +45,16 @@ int PrintTests(int argc, char *argv[]) int __cdecl main(int argc, char *argv[]) { +#if defined(__wasm__) + argc = 2; + argv = (char**)malloc(2 * sizeof(char*)); + argv[0] = "paltests"; + argv[1] = "PrintPalTests"; + argv[1] = "samples/test1/paltest_samples_test1"; + //argv[1] = "c_runtime/isdigit/test1/paltest_isdigit_test1"; + //argv[1] = "c_runtime/atof/test1/paltest_atof_test1"; +#endif + if (argc < 2) { return PrintUsage(argc, argv); From b31bd173404a9994fb23677702405cc193946a24 Mon Sep 17 00:00:00 2001 From: Katelyn Gadd Date: Tue, 28 Jan 2025 22:17:34 -0800 Subject: [PATCH 10/63] Prototype pal test suite runner --- src/coreclr/pal/tests/palsuite/paltests.cpp | 12 +--- .../pal/tests/palsuite/wasm/index.html | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/src/coreclr/pal/tests/palsuite/paltests.cpp b/src/coreclr/pal/tests/palsuite/paltests.cpp index 3de4be18b3fd19..07e8ff6ece73b5 100644 --- a/src/coreclr/pal/tests/palsuite/paltests.cpp +++ b/src/coreclr/pal/tests/palsuite/paltests.cpp @@ -45,16 +45,6 @@ int PrintTests(int argc, char *argv[]) int __cdecl main(int argc, char *argv[]) { -#if defined(__wasm__) - argc = 2; - argv = (char**)malloc(2 * sizeof(char*)); - argv[0] = "paltests"; - argv[1] = "PrintPalTests"; - argv[1] = "samples/test1/paltest_samples_test1"; - //argv[1] = "c_runtime/isdigit/test1/paltest_isdigit_test1"; - //argv[1] = "c_runtime/atof/test1/paltest_atof_test1"; -#endif - if (argc < 2) { return PrintUsage(argc, argv); @@ -64,7 +54,7 @@ int __cdecl main(int argc, char *argv[]) { return PrintTests(argc, argv); } - + PALTest *testCur = PALTest::s_tests; for (;testCur != 0; testCur = testCur->_next) { diff --git a/src/coreclr/pal/tests/palsuite/wasm/index.html b/src/coreclr/pal/tests/palsuite/wasm/index.html index 3fd699c486edc1..1f7402232c2ab3 100644 --- a/src/coreclr/pal/tests/palsuite/wasm/index.html +++ b/src/coreclr/pal/tests/palsuite/wasm/index.html @@ -6,7 +6,58 @@

PAL Tests WASM

+
+    
From c8e6f43101924687a9be6223d3a3a56eb1a26841 Mon Sep 17 00:00:00 2001 From: Katelyn Gadd Date: Tue, 28 Jan 2025 22:32:10 -0800 Subject: [PATCH 11/63] Reload between tests to work around stack exhaustion --- .../pal/tests/palsuite/wasm/index.html | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/coreclr/pal/tests/palsuite/wasm/index.html b/src/coreclr/pal/tests/palsuite/wasm/index.html index 1f7402232c2ab3..979bee20c37216 100644 --- a/src/coreclr/pal/tests/palsuite/wasm/index.html +++ b/src/coreclr/pal/tests/palsuite/wasm/index.html @@ -10,16 +10,21 @@

PAL Tests WASM