From a6927e89f5032ef1a54a73b81cb4dc27db769983 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Mon, 25 May 2026 20:48:02 +0200 Subject: [PATCH 1/4] protect __stack_pointer --- eng/native.wasm.targets | 1 + eng/native/configureoptimization.cmake | 3 +++ src/coreclr/hosts/corerun/CMakeLists.txt | 4 +++- src/mono/browser/build/BrowserWasmApp.CoreCLR.targets | 5 ++++- src/native/corehost/browserhost/CMakeLists.txt | 2 ++ 5 files changed, 13 insertions(+), 2 deletions(-) diff --git a/eng/native.wasm.targets b/eng/native.wasm.targets index 6df6de93a22b55..8b86ba9b2df243 100644 --- a/eng/native.wasm.targets +++ b/eng/native.wasm.targets @@ -81,6 +81,7 @@ + <_EmccExportedRuntimeMethods>@(EmccExportedRuntimeMethod -> '%(Identity)',',') diff --git a/eng/native/configureoptimization.cmake b/eng/native/configureoptimization.cmake index 9669115fc931a4..3c5057c3477329 100644 --- a/eng/native/configureoptimization.cmake +++ b/eng/native/configureoptimization.cmake @@ -9,6 +9,9 @@ elseif(CLR_CMAKE_HOST_UNIX) if(CLR_CMAKE_TARGET_ANDROID) # -O2 optimization generates faster/smaller code on Android add_compile_options($<$:-O2>) + elseif (CLR_CMAKE_TARGET_BROWSER) + # -O2 disables metadce optimization and protects __stack_pointer + add_compile_options($<$:-O2>) else() add_compile_options($<$:-O3>) endif() diff --git a/src/coreclr/hosts/corerun/CMakeLists.txt b/src/coreclr/hosts/corerun/CMakeLists.txt index 31d3d0acf2a69d..93909d44fe034c 100644 --- a/src/coreclr/hosts/corerun/CMakeLists.txt +++ b/src/coreclr/hosts/corerun/CMakeLists.txt @@ -89,9 +89,11 @@ else() -sEXPORT_ES6=1 -sEXIT_RUNTIME=1 -sEXPORTED_RUNTIME_METHODS=CORERUN,ENV,${CMAKE_EMCC_EXPORTED_RUNTIME_METHODS} - -sEXPORTED_FUNCTIONS=_main,___stack_pointer,${CMAKE_EMCC_EXPORTED_FUNCTIONS} + -sEXPORTED_FUNCTIONS=_main,${CMAKE_EMCC_EXPORTED_FUNCTIONS} -sEXPORT_NAME=createDotnetRuntime -sENVIRONMENT=node,shell,web + -lexports.js + -Wl,--export=__stack_pointer -Wl,--error-limit=0) if (CORERUN_IN_BROWSER) diff --git a/src/mono/browser/build/BrowserWasmApp.CoreCLR.targets b/src/mono/browser/build/BrowserWasmApp.CoreCLR.targets index 74d7508e710171..67379638d5f356 100644 --- a/src/mono/browser/build/BrowserWasmApp.CoreCLR.targets +++ b/src/mono/browser/build/BrowserWasmApp.CoreCLR.targets @@ -555,7 +555,6 @@ <_EmccLinkStepArgs Include="-s EXPORT_ES6=1" /> - <_EmccLinkStepArgs Include="-lexports.js" /> <_EmccLinkStepArgs Include="-s INITIAL_MEMORY=$(WasmInitialHeapSize)" /> @@ -598,6 +597,10 @@ <_EmccLinkStepArgs Include="$(EmccExtraLDFlags)" /> + + <_EmccLinkStepArgs Include="-lexports.js" /> + <_EmccLinkStepArgs Include="-Wl,--export=__stack_pointer" /> + <_EmccLinkStepArgs Include="-Wl,--error-limit=0" /> diff --git a/src/native/corehost/browserhost/CMakeLists.txt b/src/native/corehost/browserhost/CMakeLists.txt index ebba89e8c307c8..42f72ba5c0f911 100644 --- a/src/native/corehost/browserhost/CMakeLists.txt +++ b/src/native/corehost/browserhost/CMakeLists.txt @@ -122,6 +122,8 @@ target_link_options(browserhost PRIVATE -sEXPORT_NAME=createDotnetRuntime -sENVIRONMENT=web,webview,worker,node,shell --emit-symbol-map + -lexports.js + -Wl,--export=__stack_pointer -Wl,--error-limit=0) target_link_libraries(browserhost PRIVATE From 5c9dc0a4f38d4d82c9890e50721bd15da5e8b0bd Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Wed, 27 May 2026 11:03:45 +0200 Subject: [PATCH 2/4] fix link flags --- eng/native/configureoptimization.cmake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/eng/native/configureoptimization.cmake b/eng/native/configureoptimization.cmake index 3c5057c3477329..f50756b42aa9ce 100644 --- a/eng/native/configureoptimization.cmake +++ b/eng/native/configureoptimization.cmake @@ -10,8 +10,10 @@ elseif(CLR_CMAKE_HOST_UNIX) # -O2 optimization generates faster/smaller code on Android add_compile_options($<$:-O2>) elseif (CLR_CMAKE_TARGET_BROWSER) - # -O2 disables metadce optimization and protects __stack_pointer - add_compile_options($<$:-O2>) + # -O2 prevents emscripten metadce from stripping user-requested global exports + # Must override CMAKE_*_FLAGS_RELEASE because those propagate to the link line + set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG") + set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") else() add_compile_options($<$:-O3>) endif() From a7f0c92bbcddabc5feccfe6c988ba3b463ea433a Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Wed, 27 May 2026 11:17:18 +0200 Subject: [PATCH 3/4] better --- eng/native/configureoptimization.cmake | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/eng/native/configureoptimization.cmake b/eng/native/configureoptimization.cmake index f50756b42aa9ce..f568203c726d3e 100644 --- a/eng/native/configureoptimization.cmake +++ b/eng/native/configureoptimization.cmake @@ -11,9 +11,7 @@ elseif(CLR_CMAKE_HOST_UNIX) add_compile_options($<$:-O2>) elseif (CLR_CMAKE_TARGET_BROWSER) # -O2 prevents emscripten metadce from stripping user-requested global exports - # Must override CMAKE_*_FLAGS_RELEASE because those propagate to the link line - set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG") - set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") + add_link_options($<$:-O2>) else() add_compile_options($<$:-O3>) endif() From 9523fb74bfa3cdd039a42bac2187c954d6dc1991 Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Wed, 27 May 2026 11:27:48 +0200 Subject: [PATCH 4/4] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/mono/browser/build/BrowserWasmApp.CoreCLR.targets | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mono/browser/build/BrowserWasmApp.CoreCLR.targets b/src/mono/browser/build/BrowserWasmApp.CoreCLR.targets index 67379638d5f356..40b486404316fd 100644 --- a/src/mono/browser/build/BrowserWasmApp.CoreCLR.targets +++ b/src/mono/browser/build/BrowserWasmApp.CoreCLR.targets @@ -597,8 +597,9 @@ <_EmccLinkStepArgs Include="$(EmccExtraLDFlags)" /> - + <_EmccLinkStepArgs Include="-lexports.js" /> + <_EmccLinkStepArgs Include="-Wl,--export=__stack_pointer" />