Skip to content

Fix use-after-free crash in BrowserView.get_instance on macOS#1797

Open
smparkes wants to merge 2 commits intor0x0r:masterfrom
smparkes:fix/get-instance-use-after-free
Open

Fix use-after-free crash in BrowserView.get_instance on macOS#1797
smparkes wants to merge 2 commits intor0x0r:masterfrom
smparkes:fix/get-instance-use-after-free

Conversation

@smparkes
Copy link
Copy Markdown

On macOS ARM64, the app crashes with EXC_BREAKPOINT (SIGTRAP, pointer authentication trap) in object_getClass when a deferred Cocoa callback (performSelectorOnMainThread) fires after a window has been destroyed.

Root cause: get_instance iterates BrowserView.instances comparing each instance's .window attribute against a target NSWindow. When a window has been destroyed (windowWillClose_ releases the window), the .window attribute points to freed memory. Comparing it triggers object_getClass on the freed pointer, which fails the ARM64 pointer authentication check and kills the process.

The original code had two bugs:

  1. Used bare getattr(i, attr) — crashes if the attribute value is a freed Cocoa object whose eq triggers object_getClass
  2. Caught only AttributeError and used break — stops searching on any missing attribute, missing valid instances later in the dict

Fix get_instance to:

  • Use getattr(i, attr, None) with a None check to skip freed/cleared attributes without comparing them
  • Catch all exceptions (not just AttributeError) during comparison
  • Continue iterating instead of breaking, so later valid instances are still found

Also fix windowWillClose_ to:

  • Clear the window delegate (setDelegate_(None)) before releasing, preventing further delegate callbacks on the freed window
  • Guard against missing instance (if not i: return)
  • Guard webview access (if i.webview:) before cleanup

And fix windowShouldClose_ to return YES if instance not found.

@smparkes smparkes force-pushed the fix/get-instance-use-after-free branch from a059824 to b72b9ce Compare March 26, 2026 20:41
@smparkes
Copy link
Copy Markdown
Author

Hmmm ... not sure about

Comprehensive CI / macOS (pull_request)Cancelled after 20m

Timeout?

smparkes and others added 2 commits March 28, 2026 08:42
- Replace OpenFolderDialog's FileDialogNative internals with FolderBrowserDialog
  (FileDialogNative+IFileDialog doesn't exist in .NET 8)
- Update WebView2 DLLs from 1.0.2957 (.NET Framework) to 1.0.3240 (netcoreapp3.0)
  (old DLLs reference System.Windows.Forms.ContextMenu, removed in .NET 6+)
- Skip .NET Framework registry check in _is_chromium() when using coreclr
- Add explicit clr.AddReference('Microsoft.Win32.SystemEvents') for coreclr
  (not auto-loaded like on netfx)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
On macOS ARM64, the app crashes with EXC_BREAKPOINT (SIGTRAP, pointer
authentication trap) in object_getClass when a deferred Cocoa callback
(performSelectorOnMainThread) fires after a window has been destroyed.

Root cause: get_instance iterates BrowserView.instances comparing each
instance's .window attribute against a target NSWindow. When a window
has been destroyed (windowWillClose_ releases the window), the .window
attribute points to freed memory. Comparing it triggers object_getClass
on the freed pointer, which fails the ARM64 pointer authentication
check and kills the process.

The original code had two bugs:
1. Used bare getattr(i, attr) — crashes if the attribute value is a
   freed Cocoa object whose __eq__ triggers object_getClass
2. Caught only AttributeError and used break — stops searching on any
   missing attribute, missing valid instances later in the dict

Fix get_instance to:
- Use getattr(i, attr, None) with a None check to skip freed/cleared
  attributes without comparing them
- Catch all exceptions (not just AttributeError) during comparison
- Continue iterating instead of breaking, so later valid instances
  are still found

Also fix windowWillClose_ to:
- Clear the window delegate (setDelegate_(None)) before releasing,
  preventing further delegate callbacks on the freed window
- Guard against missing instance (if not i: return)
- Guard webview access (if i.webview:) before cleanup

And fix windowShouldClose_ to return YES if instance not found.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@smparkes smparkes force-pushed the fix/get-instance-use-after-free branch from b72b9ce to a07f8a7 Compare March 28, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant