memscope-rs 0.2.3

A memory tracking library for Rust applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Relation Inference - Memory relationship graph construction.
//!
//! Infers relationships between heap allocations by analyzing pointer patterns,
//! content similarity, and allocation metadata.
//!
//! # Supported Relations
//!
//! | Relation | Meaning | Signal |
//! |----------|---------|--------|
//! | **Owner** | A holds a pointer into B | Pointer scan + RangeMap |
//! | **Slice** | A is a sub-region of B | Pointer falls inside B (not at start) |
//! | **Clone** | A is a copy of B | Same type/size/stack + content match |
//!
//! # Architecture
//!
//! ```text
//! ActiveAllocation + MemoryView
//!//!//!   UTI Engine (TypeGuess)
//!//!//!   RangeMap (address → alloc index)
//!//!//!   Relation Engine (Owner / Slice / Clone)
//!//!//!   RelationGraph
//! ```

mod clone_detector;
mod container_detector;
mod graph_builder;
mod pointer_scan;
mod range_map;
mod shared_detector;
mod slice_detector;

pub use clone_detector::{detect_clones, CloneConfig};
pub use container_detector::{detect_containers, ContainerConfig};
pub use graph_builder::{GraphBuilderConfig, RelationGraphBuilder};
pub use pointer_scan::{detect_owner, InferenceRecord};
pub use range_map::RangeMap;
pub use shared_detector::detect_shared;
pub use slice_detector::detect_slice;

/// A relationship between two allocations in the graph.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Relation {
    /// A owns heap memory (HeapOwner → heap).
    Owns,
    /// A contains B (Container → HeapOwner).
    Contains,
    /// A and B share ownership (Arc/Rc).
    Shares,
    /// A is a view into a sub-region of B (e.g., &[T] into Vec).
    Slice,
    /// A is a copy of B (same type, size, stack, content).
    Clone,
    /// A is a later evolution of B (same variable, later allocation, e.g., Vec reallocation).
    Evolution,
    /// A is an Arc clone of B (StackOwner-based Arc clone detection).
    ArcClone,
    /// A is an Rc clone of B (StackOwner-based Rc clone detection).
    RcClone,
    /// A is an immutable borrow of B (&T).
    ImmutableBorrow,
    /// A is a mutable borrow of B (&mut T).
    MutableBorrow,
}

impl std::fmt::Display for Relation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Relation::Owns => write!(f, "Owns"),
            Relation::Contains => write!(f, "Contains"),
            Relation::Shares => write!(f, "Shares"),
            Relation::Slice => write!(f, "Slice"),
            Relation::Clone => write!(f, "Clone"),
            Relation::Evolution => write!(f, "Evolution"),
            Relation::ArcClone => write!(f, "ArcClone"),
            Relation::RcClone => write!(f, "RcClone"),
            Relation::ImmutableBorrow => write!(f, "ImmutableBorrow"),
            Relation::MutableBorrow => write!(f, "MutableBorrow"),
        }
    }
}

/// An edge in the relation graph.
#[derive(Debug, Clone)]
pub struct RelationEdge {
    /// Source allocation ID (index into the allocations list).
    pub from: usize,
    /// Target allocation ID.
    pub to: usize,
    /// The type of relationship.
    pub relation: Relation,
}

/// A relation graph connecting allocations.
#[derive(Debug, Default, Clone)]
pub struct RelationGraph {
    /// All edges in the graph.
    pub edges: Vec<RelationEdge>,
}

impl RelationGraph {
    /// Create a new empty relation graph.
    pub fn new() -> Self {
        Self::default()
    }

    /// Build relation graph from MemoryView.
    ///
    /// Creates a relation graph from the allocations in a MemoryView.
    /// This is a convenience method for the unified analyzer API.
    pub fn from_view(view: &crate::view::MemoryView) -> Self {
        use crate::snapshot::types::ActiveAllocation;

        let allocations: Vec<ActiveAllocation> =
            view.allocations().iter().map(|a| (*a).clone()).collect();

        RelationGraphBuilder::build(&allocations, None)
    }

    /// Add a single edge to the graph.
    ///
    /// Does not add duplicate edges (same from/to/relation).
    pub fn add_edge(&mut self, from: usize, to: usize, relation: Relation) {
        // Skip self-edges.
        if from == to {
            return;
        }
        // Skip duplicates.
        let exists = self
            .edges
            .iter()
            .any(|e| e.from == from && e.to == to && e.relation == relation);
        if !exists {
            self.edges.push(RelationEdge { from, to, relation });
        }
    }

    /// Add multiple edges at once.
    pub fn add_edges(&mut self, edges: Vec<RelationEdge>) {
        for edge in edges {
            self.add_edge(edge.from, edge.to, edge.relation);
        }
    }

    /// Get all inbound edges to a given node.
    pub fn get_inbound_edges(&self, node: usize) -> Vec<&RelationEdge> {
        self.edges.iter().filter(|e| e.to == node).collect()
    }

    /// Get all outbound edges from a given node.
    pub fn get_outbound_edges(&self, node: usize) -> Vec<&RelationEdge> {
        self.edges.iter().filter(|e| e.from == node).collect()
    }

    /// Get the number of edges in the graph.
    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }

    /// Get all unique node IDs referenced in the graph.
    pub fn all_nodes(&self) -> Vec<usize> {
        let mut nodes: Vec<usize> = self.edges.iter().flat_map(|e| [e.from, e.to]).collect();
        nodes.sort();
        nodes.dedup();
        nodes
    }

    /// Detect cycles in the relation graph using DFS.
    ///
    /// Returns a list of cycle edges as `(from, to, relation)` tuples.
    /// Empty result means no cycles detected.
    pub fn detect_cycles(&self) -> Vec<(usize, usize, Relation)> {
        if self.edges.is_empty() {
            return Vec::new();
        }

        use crate::analysis::relationship_cycle_detector::detect_cycles_direct;

        let relationships: Vec<(usize, usize)> =
            self.edges.iter().map(|e| (e.from, e.to)).collect();

        let cycle_indices = detect_cycles_direct(&relationships);

        cycle_indices
            .into_iter()
            .filter_map(|(from_idx, to_idx)| {
                self.edges.iter().find_map(|e| {
                    if e.from == from_idx && e.to == to_idx {
                        Some((e.from, e.to, e.relation.clone()))
                    } else {
                        None
                    }
                })
            })
            .collect()
    }
}

mod tests {
    #[allow(unused_imports)]
    use crate::{Relation, RelationEdge, RelationGraph};

    #[test]
    fn test_graph_add_edge() {
        let mut graph = RelationGraph::new();

        graph.add_edge(0, 1, Relation::Owns);

        assert_eq!(graph.edge_count(), 1);

        assert_eq!(graph.edges[0].from, 0);

        assert_eq!(graph.edges[0].to, 1);

        assert_eq!(graph.edges[0].relation, Relation::Owns);
    }

    #[test]

    fn test_graph_no_self_edges() {
        let mut graph = RelationGraph::new();

        graph.add_edge(0, 0, Relation::Owns);

        assert_eq!(graph.edge_count(), 0);
    }

    #[test]

    fn test_graph_no_duplicate_edges() {
        let mut graph = RelationGraph::new();

        graph.add_edge(0, 1, Relation::Owns);

        graph.add_edge(0, 1, Relation::Owns);

        assert_eq!(graph.edge_count(), 1);
    }

    #[test]

    fn test_graph_different_relations_allowed() {
        let mut graph = RelationGraph::new();

        graph.add_edge(0, 1, Relation::Owns);

        graph.add_edge(0, 1, Relation::Clone);

        assert_eq!(graph.edge_count(), 2);
    }

    #[test]

    fn test_graph_inbound_outbound_edges() {
        let mut graph = RelationGraph::new();

        graph.add_edge(0, 2, Relation::Owns);

        graph.add_edge(1, 2, Relation::Owns);

        graph.add_edge(2, 3, Relation::Slice);

        let inbound_to_2 = graph.get_inbound_edges(2);

        assert_eq!(inbound_to_2.len(), 2);

        let outbound_from_2 = graph.get_outbound_edges(2);

        assert_eq!(outbound_from_2.len(), 1);
    }

    #[test]

    fn test_graph_all_nodes() {
        let mut graph = RelationGraph::new();

        graph.add_edge(3, 1, Relation::Owns);

        graph.add_edge(1, 2, Relation::Slice);

        let nodes = graph.all_nodes();

        assert_eq!(nodes, vec![1, 2, 3]);
    }

    #[test]

    fn test_graph_add_edges_batch() {
        let mut graph = RelationGraph::new();

        graph.add_edges(vec![
            RelationEdge {
                from: 0,

                to: 1,

                relation: Relation::Owns,
            },
            RelationEdge {
                from: 1,

                to: 2,

                relation: Relation::Slice,
            },
        ]);

        assert_eq!(graph.edge_count(), 2);
    }

    #[test]

    fn test_graph_detect_cycles_none() {
        // Linear chain: 0 → 1 → 2 → no cycles.

        let mut graph = RelationGraph::new();

        graph.add_edge(0, 1, Relation::Owns);

        graph.add_edge(1, 2, Relation::Slice);

        let cycles = graph.detect_cycles();

        assert!(cycles.is_empty());
    }

    #[test]

    fn test_graph_detect_cycles_simple() {
        // Cycle: 0 → 1 → 0

        let mut graph = RelationGraph::new();

        graph.add_edge(0, 1, Relation::Owns);

        graph.add_edge(1, 0, Relation::Owns);

        let cycles = graph.detect_cycles();

        // Should detect at least one cycle edge.

        assert!(!cycles.is_empty());

        // Both edges should be part of the cycle.

        let edge_pairs: Vec<_> = cycles.iter().map(|(f, t, _)| (*f, *t)).collect();

        assert!(edge_pairs.contains(&(0, 1)) || edge_pairs.contains(&(1, 0)));
    }

    #[test]

    fn test_graph_detect_cycles_longer() {
        // Cycle: 0 → 1 → 2 → 0

        let mut graph = RelationGraph::new();

        graph.add_edge(0, 1, Relation::Owns);

        graph.add_edge(1, 2, Relation::Slice);

        graph.add_edge(2, 0, Relation::Clone);

        let cycles = graph.detect_cycles();

        assert!(!cycles.is_empty());

        // Verify the cycle includes the back-edge (2 → 0).

        let has_back_edge = cycles.iter().any(|(f, t, _)| *f == 2 && *t == 0);

        assert!(has_back_edge, "cycle should contain back-edge (2, 0)");
    }

    #[test]

    fn test_graph_detect_cycles_empty() {
        let graph = RelationGraph::new();

        let cycles = graph.detect_cycles();

        assert!(cycles.is_empty());
    }

    #[test]

    fn test_graph_detect_cycles_self_loop_blocked() {
        // Self-edges are blocked by add_edge, so no cycle possible.

        let mut graph = RelationGraph::new();

        graph.add_edge(0, 0, Relation::Owns);

        assert_eq!(graph.edge_count(), 0);

        let cycles = graph.detect_cycles();

        assert!(cycles.is_empty());
    }

    #[test]

    fn test_graph_detect_cycles_diamond() {
        // Diamond: 0 → 1, 0 → 2, 1 → 3, 2 → 3, 3 → 0

        // Cycles: 0→1→3→0 and 0→2→3→0

        let mut graph = RelationGraph::new();

        graph.add_edge(0, 1, Relation::Owns);

        graph.add_edge(0, 2, Relation::Owns);

        graph.add_edge(1, 3, Relation::Slice);

        graph.add_edge(2, 3, Relation::Slice);

        graph.add_edge(3, 0, Relation::Clone);

        let cycles = graph.detect_cycles();

        assert!(!cycles.is_empty());
    }
}