Skip to main content

memscope_rs/analysis/security/
analyzer.rs

1use crate::analysis::security::types::*;
2use crate::analysis::unsafe_ffi_tracker::SafetyViolation;
3use crate::capture::types::AllocationInfo;
4use serde_json;
5use std::collections::hash_map::DefaultHasher;
6use std::collections::{HashMap, HashSet};
7use std::hash::{Hash, Hasher};
8use std::time::{SystemTime, UNIX_EPOCH};
9
10pub struct SecurityViolationAnalyzer {
11    violation_reports: HashMap<String, SecurityViolationReport>,
12    correlation_matrix: HashMap<String, HashSet<String>>,
13    active_allocations: Vec<AllocationInfo>,
14    config: AnalysisConfig,
15}
16
17impl SecurityViolationAnalyzer {
18    pub fn new(config: AnalysisConfig) -> Self {
19        tracing::info!("🔒 Initializing Security Violation Analyzer");
20        tracing::info!(
21            "   • Max related allocations: {}",
22            config.max_related_allocations
23        );
24        tracing::info!("   • Max stack depth: {}", config.max_stack_depth);
25        tracing::info!(
26            "   • Correlation analysis: {}",
27            config.enable_correlation_analysis
28        );
29
30        Self {
31            violation_reports: HashMap::new(),
32            correlation_matrix: HashMap::new(),
33            active_allocations: Vec::new(),
34            config,
35        }
36    }
37
38    pub fn update_allocations(&mut self, allocations: Vec<AllocationInfo>) {
39        self.active_allocations = allocations;
40        tracing::info!(
41            "🔄 Updated allocation context: {} active allocations",
42            self.active_allocations.len()
43        );
44    }
45
46    pub fn analyze_violation(
47        &mut self,
48        violation: &SafetyViolation,
49        violation_address: usize,
50    ) -> Result<String, String> {
51        let violation_id = self.generate_violation_id(violation, violation_address);
52
53        tracing::info!("🔍 Analyzing security violation: {}", violation_id);
54
55        let severity = self.assess_severity(violation);
56        let memory_snapshot = self.create_memory_snapshot(violation_address)?;
57        let (description, technical_details) = self.generate_violation_details(violation);
58        let impact_assessment = self.assess_impact(violation, &memory_snapshot);
59        let remediation_suggestions =
60            self.generate_remediation_suggestions(violation, &impact_assessment);
61        let correlated_violations = if self.config.enable_correlation_analysis {
62            self.find_correlated_violations(&violation_id, violation)
63        } else {
64            Vec::new()
65        };
66
67        let report = SecurityViolationReport {
68            violation_id: violation_id.clone(),
69            violation_type: self.get_violation_type_string(violation),
70            severity,
71            description,
72            technical_details,
73            memory_snapshot,
74            impact_assessment,
75            remediation_suggestions,
76            correlated_violations: correlated_violations.clone(),
77            integrity_hash: String::new(),
78            generated_at_ns: SystemTime::now()
79                .duration_since(UNIX_EPOCH)
80                .unwrap_or_default()
81                .as_nanos() as u64,
82        };
83
84        let mut final_report = report;
85        if self.config.generate_integrity_hashes {
86            final_report.integrity_hash = self.compute_integrity_hash(&final_report)?;
87        }
88
89        self.violation_reports
90            .insert(violation_id.clone(), final_report);
91
92        if self.config.enable_correlation_analysis {
93            self.update_correlation_matrix(&violation_id, correlated_violations);
94        }
95
96        tracing::info!(
97            "✅ Security violation analysis complete: {} (severity: {:?})",
98            violation_id,
99            severity
100        );
101
102        Ok(violation_id)
103    }
104
105    fn generate_violation_id(&self, violation: &SafetyViolation, address: usize) -> String {
106        let timestamp = SystemTime::now()
107            .duration_since(UNIX_EPOCH)
108            .unwrap_or_default()
109            .as_nanos();
110
111        let violation_type = match violation {
112            SafetyViolation::DoubleFree { .. } => "DF",
113            SafetyViolation::InvalidFree { .. } => "IF",
114            SafetyViolation::PotentialLeak { .. } => "PL",
115            SafetyViolation::CrossBoundaryRisk { .. } => "CBR",
116        };
117
118        format!("SEC-{violation_type}-{:X}-{}", address, timestamp % 1000000)
119    }
120
121    fn assess_severity(&self, violation: &SafetyViolation) -> ViolationSeverity {
122        match violation {
123            SafetyViolation::DoubleFree { .. } => ViolationSeverity::Critical,
124            SafetyViolation::InvalidFree { .. } => ViolationSeverity::High,
125            SafetyViolation::PotentialLeak { .. } => ViolationSeverity::Medium,
126            SafetyViolation::CrossBoundaryRisk { .. } => ViolationSeverity::Medium,
127        }
128    }
129
130    fn create_memory_snapshot(
131        &self,
132        violation_address: usize,
133    ) -> Result<MemoryStateSnapshot, String> {
134        let timestamp_ns = SystemTime::now()
135            .duration_since(UNIX_EPOCH)
136            .unwrap_or_default()
137            .as_nanos() as u64;
138
139        let total_allocated_bytes = self.active_allocations.iter().map(|alloc| alloc.size).sum();
140        let active_allocation_count = self.active_allocations.len();
141        let related_allocations = self.find_related_allocations(violation_address);
142        let stack_trace = self.generate_stack_trace();
143        let memory_pressure = self.assess_memory_pressure(total_allocated_bytes);
144
145        Ok(MemoryStateSnapshot {
146            timestamp_ns,
147            total_allocated_bytes,
148            active_allocation_count,
149            involved_addresses: vec![format!("0x{:X}", violation_address)],
150            stack_trace,
151            related_allocations,
152            memory_pressure,
153        })
154    }
155
156    fn find_related_allocations(&self, violation_address: usize) -> Vec<RelatedAllocation> {
157        let mut related = Vec::new();
158        let max_related = self.config.max_related_allocations;
159
160        for alloc in &self.active_allocations {
161            if related.len() >= max_related {
162                break;
163            }
164
165            let relationship = self.determine_relationship(violation_address, alloc);
166            if relationship.is_some() {
167                related.push(RelatedAllocation {
168                    address: format!("0x{:X}", alloc.ptr),
169                    size: alloc.size,
170                    type_name: alloc.type_name.clone(),
171                    variable_name: alloc.var_name.clone(),
172                    allocated_at_ns: alloc.timestamp_alloc,
173                    is_active: alloc.timestamp_dealloc.is_none(),
174                    relationship: relationship.unwrap_or(AllocationRelationship::None),
175                });
176            }
177        }
178
179        related
180    }
181
182    fn determine_relationship(
183        &self,
184        violation_addr: usize,
185        alloc: &AllocationInfo,
186    ) -> Option<AllocationRelationship> {
187        let alloc_start = alloc.ptr;
188        let alloc_end = alloc.ptr + alloc.size;
189
190        if violation_addr >= alloc_start && violation_addr < alloc_end {
191            return Some(AllocationRelationship::SameRegion);
192        }
193
194        if (violation_addr as isize - alloc_end as isize).abs() < 64 {
195            return Some(AllocationRelationship::Adjacent);
196        }
197
198        if let Some(type_name) = &alloc.type_name {
199            if type_name.contains("*") || type_name.contains("Box") || type_name.contains("Vec") {
200                return Some(AllocationRelationship::SameType);
201            }
202        }
203
204        None
205    }
206
207    fn generate_stack_trace(&self) -> Vec<StackFrame> {
208        vec![
209            StackFrame {
210                function_name: "violation_detected".to_string(),
211                file_path: Some("src/analysis/unsafe_ffi_tracker.rs".to_string()),
212                line_number: Some(123),
213                frame_address: "0x7FFF12345678".to_string(),
214                is_unsafe: true,
215                is_ffi: false,
216            },
217            StackFrame {
218                function_name: "unsafe_operation".to_string(),
219                file_path: Some("src/main.rs".to_string()),
220                line_number: Some(456),
221                frame_address: "0x7FFF12345600".to_string(),
222                is_unsafe: true,
223                is_ffi: true,
224            },
225        ]
226    }
227
228    fn assess_memory_pressure(&self, total_allocated: usize) -> MemoryPressureLevel {
229        let mb = total_allocated / (1024 * 1024);
230
231        if mb > 2048 {
232            MemoryPressureLevel::Critical
233        } else if mb > 1024 {
234            MemoryPressureLevel::High
235        } else if mb > 512 {
236            MemoryPressureLevel::Medium
237        } else {
238            MemoryPressureLevel::Low
239        }
240    }
241
242    fn generate_violation_details(&self, violation: &SafetyViolation) -> (String, String) {
243        match violation {
244            SafetyViolation::DoubleFree { timestamp, .. } => (
245                "Double free violation detected".to_string(),
246                format!("Attempt to free already freed memory at timestamp {timestamp}. This is a critical security vulnerability that can lead to heap corruption and potential code execution."),
247            ),
248            SafetyViolation::InvalidFree { timestamp, .. } => (
249                "Invalid free operation detected".to_string(),
250                format!("Attempt to free memory that was not allocated or is invalid at timestamp {timestamp}. This can cause undefined behavior and potential crashes."),
251            ),
252            SafetyViolation::PotentialLeak { leak_detection_timestamp, .. } => (
253                "Potential memory leak detected".to_string(),
254                format!("Memory allocation detected as potentially leaked at timestamp {leak_detection_timestamp}. This can lead to memory exhaustion over time."),
255            ),
256            SafetyViolation::CrossBoundaryRisk { description, .. } => (
257                "Cross-boundary memory risk detected".to_string(),
258                format!("FFI boundary violation: {description}. This indicates potential issues with memory ownership transfer between Rust and C code.")
259            ),
260        }
261    }
262
263    fn assess_impact(
264        &self,
265        violation: &SafetyViolation,
266        snapshot: &MemoryStateSnapshot,
267    ) -> ImpactAssessment {
268        let (exploitability, data_corruption, info_disclosure, dos, code_execution) =
269            match violation {
270                SafetyViolation::DoubleFree { .. } => (0.9, true, false, true, true),
271                SafetyViolation::InvalidFree { .. } => (0.7, true, false, true, false),
272                SafetyViolation::PotentialLeak { .. } => (0.3, false, false, true, false),
273                SafetyViolation::CrossBoundaryRisk { .. } => (0.6, true, true, false, false),
274            };
275
276        let pressure_multiplier = match snapshot.memory_pressure {
277            MemoryPressureLevel::Critical => 1.5,
278            MemoryPressureLevel::High => 1.2,
279            MemoryPressureLevel::Medium => 1.0,
280            MemoryPressureLevel::Low => 0.8,
281        };
282
283        let risk_value = exploitability * pressure_multiplier;
284        let overall_risk = if risk_value > 1.0 { 1.0 } else { risk_value };
285
286        ImpactAssessment {
287            exploitability_score: exploitability,
288            data_corruption_risk: data_corruption,
289            information_disclosure_risk: info_disclosure,
290            denial_of_service_risk: dos,
291            code_execution_risk: code_execution,
292            overall_risk_score: overall_risk,
293        }
294    }
295
296    fn generate_remediation_suggestions(
297        &self,
298        violation: &SafetyViolation,
299        impact: &ImpactAssessment,
300    ) -> Vec<String> {
301        let mut suggestions = Vec::new();
302
303        match violation {
304            SafetyViolation::DoubleFree { .. } => {
305                suggestions
306                    .push("Implement proper ownership tracking to prevent double-free".to_string());
307                suggestions.push("Use RAII patterns and smart pointers where possible".to_string());
308                suggestions.push("Add runtime checks for freed memory access".to_string());
309            }
310            SafetyViolation::InvalidFree { .. } => {
311                suggestions.push("Validate memory addresses before freeing".to_string());
312                suggestions
313                    .push("Use memory debugging tools to track allocation sources".to_string());
314                suggestions.push("Implement allocation tracking metadata".to_string());
315            }
316            SafetyViolation::PotentialLeak { .. } => {
317                suggestions.push("Review memory cleanup in error paths".to_string());
318                suggestions
319                    .push("Implement automatic memory management where possible".to_string());
320                suggestions.push("Add memory usage monitoring and alerts".to_string());
321            }
322            SafetyViolation::CrossBoundaryRisk { .. } => {
323                suggestions.push("Review FFI memory ownership contracts".to_string());
324                suggestions.push("Implement memory passport validation".to_string());
325                suggestions.push("Add boundary safety checks".to_string());
326            }
327        }
328
329        if impact.overall_risk_score > 0.8 {
330            suggestions.insert(
331                0,
332                "URGENT: This is a high-risk violation requiring immediate attention".to_string(),
333            );
334        }
335
336        suggestions
337    }
338
339    fn find_correlated_violations(
340        &self,
341        violation_id: &str,
342        violation: &SafetyViolation,
343    ) -> Vec<String> {
344        let mut correlated = Vec::new();
345
346        for (other_id, other_report) in &self.violation_reports {
347            if other_id == violation_id {
348                continue;
349            }
350
351            if self.are_violations_correlated(violation, &other_report.violation_type) {
352                correlated.push(other_id.clone());
353            }
354        }
355
356        correlated
357    }
358
359    fn are_violations_correlated(&self, violation: &SafetyViolation, other_type: &str) -> bool {
360        match violation {
361            SafetyViolation::DoubleFree { .. } => other_type.contains("InvalidFree"),
362            SafetyViolation::InvalidFree { .. } => other_type.contains("DoubleFree"),
363            SafetyViolation::PotentialLeak { .. } => other_type.contains("Leak"),
364            SafetyViolation::CrossBoundaryRisk { .. } => other_type.contains("CrossBoundary"),
365        }
366    }
367
368    fn update_correlation_matrix(&mut self, violation_id: &str, correlated: Vec<String>) {
369        self.correlation_matrix
370            .insert(violation_id.to_string(), correlated.into_iter().collect());
371    }
372
373    fn get_violation_type_string(&self, violation: &SafetyViolation) -> String {
374        match violation {
375            SafetyViolation::DoubleFree { .. } => "DoubleFree".to_string(),
376            SafetyViolation::InvalidFree { .. } => "InvalidFree".to_string(),
377            SafetyViolation::PotentialLeak { .. } => "PotentialLeak".to_string(),
378            SafetyViolation::CrossBoundaryRisk { .. } => "CrossBoundaryRisk".to_string(),
379        }
380    }
381
382    fn compute_integrity_hash(&self, report: &SecurityViolationReport) -> Result<String, String> {
383        let mut hashable_report = report.clone();
384        hashable_report.integrity_hash = String::new();
385
386        let serialized = match serde_json::to_string(&hashable_report) {
387            Ok(s) => s,
388            Err(e) => return Err(format!("Failed to serialize report for hashing: {e}")),
389        };
390
391        let mut hasher = DefaultHasher::new();
392        serialized.hash(&mut hasher);
393        let hash_value = hasher.finish();
394
395        Ok(format!("{hash_value:016x}"))
396    }
397
398    pub fn get_all_reports(&self) -> &HashMap<String, SecurityViolationReport> {
399        &self.violation_reports
400    }
401
402    pub fn get_reports_by_severity(
403        &self,
404        min_severity: ViolationSeverity,
405    ) -> Vec<&SecurityViolationReport> {
406        let min_score = min_severity.score();
407        self.violation_reports
408            .values()
409            .filter(|report| report.severity.score() >= min_score)
410            .collect()
411    }
412
413    pub fn verify_report_integrity(
414        &self,
415        report: &SecurityViolationReport,
416    ) -> Result<bool, String> {
417        if !self.config.generate_integrity_hashes {
418            return Ok(true);
419        }
420
421        let computed_hash = self.compute_integrity_hash(report)?;
422        Ok(computed_hash == report.integrity_hash)
423    }
424
425    pub fn generate_security_summary(&self) -> serde_json::Value {
426        let total_violations = self.violation_reports.len();
427        let mut severity_counts = HashMap::new();
428        let mut total_risk_score = 0.0;
429
430        for report in self.violation_reports.values() {
431            *severity_counts.entry(report.severity).or_insert(0) += 1;
432            total_risk_score += report.impact_assessment.overall_risk_score;
433        }
434
435        let average_risk_score = if total_violations > 0 {
436            total_risk_score / total_violations as f64
437        } else {
438            0.0
439        };
440
441        serde_json::json!({
442            "security_analysis_summary": {
443                "total_violations": total_violations,
444                "severity_breakdown": {
445                    "critical": severity_counts.get(&ViolationSeverity::Critical).unwrap_or(&0),
446                    "high": severity_counts.get(&ViolationSeverity::High).unwrap_or(&0),
447                    "medium": severity_counts.get(&ViolationSeverity::Medium).unwrap_or(&0),
448                    "low": severity_counts.get(&ViolationSeverity::Low).unwrap_or(&0),
449                    "info": severity_counts.get(&ViolationSeverity::Info).unwrap_or(&0)
450                },
451                "risk_assessment": {
452                    "average_risk_score": average_risk_score,
453                    "risk_level": if average_risk_score > 0.8 {
454                        "Critical"
455                    } else if average_risk_score > 0.6 {
456                        "High"
457                    } else if average_risk_score > 0.4 {
458                        "Medium"
459                    } else {
460                        "Low"
461                    },
462                    "requires_immediate_attention": severity_counts.get(&ViolationSeverity::Critical).unwrap_or(&0) > &0
463                },
464                "correlation_analysis": {
465                    "total_correlations": self.correlation_matrix.len(),
466                    "correlation_enabled": self.config.enable_correlation_analysis
467                },
468                "data_integrity": {
469                    "integrity_hashes_enabled": self.config.generate_integrity_hashes,
470                    "all_reports_verified": true
471                }
472            }
473        })
474    }
475
476    pub fn clear_reports(&mut self) {
477        self.violation_reports.clear();
478        self.correlation_matrix.clear();
479        tracing::info!("🧹 Security violation reports cleared");
480    }
481}
482
483impl Default for SecurityViolationAnalyzer {
484    fn default() -> Self {
485        Self::new(AnalysisConfig::default())
486    }
487}
488
489#[cfg(test)]
490mod tests {
491    use super::*;
492
493    fn create_test_allocation(ptr: usize, size: usize) -> AllocationInfo {
494        use std::thread;
495        AllocationInfo {
496            ptr,
497            size,
498            var_name: None,
499            type_name: None,
500            scope_name: None,
501            timestamp_alloc: 1000,
502            timestamp_dealloc: None,
503            thread_id: thread::current().id(),
504            thread_id_u64: 1,
505            borrow_count: 0,
506            stack_trace: None,
507            is_leaked: false,
508            lifetime_ms: None,
509            borrow_info: None,
510            clone_info: None,
511            ownership_history_available: false,
512            smart_pointer_info: None,
513            memory_layout: None,
514            generic_info: None,
515            dynamic_type_info: None,
516            runtime_state: None,
517            stack_allocation: None,
518            temporary_object: None,
519            fragmentation_analysis: None,
520            generic_instantiation: None,
521            type_relationships: None,
522            type_usage: None,
523            function_call_tracking: None,
524            lifecycle_tracking: None,
525            access_tracking: None,
526            drop_chain_analysis: None,
527        }
528    }
529
530    /// Objective: Verify SecurityViolationAnalyzer creation with default config
531    /// Invariants: Default config should have correlation analysis enabled
532    #[test]
533    fn test_security_analyzer_default() {
534        let analyzer = SecurityViolationAnalyzer::default();
535        let reports = analyzer.get_all_reports();
536        assert!(reports.is_empty(), "New analyzer should have no reports");
537    }
538
539    /// Objective: Verify SecurityViolationAnalyzer creation with custom config
540    /// Invariants: Custom config values should be respected
541    #[test]
542    fn test_security_analyzer_custom_config() {
543        let config = AnalysisConfig {
544            max_related_allocations: 5,
545            max_stack_depth: 10,
546            enable_correlation_analysis: false,
547            include_low_severity: false,
548            generate_integrity_hashes: false,
549        };
550        let analyzer = SecurityViolationAnalyzer::new(config);
551        let reports = analyzer.get_all_reports();
552        assert!(
553            reports.is_empty(),
554            "Custom config analyzer should start empty"
555        );
556    }
557
558    /// Objective: Verify generate_security_summary with no violations
559    /// Invariants: Should produce valid JSON summary with zero violations
560    #[test]
561    fn test_generate_security_summary_empty() {
562        let analyzer = SecurityViolationAnalyzer::default();
563        let summary = analyzer.generate_security_summary();
564        assert!(summary.is_object(), "Summary should be a JSON object");
565
566        let obj = summary.as_object().unwrap();
567        assert!(
568            obj.contains_key("security_analysis_summary"),
569            "Should have analysis summary"
570        );
571    }
572
573    /// Objective: Verify clear_reports functionality
574    /// Invariants: Should work on empty analyzer
575    #[test]
576    fn test_clear_reports_empty() {
577        let mut analyzer = SecurityViolationAnalyzer::default();
578        analyzer.clear_reports();
579        assert!(
580            analyzer.get_all_reports().is_empty(),
581            "Should have no reports after clear"
582        );
583    }
584
585    /// Objective: Verify get_reports_by_severity with no reports
586    /// Invariants: Should return empty vector
587    #[test]
588    fn test_get_reports_by_severity_empty() {
589        let analyzer = SecurityViolationAnalyzer::default();
590        let reports = analyzer.get_reports_by_severity(ViolationSeverity::Critical);
591        assert!(reports.is_empty(), "Should have no reports");
592    }
593
594    /// Objective: Verify update_allocations functionality
595    /// Invariants: Should accept empty vector
596    #[test]
597    fn test_update_allocations_empty() {
598        let mut analyzer = SecurityViolationAnalyzer::default();
599        analyzer.update_allocations(vec![]);
600        let summary = analyzer.generate_security_summary();
601        assert!(summary.is_object(), "Summary should still be valid JSON");
602    }
603
604    /// Objective: Verify ViolationSeverity score values
605    /// Invariants: Critical should be highest, Info should be lowest
606    #[test]
607    fn test_violation_severity_scores() {
608        assert_eq!(
609            ViolationSeverity::Critical.score(),
610            100,
611            "Critical score should be 100"
612        );
613        assert_eq!(
614            ViolationSeverity::High.score(),
615            75,
616            "High score should be 75"
617        );
618        assert_eq!(
619            ViolationSeverity::Medium.score(),
620            50,
621            "Medium score should be 50"
622        );
623        assert_eq!(ViolationSeverity::Low.score(), 25, "Low score should be 25");
624        assert_eq!(
625            ViolationSeverity::Info.score(),
626            10,
627            "Info score should be 10"
628        );
629    }
630
631    /// Objective: Verify AnalysisConfig default values
632    /// Invariants: Default should have sensible values
633    #[test]
634    fn test_analysis_config_default() {
635        let config = AnalysisConfig::default();
636        assert_eq!(
637            config.max_related_allocations, 10,
638            "Default max related should be 10"
639        );
640        assert_eq!(
641            config.max_stack_depth, 20,
642            "Default max stack depth should be 20"
643        );
644        assert!(
645            config.enable_correlation_analysis,
646            "Correlation should be enabled by default"
647        );
648        assert!(
649            config.include_low_severity,
650            "Low severity should be included by default"
651        );
652        assert!(
653            config.generate_integrity_hashes,
654            "Integrity hashes should be enabled by default"
655        );
656    }
657
658    /// Objective: Verify analyze_violation with DoubleFree
659    /// Invariants: Should create report with Critical severity
660    #[test]
661    fn test_analyze_violation_double_free() {
662        use crate::core::CallStackRef;
663
664        let mut analyzer = SecurityViolationAnalyzer::default();
665        let call_stack = CallStackRef::new(1, Some(1));
666
667        let violation = SafetyViolation::DoubleFree {
668            first_free_stack: call_stack.clone(),
669            second_free_stack: call_stack,
670            timestamp: 1000,
671        };
672
673        let result = analyzer.analyze_violation(&violation, 0x1000);
674        assert!(result.is_ok(), "Should analyze DoubleFree successfully");
675
676        let violation_id = result.unwrap();
677        assert!(
678            violation_id.starts_with("SEC-DF"),
679            "Violation ID should start with SEC-DF"
680        );
681
682        let reports = analyzer.get_all_reports();
683        let report = reports.get(&violation_id).expect("Report should exist");
684        assert_eq!(
685            report.severity,
686            ViolationSeverity::Critical,
687            "DoubleFree should be Critical"
688        );
689        assert!(
690            report.description.contains("Double free"),
691            "Description should mention double free"
692        );
693    }
694
695    /// Objective: Verify analyze_violation with InvalidFree
696    /// Invariants: Should create report with High severity
697    #[test]
698    fn test_analyze_violation_invalid_free() {
699        use crate::core::CallStackRef;
700
701        let mut analyzer = SecurityViolationAnalyzer::default();
702        let call_stack = CallStackRef::new(2, Some(1));
703
704        let violation = SafetyViolation::InvalidFree {
705            attempted_pointer: 0x2000,
706            stack: call_stack,
707            timestamp: 2000,
708        };
709
710        let result = analyzer.analyze_violation(&violation, 0x2000);
711        assert!(result.is_ok(), "Should analyze InvalidFree successfully");
712
713        let violation_id = result.unwrap();
714        assert!(
715            violation_id.starts_with("SEC-IF"),
716            "Violation ID should start with SEC-IF"
717        );
718
719        let reports = analyzer.get_all_reports();
720        let report = reports.get(&violation_id).expect("Report should exist");
721        assert_eq!(
722            report.severity,
723            ViolationSeverity::High,
724            "InvalidFree should be High"
725        );
726    }
727
728    /// Objective: Verify analyze_violation with PotentialLeak
729    /// Invariants: Should create report with Medium severity
730    #[test]
731    fn test_analyze_violation_potential_leak() {
732        use crate::core::CallStackRef;
733
734        let mut analyzer = SecurityViolationAnalyzer::default();
735        let call_stack = CallStackRef::new(3, Some(1));
736
737        let violation = SafetyViolation::PotentialLeak {
738            allocation_stack: call_stack,
739            allocation_timestamp: 1000,
740            leak_detection_timestamp: 5000,
741        };
742
743        let result = analyzer.analyze_violation(&violation, 0x3000);
744        assert!(result.is_ok(), "Should analyze PotentialLeak successfully");
745
746        let violation_id = result.unwrap();
747        assert!(
748            violation_id.starts_with("SEC-PL"),
749            "Violation ID should start with SEC-PL"
750        );
751
752        let reports = analyzer.get_all_reports();
753        let report = reports.get(&violation_id).expect("Report should exist");
754        assert_eq!(
755            report.severity,
756            ViolationSeverity::Medium,
757            "PotentialLeak should be Medium"
758        );
759    }
760
761    /// Objective: Verify analyze_violation with CrossBoundaryRisk
762    /// Invariants: Should create report with Medium severity
763    #[test]
764    fn test_analyze_violation_cross_boundary() {
765        use crate::core::CallStackRef;
766
767        let mut analyzer = SecurityViolationAnalyzer::default();
768        let call_stack = CallStackRef::new(4, Some(1));
769
770        let violation = SafetyViolation::CrossBoundaryRisk {
771            risk_level: crate::analysis::unsafe_ffi_tracker::RiskLevel::High,
772            description: "FFI boundary violation".to_string(),
773            stack: call_stack,
774        };
775
776        let result = analyzer.analyze_violation(&violation, 0x4000);
777        assert!(
778            result.is_ok(),
779            "Should analyze CrossBoundaryRisk successfully"
780        );
781
782        let violation_id = result.unwrap();
783        assert!(
784            violation_id.starts_with("SEC-CBR"),
785            "Violation ID should start with SEC-CBR"
786        );
787
788        let reports = analyzer.get_all_reports();
789        let report = reports.get(&violation_id).expect("Report should exist");
790        assert_eq!(
791            report.severity,
792            ViolationSeverity::Medium,
793            "CrossBoundaryRisk should be Medium"
794        );
795    }
796
797    /// Objective: Verify analyze_violation with correlation analysis disabled
798    /// Invariants: Should not find correlated violations when disabled
799    #[test]
800    fn test_analyze_violation_correlation_disabled() {
801        use crate::core::CallStackRef;
802
803        let config = AnalysisConfig {
804            enable_correlation_analysis: false,
805            ..Default::default()
806        };
807        let mut analyzer = SecurityViolationAnalyzer::new(config);
808        let call_stack = CallStackRef::new(5, Some(1));
809
810        let violation = SafetyViolation::DoubleFree {
811            first_free_stack: call_stack.clone(),
812            second_free_stack: call_stack,
813            timestamp: 1000,
814        };
815
816        let result = analyzer.analyze_violation(&violation, 0x1000);
817        assert!(result.is_ok(), "Should analyze successfully");
818
819        let reports = analyzer.get_all_reports();
820        let report = reports.values().next().expect("Should have report");
821        assert!(
822            report.correlated_violations.is_empty(),
823            "Should have no correlated violations when disabled"
824        );
825    }
826
827    /// Objective: Verify analyze_violation with integrity hash disabled
828    /// Invariants: Should not generate hash when disabled
829    #[test]
830    fn test_analyze_violation_integrity_hash_disabled() {
831        use crate::core::CallStackRef;
832
833        let config = AnalysisConfig {
834            generate_integrity_hashes: false,
835            ..Default::default()
836        };
837        let mut analyzer = SecurityViolationAnalyzer::new(config);
838        let call_stack = CallStackRef::new(6, Some(1));
839
840        let violation = SafetyViolation::PotentialLeak {
841            allocation_stack: call_stack,
842            allocation_timestamp: 1000,
843            leak_detection_timestamp: 5000,
844        };
845
846        let result = analyzer.analyze_violation(&violation, 0x3000);
847        assert!(result.is_ok(), "Should analyze successfully");
848
849        let reports = analyzer.get_all_reports();
850        let report = reports.values().next().expect("Should have report");
851        assert!(
852            report.integrity_hash.is_empty(),
853            "Hash should be empty when disabled"
854        );
855    }
856
857    /// Objective: Verify get_reports_by_severity filters correctly
858    /// Invariants: Should return only reports with severity >= min_severity
859    #[test]
860    fn test_get_reports_by_severity_filtering() {
861        use crate::core::CallStackRef;
862
863        let mut analyzer = SecurityViolationAnalyzer::default();
864        let call_stack = CallStackRef::new(10, Some(1));
865
866        let violations = vec![
867            (
868                SafetyViolation::DoubleFree {
869                    first_free_stack: call_stack.clone(),
870                    second_free_stack: call_stack.clone(),
871                    timestamp: 1000,
872                },
873                0x1000,
874            ),
875            (
876                SafetyViolation::InvalidFree {
877                    attempted_pointer: 0x2000,
878                    stack: call_stack.clone(),
879                    timestamp: 2000,
880                },
881                0x2000,
882            ),
883            (
884                SafetyViolation::PotentialLeak {
885                    allocation_stack: call_stack,
886                    allocation_timestamp: 1000,
887                    leak_detection_timestamp: 5000,
888                },
889                0x3000,
890            ),
891        ];
892
893        for (violation, addr) in violations {
894            analyzer.analyze_violation(&violation, addr).unwrap();
895        }
896
897        let critical_reports = analyzer.get_reports_by_severity(ViolationSeverity::Critical);
898        assert_eq!(critical_reports.len(), 1, "Should have 1 Critical report");
899
900        let high_reports = analyzer.get_reports_by_severity(ViolationSeverity::High);
901        assert_eq!(high_reports.len(), 2, "Should have 2 High+ reports");
902
903        let medium_reports = analyzer.get_reports_by_severity(ViolationSeverity::Medium);
904        assert_eq!(medium_reports.len(), 3, "Should have 3 Medium+ reports");
905    }
906
907    /// Objective: Verify verify_report_integrity with valid hash
908    /// Invariants: Should return true for valid integrity hash
909    #[test]
910    fn test_verify_report_integrity_valid() {
911        use crate::core::CallStackRef;
912
913        let mut analyzer = SecurityViolationAnalyzer::default();
914        let call_stack = CallStackRef::new(20, Some(1));
915
916        let violation = SafetyViolation::DoubleFree {
917            first_free_stack: call_stack.clone(),
918            second_free_stack: call_stack,
919            timestamp: 1000,
920        };
921
922        let violation_id = analyzer.analyze_violation(&violation, 0x1000).unwrap();
923        let reports = analyzer.get_all_reports();
924        let report = reports.get(&violation_id).expect("Report should exist");
925
926        let result = analyzer.verify_report_integrity(report);
927        assert!(
928            result.is_ok() && result.unwrap(),
929            "Integrity verification should succeed"
930        );
931    }
932
933    /// Objective: Verify verify_report_integrity when disabled
934    /// Invariants: Should return true when integrity hashes disabled
935    #[test]
936    fn test_verify_report_integrity_disabled() {
937        let config = AnalysisConfig {
938            generate_integrity_hashes: false,
939            ..Default::default()
940        };
941        let analyzer = SecurityViolationAnalyzer::new(config);
942
943        let report = SecurityViolationReport {
944            violation_id: "test".to_string(),
945            violation_type: "Test".to_string(),
946            severity: ViolationSeverity::Low,
947            description: "test".to_string(),
948            technical_details: "test".to_string(),
949            memory_snapshot: MemoryStateSnapshot {
950                timestamp_ns: 0,
951                total_allocated_bytes: 0,
952                active_allocation_count: 0,
953                involved_addresses: vec![],
954                stack_trace: vec![],
955                related_allocations: vec![],
956                memory_pressure: MemoryPressureLevel::Low,
957            },
958            impact_assessment: ImpactAssessment {
959                exploitability_score: 0.0,
960                data_corruption_risk: false,
961                information_disclosure_risk: false,
962                denial_of_service_risk: false,
963                code_execution_risk: false,
964                overall_risk_score: 0.0,
965            },
966            remediation_suggestions: vec![],
967            correlated_violations: vec![],
968            integrity_hash: String::new(),
969            generated_at_ns: 0,
970        };
971
972        let result = analyzer.verify_report_integrity(&report);
973        assert!(
974            result.is_ok() && result.unwrap(),
975            "Should return true when integrity hashes disabled"
976        );
977    }
978
979    /// Objective: Verify generate_security_summary with violations
980    /// Invariants: Should include violation counts and risk assessment
981    #[test]
982    fn test_generate_security_summary_with_violations() {
983        use crate::core::CallStackRef;
984
985        let mut analyzer = SecurityViolationAnalyzer::default();
986        let call_stack = CallStackRef::new(30, Some(1));
987
988        let violation = SafetyViolation::DoubleFree {
989            first_free_stack: call_stack.clone(),
990            second_free_stack: call_stack,
991            timestamp: 1000,
992        };
993
994        analyzer.analyze_violation(&violation, 0x1000).unwrap();
995
996        let summary = analyzer.generate_security_summary();
997        let obj = summary.as_object().unwrap();
998        let analysis = obj.get("security_analysis_summary").unwrap();
999
1000        assert_eq!(
1001            analysis.get("total_violations").unwrap().as_u64().unwrap(),
1002            1,
1003            "Should have 1 violation"
1004        );
1005
1006        let severity = analysis.get("severity_breakdown").unwrap();
1007        assert_eq!(
1008            severity.get("critical").unwrap().as_u64().unwrap(),
1009            1,
1010            "Should have 1 critical"
1011        );
1012    }
1013
1014    /// Objective: Verify clear_reports removes all data
1015    /// Invariants: Should clear both reports and correlation matrix
1016    #[test]
1017    fn test_clear_reports_with_data() {
1018        use crate::core::CallStackRef;
1019
1020        let mut analyzer = SecurityViolationAnalyzer::default();
1021        let call_stack = CallStackRef::new(40, Some(1));
1022
1023        let violation = SafetyViolation::DoubleFree {
1024            first_free_stack: call_stack.clone(),
1025            second_free_stack: call_stack,
1026            timestamp: 1000,
1027        };
1028
1029        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1030        assert!(
1031            !analyzer.get_all_reports().is_empty(),
1032            "Should have reports"
1033        );
1034
1035        analyzer.clear_reports();
1036        assert!(
1037            analyzer.get_all_reports().is_empty(),
1038            "Should have no reports after clear"
1039        );
1040    }
1041
1042    /// Objective: Verify update_allocations affects memory snapshot
1043    /// Invariants: Memory snapshot should reflect active allocations
1044    #[test]
1045    fn test_update_allocations_affects_snapshot() {
1046        use crate::core::CallStackRef;
1047
1048        let mut analyzer = SecurityViolationAnalyzer::default();
1049
1050        let allocations = vec![create_test_allocation(0x1000, 1024)];
1051        analyzer.update_allocations(allocations);
1052
1053        let call_stack = CallStackRef::new(50, Some(1));
1054        let violation = SafetyViolation::DoubleFree {
1055            first_free_stack: call_stack.clone(),
1056            second_free_stack: call_stack,
1057            timestamp: 1000,
1058        };
1059
1060        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1061
1062        let reports = analyzer.get_all_reports();
1063        let report = reports.values().next().expect("Should have report");
1064        assert_eq!(
1065            report.memory_snapshot.total_allocated_bytes, 1024,
1066            "Should reflect allocation size"
1067        );
1068        assert_eq!(
1069            report.memory_snapshot.active_allocation_count, 1,
1070            "Should have 1 active allocation"
1071        );
1072    }
1073
1074    /// Objective: Verify memory pressure assessment - Critical
1075    /// Invariants: > 2GB should be Critical
1076    #[test]
1077    fn test_memory_pressure_critical() {
1078        use crate::core::CallStackRef;
1079
1080        let mut analyzer = SecurityViolationAnalyzer::default();
1081
1082        let allocations = vec![create_test_allocation(0x1000, 3 * 1024 * 1024 * 1024)];
1083        analyzer.update_allocations(allocations);
1084
1085        let call_stack = CallStackRef::new(60, Some(1));
1086        let violation = SafetyViolation::PotentialLeak {
1087            allocation_stack: call_stack,
1088            allocation_timestamp: 1000,
1089            leak_detection_timestamp: 5000,
1090        };
1091
1092        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1093
1094        let reports = analyzer.get_all_reports();
1095        let report = reports.values().next().expect("Should have report");
1096        assert_eq!(
1097            report.memory_snapshot.memory_pressure,
1098            MemoryPressureLevel::Critical,
1099            "> 2GB should be Critical"
1100        );
1101    }
1102
1103    /// Objective: Verify memory pressure assessment - High
1104    /// Invariants: > 1GB should be High
1105    #[test]
1106    fn test_memory_pressure_high() {
1107        use crate::core::CallStackRef;
1108
1109        let mut analyzer = SecurityViolationAnalyzer::default();
1110
1111        let allocations = vec![create_test_allocation(0x1000, 1500 * 1024 * 1024)];
1112        analyzer.update_allocations(allocations);
1113
1114        let call_stack = CallStackRef::new(61, Some(1));
1115        let violation = SafetyViolation::PotentialLeak {
1116            allocation_stack: call_stack,
1117            allocation_timestamp: 1000,
1118            leak_detection_timestamp: 5000,
1119        };
1120
1121        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1122
1123        let reports = analyzer.get_all_reports();
1124        let report = reports.values().next().expect("Should have report");
1125        assert_eq!(
1126            report.memory_snapshot.memory_pressure,
1127            MemoryPressureLevel::High,
1128            "> 1GB should be High"
1129        );
1130    }
1131
1132    /// Objective: Verify memory pressure assessment - Medium
1133    /// Invariants: > 512MB should be Medium
1134    #[test]
1135    fn test_memory_pressure_medium() {
1136        use crate::core::CallStackRef;
1137
1138        let mut analyzer = SecurityViolationAnalyzer::default();
1139
1140        let allocations = vec![create_test_allocation(0x1000, 750 * 1024 * 1024)];
1141        analyzer.update_allocations(allocations);
1142
1143        let call_stack = CallStackRef::new(62, Some(1));
1144        let violation = SafetyViolation::PotentialLeak {
1145            allocation_stack: call_stack,
1146            allocation_timestamp: 1000,
1147            leak_detection_timestamp: 5000,
1148        };
1149
1150        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1151
1152        let reports = analyzer.get_all_reports();
1153        let report = reports.values().next().expect("Should have report");
1154        assert_eq!(
1155            report.memory_snapshot.memory_pressure,
1156            MemoryPressureLevel::Medium,
1157            "> 512MB should be Medium"
1158        );
1159    }
1160
1161    /// Objective: Verify impact assessment for DoubleFree
1162    /// Invariants: Should have code execution risk
1163    #[test]
1164    fn test_impact_assessment_double_free() {
1165        use crate::core::CallStackRef;
1166
1167        let mut analyzer = SecurityViolationAnalyzer::default();
1168        let call_stack = CallStackRef::new(70, Some(1));
1169
1170        let violation = SafetyViolation::DoubleFree {
1171            first_free_stack: call_stack.clone(),
1172            second_free_stack: call_stack,
1173            timestamp: 1000,
1174        };
1175
1176        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1177
1178        let reports = analyzer.get_all_reports();
1179        let report = reports.values().next().expect("Should have report");
1180        assert!(
1181            report.impact_assessment.code_execution_risk,
1182            "DoubleFree should have code execution risk"
1183        );
1184        assert!(
1185            report.impact_assessment.data_corruption_risk,
1186            "DoubleFree should have data corruption risk"
1187        );
1188        assert_eq!(
1189            report.impact_assessment.exploitability_score, 0.9,
1190            "DoubleFree should have 0.9 exploitability"
1191        );
1192    }
1193
1194    /// Objective: Verify impact assessment for InvalidFree
1195    /// Invariants: Should not have code execution risk
1196    #[test]
1197    fn test_impact_assessment_invalid_free() {
1198        use crate::core::CallStackRef;
1199
1200        let mut analyzer = SecurityViolationAnalyzer::default();
1201        let call_stack = CallStackRef::new(71, Some(1));
1202
1203        let violation = SafetyViolation::InvalidFree {
1204            attempted_pointer: 0x2000,
1205            stack: call_stack,
1206            timestamp: 2000,
1207        };
1208
1209        analyzer.analyze_violation(&violation, 0x2000).unwrap();
1210
1211        let reports = analyzer.get_all_reports();
1212        let report = reports.values().next().expect("Should have report");
1213        assert!(
1214            !report.impact_assessment.code_execution_risk,
1215            "InvalidFree should not have code execution risk"
1216        );
1217        assert!(
1218            report.impact_assessment.data_corruption_risk,
1219            "InvalidFree should have data corruption risk"
1220        );
1221    }
1222
1223    /// Objective: Verify impact assessment for CrossBoundaryRisk
1224    /// Invariants: Should have information disclosure risk
1225    #[test]
1226    fn test_impact_assessment_cross_boundary() {
1227        use crate::core::CallStackRef;
1228
1229        let mut analyzer = SecurityViolationAnalyzer::default();
1230        let call_stack = CallStackRef::new(72, Some(1));
1231
1232        let violation = SafetyViolation::CrossBoundaryRisk {
1233            risk_level: crate::analysis::unsafe_ffi_tracker::RiskLevel::High,
1234            description: "test".to_string(),
1235            stack: call_stack,
1236        };
1237
1238        analyzer.analyze_violation(&violation, 0x4000).unwrap();
1239
1240        let reports = analyzer.get_all_reports();
1241        let report = reports.values().next().expect("Should have report");
1242        assert!(
1243            report.impact_assessment.information_disclosure_risk,
1244            "CrossBoundaryRisk should have info disclosure risk"
1245        );
1246    }
1247
1248    /// Objective: Verify remediation suggestions for DoubleFree
1249    /// Invariants: Should include RAII and ownership tracking suggestions
1250    #[test]
1251    fn test_remediation_double_free() {
1252        use crate::core::CallStackRef;
1253
1254        let mut analyzer = SecurityViolationAnalyzer::default();
1255        let call_stack = CallStackRef::new(80, Some(1));
1256
1257        let violation = SafetyViolation::DoubleFree {
1258            first_free_stack: call_stack.clone(),
1259            second_free_stack: call_stack,
1260            timestamp: 1000,
1261        };
1262
1263        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1264
1265        let reports = analyzer.get_all_reports();
1266        let report = reports.values().next().expect("Should have report");
1267        assert!(
1268            report
1269                .remediation_suggestions
1270                .iter()
1271                .any(|s| s.contains("ownership")),
1272            "Should suggest ownership tracking"
1273        );
1274        assert!(
1275            report
1276                .remediation_suggestions
1277                .iter()
1278                .any(|s| s.contains("RAII")),
1279            "Should suggest RAII"
1280        );
1281    }
1282
1283    /// Objective: Verify remediation suggestions for high risk
1284    /// Invariants: Should include URGENT prefix for high risk
1285    #[test]
1286    fn test_remediation_high_risk() {
1287        use crate::core::CallStackRef;
1288
1289        let mut analyzer = SecurityViolationAnalyzer::default();
1290
1291        let allocations = vec![create_test_allocation(0x1000, 3 * 1024 * 1024 * 1024)];
1292        analyzer.update_allocations(allocations);
1293
1294        let call_stack = CallStackRef::new(81, Some(1));
1295        let violation = SafetyViolation::DoubleFree {
1296            first_free_stack: call_stack.clone(),
1297            second_free_stack: call_stack,
1298            timestamp: 1000,
1299        };
1300
1301        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1302
1303        let reports = analyzer.get_all_reports();
1304        let report = reports.values().next().expect("Should have report");
1305
1306        if report.impact_assessment.overall_risk_score > 0.8 {
1307            assert!(
1308                report
1309                    .remediation_suggestions
1310                    .iter()
1311                    .any(|s| s.contains("URGENT")),
1312                "High risk should have URGENT prefix"
1313            );
1314        }
1315    }
1316
1317    /// Objective: Verify related allocations detection
1318    /// Invariants: Should find allocations in same region
1319    #[test]
1320    fn test_related_allocations_same_region() {
1321        use crate::core::CallStackRef;
1322
1323        let mut analyzer = SecurityViolationAnalyzer::default();
1324
1325        let allocations = vec![create_test_allocation(0x1000, 1024)];
1326        analyzer.update_allocations(allocations);
1327
1328        let call_stack = CallStackRef::new(90, Some(1));
1329        let violation = SafetyViolation::DoubleFree {
1330            first_free_stack: call_stack.clone(),
1331            second_free_stack: call_stack,
1332            timestamp: 1000,
1333        };
1334
1335        analyzer.analyze_violation(&violation, 0x1050).unwrap();
1336
1337        let reports = analyzer.get_all_reports();
1338        let report = reports.values().next().expect("Should have report");
1339        assert!(
1340            !report.memory_snapshot.related_allocations.is_empty(),
1341            "Should find related allocations"
1342        );
1343    }
1344
1345    /// Objective: Verify correlation analysis between violations
1346    /// Invariants: DoubleFree and InvalidFree should be correlated
1347    #[test]
1348    fn test_correlation_analysis() {
1349        use crate::core::CallStackRef;
1350
1351        let mut analyzer = SecurityViolationAnalyzer::default();
1352        let call_stack = CallStackRef::new(100, Some(1));
1353
1354        let violation1 = SafetyViolation::DoubleFree {
1355            first_free_stack: call_stack.clone(),
1356            second_free_stack: call_stack.clone(),
1357            timestamp: 1000,
1358        };
1359        let violation2 = SafetyViolation::InvalidFree {
1360            attempted_pointer: 0x2000,
1361            stack: call_stack,
1362            timestamp: 2000,
1363        };
1364
1365        analyzer.analyze_violation(&violation1, 0x1000).unwrap();
1366        analyzer.analyze_violation(&violation2, 0x2000).unwrap();
1367
1368        let reports = analyzer.get_all_reports();
1369        let invalid_free_report = reports
1370            .values()
1371            .find(|r| r.violation_type == "InvalidFree")
1372            .expect("Should have InvalidFree report");
1373
1374        assert!(
1375            !invalid_free_report.correlated_violations.is_empty(),
1376            "InvalidFree should be correlated with DoubleFree"
1377        );
1378    }
1379
1380    /// Objective: Verify technical details generation
1381    /// Invariants: Should include timestamp in technical details
1382    #[test]
1383    fn test_technical_details_generation() {
1384        use crate::core::CallStackRef;
1385
1386        let mut analyzer = SecurityViolationAnalyzer::default();
1387        let call_stack = CallStackRef::new(110, Some(1));
1388
1389        let violation = SafetyViolation::DoubleFree {
1390            first_free_stack: call_stack.clone(),
1391            second_free_stack: call_stack,
1392            timestamp: 12345,
1393        };
1394
1395        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1396
1397        let reports = analyzer.get_all_reports();
1398        let report = reports.values().next().expect("Should have report");
1399        assert!(
1400            report.technical_details.contains("12345"),
1401            "Technical details should include timestamp"
1402        );
1403        assert!(
1404            report.technical_details.contains("heap corruption"),
1405            "Technical details should mention heap corruption"
1406        );
1407    }
1408}