Skip to content

[wasm][coreclr] Get further in the runtime initialization #116383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 52 commits into
base: main
Choose a base branch
from

Conversation

radekdoulik
Copy link
Member

@radekdoulik radekdoulik commented Jun 6, 2025

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

Use flat PE image loading on wasm

Do not use unsupported syscalls

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
@radekdoulik radekdoulik force-pushed the wasm-coreclr-wip-3-squashed branch from c49f135 to b32ad25 Compare June 6, 2025 16:23
@radekdoulik radekdoulik added this to the Future milestone Jun 6, 2025
@am11 am11 added arch-wasm WebAssembly architecture os-browser Browser variant of arch-wasm labels Jun 8, 2025
Copy link
Contributor

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>
radekdoulik and others added 17 commits June 20, 2025 07:29
And do not build coreclr
Disable larger part of GCToOSInterface::Initialize than before
Make InitializeFlushProcessWriteBuffers no-op on wasm
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Return false from ExecutableAllocator::IsDoubleMappingEnabled instead
of making the double mapping init no-op on wasm
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Fix few asserts
Define PEIMAGE_FLAT_LAYOUT_ONLY in inc\switches.h
Replace __wasm__ where appropriate
Do not use __builtin_return_address on wasm
Add InvokeCalliStub to helpers
Update comments and remove unwanted fields
Add comment to the wasm specific call description data fields
Add portability assert in DispatchCallSimple to light up if we get
called through this path
Use FEATURE_JIT instead of not WASM check
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_link_options(-fwasm-exceptions -sEXIT_RUNTIME=1)

This is duplicated in src/coreclr/hosts/corewasmrun/CMakeLists.txt. Do we need it here?

set_property(TARGET coreclr APPEND_STRING PROPERTY LINK_DEPENDS ${EXPORTS_FILE})
set_property(TARGET coreclr APPEND_STRING PROPERTY LINK_FLAGS ${EXPORTS_LINKER_OPTION})
set_property(TARGET coreclr APPEND_STRING PROPERTY LINK_DEPENDS ${EXPORTS_FILE})
endif(NOT CLR_CMAKE_HOST_ARCH_WASM)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
endif(NOT CLR_CMAKE_HOST_ARCH_WASM)
set(LIB_CORDBEE cordbee_wks)
set(LIB_INTEROP interop)
set(LIB_CDAC_CONTRACT_DESCRIPTOR cdac_contract_descriptor)
endif(NOT CLR_CMAKE_HOST_ARCH_WASM)

Comment on lines +78 to +83

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Folded this into the earlier if?

@@ -576,7 +577,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you believe that the rest of this implementation is going to work on wasm?

runtimelab branch reimplemented it from scratch for wasm: https://github.com/dotnet/runtimelab/blob/feature/NativeAOT-LLVM/src/coreclr/gc/wasm/gcenv.wasm.cpp . dotnet/runtimelab#1510 describes the motivation. Should we adopt this implementation instead of trying to ifdef what's here today?

@@ -624,9 +625,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dtto

@@ -6047,11 +6047,13 @@ BOOL Thread::SetStackLimits(SetStackLimitScope scope)
{
m_CacheStackBase = GetStackUpperBound();
m_CacheStackLimit = GetStackLowerBound();
#ifndef TARGET_WASM // stack can start at address 0 on wasm/emscripten and usually does
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed now that we are using emscripten_stack_get_base?

@@ -515,6 +515,9 @@ void MethodDescCallSite::CallTargetWorker(const ARG_SLOT *pArguments, ARG_SLOT *
CallDescrData callDescrData;

callDescrData.pSrc = pTransitionBlock + sizeof(TransitionBlock);
#ifdef TARGET_WASM
callDescrData.pTransitionBlock = (TransitionBlock*)pTransitionBlock;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can live with what's in this PR, but we should avoid growing it organically. We want to have discussion about how the calling convention should actually work before doing more work on it here.

this PR vs. extra PR

This PR has a bunch of good changes and a bunch of changes that need more work. If you would like to, I can go over it with you over it and separate the good non-controversial changes and get those in, and then work on the rest separately.


// for numeric_limits
#include <limits>

FCDECL1(float, JIT_ULng2Flt, uint64_t val);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this incorrectly resolved merge conflict?

@@ -201,6 +201,7 @@ DEFINE_METASIG_T(SM(Exception_Obj_RefIntPtr_RetVoidPtr, C(EXCEPTION) j r(I), P(v
#endif // FEATURE_OBJCMARSHAL
DEFINE_METASIG(SM(Int_RetVoid, i, v))
DEFINE_METASIG(SM(Int_RetObj, i, j))
DEFINE_METASIG(SM(Int_RetInt, i, i))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrectly resolved merge conflict?

@@ -2664,18 +2664,22 @@ FlushProcessWriteBuffers()
int status = pthread_mutex_lock(&flushProcessWriteBuffersMutex);
FATAL_ASSERT(status == 0, "Failed to lock the flushProcessWriteBuffersMutex lock");

#ifndef TARGET_BROWSER
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is same code as what's in src/coreclr/gc/unix/gcenv.unix.cpp. It should be ifdefed same way.

(Ideally, we would move this to minipal to avoid the duplication - but that would be larger change.)

@@ -260,7 +266,11 @@ TADDR PEAssembly::GetIL(RVA il)
CONTRACT_END;

PEImageLayout *image = NULL;
#ifdef PEIMAGE_FLAT_LAYOUT_ONLY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to return the flat layout from GetLoadedLayout. I suspect that we would need to ifdef a lot of calls to GetLoadedLayout otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm WebAssembly architecture area-VM-coreclr os-browser Browser variant of arch-wasm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants