wayland/toplevel: defer Toplevel/handle destruction to avoid use-after-free#878
Closed
sunshowers wants to merge 1 commit into
Closed
wayland/toplevel: defer Toplevel/handle destruction to avoid use-after-free#878sunshowers wants to merge 1 commit into
sunshowers wants to merge 1 commit into
Conversation
…r-free Attempt to fix an issue where quickshell sometimes crashes on monitor wakeup. The stack trace [can be found here](https://gist.github.com/sunshowers/e439bd41d78fb5f189088471fc8d3e36). Normally, when the V4 engine hands a `QObject*` to JavaScript it creates a `QObjectWrapper` guarded through the object's `QQmlData`. This allows a later delete to fire `QObject::destroyed` and to invalidate the wrapper. But if the object is turned into a `QVariant`, it goes back to being a bare, untracked pointer with none of that functionality. This can happen if a `Toplevel` is embedded in a `QVariantList` or `QVariantMap`, such as what `ScriptModel` stores. Now, if `Toplevel` and `ToplevelHandle` were then destroyed synchronously the moment the compositor closed a toplevel, a consumer holding the corresponding `QObject*` would hold on to a dangling reference. Accessing this later would then segfault. This patch makes it so that both destructions are deferred with, `deleteLater()`. This matches how screen wrappers are already freed in `QuickshellTracked::updateScreens`, and should hopefully fix this issue. (Note that this issue is only fixed on the read path, not the action path which still has a use-after-free. My thoughts are the action path is relatively rare, so it might be worth waiting to design a better fix for this which focuses on making invalid states unrepresentable, though naturally the ability to do so is more limited than it would be in a language with affine typing like Rust. One option is to do a larger rework like using a generational arena with indexes.) Why must both `Toplevel` and `ToplevelHandle` be deferred? If `Toplevel` were deferred but `ToplevelHandle` were not, then `Toplevel` would be stuck holding a dangling pointer itself.
Author
|
Spent some time looking at my crashing core in gdb and I'm now a bit less convinced that this is the issue I hit. Marking this as draft while I investigate further. |
Author
|
This is a partial fix but not a complete one, since raw pointers can be held for much longer than a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Attempt to fix an issue where quickshell sometimes crashes on monitor wakeup. The stack trace can be found here.
Normally, when the V4 engine hands a
QObject*to JavaScript it creates aQObjectWrapperguarded through the object'sQQmlData. This allows a later delete to fireQObject::destroyedand to invalidate the wrapper.But if the object is turned into a
QVariant, it goes back to being a bare, untracked pointer with none of that functionality. This can happen if aToplevelis embedded in aQVariantListorQVariantMap, such as whatScriptModelstores.Now, if
ToplevelandToplevelHandlewere then destroyed synchronously the moment the compositor closed a toplevel, a consumer holding the correspondingQObject*would hold on to a dangling reference. Accessing this later would then segfault.This patch makes it so that both destructions are deferred with
deleteLater(). This matches how screen wrappers are already freed inQuickshellTracked::updateScreens, and should hopefully fix this issue. (Note that this issue is only fixed on the read path, not the action path which still has a use-after-free. My thoughts are the action path is relatively rare, so it might be worth waiting to design a better fix for this which focuses on making invalid states unrepresentable, though naturally the ability to do so is more limited than it would be in a language with affine typing like Rust. One option is to do a larger rework like using a generational arena with indexes.)Why must both
ToplevelandToplevelHandlebe deferred? IfToplevelwere deferred butToplevelHandlewere not, thenToplevelwould be stuck holding a dangling pointer itself.