Skip to content

Commit 242c38b

Browse files
radekdoulikCopilotjkotas
authored
Enable debugger-free Hot Reload on CoreCLR browser WebAssembly (#135496)
<!-- --> ## Summary Enable debugger-free metadata updates and portable-entrypoint code versioning on CoreCLR browser and WASI WebAssembly. Browser uses the existing `dotnet watch` integration; WASI supports `MetadataUpdater.ApplyUpdate`, not watch transport. Updates affect subsequent calls; active frames keep running their original code. - Guard unsupported frame remapping and handle an absent debugger interface. - Enable browser/WASI updater regressions, preserving existing Mono quarantines. Configure the WASI test guest environment and deploy delta fixtures beside their assemblies. - Add external-app WebCIL configuration coverage. Reuses metadata plumbing from [#135377](#135377). No debugger implementation or new public API. Related to [#134337](#134337). ## Validation macOS arm64: - October 22, merged main `c871829bb08`: Debug browser/WASI `clr+libs` passed. Each updater suite: 23 passed, zero failures, two existing skips. - Chromium functional: both deltas, exit 42. Generated `browseronly` archive contains the runner and both delta generations; archive/Mono gates checked for Unix/Windows properties. - October 15, older base: native Release build and WebCIL regression passed. Real browser `dotnet watch`: two updates, preserved state/page identity, no managed debugger. Release/R2R, Windows/Linux execution, and attached-profiler/debugger scenarios were not repeated after this merge. ## Cost September 30 measurements on the older base: native wasm +41,901 B raw / +15,069 B gzip; `MethodDescCodeData` 8 → 16 B per allocated record. Feature overhead is always present; production-size measurements have not been repeated on the current base. > [!NOTE] > This description was prepared with GitHub Copilot. --------- Co-authored-by: Copilot App <[email protected]> Co-authored-by: Jan Kotas <[email protected]> Copilot-Session: 9a6e8921-91d9-449b-b60c-7366dddfb7f4
1 parent 6500646 commit 242c38b

13 files changed

Lines changed: 100 additions & 25 deletions

File tree

‎src/coreclr/clrdefinitions.cmake‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ if(CLR_CMAKE_HOST_WIN32)
5858
add_compile_definitions(NOMINMAX)
5959
endif(CLR_CMAKE_HOST_WIN32)
6060

61-
if (NOT ((CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX) OR CLR_CMAKE_TARGET_ARCH_WASM))
61+
if (NOT (CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX))
6262
set(FEATURE_METADATA_UPDATER 1)
6363
add_compile_definitions(FEATURE_METADATA_UPDATER)
6464
endif()

‎src/coreclr/vm/encee.cpp‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ HRESULT EditAndContinueModule::AddField(mdFieldDef token)
556556
return hr;
557557
}
558558

559+
#ifdef FEATURE_REMAP_FUNCTION
559560
//---------------------------------------------------------------------------------------
560561
//
561562
// JitUpdatedFunction - Jit the new version of a function for EnC.
@@ -660,6 +661,7 @@ PCODE EditAndContinueModule::JitUpdatedFunction( MethodDesc *pMD,
660661

661662
return jittedCode;
662663
}
664+
#endif // FEATURE_REMAP_FUNCTION
663665

664666

665667
//-----------------------------------------------------------------------------
@@ -668,7 +670,6 @@ PCODE EditAndContinueModule::JitUpdatedFunction( MethodDesc *pMD,
668670
// 1) jit the new function
669671
// 2) set the IP to newILOffset within that new function
670672
// 3) adjust local variables (particularly enregistered vars) to the new func.
671-
// It will not return.
672673
//
673674
// Params:
674675
// pMD - method desc for method being updated. This is not enc-version aware.
@@ -678,16 +679,16 @@ PCODE EditAndContinueModule::JitUpdatedFunction( MethodDesc *pMD,
678679
// pOrigContext - context of thread pointing into original version of the function.
679680
//
680681
// This function must be called on the thread that's executing the old function.
681-
// This function does not return. Instead, it will remap this thread directly
682-
// to be executing the new function.
682+
// Successful remapping transfers execution to the new function and does not return.
683+
// Returns E_NOTIMPL if frame remapping is unsupported.
683684
//-----------------------------------------------------------------------------
684685
HRESULT EditAndContinueModule::ResumeInUpdatedFunction(
685686
MethodDesc *pMD,
686687
void *oldDebuggerFuncHandle,
687688
SIZE_T newILOffset,
688689
CONTEXT *pOrigContext)
689690
{
690-
#if defined(TARGET_ARM) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
691+
#ifndef FEATURE_REMAP_FUNCTION
691692
return E_NOTIMPL;
692693
#else
693694
LOG((LF_ENC, LL_INFO100, "EACM::ResumeInUpdatedFunction for %s::%s at IL offset 0x%zx\n",
@@ -787,7 +788,7 @@ HRESULT EditAndContinueModule::ResumeInUpdatedFunction(
787788
// Win32 handlers on the stack so cannot ever return from this function.
788789
EEPOLICY_HANDLE_FATAL_ERROR(CORDBG_E_ENC_INTERNAL_ERROR);
789790
return E_FAIL;
790-
#endif // #if defined(TARGET_ARM) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
791+
#endif // !FEATURE_REMAP_FUNCTION
791792

792793
}
793794

@@ -1718,7 +1719,8 @@ EncApproxFieldDescIterator::EncApproxFieldDescIterator(MethodTable *pMT, int ite
17181719

17191720
#ifndef DACCESS_COMPILE
17201721
// can't fixup for EnC on the debugger thread
1721-
_ASSERTE((g_pDebugInterface->GetRCThreadId() != GetCurrentThreadId()) || !(m_flags & FixUpEncFields));
1722+
_ASSERTE(g_pDebugInterface == nullptr ||
1723+
(g_pDebugInterface->GetRCThreadId() != GetCurrentThreadId()) || !(m_flags & FixUpEncFields));
17221724
#endif
17231725

17241726
m_pCurrListElem = NULL;
@@ -1780,7 +1782,7 @@ PTR_FieldDesc EncApproxFieldDescIterator::Next()
17801782
// this list. Can't simply fixup the field always because loading triggers GC and many
17811783
// code paths can't tolerate that.
17821784
_ASSERTE( !(pFD->NeedsFixup()) ||
1783-
( g_pDebugInterface->GetRCThreadId() == GetCurrentThreadId() ) );
1785+
(g_pDebugInterface != nullptr && g_pDebugInterface->GetRCThreadId() == GetCurrentThreadId()) );
17841786
#endif
17851787

17861788
return dac_cast<PTR_FieldDesc>(pFD);

‎src/coreclr/vm/encee.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,10 @@ class EditAndContinueModule : public Module
258258
// Called when a new field has been added to the module's metadata
259259
HRESULT AddField(mdFieldDef token);
260260

261+
#ifdef FEATURE_REMAP_FUNCTION
261262
// JIT the new version of a function for EnC
262263
PCODE JitUpdatedFunction(MethodDesc *pMD, T_CONTEXT *pContext);
264+
#endif // FEATURE_REMAP_FUNCTION
263265

264266
public:
265267
// Remap execution to the latest version of an edited method

‎src/coreclr/vm/method.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3415,6 +3415,9 @@ void MethodDesc::TrySetInitialCodeEntryPointForVersionableMethod(
34153415
_ASSERTE(IsVersionable());
34163416
_ASSERTE(mayHaveEntryPointSlotsToBackpatch == MayHaveEntryPointSlotsToBackpatch());
34173417

3418+
#ifdef FEATURE_PORTABLE_ENTRYPOINTS
3419+
SetStableEntryPointInterlocked(entryPoint);
3420+
#else // !FEATURE_PORTABLE_ENTRYPOINTS
34183421
if (mayHaveEntryPointSlotsToBackpatch)
34193422
{
34203423
TryBackpatchEntryPointSlotsFromPrestub(entryPoint);
@@ -3424,6 +3427,7 @@ void MethodDesc::TrySetInitialCodeEntryPointForVersionableMethod(
34243427
_ASSERTE(IsVersionableWithPrecode());
34253428
GetOrCreatePrecode()->SetTargetInterlocked(entryPoint, TRUE /* fOnlyRedirectFromPrestub */);
34263429
}
3430+
#endif // FEATURE_PORTABLE_ENTRYPOINTS
34273431
}
34283432
#endif // FEATURE_CODE_VERSIONING
34293433

@@ -3481,8 +3485,10 @@ void MethodDesc::ResetCodeEntryPoint()
34813485
#endif
34823486

34833487
#ifdef FEATURE_PORTABLE_ENTRYPOINTS
3488+
// Resetting the portable entry point alone is sufficient; these targets have no
3489+
// backpatchable slots or precode to additionally reset.
34843490
ResetPortableEntryPoint();
3485-
#endif // FEATURE_PORTABLE_ENTRYPOINTS
3491+
#else // !FEATURE_PORTABLE_ENTRYPOINTS
34863492

34873493
if (MayHaveEntryPointSlotsToBackpatch())
34883494
{
@@ -3495,6 +3501,7 @@ void MethodDesc::ResetCodeEntryPoint()
34953501
{
34963502
GetPrecode()->ResetTargetInterlocked();
34973503
}
3504+
#endif // FEATURE_PORTABLE_ENTRYPOINTS
34983505
}
34993506
#endif // FEATURE_CODE_VERSIONING
35003507

‎src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.MethodBody1/MethodBody1.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
namespace System.Reflection.Metadata.ApplyUpdate.Test
55
{
66
public class MethodBody1 {
7+
public static int CallCount;
8+
79
public static string StaticMethod1 () {
10+
CallCount++;
811
return "OLD STRING";
912
}
1013
}

‎src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.MethodBody1/MethodBody1_v1.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
namespace System.Reflection.Metadata.ApplyUpdate.Test
55
{
66
public class MethodBody1 {
7+
public static int CallCount;
8+
79
public static string StaticMethod1 () {
10+
CallCount++;
811
return "NEW STRING";
912
}
1013
}

‎src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.MethodBody1/MethodBody1_v2.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
namespace System.Reflection.Metadata.ApplyUpdate.Test
55
{
66
public class MethodBody1 {
7+
public static int CallCount;
8+
79
public static string StaticMethod1 () {
10+
CallCount++;
811
return "NEWEST STRING";
912
}
1013
}

‎src/libraries/System.Runtime.Loader/tests/ApplyUpdateTest.cs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,27 @@ void StaticMethodBodyUpdate()
142142
ApplyUpdateUtil.TestCase(static () =>
143143
{
144144
var assm = typeof (ApplyUpdate.Test.MethodBody1).Assembly;
145+
Func<string> method = ApplyUpdate.Test.MethodBody1.StaticMethod1;
146+
int initialCallCount = ApplyUpdate.Test.MethodBody1.CallCount;
145147

146148
var r = ApplyUpdate.Test.MethodBody1.StaticMethod1();
147149
Assert.Equal("OLD STRING", r);
150+
Assert.Equal("OLD STRING", method());
151+
Assert.Equal(initialCallCount + 2, ApplyUpdate.Test.MethodBody1.CallCount);
148152

149153
ApplyUpdateUtil.ApplyUpdate(assm);
150154

151155
r = ApplyUpdate.Test.MethodBody1.StaticMethod1();
152156
Assert.Equal("NEW STRING", r);
157+
Assert.Equal("NEW STRING", method());
158+
Assert.Equal(initialCallCount + 4, ApplyUpdate.Test.MethodBody1.CallCount);
153159

154160
ApplyUpdateUtil.ApplyUpdate(assm);
155161

156162
r = ApplyUpdate.Test.MethodBody1.StaticMethod1 ();
157163
Assert.Equal ("NEWEST STRING", r);
164+
Assert.Equal("NEWEST STRING", method());
165+
Assert.Equal(initialCallCount + 6, ApplyUpdate.Test.MethodBody1.CallCount);
158166
});
159167
}
160168

@@ -1026,8 +1034,11 @@ public static void TestGenericAddStaticField()
10261034

10271035
ApplyUpdateUtil.ApplyUpdate(assm);
10281036

1029-
// there are two updates - the first adds the fields, the second one updates the
1030-
// methods to use the new fields
1037+
x.TestMethod();
1038+
Assert.Equal("spqr", x.GetField());
1039+
Assert.Equal(0.0, y.GetField());
1040+
1041+
// The second update switches the methods to the newly added field.
10311042
ApplyUpdateUtil.ApplyUpdate(assm);
10321043

10331044
x.TestMethod();

‎src/libraries/System.Runtime.Loader/tests/System.Runtime.Loader.Tests.csproj‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<!-- Also ask Mono to use make sequence points even without a debugger attached to match "dotnet watch" behavior -->
1111
<WasmXHarnessMonoArgs Condition="'$(TargetOS)' == 'browser'">--setenv=DOTNET_MODIFIABLE_ASSEMBLIES=debug --setenv=MONO_DEBUG=gen-seq-points</WasmXHarnessMonoArgs>
1212

13-
<!-- CoreCLR does not build the metadata updater for wasm, so ApplyUpdate tests are excluded for CoreCLR on WASI below. -->
1413
<!-- disabled due to https://github.com/dotnet/runtime/issues/65672 -->
1514
<XUnitUseRandomizedTestOrderer>false</XUnitUseRandomizedTestOrderer>
1615
<MetadataUpdaterSupport>true</MetadataUpdaterSupport>
@@ -19,8 +18,12 @@
1918
<EnableAotAnalyzer>false</EnableAotAnalyzer>
2019
</PropertyGroup>
2120

22-
<!-- ActiveIssue https://github.com/dotnet/runtime/issues/114526 deadlocks on linux CI -->
23-
<ItemGroup Condition="('$(ContinuousIntegrationBuild)' != 'true' or '$(TargetOS)' != 'browser' or '$(OS)' == 'Windows_NT') and ('$(TargetOS)' != 'wasi' or '$(RuntimeFlavor)' != 'CoreCLR')">
21+
<ItemGroup Condition="'$(TargetOS)' == 'wasi' and '$(RuntimeFlavor)' == 'CoreCLR'">
22+
<WasmEnvironmentVariable Include="DOTNET_MODIFIABLE_ASSEMBLIES" Value="debug" />
23+
</ItemGroup>
24+
25+
<!-- ActiveIssue https://github.com/dotnet/runtime/issues/114526: hot reload tests deadlock on Mono -->
26+
<ItemGroup Condition="'$(RuntimeFlavor)' != 'Mono' or '$(ContinuousIntegrationBuild)' != 'true' or '$(TargetOS)' != 'browser' or '$(OS)' == 'Windows_NT'">
2427
<Compile Include="ApplyUpdateTest.cs" />
2528
<Compile Include="ApplyUpdateUtil.cs" />
2629
<Compile Include="MetadataUpdateHandlerAttributeTest.cs" />
@@ -72,8 +75,8 @@
7275
<ProjectReference Include="ReferencedClassLibVersion2\ReferencedClassLibVersion2.csproj" ReferenceOutputAssembly="false" OutputItemType="EmbeddedResource" Link="ReferencedClassLibVersion2.dll" />
7376
</ItemGroup>
7477

75-
<!-- ActiveIssue https://github.com/dotnet/runtime/issues/114526 deadlocks on linux CI -->
76-
<ItemGroup Condition="('$(ContinuousIntegrationBuild)' != 'true' or '$(TargetOS)' != 'browser' or '$(OS)' == 'Windows_NT') and ('$(TargetOS)' != 'wasi' or '$(RuntimeFlavor)' != 'CoreCLR')">
78+
<!-- ActiveIssue https://github.com/dotnet/runtime/issues/114526: hot reload tests deadlock on Mono -->
79+
<ItemGroup Condition="'$(RuntimeFlavor)' != 'Mono' or '$(ContinuousIntegrationBuild)' != 'true' or '$(TargetOS)' != 'browser' or '$(OS)' == 'Windows_NT'">
7780
<!-- ApplyUpdate tests -->
7881
<ProjectReference Include="ApplyUpdate\System.Reflection.Metadata.ApplyUpdate.Test.AsyncMethodChange\System.Reflection.Metadata.ApplyUpdate.Test.AsyncMethodChange.csproj" />
7982
<ProjectReference Include="ApplyUpdate\System.Reflection.Metadata.ApplyUpdate.Test.CustomAttributeDelete\System.Reflection.Metadata.ApplyUpdate.Test.CustomAttributeDelete.csproj" />
@@ -123,6 +126,18 @@
123126
</ItemGroup>
124127
</Target>
125128

129+
<Target Name="IncludeHotReloadDeltasInWasiBundle"
130+
Condition="'$(TargetOS)' == 'wasi' and '$(RuntimeFlavor)' == 'CoreCLR'"
131+
DependsOnTargets="GetCopyToOutputDirectoryItems"
132+
BeforeTargets="PrepareForWasiBuildApp">
133+
<ItemGroup>
134+
<!-- ApplyUpdate locates deltas beside the assembly under managed/. -->
135+
<WasmFilesToIncludeInFileSystem Include="@(AllItemsFullPathWithTargetPath)"
136+
Condition="'%(Extension)' == '.dmeta' or '%(Extension)' == '.dil' or '%(Extension)' == '.dpdb'"
137+
TargetPath="managed/%(Filename)%(Extension)" />
138+
</ItemGroup>
139+
</Target>
140+
126141
<!-- Remove the bind-failure test assemblies from the output directory.
127142
They must remain in deps.json (so the host adds them to the TPA list) but must not be
128143
physically present on disk when the test process starts. The tests recreate the files

‎src/libraries/tests.proj‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@
6767
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Numerics.Tensors\tests\Net8Tests\System.Numerics.Tensors.Net8.Tests.csproj" />
6868
</ItemGroup>
6969

70-
<!-- ActiveIssue https://github.com/dotnet/runtime/issues/114526 deadlocks on linux CI -->
71-
<ItemGroup Condition="'$(TargetOS)' == 'browser' and '$(OS)' != 'Windows_NT' and '$(ContinuousIntegrationBuild)' == 'true'">
70+
<ItemGroup Condition="'$(TargetOS)' == 'browser'">
7271
<ProjectExclusions Include="$(MonoProjectRoot)sample/wasm/**/*.csproj" />
73-
<ProjectExclusions Include="$(RepoRoot)src/tests/FunctionalTests/WebAssembly/Browser/HotReload/WebAssembly.Browser.HotReload.Test.csproj" />
72+
<!-- ActiveIssue https://github.com/dotnet/runtime/issues/114526: hot reload tests deadlock on Mono -->
73+
<ProjectExclusions Include="$(RepoRoot)src/tests/FunctionalTests/WebAssembly/Browser/HotReload/WebAssembly.Browser.HotReload.Test.csproj"
74+
Condition="'$(RuntimeFlavor)' == 'Mono'" />
7475
</ItemGroup>
7576

7677
<!-- Samples that require a multi-threaded runtime -->

0 commit comments

Comments
 (0)