Repository navigation
desktop_drop: fix use-after-free when posting Windows platform tasks - #526
Merged
Merged
Conversation
FlutterDesktopEnginePostPlatformThreadTask in current stable Flutter builds its task context from a temporary whose destructor invokes on_cancel right away, so the plugin freed each queued FFI task before it ran and freed it again afterwards. This affected every Dart -> native call and every native -> Dart reply on Windows. Pass no on_cancel and let the callback own the task. The engine is fixed upstream in flutter/flutter#193412; until the minimum Flutter version includes it, a task discarded at engine shutdown leaks instead.
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.
Problem
FlutterDesktopEnginePostPlatformThreadTaskin current stable Flutter (checked 3.47.7) builds its task context from a temporary:auto context = std::make_shared<Context>(Context{callback, on_cancel, user_data});The temporary's destructor sees
invoked == falseand callson_cancelimmediately. Ouron_canceldeletes the queuedstd::function, so when the task runs, the callback reads freed memory and then deletes it a second time.This happens on every
FfiTransport::Poston Windows: every Dart -> native call (includinginitializewhen aDropTargetmounts) and every native -> Dart reply. desktop_drop 0.9.0 crashes on Windows at startup.Upstream fixed this in flutter/flutter#193412 (constructs
Contextin place and disallows copies). It is not in the current stable release yet.Fix
Pass
nullptrason_canceland let the callback own the task. This works on both the affected and the fixed engines. The trade-off: a task the engine discards at shutdown leaks instead of being freed.desktop_webview_windowuses the same workaround.Verification (Windows, Flutter 3.47.7, Debug)
flutter test: 23 tests passed.exampleDebug build succeeded.origin/main: crashes right after startup,0xC0000005indesktop_drop_plugin.dll(APPCRASH event 1000).Not covered: real drag-and-drop on Windows (no Windows integration scenarios exist for this package), and the engine-shutdown path.