Skip to main content

memscope_rs/analysis/safety/
types.rs

1use crate::analysis::unsafe_ffi_tracker::{RiskLevel, StackFrame};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
6pub enum RiskFactorType {
7    RawPointerDereference,
8    UnsafeDataRace,
9    InvalidTransmute,
10    FfiCall,
11    ManualMemoryManagement,
12    CrossBoundaryTransfer,
13    UseAfterFree,
14    BufferOverflow,
15    LifetimeViolation,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct RiskFactor {
20    pub factor_type: RiskFactorType,
21    pub severity: f64,
22    pub confidence: f64,
23    pub description: String,
24    pub source_location: Option<String>,
25    pub call_stack: Vec<StackFrame>,
26    pub mitigation: String,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct RiskAssessment {
31    pub risk_level: RiskLevel,
32    pub risk_score: f64,
33    pub risk_factors: Vec<RiskFactor>,
34    pub confidence_score: f64,
35    pub mitigation_suggestions: Vec<String>,
36    pub assessment_timestamp: u64,
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct UnsafeReport {
41    pub report_id: String,
42    pub source: UnsafeSource,
43    pub risk_assessment: RiskAssessment,
44    pub dynamic_violations: Vec<DynamicViolation>,
45    pub related_passports: Vec<String>,
46    pub memory_context: MemoryContext,
47    pub generated_at: u64,
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub enum UnsafeSource {
52    UnsafeBlock {
53        location: String,
54        function: String,
55        file_path: Option<String>,
56        line_number: Option<u32>,
57    },
58    FfiFunction {
59        library: String,
60        function: String,
61        call_site: String,
62    },
63    RawPointer {
64        operation: String,
65        location: String,
66    },
67    Transmute {
68        from_type: String,
69        to_type: String,
70        location: String,
71    },
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct DynamicViolation {
76    pub violation_type: ViolationType,
77    pub memory_address: usize,
78    pub memory_size: usize,
79    pub detected_at: u64,
80    pub call_stack: Vec<StackFrame>,
81    pub severity: RiskLevel,
82    pub context: String,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub enum ViolationType {
87    DoubleFree,
88    UseAfterFree,
89    BufferOverflow,
90    InvalidAccess,
91    DataRace,
92    FfiBoundaryViolation,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct MemoryContext {
97    pub total_allocated: usize,
98    pub active_allocations: usize,
99    pub memory_pressure: MemoryPressureLevel,
100    pub allocation_patterns: Vec<AllocationPattern>,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub enum MemoryPressureLevel {
105    Low,
106    Medium,
107    High,
108    Critical,
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct AllocationPattern {
113    pub pattern_type: String,
114    pub frequency: u32,
115    pub average_size: usize,
116    pub risk_level: RiskLevel,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct MemoryPassport {
121    pub passport_id: String,
122    pub allocation_ptr: usize,
123    pub size_bytes: usize,
124    pub status_at_shutdown: PassportStatus,
125    pub lifecycle_events: Vec<PassportEvent>,
126    pub risk_assessment: RiskAssessment,
127    pub created_at: u64,
128    pub updated_at: u64,
129}
130
131#[derive(Debug, Clone, Serialize, Deserialize)]
132pub enum PassportStatus {
133    FreedByRust,
134    HandoverToFfi,
135    FreedByForeign,
136    ReclaimedByRust,
137    InForeignCustody,
138    Unknown,
139}
140
141#[derive(Debug, Clone, Serialize, Deserialize)]
142pub struct PassportEvent {
143    pub event_type: PassportEventType,
144    pub timestamp: u64,
145    pub context: String,
146    pub call_stack: Vec<StackFrame>,
147    pub metadata: HashMap<String, String>,
148}
149
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub enum PassportEventType {
152    AllocatedInRust,
153    HandoverToFfi,
154    FreedByForeign,
155    ReclaimedByRust,
156    BoundaryAccess,
157    OwnershipTransfer,
158}