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 lua_gc::{Marker, Trace};
20use crate::state::{LuaState, GlobalState};
21use crate::string::{LuaStringImpl, LuaUserDataImpl};
22use lua_types::{LuaClosure, LuaValue};
23
24/// Phase-B internal richer LuaString. The byte buffer is a Rust `Rc<[u8]>`
25/// (not GC-managed); no fields to mark.
26impl Trace for LuaStringImpl {
27 fn trace(&self, _m: &mut Marker) {}
28}
29
30/// Phase-B internal userdata. Both `metatable` and `uv` are currently
31/// `Option<()>` / `Vec<()>` stubs — no GC edges to walk yet. Becomes
32/// real when userdata machinery lands post-D-1.
33impl Trace for LuaUserDataImpl {
34 fn trace(&self, _m: &mut Marker) {}
35}
36
37impl Trace for LuaState {
38 fn trace(&self, m: &mut Marker) {
39 // C: `traversethread` in lgc.c walks the live portion of the stack
40 // and the open-upvalue list. Trace frame-bounded live ranges instead of
41 // every slot up to `ci.top`: that reserved tail can contain stale values
42 // from previous calls. Lua locals that sit above the transient `top` are
43 // added explicitly from debug local metadata.
44 let trace_debug_locals = self.cached_thread_id == self.global.borrow().current_thread_id;
45 let mut ci_idx = Some(self.ci);
46 while let Some(idx) = ci_idx {
47 let ci = &self.call_info[idx.as_usize()];
48 let start = ci.func.0 as usize;
49 let end_idx = if idx == self.ci {
50 self.top.0 as usize
51 } else if let Some(next) = ci.next {
52 self.call_info[next.as_usize()].func.0 as usize
53 } else {
54 self.top.0 as usize
55 };
56 let end = end_idx.min(self.stack.len());
57 if start < end {
58 for slot in &self.stack[start..end] {
59 slot.val.trace(m);
60 }
61 }
62 if trace_debug_locals && ci.is_lua() {
63 if let Some(slot) = self.stack.get(ci.func.0 as usize) {
64 if let LuaValue::Function(LuaClosure::Lua(cl)) = &slot.val {
65 let pc = ci.saved_pc().saturating_sub(1) as i32;
66 let base = ci.func.0 as usize + 1;
67 let mut n = 1i32;
68 while crate::func::get_local_name(&cl.proto, n, pc).is_some() {
69 let idx = base + (n as usize - 1);
70 if let Some(local_slot) = self.stack.get(idx) {
71 local_slot.val.trace(m);
72 }
73 n += 1;
74 }
75 }
76 }
77 }
78 ci_idx = ci.previous;
79 }
80
81 for uv in self.openupval.iter() {
82 uv.trace(m);
83 }
84
85 // PORT NOTE: `global` (Rc<RefCell<GlobalState>>) is reached from the
86 // heap's root via GlobalState::trace; tracing it from each thread
87 // would re-enter the root and is explicitly excluded.
88 // PORT NOTE: `call_info` entries carry pc offsets and stack indices
89 // but no direct GcRef fields. The active closure is reached through
90 // the stack slot at `ci.func`, already covered by the stack walk.
91 // PORT NOTE: `tbclist` holds StackIdx values only; the to-be-closed
92 // objects themselves live on the stack and are traced there.
93 }
94}
95
96impl Trace for GlobalState {
97 fn trace(&self, m: &mut Marker) {
98 // C: `restartcollection` in lgc.c marks mainthread, l_registry, the
99 // per-type metatables, and pending finalizers. We expand the set to
100 // include preallocated short strings (memerrmsg, tmname[]) and the
101 // open-upvalue thread list, both of which the panic-driven Phase-D
102 // mega-loop expects to see at the root.
103
104 self.l_registry.trace(m);
105
106 // PORT NOTE (phase-b-reconcile): The lua-types LuaTable placeholder is
107 // storage-less, so `globals` and `loaded` cannot live inside the registry
108 // table (see `init_registry`). They are kept as direct GlobalState fields
109 // and must be traced explicitly as roots; once the placeholder reconciles
110 // with vm::LuaTable, these become reachable via `l_registry` and the two
111 // lines below disappear.
112 self.globals.trace(m);
113 self.loaded.trace(m);
114
115 if let Some(t) = &self.mainthread {
116 t.trace(m);
117 }
118
119 self.main_thread_value.trace(m);
120
121 if self.current_thread_id != self.main_thread_id {
122 if let Some(entry) = self.threads.get(&self.current_thread_id) {
123 entry.value.trace(m);
124 }
125 }
126
127 // Registered coroutines are not roots by registration alone. The
128 // post-mark hook traces stacks only for thread handles that were
129 // reached from a real root, matching Lua's collectable coroutine
130 // semantics.
131
132 for slot in self.mt.iter() {
133 if let Some(t) = slot {
134 t.trace(m);
135 }
136 }
137
138 for s in self.tmname.iter() {
139 s.trace(m);
140 }
141
142 self.memerrmsg.trace(m);
143
144 for th in self.twups.iter() {
145 th.trace(m);
146 }
147
148 // The short-string intern cache holds `GcRef<LuaString>` values that
149 // callers (parser, stdlib) reuse by pointer-equality across
150 // `intern_str` calls. C-Lua treats this as a weak table cleared during
151 // the atomic weak-table pass (`clearbykeys`); we have no incremental
152 // weak-sweep yet, so leaving these untraced would leave the HashMap
153 // with dangling `Gc<LuaString>` entries after the very next collect.
154 // Trace them as strong roots until the weak-sweep machinery lands.
155 for s in self.interned_lt.values() {
156 s.trace(m);
157 }
158 for row in self.strcache.iter() {
159 for s in row.iter() {
160 s.trace(m);
161 }
162 }
163
164 // Do not trace `gc_tracked_long_strings` here. That vector is memory
165 // accounting metadata, not an owning root. Lua C treats strings as
166 // non-weak only when they are reached through a surviving table entry
167 // (`iscleared` marks them during weak cleanup); our post-mark weak pass
168 // mirrors that by marking string keys/values returned from
169 // `prune_weak_dead`. Rooting the whole accounting list would keep dead
170 // long strings alive and break gc.lua's weak-string-key checks.
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: tables in this list
180 // are awaiting their `__gc` call but are otherwise dead, and the
181 // table (plus its descendants) must survive long enough for the
182 // finalizer to run.
183 for t in self.to_be_finalized.iter() {
184 t.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// ──────────────────────────────────────────────────────────────────────────────