Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Wrap arena in an Rc to prevent UB (#66293)

Cameron Mcloughlin2529740084ed
diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs
index 7eca7f2f206..f321644eefb 100644
--- a/crates/gpui/src/app.rs
+++ b/crates/gpui/src/app.rs
@@ -897,7 +897,12 @@ pub struct App {
/// Per-App element arena. This isolates element allocations between different
/// App instances (important for tests where multiple Apps run concurrently).
- pub(crate) element_arena: RefCell<Arena>,
+ ///
+ /// Kept in its own allocation because draws reach it through a raw pointer
+ /// (`CURRENT_ELEMENT_ARENA`) while a `&mut App` is live. If it were stored
+ /// inline, those accesses would alias memory the `&mut App` claims
+ /// exclusively, which is undefined behavior.
+ pub(crate) element_arena: Rc<RefCell<Arena>>,
/// Per-App event arena.
pub(crate) event_arena: Arena,
@@ -1055,7 +1060,7 @@ impl App {
#[cfg(any(test, feature = "test-support", debug_assertions))]
name: None,
- element_arena: RefCell::new(Arena::new(1024 * 1024)),
+ element_arena: Rc::new(RefCell::new(Arena::new(1024 * 1024))),
event_arena: Arena::new(1024 * 1024),
#[cfg(any(test, gpui_leak_detection))]
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index ff1ba124993..44e47d2ca64 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -483,10 +483,10 @@ impl ArenaClearNeeded {
/// reference.
pub fn clear(self, cx: &mut App) {
assert!(
- std::ptr::eq(self.arena, &cx.element_arena),
+ std::ptr::eq(self.arena, &*cx.element_arena),
"ArenaClearNeeded::clear called with a different App than the draw ran against"
);
- cx.element_arena.borrow_mut().clear();
+ RefCell::borrow_mut(&cx.element_arena).clear();
}
}