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
432
433
434
435
436
437
//! Integration tests for Analysis module
//!
//! Tests cover:
//! - Core detection: leaks, UAF, overflow, safety violations
//! - Boundary conditions: empty data, single allocation
//! - Edge cases: multiple issues, complex patterns

use memscope_rs::analysis::{
    AnalysisManager, Detector, LeakDetector, LeakDetectorConfig, LifecycleDetector,
    LifecycleDetectorConfig, OverflowDetector, OverflowDetectorConfig, SafetyDetector,
    SafetyDetectorConfig, UafDetector, UafDetectorConfig,
};
use memscope_rs::capture::types::{AllocationInfo, MemoryStats};

// ============================================================================
// Test Helpers
// ============================================================================

fn create_test_allocations() -> Vec<AllocationInfo> {
    vec![
        AllocationInfo::new(0x1000, 2048),
        AllocationInfo::new(0x2000, 4096),
        AllocationInfo::new(0x3000, 1024),
    ]
}

fn create_leaked_allocations() -> Vec<AllocationInfo> {
    let mut allocations = create_test_allocations();
    for alloc in &mut allocations {
        alloc.is_leaked = true;
    }
    allocations
}

// ============================================================================
// Leak Detection Tests
// ============================================================================

#[test]
fn test_leak_detector_finds_no_leaks_in_normal_allocations() {
    let detector = LeakDetector::new(LeakDetectorConfig::default());
    let allocations = create_test_allocations();

    let result = detector.detect(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should not trigger leak detection"
    );
}

#[test]
fn test_leak_detector_identifies_leaked_memory() {
    let detector = LeakDetector::new(LeakDetectorConfig::default());
    let allocations = create_leaked_allocations();

    let result = detector.detect(&allocations);
    assert_eq!(
        result.issues.len(),
        3,
        "Should detect exactly 3 leaked allocations"
    );
}

#[test]
fn test_leak_detector_handles_empty_input() {
    let detector = LeakDetector::new(LeakDetectorConfig::default());
    let empty: Vec<AllocationInfo> = vec![];

    let result = detector.detect(&empty);
    assert!(
        result.issues.is_empty(),
        "Empty input should produce no issues"
    );
}

#[test]
fn test_leak_detector_with_single_leaked_allocation() {
    let detector = LeakDetector::new(LeakDetectorConfig::default());
    let mut allocations = vec![AllocationInfo::new(0x1000, 1024)];
    allocations[0].is_leaked = true;

    let result = detector.detect(&allocations);
    assert_eq!(result.issues.len(), 1, "Should detect single leak");
}

// ============================================================================
// UAF Detection Tests
// ============================================================================

#[test]
fn test_uaf_detector_no_issues_for_normal_allocations() {
    let detector = UafDetector::new(UafDetectorConfig::default());
    let allocations = create_test_allocations();

    let result = detector.detect(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should not trigger UAF detection"
    );
}

#[test]
fn test_uaf_detector_handles_empty_input() {
    let detector = UafDetector::new(UafDetectorConfig::default());
    let empty: Vec<AllocationInfo> = vec![];

    let result = detector.detect(&empty);
    assert!(
        result.issues.is_empty(),
        "Empty input should produce no UAF issues"
    );
}

// ============================================================================
// Overflow Detection Tests
// ============================================================================

#[test]
fn test_overflow_detector_no_issues_for_normal_allocations() {
    let detector = OverflowDetector::new(OverflowDetectorConfig::default());
    let allocations = create_test_allocations();

    let result = detector.detect(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should not trigger overflow detection"
    );
}

#[test]
fn test_overflow_detector_handles_empty_input() {
    let detector = OverflowDetector::new(OverflowDetectorConfig::default());
    let empty: Vec<AllocationInfo> = vec![];

    let result = detector.detect(&empty);
    assert!(
        result.issues.is_empty(),
        "Empty input should produce no overflow issues"
    );
}

// ============================================================================
// Safety Detection Tests
// ============================================================================

#[test]
fn test_safety_detector_no_issues_for_normal_allocations() {
    let detector = SafetyDetector::new(SafetyDetectorConfig::default());
    let allocations = create_test_allocations();

    let result = detector.detect(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should not trigger safety detection"
    );
}

#[test]
fn test_safety_detector_handles_empty_input() {
    let detector = SafetyDetector::new(SafetyDetectorConfig::default());
    let empty: Vec<AllocationInfo> = vec![];

    let result = detector.detect(&empty);
    assert!(
        result.issues.is_empty(),
        "Empty input should produce no safety issues"
    );
}

// ============================================================================
// Lifecycle Detection Tests
// ============================================================================

#[test]
fn test_lifecycle_detector_no_issues_for_normal_allocations() {
    let detector = LifecycleDetector::new(LifecycleDetectorConfig::default());
    let allocations = create_test_allocations();

    let result = detector.detect(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should not trigger lifecycle detection"
    );
}

#[test]
fn test_lifecycle_detector_handles_empty_input() {
    let detector = LifecycleDetector::new(LifecycleDetectorConfig::default());
    let empty: Vec<AllocationInfo> = vec![];

    let result = detector.detect(&empty);
    assert!(
        result.issues.is_empty(),
        "Empty input should produce no lifecycle issues"
    );
}

// ============================================================================
// Analysis Manager Tests
// ============================================================================

#[test]
fn test_analysis_manager_fragmentation_calculation() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_fragmentation(&allocations);
    assert!(
        result.fragmentation_ratio >= 0.0 && result.fragmentation_ratio <= 1.0,
        "Fragmentation ratio should be between 0 and 1"
    );
}

#[test]
fn test_analysis_manager_empty_allocations() {
    let manager = AnalysisManager::new();
    let empty: Vec<AllocationInfo> = vec![];

    let frag = manager.analyze_fragmentation(&empty);
    assert_eq!(
        frag.fragmentation_ratio, 0.0,
        "Empty allocations should have 0 fragmentation"
    );

    let libs = manager.analyze_system_libraries(&empty);
    assert_eq!(
        libs.std_collections.allocation_count, 0,
        "Empty allocations should have no std collections"
    );
}

#[test]
fn test_analysis_manager_large_dataset() {
    let manager = AnalysisManager::new();

    let allocations: Vec<AllocationInfo> = (0..10000)
        .map(|i| AllocationInfo::new(0x1000 + i * 0x100, (i % 100 + 1) * 64))
        .collect();

    let frag = manager.analyze_fragmentation(&allocations);
    assert!(
        frag.fragmentation_ratio >= 0.0,
        "Large dataset should produce valid fragmentation ratio"
    );
}

#[test]
fn test_analysis_manager_comprehensive_analysis() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();
    let stats = MemoryStats::new();

    let report = manager.perform_comprehensive_analysis(&allocations, &stats);
    assert!(
        report.analysis_timestamp > 0,
        "Report should have valid timestamp"
    );
}

#[test]
fn test_analysis_manager_circular_reference_analysis() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_circular_references(&allocations);
    assert_eq!(
        result.total_smart_pointers, 0,
        "Basic allocations should have no smart pointers"
    );
}

#[test]
fn test_analysis_manager_borrow_pattern_analysis() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_borrow_patterns(&allocations);
    assert!(
        result.patterns.is_empty(),
        "Basic allocations should have no borrow patterns"
    );
}

#[test]
fn test_analysis_manager_generic_type_analysis() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_generic_types(&allocations);
    assert_eq!(
        result.total_instances, 0,
        "Basic allocations should have no generic instances"
    );
}

#[test]
fn test_analysis_manager_async_pattern_analysis() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_async_patterns(&allocations);
    assert!(
        result.patterns.is_empty(),
        "Basic allocations should have no async patterns"
    );
}

#[test]
fn test_analysis_manager_closure_pattern_analysis() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_closure_patterns(&allocations);
    assert!(
        result.detected_closures.is_empty(),
        "Basic allocations should have no closures"
    );
}

#[test]
fn test_analysis_manager_lifecycle_pattern_analysis() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_lifecycle_patterns(&allocations);
    assert!(
        result.drop_events.is_empty(),
        "Basic allocations should have no drop events"
    );
}

// ============================================================================
// Boundary Condition Tests
// ============================================================================

#[test]
fn test_single_allocation_analysis() {
    let manager = AnalysisManager::new();
    let allocations = vec![AllocationInfo::new(0x1000, 1024)];

    let frag = manager.analyze_fragmentation(&allocations);
    assert_eq!(
        frag.fragmentation_ratio, 0.0,
        "Single allocation should have 0 fragmentation"
    );
}

#[test]
fn test_very_large_allocation() {
    let manager = AnalysisManager::new();
    let allocations = vec![AllocationInfo::new(0x1000, 1024 * 1024 * 1024)]; // 1GB

    let frag = manager.analyze_fragmentation(&allocations);
    assert!(
        frag.fragmentation_ratio >= 0.0,
        "Large allocation should produce valid fragmentation"
    );
}

#[test]
fn test_many_small_allocations() {
    let manager = AnalysisManager::new();

    let allocations: Vec<AllocationInfo> = (0..10000)
        .map(|i| AllocationInfo::new(0x1000 + i, 16))
        .collect();

    let result = manager.analyze_fragmentation(&allocations);
    assert!(
        result.fragmentation_ratio >= 0.0,
        "Many small allocations should produce valid fragmentation"
    );
}

// ============================================================================
// Integration Tests
// ============================================================================

#[test]
fn test_memory_leak_analysis_no_leaks() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_memory_leaks(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should have no leaks"
    );
}

#[test]
fn test_use_after_free_analysis_no_issues() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_use_after_free(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should have no UAF issues"
    );
}

#[test]
fn test_buffer_overflow_analysis_no_issues() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_buffer_overflow(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should have no overflow issues"
    );
}

#[test]
fn test_safety_violation_analysis_no_issues() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_safety_violations(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should have no safety violations"
    );
}

#[test]
fn test_lifecycle_issue_analysis_no_issues() {
    let manager = AnalysisManager::new();
    let allocations = create_test_allocations();

    let result = manager.analyze_lifecycle_issues(&allocations);
    assert!(
        result.issues.is_empty(),
        "Normal allocations should have no lifecycle issues"
    );
}