Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix use-after-free if JNI Yoga nodes are garbage collected using mult…
…iple threads (#37243) Summary: Pull Request resolved: #37243 X-link: facebook/litho#944 X-link: facebook/yoga#1279 Java bindings for Yoga rely solely on garbage collection for memory management. Each Java `YogaNode` has references to its children and parent Java Nodes. This means, for a node to be garbage collected, it cannot be reachable from any user accessible node. Each node then has single ownership of a `YGNodeRef`. When the `YogaNode` is garbage collected, a finalizer is run to call `YGNodeFree` 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. Because we know the entire connected tree is dead, there is no need to remove dangling pointers, so I want to expose a way to just free a Yoga Node, without it mutating the tree as a side effect. This adds a currently private `YGNodeDeallocate` that frees without traversal. Ideally from naming this is what `YGNodeFree` would do, but we think changing the behavior of that might be too disruptive to OSS. At the same time there may be other memory safety related API changes we would like to eventually make, so this isn't made public beyond the JNI bindings to prevent needing to transition more APIs. Changelog: [Internal] Reviewed By: rshest Differential Revision: D45556206 fbshipit-source-id: 62a1394c6f6bdc2b437b388098ea362a0fbcd0f7
- Loading branch information