Fix Use After Free With Concurrent Java GC #1279
Reference in New Issue
Block a user
No description provided.
Delete Branch "export-D45556206"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary:
Fixes https://github.com/facebook/yoga/issues/1271
Java bindings for Yoga rely solely on garbage collection for memory management. Each Java
YogaNode
has references to its children and parent, along with anYGNodeRef
which it owns. When theYogaNode
is garbage collected, a finalizer is run to callYGNodeFree
and free the underlying native Yoga Node.This may cause a use-after-free if finalizers are run from multiple threads. This is because
YGNodeFree
does more than just freeing, but instead also interacts with its parent and children nodes to detach itself, and remove any dangling pointers. If multiple threads run finalizers at once, one may traverse and try to mutate a node which another is freeing.This bit of behavior is counterintuitive from the API name anyway, so this diff makes a breaking change to
YGNodeFree
to instead mostly just be afree()
(with a tracing step whose publishing should be thread safe). The existing behavior is exposed in an added functionYGNodeDetachAndFree()
.The majority of existing
YGNodeFree
usages will probably still work under the new behavior, but we don't have that many in fbsource (many instead callYGNodeFreeRecursive
to free a whole tree at once). So for safety, I replaced existing usages ofYGNodeFree
outside of Yoga withYGNodeDetachAndFree
to preserve existing behavior.JavaScript and C# bindings also use
YGNodeFree()
, and both allow explicit freeing. For now I switched both of their behavior to detachment, to avoid exposing the ability to managed languages to corrupt memory.Differential Revision: D45556206
This pull request was exported from Phabricator. Differential Revision: D45556206
This pull request was exported from Phabricator. Differential Revision: D45556206
This pull request was exported from Phabricator. Differential Revision: D45556206
This pull request was exported from Phabricator. Differential Revision: D45556206
This pull request was exported from Phabricator. Differential Revision: D45556206
This pull request has been merged in facebook/yoga@3b088c3383.
Pull request closed