Skip to content

Commit 0a1432e

Browse files
committed
[linux] Use GWeakRef for engine and parent in FlAccessibleNode
FlAccessibleNode registered its engine and parent weak pointers on the node itself rather than on the target objects, so the pointers were never cleared when the engine or parent was destroyed. The AT-SPI bridge can keep nodes alive past engine teardown, so subsequent calls would dereference freed memory. Store the engine and parent as GWeakRef and fetch a strong reference where needed, returning safely when the target has been destroyed. This matches the rest of the embedder. Add tests covering access after engine and parent destruction. Fixes flutter#188660
1 parent 1661171 commit 0a1432e

2 files changed

Lines changed: 55 additions & 26 deletions

File tree

‎engine/src/flutter/shell/platform/linux/fl_accessible_node.cc‎

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ struct FlAccessibleNodePrivate {
4545
AtkObject parent_instance;
4646

4747
// Weak reference to the engine this node is created for.
48-
FlEngine* engine;
48+
GWeakRef engine;
4949

5050
/// The unique identifier of the view to which this node belongs.
5151
FlutterViewId view_id;
5252

5353
// Weak reference to the parent node of this one or %NULL.
54-
AtkObject* parent;
54+
GWeakRef parent;
5555

5656
int32_t node_id;
5757
gchar* name;
@@ -152,10 +152,7 @@ static void fl_accessible_node_set_property(GObject* object,
152152
FlAccessibleNodePrivate* priv = FL_ACCESSIBLE_NODE_GET_PRIVATE(object);
153153
switch (prop_id) {
154154
case PROP_ENGINE:
155-
g_assert(priv->engine == nullptr);
156-
priv->engine = FL_ENGINE(g_value_get_object(value));
157-
g_object_add_weak_pointer(object,
158-
reinterpret_cast<gpointer*>(&priv->engine));
155+
g_weak_ref_init(&priv->engine, g_value_get_object(value));
159156
break;
160157
case PROP_VIEW_ID:
161158
priv->view_id = g_value_get_int64(value);
@@ -172,16 +169,8 @@ static void fl_accessible_node_set_property(GObject* object,
172169
static void fl_accessible_node_dispose(GObject* object) {
173170
FlAccessibleNodePrivate* priv = FL_ACCESSIBLE_NODE_GET_PRIVATE(object);
174171

175-
if (priv->engine != nullptr) {
176-
g_object_remove_weak_pointer(object,
177-
reinterpret_cast<gpointer*>(&(priv->engine)));
178-
priv->engine = nullptr;
179-
}
180-
if (priv->parent != nullptr) {
181-
g_object_remove_weak_pointer(object,
182-
reinterpret_cast<gpointer*>(&(priv->parent)));
183-
priv->parent = nullptr;
184-
}
172+
g_weak_ref_clear(&priv->engine);
173+
g_weak_ref_clear(&priv->parent);
185174
g_clear_pointer(&priv->name, g_free);
186175
g_clear_pointer(&priv->actions, g_ptr_array_unref);
187176
g_clear_pointer(&priv->children, g_ptr_array_unref);
@@ -198,7 +187,8 @@ static const gchar* fl_accessible_node_get_name(AtkObject* accessible) {
198187
// Implements AtkObject::get_parent.
199188
static AtkObject* fl_accessible_node_get_parent(AtkObject* accessible) {
200189
FlAccessibleNodePrivate* priv = FL_ACCESSIBLE_NODE_GET_PRIVATE(accessible);
201-
return priv->parent;
190+
g_autoptr(AtkObject) parent = ATK_OBJECT(g_weak_ref_get(&priv->parent));
191+
return parent;
202192
}
203193

204194
// Implements AtkObject::get_index_in_parent.
@@ -316,9 +306,10 @@ static void fl_accessible_node_get_extents(AtkComponent* component,
316306

317307
*x = 0;
318308
*y = 0;
319-
if (priv->parent != nullptr) {
320-
atk_component_get_extents(ATK_COMPONENT(priv->parent), x, y, nullptr,
321-
nullptr, coord_type);
309+
g_autoptr(AtkObject) parent = ATK_OBJECT(g_weak_ref_get(&priv->parent));
310+
if (parent != nullptr) {
311+
atk_component_get_extents(ATK_COMPONENT(parent), x, y, nullptr, nullptr,
312+
coord_type);
322313
}
323314

324315
*x += priv->x;
@@ -336,7 +327,8 @@ static AtkLayer fl_accessible_node_get_layer(AtkComponent* component) {
336327
static gboolean fl_accessible_node_do_action(AtkAction* action, gint i) {
337328
FlAccessibleNodePrivate* priv = FL_ACCESSIBLE_NODE_GET_PRIVATE(action);
338329

339-
if (priv->engine == nullptr) {
330+
g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&priv->engine));
331+
if (engine == nullptr) {
340332
return FALSE;
341333
}
342334

@@ -488,8 +480,12 @@ static void fl_accessible_node_perform_action_impl(
488480
FlutterSemanticsAction action,
489481
GBytes* data) {
490482
FlAccessibleNodePrivate* priv = FL_ACCESSIBLE_NODE_GET_PRIVATE(self);
491-
fl_engine_dispatch_semantics_action(priv->engine, priv->view_id,
492-
priv->node_id, action, data);
483+
g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&priv->engine));
484+
if (engine == nullptr) {
485+
return;
486+
}
487+
fl_engine_dispatch_semantics_action(engine, priv->view_id, priv->node_id,
488+
action, data);
493489
}
494490

495491
static void fl_accessible_node_class_init(FlAccessibleNodeClass* klass) {
@@ -553,6 +549,7 @@ static void fl_accessible_node_action_interface_init(AtkActionIface* iface) {
553549

554550
static void fl_accessible_node_init(FlAccessibleNode* self) {
555551
FlAccessibleNodePrivate* priv = FL_ACCESSIBLE_NODE_GET_PRIVATE(self);
552+
g_weak_ref_init(&priv->parent, nullptr);
556553
priv->actions = g_ptr_array_new();
557554
priv->children = g_ptr_array_new_with_free_func(g_object_unref);
558555
}
@@ -571,10 +568,8 @@ void fl_accessible_node_set_parent(FlAccessibleNode* self,
571568
gint index) {
572569
g_return_if_fail(FL_IS_ACCESSIBLE_NODE(self));
573570
FlAccessibleNodePrivate* priv = FL_ACCESSIBLE_NODE_GET_PRIVATE(self);
574-
priv->parent = parent;
571+
g_weak_ref_set(&priv->parent, parent);
575572
priv->index = index;
576-
g_object_add_weak_pointer(G_OBJECT(self),
577-
reinterpret_cast<gpointer*>(&(priv->parent)));
578573
}
579574

580575
void fl_accessible_node_set_children(FlAccessibleNode* self,

‎engine/src/flutter/shell/platform/linux/fl_accessible_node_test.cc‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,37 @@ TEST_F(FlAccessibleNodeTest, SetActions) {
349349
EXPECT_STREQ(atk_action_get_name(ATK_ACTION(node), 0), "Tap");
350350
EXPECT_STREQ(atk_action_get_name(ATK_ACTION(node), 1), "LongPress");
351351
}
352+
353+
// The AT-SPI bridge can keep nodes alive past engine teardown. Accessing a
354+
// node after its engine is destroyed must not dereference freed memory.
355+
// https://github.com/flutter/flutter/issues/188660
356+
TEST_F(FlAccessibleNodeTest, SurvivesEngineDestruction) {
357+
g_autoptr(FlDartProject) project = fl_dart_project_new();
358+
FlEngine* local_engine = fl_engine_new(project);
359+
g_autoptr(FlAccessibleNode) node =
360+
fl_accessible_node_new(local_engine, 123, 0);
361+
fl_accessible_node_set_actions(node, kFlutterSemanticsActionTap);
362+
363+
// Destroy the engine while the node is still alive.
364+
g_object_unref(local_engine);
365+
366+
// Performing an action no longer reaches the engine, but does not crash.
367+
EXPECT_FALSE(atk_action_do_action(ATK_ACTION(node), 0));
368+
}
369+
370+
// Accessing a node after its parent is destroyed must not dereference freed
371+
// memory.
372+
TEST_F(FlAccessibleNodeTest, SurvivesParentDestruction) {
373+
FlAccessibleNode* root = fl_accessible_node_new(engine, 123, 0);
374+
g_autoptr(FlAccessibleNode) child = fl_accessible_node_new(engine, 123, 1);
375+
fl_accessible_node_set_parent(child, ATK_OBJECT(root), 0);
376+
377+
// Destroy the parent while the child is still alive.
378+
g_object_unref(root);
379+
380+
// The child no longer reports a parent, but does not crash.
381+
EXPECT_EQ(atk_object_get_parent(ATK_OBJECT(child)), nullptr);
382+
gint x = 0, y = 0, width = 0, height = 0;
383+
atk_component_get_extents(ATK_COMPONENT(child), &x, &y, &width, &height,
384+
ATK_XY_SCREEN);
385+
}

0 commit comments

Comments
 (0)