lua_vm/trace_impls.rs
1//! Phase-D `Trace` implementations for GC-rooted types defined in this
2//! crate. Types in `lua-types` (LuaValue, LuaString, UpVal) have their
3//! Trace impls in `lua-types/src/trace_impls.rs` because of Rust's orphan
4//! rule.
5//!
6//! Each impl below is a `todo!("phase-d: trace X")` stub. The
7//! panic-driven mega-loop surfaces each one when a runtime path triggers
8//! `Heap::full_collect`. Each agent works on ONE type — no family
9//! expansion (Trace impls have subtle invariants).
10//!
11//! Implementation guidance for agents:
12//! 1. Read the type definition; enumerate every field
13//! 2. For every `Gc<T>`, `GcRef<T>`, or container (Vec/Option/HashMap)
14//! thereof, call `m.mark(field)` or `field.trace(m)` appropriately
15//! 3. Skip non-GC fields (primitives, `String`, `Vec<u8>`)
16//! 4. Skip "intentionally not traced" fields (weak refs)
17//! 5. Reference `reference/lua-5.4.7/src/lgc.c`'s `reallymarkobject`
18
19use crate::state::{FinalizerObject, GlobalState, LuaState};
20use crate::string::{LuaStringImpl, LuaUserDataImpl};
21use lua_gc::{Marker, Trace};
22
23/// Phase-B internal richer LuaString. The byte buffer is a Rust `Rc<[u8]>`
24/// (not GC-managed); no fields to mark.
25impl Trace for LuaStringImpl {
26
27 fn type_name(&self) -> &'static str {
28 std::any::type_name::<Self>()
29 }
30
31 fn trace(&self, _m: &mut Marker) {}
32}
33
34/// Phase-B internal userdata. Both `metatable` and `uv` are currently
35/// `Option<()>` / `Vec<()>` stubs — no GC edges to walk yet. Becomes
36/// real when userdata machinery lands post-D-1.
37impl Trace for LuaUserDataImpl {
38
39 fn type_name(&self) -> &'static str {
40 std::any::type_name::<Self>()
41 }
42
43 fn trace(&self, _m: &mut Marker) {}
44}
45
46impl Trace for FinalizerObject {
47
48 fn type_name(&self) -> &'static str {
49 std::any::type_name::<Self>()
50 }
51
52 fn trace(&self, m: &mut Marker) {
53 match self {
54 FinalizerObject::Table(t) => t.trace(m),
55 FinalizerObject::UserData(u) => u.trace(m),
56 }
57 }
58}
59
60impl Trace for LuaState {
61
62 fn type_name(&self) -> &'static str {
63 std::any::type_name::<Self>()
64 }
65
66 fn trace(&self, m: &mut Marker) {
67 // C's traversethread (lgc.c) walks [stack .. top) and relies on two
68 // companion invariants this port mirrors via `gc_trace_bound` (the
69 // savestate half — widen to ci.top only for a Lua current frame)
70 // and `clear_dead_stack_tail` (the atomic-clear half, run before
71 // every collect). Every slot below the bound is therefore
72 // valid-or-nil; the old frame-bounded range walk and the saved_pc
73 // debug-local heuristic (#140 bug B's two faces) are gone.
74 let bound = self.gc_trace_bound();
75 for slot in &self.stack[..bound] {
76 slot.val.trace(m);
77 }
78
79 for uv in self.openupval.iter() {
80 uv.trace(m);
81 }
82
83 // PORT NOTE: `global` (Rc<RefCell<GlobalState>>) is reached from the
84 // heap's root via GlobalState::trace; tracing it from each thread
85 // would re-enter the root and is explicitly excluded.
86 // PORT NOTE: `call_info` entries carry pc offsets and stack indices
87 // but no direct GcRef fields. The active closure is reached through
88 // the stack slot at `ci.func`, already covered by the stack walk.
89 // PORT NOTE: `tbclist` holds StackIdx values only; the to-be-closed
90 // objects themselves live on the stack and are traced there.
91 }
92}
93
94impl Trace for GlobalState {
95
96 fn type_name(&self) -> &'static str {
97 std::any::type_name::<Self>()
98 }
99
100 fn trace(&self, m: &mut Marker) {
101 // per-type metatables, and pending finalizers. We expand the set to
102 // include preallocated short strings (memerrmsg, tmname[]) and the
103 // open-upvalue thread list, both of which the panic-driven Phase-D
104 // mega-loop expects to see at the root.
105
106 self.l_registry.trace(m);
107
108 // Values held by Rust-side embedding handles are rooted outside the
109 // Lua registry table so handle Drop can unroot without touching the
110 // Lua stack/API. They are still ordinary GC roots during marking.
111 for value in self.external_roots.iter_values() {
112 value.trace(m);
113 }
114
115 // Cross-thread open-upvalue mirrors are live roots while a coroutine
116 // resume holds the home thread's stack behind an outer mutable borrow.
117 for value in self.cross_thread_upvals.values() {
118 value.trace(m);
119 }
120
121 // PORT NOTE (phase-b-reconcile): The lua-types LuaTable placeholder is
122 // storage-less, so `globals` and `loaded` cannot live inside the registry
123 // table (see `init_registry`). They are kept as direct GlobalState fields
124 // and must be traced explicitly as roots; once the placeholder reconciles
125 // with vm::LuaTable, these become reachable via `l_registry` and the two
126 // lines below disappear.
127 self.globals.trace(m);
128 self.loaded.trace(m);
129
130 if let Some(t) = &self.mainthread {
131 t.trace(m);
132 }
133
134 self.main_thread_value.trace(m);
135
136 if self.current_thread_id != self.main_thread_id {
137 if let Some(entry) = self.threads.get(&self.current_thread_id) {
138 entry.value.trace(m);
139 }
140 }
141
142 // Registered coroutines are not roots by registration alone. The
143 // post-mark hook traces stacks only for thread handles that were
144 // reached from a real root, matching Lua's collectable coroutine
145 // semantics.
146
147 for slot in self.mt.iter() {
148 if let Some(t) = slot {
149 t.trace(m);
150 }
151 }
152
153 for s in self.tmname.iter() {
154 s.trace(m);
155 }
156
157 self.memerrmsg.trace(m);
158
159 for th in self.twups.iter() {
160 th.trace(m);
161 }
162
163 // `interned_lt` is a weak short-string cache. The collector prunes
164 // unmarked entries from the post-mark hook instead of tracing them as
165 // roots here.
166 for row in self.strcache.iter() {
167 for s in row.iter() {
168 s.trace(m);
169 }
170 }
171
172 // Pending finalizers are NOT traced here — that's what lets the mark
173 // phase distinguish "still reachable from the user program" from
174 // "only kept alive by the finalizer registry". `collect_via_heap`'s
175 // post-mark hook checks each entry against the visited set; an
176 // unvisited entry is moved to `to_be_finalized` and explicitly
177 // marked there so it survives the sweep.
178 //
179 // `to_be_finalized` IS traced as a strong root: objects in this list
180 // are awaiting their `__gc` call but are otherwise dead, and the
181 // object (plus its descendants) must survive long enough for the
182 // finalizer to run.
183 for object in self.finalizers.to_be_finalized().iter() {
184 object.trace(m);
185 }
186
187 // Trace suspended parent stacks. When a coroutine is running, any
188 // parent threads are suspended and their stacks are not reachable from
189 // `threads` (which only holds coroutines, not the main thread). Before
190 // `aux_resume` resumes a coroutine it pushes a snapshot of the parent's
191 // live stack onto `suspended_parent_stacks` so those GC-managed values
192 // remain marked during collections triggered from inside the coroutine.
193 for stack_snapshot in self.suspended_parent_stacks.iter() {
194 for v in stack_snapshot.iter() {
195 v.trace(m);
196 }
197 }
198 for upval_snapshot in self.suspended_parent_open_upvals.iter() {
199 for uv in upval_snapshot.iter() {
200 uv.trace(m);
201 }
202 }
203
204 // PORT NOTE: `strt` (the internal LuaStringImpl intern table) is a
205 // weak table in C; entries are cleared during the atomic weak-table
206 // pass (`clearbykeys`), not marked as roots. The current port has no
207 // incremental weak-sweep, but `strt` is keyed by byte-content rather
208 // than by `Gc` identity, so a dangling entry there is silently
209 // recreated by the next `intern_str` — no UAF, unlike `interned_lt`.
210 // PORT NOTE: `fixedgc` holds objects pre-marked fixed/black at
211 // allocation (`luaC_fix`); the mark phase never re-visits them, and
212 // `dyn Collectable` does not implement `Trace` here.
213 // PORT NOTE: `allgc`, `finobj`, `gray`, `grayagain`, `tobefnz`,
214 // `weak`, `ephemeron`, `allweak` are GC bookkeeping lists owned by
215 // `heap` — they are the universe of allocated objects, not roots.
216 }
217}
218
219// ──────────────────────────────────────────────────────────────────────────────
220// PORT STATUS
221// source: n/a (GC Trace impls bridging lua-vm and lua-gc)
222// target_crate: lua-vm
223// confidence: high
224// todos: 0
225// port_notes: 0
226// unsafe_blocks: 0
227// notes: Implements lua_gc::Trace for LuaState + GlobalState. C does this via
228// hand-written mark routines in lgc.c; we use a trait dispatch.
229// ──────────────────────────────────────────────────────────────────────────────