Skip to content

fix: dangling raw_ptr api::Protocol::protocol_registry_ - #50829

Merged
ckerr merged 1 commit into
mainfrom
fix/dangling-raw-ptr-Protocol-protocol_registry_
Apr 11, 2026
Merged

ckerr merged 1 commit into
mainfrom
fix/dangling-raw-ptr-Protocol-protocol_registry_

Conversation

@ckerr

@ckerr ckerr commented Apr 9, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Fix another dangling raw_ptr bug, This time it's api::Protocol::protocol_registry_, which is a raw_ptr to a ProtocolRegistry owned by BrowserContext. That gets destroyed in DestroyAllContexts() but the JS api::Protocol object lives on.

This PR changes Protocol::protocol_registry_ to be a base::WeakPtr and updates its users to check for nullptr before use.

The raw_ptr error:

  [2745980:0408/194330.631200:ERROR:base/allocator/partition_alloc_support.cc:682] A freed allocation is still referenced by a dangling pointer at exit, or at test end. Leaked raw_ptr/raw_ref could cause PartitionAlloc's quarantine memory bloat.
  
  Memory was released on:
  Task trace:
  No active task.
  #0 0x6088d93dd8d2 base::debug::CollectStackTrace() [../../base/debug/stack_trace_posix.cc:1050:7]
  #1 0x6088d93c4d71 base::debug::StackTrace::StackTrace() [../../base/debug/stack_trace.cc:280:20]
  #2 0x6088d93e5586 base::allocator::(anonymous namespace)::DanglingRawPtrDetected() [../../base/allocator/partition_alloc_support.cc:438:11]
  #3 0x6088d94a33e2 allocator_shim::internal::PartitionAllocFunctionsInternal<>::FreeWithSize() [../../base/allocator/partition_allocator/src/partition_alloc/in_slot_metadata.h:389:5]
  #4 0x6088d21e4771 electron::ElectronBrowserContext::~ElectronBrowserContext() [../../third_party/libc++/src/include/__memory/unique_ptr.h:74:5]
  #5 0x6088d21e492e electron::ElectronBrowserContext::~ElectronBrowserContext() [../../electron/shell/browser/electron_browser_context.cc:418:51]
  #6 0x6088d21e3856 electron::ElectronBrowserContext::DestroyAllContexts() [../../third_party/libc++/src/include/__memory/unique_ptr.h:74:5]
  #7 0x6088d21eb3e0 electron::ElectronBrowserMainParts::PostMainMessageLoopRun() [../../electron/shell/browser/electron_browser_main_parts.cc:621:3]
  #8 0x6088d713f6a7 content::BrowserMainLoop::ShutdownThreadsAndCleanUp() [../../content/browser/browser_main_loop.cc:1164:13]
  #9 0x6088d7141a70 content::BrowserMainRunnerImpl::Shutdown() [../../content/browser/browser_main_runner_impl.cc:175:17]
  #10 0x6088d713b2b1 content::BrowserMain() [../../content/browser/browser_main.cc:41:16]
  #11 0x6088d28aff57 content::RunBrowserProcessMain() [../../content/app/content_main_runner_impl.cc:701:10]
  #12 0x6088d28b3587 content::ContentMainRunnerImpl::RunBrowser() [../../content/app/content_main_runner_impl.cc:1325:10]
  #13 0x6088d28b2886 content::ContentMainRunnerImpl::Run() [../../content/app/content_main_runner_impl.cc:1155:12]
  #14 0x6088d28ae612 content::RunContentProcess() [../../content/app/content_main.cc:356:36]
  #15 0x6088d28ae800 content::ContentMain() [../../content/app/content_main.cc:369:10]
  #16 0x6088d209a591 main [../../electron/shell/app/electron_main_linux.cc:50:10]
  #17 0x7c298e02a575 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a574)
  #18 0x7c298e02a628 __libc_start_main
  #19 0x6088d207e02a _start
  
  
  [2745980:0408/194330.671770:ERROR:base/allocator/partition_alloc_support.cc:699] Dangling reference from:
  
  [2745980:0408/194330.671780:ERROR:base/allocator/partition_alloc_support.cc:700] #0 0x6088d9423916 base::internal::InstanceTracer::TraceImpl() [../../base/allocator/partition_allocator/src/partition_alloc/pointers/instance_tracer.cc:30:5]
  #1 0x6088d211f49e electron::api::Protocol::Protocol() [../../base/allocator/partition_allocator/src/partition_alloc/pointers/instance_tracer.h:49:5]
  #2 0x6088d21201fc electron::api::Protocol::Create() [../../electron/shell/browser/api/electron_api_protocol.cc:302:48]
  #3 0x6088d2138972 electron::api::Session::Session() [../../electron/shell/browser/api/electron_api_session.cc:563:7]
  #4 0x6088d214dfc4 cppgc::MakeGarbageCollectedTrait<>::Call<>() [../../v8/include/cppgc/allocation.h:239:32]
  #5 0x6088d2143263 electron::api::Session::FromOrCreate() [../../v8/include/cppgc/allocation.h:277:7]
  #6 0x6088d21434fb electron::api::Session::FromPartition() [../../electron/shell/browser/api/electron_api_session.cc:1745:10]
  #7 0x6088d2179b6c electron::api::WebContents::WebContents() [../../electron/shell/browser/api/electron_api_web_contents.cc:914:15]
  #8 0x6088d21943eb electron::api::WebContents::New() [../../electron/shell/browser/api/electron_api_web_contents.cc:4745:45]
  #9 0x6088d219482a electron::api::WebContents::CreateFromWebPreferences() [../../electron/shell/browser/api/electron_api_web_contents.cc:4829:20]
  #10 0x6088d21b036a electron::api::WebContentsView::New() [../../electron/shell/browser/api/electron_api_web_contents_view.cc:216:7]
  #11 0x6088d20a50db base::RepeatingCallback<>::Run() [../../base/functional/callback.h:346:12]
  #12 0x6088d20cc8f1 gin_helper::internal::InvokeNew<>() [../../electron/shell/common/gin_helper/constructor.h:49:52]
  #13 0x6088d20a5973 base::RepeatingCallback<>::Run() [../../base/functional/callback.h:346:12]
  #14 0x6088d20ccd43 gin_helper::Invoker<>::DispatchToCallback() [../../electron/shell/common/gin_helper/function_template.h:275:14]
  #15 0x6088d20cca68 gin_helper::Dispatcher<>::DispatchToCallbackImpl() [../../electron/shell/common/gin_helper/function_template.h:301:15]
  #16 0x6088d20cc99f gin_helper::Dispatcher<>::DispatchToCallback() [../../electron/shell/common/gin_helper/function_template.h:307:5]
  #17 0x6088d43a4c35 v8::internal::FunctionCallbackArguments::CallOrConstruct() [../../v8/src/api/api-arguments-inl.h:176:3]
  #18 0x6088d43a3e9d v8::internal::Builtins::InvokeApiFunction() [../../v8/src/builtins/builtins-api.cc:117:27]
  #19 0x6088d45b041d v8::internal::(anonymous namespace)::Invoke() [../../v8/src/execution/execution.cc:318:20]
  #20 0x6088d45b0e10 v8::internal::Execution::New() [../../v8/src/execution/execution.cc:601:27]
  #21 0x6088d42e8de4 v8::Function::NewInstanceWithSideEffectType() [../../v8/src/api/api.cc:5583:27]
  #22 0x6088d21af887 electron::api::WebContentsView::Create() [../../electron/shell/browser/api/electron_api_web_contents_view.cc:147:13]
  #23 0x6088d20db2b0 electron::api::BrowserWindow::BrowserWindow() [../../electron/shell/browser/api/electron_api_browser_window.cc:69:7]
  #24 0x6088d20dcafc electron::api::BrowserWindow::New() [../../electron/shell/browser/api/electron_api_browser_window.cc:320:14]
  #25 0x6088d20dd5c9 base::internal::Invoker<>::Run() [../../base/functional/bind_internal.h:673:12]
  #26 0x6088d20dd4b7 base::RepeatingCallback<>::Run() [../../base/functional/callback.h:346:12]
  #27 0x6088d20dd3da gin_helper::internal::InvokeFactory<>()::{lambda()#1}::operator()() [../../electron/shell/common/gin_helper/constructor.h:49:52]
  #28 0x6088d20dd0b6 gin_helper::internal::InvokeFactory<>() [../../third_party/libc++/src/include/__type_traits/invoke.h:90:27]
  #29 0x6088d20dcf11 gin_helper::internal::InvokeNew<>() [../../electron/shell/common/gin_helper/constructor.h:66:14]
  #30 0x6088d20a5973 base::RepeatingCallback<>::Run() [../../base/functional/callback.h:346:12]
  #31 0x6088d20ccd43 gin_helper::Invoker<>::DispatchToCallback() [../../electron/shell/common/gin_helper/function_template.h:275:14]

Slightly different error reporting this time around. I've turned on the enable_backup_ref_ptr_instance_tracer flag to get better error reports. Tested by modifying the build flags locally:

diff --git a/build/args/all.gn b/build/args/all.gn
index 45939f456f..7624114b5f 100644
--- a/build/args/all.gn
+++ b/build/args/all.gn
@@ -53,8 +53,12 @@ use_qt6 = false
 
 # https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr.md
 # TODO(vertedinde): hunt down dangling pointers on Linux
-enable_dangling_raw_ptr_checks = false
-enable_dangling_raw_ptr_feature_flag = false
+is_debug = true
+enable_dangling_raw_ptr_checks = true
+enable_dangling_raw_ptr_feature_flag = true
+enable_backup_ref_ptr_support = true
+enable_backup_ref_ptr_feature_flag = true
+enable_backup_ref_ptr_instance_tracer = true
 
 # This flag speeds up the performance of fork/execve on linux systems.
 # Ref: https://chromium-review.googlesource.com/c/v8/v8/+/4602858

Checklist

Release Notes

Notes: none.

@ckerr ckerr added semver/patch backwards-compatible bug fixes target/42-x-y PR should also be added to the "42-x-y" branch. labels Apr 9, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Apr 9, 2026
@ckerr
ckerr marked this pull request as draft April 9, 2026 15:01
@ckerr
ckerr force-pushed the fix/dangling-raw-ptr-Protocol-protocol_registry_ branch from bf8c3d2 to 95b06da Compare April 9, 2026 20:25
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Apr 10, 2026
@ckerr
ckerr force-pushed the fix/dangling-raw-ptr-Protocol-protocol_registry_ branch 2 times, most recently from 86383ce to ed3b3e9 Compare April 10, 2026 20:13
@ckerr
ckerr marked this pull request as ready for review April 10, 2026 20:13
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Apr 10, 2026
@deepak1556

deepak1556 commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

We should convert api::ServiceWorkerContext to cppgc, current lifetime based on weak callback will not run on isolate destruction and the pending subscriber leads to UAF

CleanupAtExit is the equivalent for deprecatedwrappable to ensure cleanup on isolate teardown but I would like us to rather make the longer term fix.

@deepak1556
deepak1556 marked this pull request as draft April 10, 2026 21:09
@deepak1556
deepak1556 force-pushed the fix/dangling-raw-ptr-Protocol-protocol_registry_ branch from ed3b3e9 to e61e9d4 Compare April 11, 2026 10:01
@deepak1556
deepak1556 marked this pull request as ready for review April 11, 2026 11:03

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👏

@ckerr
ckerr merged commit 5fded05 into main Apr 11, 2026
64 checks passed
@release-clerk

release-clerk Bot commented Apr 11, 2026

Copy link
Copy Markdown

No Release Notes

@ckerr
ckerr deleted the fix/dangling-raw-ptr-Protocol-protocol_registry_ branch April 11, 2026 13:53
@trop

trop Bot commented Apr 11, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "42-x-y", please check out #50951

@trop trop Bot removed the target/42-x-y PR should also be added to the "42-x-y" branch. label Apr 11, 2026
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. and removed in-flight/42-x-y labels Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged/42-x-y PR was merged to the "42-x-y" branch. new-pr 🌱 PR opened recently semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants