Skip to main content

memscope_rs/analysis/security/
types.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
4pub enum ViolationSeverity {
5    Critical,
6    High,
7    Medium,
8    Low,
9    Info,
10}
11
12impl ViolationSeverity {
13    pub fn score(&self) -> u32 {
14        match self {
15            ViolationSeverity::Critical => 100,
16            ViolationSeverity::High => 75,
17            ViolationSeverity::Medium => 50,
18            ViolationSeverity::Low => 25,
19            ViolationSeverity::Info => 10,
20        }
21    }
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct MemoryStateSnapshot {
26    pub timestamp_ns: u64,
27    pub total_allocated_bytes: usize,
28    pub active_allocation_count: usize,
29    pub involved_addresses: Vec<String>,
30    pub stack_trace: Vec<StackFrame>,
31    pub related_allocations: Vec<RelatedAllocation>,
32    pub memory_pressure: MemoryPressureLevel,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct StackFrame {
37    pub function_name: String,
38    pub file_path: Option<String>,
39    pub line_number: Option<u32>,
40    pub frame_address: String,
41    pub is_unsafe: bool,
42    pub is_ffi: bool,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct RelatedAllocation {
47    pub address: String,
48    pub size: usize,
49    pub type_name: Option<String>,
50    pub variable_name: Option<String>,
51    pub allocated_at_ns: u64,
52    pub is_active: bool,
53    pub relationship: AllocationRelationship,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub enum AllocationRelationship {
58    SameRegion,
59    Adjacent,
60    SameType,
61    SameScope,
62    DoubleFreeCandidate,
63    LeakRelated,
64    UseAfterFreeRelated,
65    None,
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub enum MemoryPressureLevel {
70    Low,
71    Medium,
72    High,
73    Critical,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct SecurityViolationReport {
78    pub violation_id: String,
79    pub violation_type: String,
80    pub severity: ViolationSeverity,
81    pub description: String,
82    pub technical_details: String,
83    pub memory_snapshot: MemoryStateSnapshot,
84    pub impact_assessment: ImpactAssessment,
85    pub remediation_suggestions: Vec<String>,
86    pub correlated_violations: Vec<String>,
87    pub integrity_hash: String,
88    pub generated_at_ns: u64,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct ImpactAssessment {
93    pub exploitability_score: f64,
94    pub data_corruption_risk: bool,
95    pub information_disclosure_risk: bool,
96    pub denial_of_service_risk: bool,
97    pub code_execution_risk: bool,
98    pub overall_risk_score: f64,
99}
100
101#[derive(Debug, Clone)]
102pub struct AnalysisConfig {
103    pub max_related_allocations: usize,
104    pub max_stack_depth: usize,
105    pub enable_correlation_analysis: bool,
106    pub include_low_severity: bool,
107    pub generate_integrity_hashes: bool,
108}
109
110impl Default for AnalysisConfig {
111    fn default() -> Self {
112        Self {
113            max_related_allocations: 10,
114            max_stack_depth: 20,
115            enable_correlation_analysis: true,
116            include_low_severity: true,
117            generate_integrity_hashes: true,
118        }
119    }
120}