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            module_path: None,
510            borrow_info: None,
511            clone_info: None,
512            ownership_history_available: false,
513            smart_pointer_info: None,
514            memory_layout: None,
515            generic_info: None,
516            dynamic_type_info: None,
517            runtime_state: None,
518            stack_allocation: None,
519            temporary_object: None,
520            fragmentation_analysis: None,
521            generic_instantiation: None,
522            type_relationships: None,
523            type_usage: None,
524            function_call_tracking: None,
525            lifecycle_tracking: None,
526            access_tracking: None,
527            drop_chain_analysis: None,
528            stack_ptr: None,
529            task_id: None,
530        }
531    }
532
533    /// Objective: Verify SecurityViolationAnalyzer creation with default config
534    /// Invariants: Default config should have correlation analysis enabled
535    #[test]
536    fn test_security_analyzer_default() {
537        let analyzer = SecurityViolationAnalyzer::default();
538        let reports = analyzer.get_all_reports();
539        assert!(reports.is_empty(), "New analyzer should have no reports");
540    }
541
542    /// Objective: Verify SecurityViolationAnalyzer creation with custom config
543    /// Invariants: Custom config values should be respected
544    #[test]
545    fn test_security_analyzer_custom_config() {
546        let config = AnalysisConfig {
547            max_related_allocations: 5,
548            max_stack_depth: 10,
549            enable_correlation_analysis: false,
550            include_low_severity: false,
551            generate_integrity_hashes: false,
552        };
553        let analyzer = SecurityViolationAnalyzer::new(config);
554        let reports = analyzer.get_all_reports();
555        assert!(
556            reports.is_empty(),
557            "Custom config analyzer should start empty"
558        );
559    }
560
561    /// Objective: Verify generate_security_summary with no violations
562    /// Invariants: Should produce valid JSON summary with zero violations
563    #[test]
564    fn test_generate_security_summary_empty() {
565        let analyzer = SecurityViolationAnalyzer::default();
566        let summary = analyzer.generate_security_summary();
567        assert!(summary.is_object(), "Summary should be a JSON object");
568
569        let obj = summary.as_object().unwrap();
570        assert!(
571            obj.contains_key("security_analysis_summary"),
572            "Should have analysis summary"
573        );
574    }
575
576    /// Objective: Verify clear_reports functionality
577    /// Invariants: Should work on empty analyzer
578    #[test]
579    fn test_clear_reports_empty() {
580        let mut analyzer = SecurityViolationAnalyzer::default();
581        analyzer.clear_reports();
582        assert!(
583            analyzer.get_all_reports().is_empty(),
584            "Should have no reports after clear"
585        );
586    }
587
588    /// Objective: Verify get_reports_by_severity with no reports
589    /// Invariants: Should return empty vector
590    #[test]
591    fn test_get_reports_by_severity_empty() {
592        let analyzer = SecurityViolationAnalyzer::default();
593        let reports = analyzer.get_reports_by_severity(ViolationSeverity::Critical);
594        assert!(reports.is_empty(), "Should have no reports");
595    }
596
597    /// Objective: Verify update_allocations functionality
598    /// Invariants: Should accept empty vector
599    #[test]
600    fn test_update_allocations_empty() {
601        let mut analyzer = SecurityViolationAnalyzer::default();
602        analyzer.update_allocations(vec![]);
603        let summary = analyzer.generate_security_summary();
604        assert!(summary.is_object(), "Summary should still be valid JSON");
605    }
606
607    /// Objective: Verify ViolationSeverity score values
608    /// Invariants: Critical should be highest, Info should be lowest
609    #[test]
610    fn test_violation_severity_scores() {
611        assert_eq!(
612            ViolationSeverity::Critical.score(),
613            100,
614            "Critical score should be 100"
615        );
616        assert_eq!(
617            ViolationSeverity::High.score(),
618            75,
619            "High score should be 75"
620        );
621        assert_eq!(
622            ViolationSeverity::Medium.score(),
623            50,
624            "Medium score should be 50"
625        );
626        assert_eq!(ViolationSeverity::Low.score(), 25, "Low score should be 25");
627        assert_eq!(
628            ViolationSeverity::Info.score(),
629            10,
630            "Info score should be 10"
631        );
632    }
633
634    /// Objective: Verify AnalysisConfig default values
635    /// Invariants: Default should have sensible values
636    #[test]
637    fn test_analysis_config_default() {
638        let config = AnalysisConfig::default();
639        assert_eq!(
640            config.max_related_allocations, 10,
641            "Default max related should be 10"
642        );
643        assert_eq!(
644            config.max_stack_depth, 20,
645            "Default max stack depth should be 20"
646        );
647        assert!(
648            config.enable_correlation_analysis,
649            "Correlation should be enabled by default"
650        );
651        assert!(
652            config.include_low_severity,
653            "Low severity should be included by default"
654        );
655        assert!(
656            config.generate_integrity_hashes,
657            "Integrity hashes should be enabled by default"
658        );
659    }
660
661    /// Objective: Verify analyze_violation with DoubleFree
662    /// Invariants: Should create report with Critical severity
663    #[test]
664    fn test_analyze_violation_double_free() {
665        use crate::core::CallStackRef;
666
667        let mut analyzer = SecurityViolationAnalyzer::default();
668        let call_stack = CallStackRef::new(1, Some(1));
669
670        let violation = SafetyViolation::DoubleFree {
671            first_free_stack: call_stack.clone(),
672            second_free_stack: call_stack,
673            timestamp: 1000,
674        };
675
676        let result = analyzer.analyze_violation(&violation, 0x1000);
677        assert!(result.is_ok(), "Should analyze DoubleFree successfully");
678
679        let violation_id = result.unwrap();
680        assert!(
681            violation_id.starts_with("SEC-DF"),
682            "Violation ID should start with SEC-DF"
683        );
684
685        let reports = analyzer.get_all_reports();
686        let report = reports.get(&violation_id).expect("Report should exist");
687        assert_eq!(
688            report.severity,
689            ViolationSeverity::Critical,
690            "DoubleFree should be Critical"
691        );
692        assert!(
693            report.description.contains("Double free"),
694            "Description should mention double free"
695        );
696    }
697
698    /// Objective: Verify analyze_violation with InvalidFree
699    /// Invariants: Should create report with High severity
700    #[test]
701    fn test_analyze_violation_invalid_free() {
702        use crate::core::CallStackRef;
703
704        let mut analyzer = SecurityViolationAnalyzer::default();
705        let call_stack = CallStackRef::new(2, Some(1));
706
707        let violation = SafetyViolation::InvalidFree {
708            attempted_pointer: 0x2000,
709            stack: call_stack,
710            timestamp: 2000,
711        };
712
713        let result = analyzer.analyze_violation(&violation, 0x2000);
714        assert!(result.is_ok(), "Should analyze InvalidFree successfully");
715
716        let violation_id = result.unwrap();
717        assert!(
718            violation_id.starts_with("SEC-IF"),
719            "Violation ID should start with SEC-IF"
720        );
721
722        let reports = analyzer.get_all_reports();
723        let report = reports.get(&violation_id).expect("Report should exist");
724        assert_eq!(
725            report.severity,
726            ViolationSeverity::High,
727            "InvalidFree should be High"
728        );
729    }
730
731    /// Objective: Verify analyze_violation with PotentialLeak
732    /// Invariants: Should create report with Medium severity
733    #[test]
734    fn test_analyze_violation_potential_leak() {
735        use crate::core::CallStackRef;
736
737        let mut analyzer = SecurityViolationAnalyzer::default();
738        let call_stack = CallStackRef::new(3, Some(1));
739
740        let violation = SafetyViolation::PotentialLeak {
741            allocation_stack: call_stack,
742            allocation_timestamp: 1000,
743            leak_detection_timestamp: 5000,
744        };
745
746        let result = analyzer.analyze_violation(&violation, 0x3000);
747        assert!(result.is_ok(), "Should analyze PotentialLeak successfully");
748
749        let violation_id = result.unwrap();
750        assert!(
751            violation_id.starts_with("SEC-PL"),
752            "Violation ID should start with SEC-PL"
753        );
754
755        let reports = analyzer.get_all_reports();
756        let report = reports.get(&violation_id).expect("Report should exist");
757        assert_eq!(
758            report.severity,
759            ViolationSeverity::Medium,
760            "PotentialLeak should be Medium"
761        );
762    }
763
764    /// Objective: Verify analyze_violation with CrossBoundaryRisk
765    /// Invariants: Should create report with Medium severity
766    #[test]
767    fn test_analyze_violation_cross_boundary() {
768        use crate::core::CallStackRef;
769
770        let mut analyzer = SecurityViolationAnalyzer::default();
771        let call_stack = CallStackRef::new(4, Some(1));
772
773        let violation = SafetyViolation::CrossBoundaryRisk {
774            risk_level: crate::analysis::unsafe_ffi_tracker::RiskLevel::High,
775            description: "FFI boundary violation".to_string(),
776            stack: call_stack,
777        };
778
779        let result = analyzer.analyze_violation(&violation, 0x4000);
780        assert!(
781            result.is_ok(),
782            "Should analyze CrossBoundaryRisk successfully"
783        );
784
785        let violation_id = result.unwrap();
786        assert!(
787            violation_id.starts_with("SEC-CBR"),
788            "Violation ID should start with SEC-CBR"
789        );
790
791        let reports = analyzer.get_all_reports();
792        let report = reports.get(&violation_id).expect("Report should exist");
793        assert_eq!(
794            report.severity,
795            ViolationSeverity::Medium,
796            "CrossBoundaryRisk should be Medium"
797        );
798    }
799
800    /// Objective: Verify analyze_violation with correlation analysis disabled
801    /// Invariants: Should not find correlated violations when disabled
802    #[test]
803    fn test_analyze_violation_correlation_disabled() {
804        use crate::core::CallStackRef;
805
806        let config = AnalysisConfig {
807            enable_correlation_analysis: false,
808            ..Default::default()
809        };
810        let mut analyzer = SecurityViolationAnalyzer::new(config);
811        let call_stack = CallStackRef::new(5, Some(1));
812
813        let violation = SafetyViolation::DoubleFree {
814            first_free_stack: call_stack.clone(),
815            second_free_stack: call_stack,
816            timestamp: 1000,
817        };
818
819        let result = analyzer.analyze_violation(&violation, 0x1000);
820        assert!(result.is_ok(), "Should analyze successfully");
821
822        let reports = analyzer.get_all_reports();
823        let report = reports.values().next().expect("Should have report");
824        assert!(
825            report.correlated_violations.is_empty(),
826            "Should have no correlated violations when disabled"
827        );
828    }
829
830    /// Objective: Verify analyze_violation with integrity hash disabled
831    /// Invariants: Should not generate hash when disabled
832    #[test]
833    fn test_analyze_violation_integrity_hash_disabled() {
834        use crate::core::CallStackRef;
835
836        let config = AnalysisConfig {
837            generate_integrity_hashes: false,
838            ..Default::default()
839        };
840        let mut analyzer = SecurityViolationAnalyzer::new(config);
841        let call_stack = CallStackRef::new(6, Some(1));
842
843        let violation = SafetyViolation::PotentialLeak {
844            allocation_stack: call_stack,
845            allocation_timestamp: 1000,
846            leak_detection_timestamp: 5000,
847        };
848
849        let result = analyzer.analyze_violation(&violation, 0x3000);
850        assert!(result.is_ok(), "Should analyze successfully");
851
852        let reports = analyzer.get_all_reports();
853        let report = reports.values().next().expect("Should have report");
854        assert!(
855            report.integrity_hash.is_empty(),
856            "Hash should be empty when disabled"
857        );
858    }
859
860    /// Objective: Verify get_reports_by_severity filters correctly
861    /// Invariants: Should return only reports with severity >= min_severity
862    #[test]
863    fn test_get_reports_by_severity_filtering() {
864        use crate::core::CallStackRef;
865
866        let mut analyzer = SecurityViolationAnalyzer::default();
867        let call_stack = CallStackRef::new(10, Some(1));
868
869        let violations = vec![
870            (
871                SafetyViolation::DoubleFree {
872                    first_free_stack: call_stack.clone(),
873                    second_free_stack: call_stack.clone(),
874                    timestamp: 1000,
875                },
876                0x1000,
877            ),
878            (
879                SafetyViolation::InvalidFree {
880                    attempted_pointer: 0x2000,
881                    stack: call_stack.clone(),
882                    timestamp: 2000,
883                },
884                0x2000,
885            ),
886            (
887                SafetyViolation::PotentialLeak {
888                    allocation_stack: call_stack,
889                    allocation_timestamp: 1000,
890                    leak_detection_timestamp: 5000,
891                },
892                0x3000,
893            ),
894        ];
895
896        for (violation, addr) in violations {
897            analyzer.analyze_violation(&violation, addr).unwrap();
898        }
899
900        let critical_reports = analyzer.get_reports_by_severity(ViolationSeverity::Critical);
901        assert_eq!(critical_reports.len(), 1, "Should have 1 Critical report");
902
903        let high_reports = analyzer.get_reports_by_severity(ViolationSeverity::High);
904        assert_eq!(high_reports.len(), 2, "Should have 2 High+ reports");
905
906        let medium_reports = analyzer.get_reports_by_severity(ViolationSeverity::Medium);
907        assert_eq!(medium_reports.len(), 3, "Should have 3 Medium+ reports");
908    }
909
910    /// Objective: Verify verify_report_integrity with valid hash
911    /// Invariants: Should return true for valid integrity hash
912    #[test]
913    fn test_verify_report_integrity_valid() {
914        use crate::core::CallStackRef;
915
916        let mut analyzer = SecurityViolationAnalyzer::default();
917        let call_stack = CallStackRef::new(20, Some(1));
918
919        let violation = SafetyViolation::DoubleFree {
920            first_free_stack: call_stack.clone(),
921            second_free_stack: call_stack,
922            timestamp: 1000,
923        };
924
925        let violation_id = analyzer.analyze_violation(&violation, 0x1000).unwrap();
926        let reports = analyzer.get_all_reports();
927        let report = reports.get(&violation_id).expect("Report should exist");
928
929        let result = analyzer.verify_report_integrity(report);
930        assert!(
931            result.is_ok() && result.unwrap(),
932            "Integrity verification should succeed"
933        );
934    }
935
936    /// Objective: Verify verify_report_integrity when disabled
937    /// Invariants: Should return true when integrity hashes disabled
938    #[test]
939    fn test_verify_report_integrity_disabled() {
940        let config = AnalysisConfig {
941            generate_integrity_hashes: false,
942            ..Default::default()
943        };
944        let analyzer = SecurityViolationAnalyzer::new(config);
945
946        let report = SecurityViolationReport {
947            violation_id: "test".to_string(),
948            violation_type: "Test".to_string(),
949            severity: ViolationSeverity::Low,
950            description: "test".to_string(),
951            technical_details: "test".to_string(),
952            memory_snapshot: MemoryStateSnapshot {
953                timestamp_ns: 0,
954                total_allocated_bytes: 0,
955                active_allocation_count: 0,
956                involved_addresses: vec![],
957                stack_trace: vec![],
958                related_allocations: vec![],
959                memory_pressure: MemoryPressureLevel::Low,
960            },
961            impact_assessment: ImpactAssessment {
962                exploitability_score: 0.0,
963                data_corruption_risk: false,
964                information_disclosure_risk: false,
965                denial_of_service_risk: false,
966                code_execution_risk: false,
967                overall_risk_score: 0.0,
968            },
969            remediation_suggestions: vec![],
970            correlated_violations: vec![],
971            integrity_hash: String::new(),
972            generated_at_ns: 0,
973        };
974
975        let result = analyzer.verify_report_integrity(&report);
976        assert!(
977            result.is_ok() && result.unwrap(),
978            "Should return true when integrity hashes disabled"
979        );
980    }
981
982    /// Objective: Verify generate_security_summary with violations
983    /// Invariants: Should include violation counts and risk assessment
984    #[test]
985    fn test_generate_security_summary_with_violations() {
986        use crate::core::CallStackRef;
987
988        let mut analyzer = SecurityViolationAnalyzer::default();
989        let call_stack = CallStackRef::new(30, Some(1));
990
991        let violation = SafetyViolation::DoubleFree {
992            first_free_stack: call_stack.clone(),
993            second_free_stack: call_stack,
994            timestamp: 1000,
995        };
996
997        analyzer.analyze_violation(&violation, 0x1000).unwrap();
998
999        let summary = analyzer.generate_security_summary();
1000        let obj = summary.as_object().unwrap();
1001        let analysis = obj.get("security_analysis_summary").unwrap();
1002
1003        assert_eq!(
1004            analysis.get("total_violations").unwrap().as_u64().unwrap(),
1005            1,
1006            "Should have 1 violation"
1007        );
1008
1009        let severity = analysis.get("severity_breakdown").unwrap();
1010        assert_eq!(
1011            severity.get("critical").unwrap().as_u64().unwrap(),
1012            1,
1013            "Should have 1 critical"
1014        );
1015    }
1016
1017    /// Objective: Verify clear_reports removes all data
1018    /// Invariants: Should clear both reports and correlation matrix
1019    #[test]
1020    fn test_clear_reports_with_data() {
1021        use crate::core::CallStackRef;
1022
1023        let mut analyzer = SecurityViolationAnalyzer::default();
1024        let call_stack = CallStackRef::new(40, Some(1));
1025
1026        let violation = SafetyViolation::DoubleFree {
1027            first_free_stack: call_stack.clone(),
1028            second_free_stack: call_stack,
1029            timestamp: 1000,
1030        };
1031
1032        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1033        assert!(
1034            !analyzer.get_all_reports().is_empty(),
1035            "Should have reports"
1036        );
1037
1038        analyzer.clear_reports();
1039        assert!(
1040            analyzer.get_all_reports().is_empty(),
1041            "Should have no reports after clear"
1042        );
1043    }
1044
1045    /// Objective: Verify update_allocations affects memory snapshot
1046    /// Invariants: Memory snapshot should reflect active allocations
1047    #[test]
1048    fn test_update_allocations_affects_snapshot() {
1049        use crate::core::CallStackRef;
1050
1051        let mut analyzer = SecurityViolationAnalyzer::default();
1052
1053        let allocations = vec![create_test_allocation(0x1000, 1024)];
1054        analyzer.update_allocations(allocations);
1055
1056        let call_stack = CallStackRef::new(50, Some(1));
1057        let violation = SafetyViolation::DoubleFree {
1058            first_free_stack: call_stack.clone(),
1059            second_free_stack: call_stack,
1060            timestamp: 1000,
1061        };
1062
1063        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1064
1065        let reports = analyzer.get_all_reports();
1066        let report = reports.values().next().expect("Should have report");
1067        assert_eq!(
1068            report.memory_snapshot.total_allocated_bytes, 1024,
1069            "Should reflect allocation size"
1070        );
1071        assert_eq!(
1072            report.memory_snapshot.active_allocation_count, 1,
1073            "Should have 1 active allocation"
1074        );
1075    }
1076
1077    /// Objective: Verify memory pressure assessment - Critical
1078    /// Invariants: > 2GB should be Critical
1079    #[test]
1080    fn test_memory_pressure_critical() {
1081        use crate::core::CallStackRef;
1082
1083        let mut analyzer = SecurityViolationAnalyzer::default();
1084
1085        let allocations = vec![create_test_allocation(0x1000, 3 * 1024 * 1024 * 1024)];
1086        analyzer.update_allocations(allocations);
1087
1088        let call_stack = CallStackRef::new(60, Some(1));
1089        let violation = SafetyViolation::PotentialLeak {
1090            allocation_stack: call_stack,
1091            allocation_timestamp: 1000,
1092            leak_detection_timestamp: 5000,
1093        };
1094
1095        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1096
1097        let reports = analyzer.get_all_reports();
1098        let report = reports.values().next().expect("Should have report");
1099        assert_eq!(
1100            report.memory_snapshot.memory_pressure,
1101            MemoryPressureLevel::Critical,
1102            "> 2GB should be Critical"
1103        );
1104    }
1105
1106    /// Objective: Verify memory pressure assessment - High
1107    /// Invariants: > 1GB should be High
1108    #[test]
1109    fn test_memory_pressure_high() {
1110        use crate::core::CallStackRef;
1111
1112        let mut analyzer = SecurityViolationAnalyzer::default();
1113
1114        let allocations = vec![create_test_allocation(0x1000, 1500 * 1024 * 1024)];
1115        analyzer.update_allocations(allocations);
1116
1117        let call_stack = CallStackRef::new(61, Some(1));
1118        let violation = SafetyViolation::PotentialLeak {
1119            allocation_stack: call_stack,
1120            allocation_timestamp: 1000,
1121            leak_detection_timestamp: 5000,
1122        };
1123
1124        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1125
1126        let reports = analyzer.get_all_reports();
1127        let report = reports.values().next().expect("Should have report");
1128        assert_eq!(
1129            report.memory_snapshot.memory_pressure,
1130            MemoryPressureLevel::High,
1131            "> 1GB should be High"
1132        );
1133    }
1134
1135    /// Objective: Verify memory pressure assessment - Medium
1136    /// Invariants: > 512MB should be Medium
1137    #[test]
1138    fn test_memory_pressure_medium() {
1139        use crate::core::CallStackRef;
1140
1141        let mut analyzer = SecurityViolationAnalyzer::default();
1142
1143        let allocations = vec![create_test_allocation(0x1000, 750 * 1024 * 1024)];
1144        analyzer.update_allocations(allocations);
1145
1146        let call_stack = CallStackRef::new(62, Some(1));
1147        let violation = SafetyViolation::PotentialLeak {
1148            allocation_stack: call_stack,
1149            allocation_timestamp: 1000,
1150            leak_detection_timestamp: 5000,
1151        };
1152
1153        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1154
1155        let reports = analyzer.get_all_reports();
1156        let report = reports.values().next().expect("Should have report");
1157        assert_eq!(
1158            report.memory_snapshot.memory_pressure,
1159            MemoryPressureLevel::Medium,
1160            "> 512MB should be Medium"
1161        );
1162    }
1163
1164    /// Objective: Verify impact assessment for DoubleFree
1165    /// Invariants: Should have code execution risk
1166    #[test]
1167    fn test_impact_assessment_double_free() {
1168        use crate::core::CallStackRef;
1169
1170        let mut analyzer = SecurityViolationAnalyzer::default();
1171        let call_stack = CallStackRef::new(70, Some(1));
1172
1173        let violation = SafetyViolation::DoubleFree {
1174            first_free_stack: call_stack.clone(),
1175            second_free_stack: call_stack,
1176            timestamp: 1000,
1177        };
1178
1179        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1180
1181        let reports = analyzer.get_all_reports();
1182        let report = reports.values().next().expect("Should have report");
1183        assert!(
1184            report.impact_assessment.code_execution_risk,
1185            "DoubleFree should have code execution risk"
1186        );
1187        assert!(
1188            report.impact_assessment.data_corruption_risk,
1189            "DoubleFree should have data corruption risk"
1190        );
1191        assert_eq!(
1192            report.impact_assessment.exploitability_score, 0.9,
1193            "DoubleFree should have 0.9 exploitability"
1194        );
1195    }
1196
1197    /// Objective: Verify impact assessment for InvalidFree
1198    /// Invariants: Should not have code execution risk
1199    #[test]
1200    fn test_impact_assessment_invalid_free() {
1201        use crate::core::CallStackRef;
1202
1203        let mut analyzer = SecurityViolationAnalyzer::default();
1204        let call_stack = CallStackRef::new(71, Some(1));
1205
1206        let violation = SafetyViolation::InvalidFree {
1207            attempted_pointer: 0x2000,
1208            stack: call_stack,
1209            timestamp: 2000,
1210        };
1211
1212        analyzer.analyze_violation(&violation, 0x2000).unwrap();
1213
1214        let reports = analyzer.get_all_reports();
1215        let report = reports.values().next().expect("Should have report");
1216        assert!(
1217            !report.impact_assessment.code_execution_risk,
1218            "InvalidFree should not have code execution risk"
1219        );
1220        assert!(
1221            report.impact_assessment.data_corruption_risk,
1222            "InvalidFree should have data corruption risk"
1223        );
1224    }
1225
1226    /// Objective: Verify impact assessment for CrossBoundaryRisk
1227    /// Invariants: Should have information disclosure risk
1228    #[test]
1229    fn test_impact_assessment_cross_boundary() {
1230        use crate::core::CallStackRef;
1231
1232        let mut analyzer = SecurityViolationAnalyzer::default();
1233        let call_stack = CallStackRef::new(72, Some(1));
1234
1235        let violation = SafetyViolation::CrossBoundaryRisk {
1236            risk_level: crate::analysis::unsafe_ffi_tracker::RiskLevel::High,
1237            description: "test".to_string(),
1238            stack: call_stack,
1239        };
1240
1241        analyzer.analyze_violation(&violation, 0x4000).unwrap();
1242
1243        let reports = analyzer.get_all_reports();
1244        let report = reports.values().next().expect("Should have report");
1245        assert!(
1246            report.impact_assessment.information_disclosure_risk,
1247            "CrossBoundaryRisk should have info disclosure risk"
1248        );
1249    }
1250
1251    /// Objective: Verify remediation suggestions for DoubleFree
1252    /// Invariants: Should include RAII and ownership tracking suggestions
1253    #[test]
1254    fn test_remediation_double_free() {
1255        use crate::core::CallStackRef;
1256
1257        let mut analyzer = SecurityViolationAnalyzer::default();
1258        let call_stack = CallStackRef::new(80, Some(1));
1259
1260        let violation = SafetyViolation::DoubleFree {
1261            first_free_stack: call_stack.clone(),
1262            second_free_stack: call_stack,
1263            timestamp: 1000,
1264        };
1265
1266        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1267
1268        let reports = analyzer.get_all_reports();
1269        let report = reports.values().next().expect("Should have report");
1270        assert!(
1271            report
1272                .remediation_suggestions
1273                .iter()
1274                .any(|s| s.contains("ownership")),
1275            "Should suggest ownership tracking"
1276        );
1277        assert!(
1278            report
1279                .remediation_suggestions
1280                .iter()
1281                .any(|s| s.contains("RAII")),
1282            "Should suggest RAII"
1283        );
1284    }
1285
1286    /// Objective: Verify remediation suggestions for high risk
1287    /// Invariants: Should include URGENT prefix for high risk
1288    #[test]
1289    fn test_remediation_high_risk() {
1290        use crate::core::CallStackRef;
1291
1292        let mut analyzer = SecurityViolationAnalyzer::default();
1293
1294        let allocations = vec![create_test_allocation(0x1000, 3 * 1024 * 1024 * 1024)];
1295        analyzer.update_allocations(allocations);
1296
1297        let call_stack = CallStackRef::new(81, Some(1));
1298        let violation = SafetyViolation::DoubleFree {
1299            first_free_stack: call_stack.clone(),
1300            second_free_stack: call_stack,
1301            timestamp: 1000,
1302        };
1303
1304        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1305
1306        let reports = analyzer.get_all_reports();
1307        let report = reports.values().next().expect("Should have report");
1308
1309        if report.impact_assessment.overall_risk_score > 0.8 {
1310            assert!(
1311                report
1312                    .remediation_suggestions
1313                    .iter()
1314                    .any(|s| s.contains("URGENT")),
1315                "High risk should have URGENT prefix"
1316            );
1317        }
1318    }
1319
1320    /// Objective: Verify related allocations detection
1321    /// Invariants: Should find allocations in same region
1322    #[test]
1323    fn test_related_allocations_same_region() {
1324        use crate::core::CallStackRef;
1325
1326        let mut analyzer = SecurityViolationAnalyzer::default();
1327
1328        let allocations = vec![create_test_allocation(0x1000, 1024)];
1329        analyzer.update_allocations(allocations);
1330
1331        let call_stack = CallStackRef::new(90, Some(1));
1332        let violation = SafetyViolation::DoubleFree {
1333            first_free_stack: call_stack.clone(),
1334            second_free_stack: call_stack,
1335            timestamp: 1000,
1336        };
1337
1338        analyzer.analyze_violation(&violation, 0x1050).unwrap();
1339
1340        let reports = analyzer.get_all_reports();
1341        let report = reports.values().next().expect("Should have report");
1342        assert!(
1343            !report.memory_snapshot.related_allocations.is_empty(),
1344            "Should find related allocations"
1345        );
1346    }
1347
1348    /// Objective: Verify correlation analysis between violations
1349    /// Invariants: DoubleFree and InvalidFree should be correlated
1350    #[test]
1351    fn test_correlation_analysis() {
1352        use crate::core::CallStackRef;
1353
1354        let mut analyzer = SecurityViolationAnalyzer::default();
1355        let call_stack = CallStackRef::new(100, Some(1));
1356
1357        let violation1 = SafetyViolation::DoubleFree {
1358            first_free_stack: call_stack.clone(),
1359            second_free_stack: call_stack.clone(),
1360            timestamp: 1000,
1361        };
1362        let violation2 = SafetyViolation::InvalidFree {
1363            attempted_pointer: 0x2000,
1364            stack: call_stack,
1365            timestamp: 2000,
1366        };
1367
1368        analyzer.analyze_violation(&violation1, 0x1000).unwrap();
1369        analyzer.analyze_violation(&violation2, 0x2000).unwrap();
1370
1371        let reports = analyzer.get_all_reports();
1372        let invalid_free_report = reports
1373            .values()
1374            .find(|r| r.violation_type == "InvalidFree")
1375            .expect("Should have InvalidFree report");
1376
1377        assert!(
1378            !invalid_free_report.correlated_violations.is_empty(),
1379            "InvalidFree should be correlated with DoubleFree"
1380        );
1381    }
1382
1383    /// Objective: Verify technical details generation
1384    /// Invariants: Should include timestamp in technical details
1385    #[test]
1386    fn test_technical_details_generation() {
1387        use crate::core::CallStackRef;
1388
1389        let mut analyzer = SecurityViolationAnalyzer::default();
1390        let call_stack = CallStackRef::new(110, Some(1));
1391
1392        let violation = SafetyViolation::DoubleFree {
1393            first_free_stack: call_stack.clone(),
1394            second_free_stack: call_stack,
1395            timestamp: 12345,
1396        };
1397
1398        analyzer.analyze_violation(&violation, 0x1000).unwrap();
1399
1400        let reports = analyzer.get_all_reports();
1401        let report = reports.values().next().expect("Should have report");
1402        assert!(
1403            report.technical_details.contains("12345"),
1404            "Technical details should include timestamp"
1405        );
1406        assert!(
1407            report.technical_details.contains("heap corruption"),
1408            "Technical details should mention heap corruption"
1409        );
1410    }
1411}