aprender-profile 0.29.0

Pure Rust system call tracer with source-aware correlation for Rust binaries
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! ML-based anomaly detection using Aprender (Sprint 23)
//!
//! Uses `KMeans` clustering to identify anomalous syscall patterns.

use aprender::cluster::KMeans;
use aprender::primitives::Matrix;
use aprender::traits::UnsupervisedEstimator;
use std::collections::HashMap;

/// Result of ML anomaly analysis
#[derive(Debug, Clone)]
pub struct MlAnomalyReport {
    /// Silhouette score for clustering quality (-1 to 1, higher is better)
    pub silhouette_score: f64,
    /// Number of clusters used
    pub num_clusters: usize,
    /// Cluster assignments for each syscall type
    pub cluster_assignments: HashMap<String, usize>,
    /// Cluster centroids (mean time per cluster)
    pub cluster_centers: Vec<f64>,
    /// Anomalous syscalls (in outlier clusters)
    pub anomalies: Vec<MlAnomaly>,
    /// Total samples analyzed
    pub total_samples: usize,
}

/// A detected ML anomaly
#[derive(Debug, Clone)]
pub struct MlAnomaly {
    /// Syscall name
    pub syscall: String,
    /// Average time in microseconds
    pub avg_time_us: f64,
    /// Cluster assignment
    pub cluster: usize,
    /// Distance from cluster center
    pub distance: f64,
}

/// ML Anomaly Analyzer using Aprender `KMeans`
pub struct MlAnomalyAnalyzer {
    num_clusters: usize,
}

impl MlAnomalyAnalyzer {
    /// Create a new ML anomaly analyzer
    pub fn new(num_clusters: usize) -> Self {
        Self { num_clusters }
    }

    /// Analyze syscall data and produce ML anomaly report
    pub fn analyze(&self, syscall_data: &HashMap<String, (u64, u64)>) -> MlAnomalyReport {
        // Convert data to feature vectors: (count, total_time_us)
        let mut syscall_names: Vec<String> = Vec::new();
        let mut features_data: Vec<f32> = Vec::new();

        for (name, (count, total_time_ns)) in syscall_data {
            if *count == 0 {
                continue;
            }
            syscall_names.push(name.clone());
            let total_time_us = *total_time_ns as f64 / 1000.0;
            let avg_time = total_time_us / *count as f64;
            // Features: average time (normalized)
            features_data.push(avg_time as f32);
        }

        // Run KMeans clustering with adjusted k
        let k = self.num_clusters.min(syscall_names.len());

        // Handle insufficient data (need at least 2 for clustering)
        if k < 2 {
            return self.insufficient_data_report(syscall_names.len());
        }

        let mut kmeans = KMeans::new(k);

        // Create matrix from features (n_samples x 1 feature)
        let features = match Matrix::from_vec(syscall_names.len(), 1, features_data.clone()) {
            Ok(m) => m,
            Err(_) => return self.insufficient_data_report(syscall_names.len()),
        };

        // Fit and predict
        if kmeans.fit(&features).is_err() {
            return self.insufficient_data_report(syscall_names.len());
        }
        let labels = kmeans.predict(&features);

        // Calculate cluster centers
        let cluster_centers = self.calculate_centers_from_features(&features_data, &labels, k);

        // Build cluster assignments
        let mut cluster_assignments = HashMap::new();
        for (i, name) in syscall_names.iter().enumerate() {
            cluster_assignments.insert(name.clone(), labels[i]);
        }

        // Calculate silhouette score
        let silhouette_score = self.calculate_silhouette_from_features(&features_data, &labels, k);

        // Identify anomalies (syscalls in smallest cluster or furthest from center)
        let anomalies = self.identify_anomalies_from_features(
            &syscall_names,
            &features_data,
            &labels,
            &cluster_centers,
        );

        MlAnomalyReport {
            silhouette_score,
            num_clusters: k,
            cluster_assignments,
            cluster_centers,
            anomalies,
            total_samples: syscall_names.len(),
        }
    }

    /// Calculate cluster centers from flat features
    fn calculate_centers_from_features(
        &self,
        features: &[f32],
        labels: &[usize],
        k: usize,
    ) -> Vec<f64> {
        let mut centers = vec![0.0; k];
        let mut counts = vec![0usize; k];

        for (i, &feature) in features.iter().enumerate() {
            let cluster = labels[i];
            centers[cluster] += f64::from(feature);
            counts[cluster] += 1;
        }

        for i in 0..k {
            if counts[i] > 0 {
                centers[i] /= counts[i] as f64;
            }
        }

        centers
    }

    /// Calculate simplified silhouette score from flat features
    fn calculate_silhouette_from_features(
        &self,
        features: &[f32],
        labels: &[usize],
        k: usize,
    ) -> f64 {
        if features.len() <= k || k <= 1 {
            return 0.0;
        }

        let mut total_score = 0.0;
        let n = features.len();

        for i in 0..n {
            let cluster_i = labels[i];

            // Calculate a(i): average distance to points in same cluster
            let mut same_cluster_dist = 0.0;
            let mut same_count = 0;

            for j in 0..n {
                if i != j && labels[j] == cluster_i {
                    same_cluster_dist += f64::from((features[i] - features[j]).abs());
                    same_count += 1;
                }
            }

            let a_i = if same_count > 0 { same_cluster_dist / f64::from(same_count) } else { 0.0 };

            // Calculate b(i): minimum average distance to other clusters
            let mut min_other_dist = f64::MAX;

            for c in 0..k {
                if c == cluster_i {
                    continue;
                }

                let mut other_dist = 0.0;
                let mut other_count = 0;

                for j in 0..n {
                    if labels[j] == c {
                        other_dist += f64::from((features[i] - features[j]).abs());
                        other_count += 1;
                    }
                }

                if other_count > 0 {
                    let avg_dist = other_dist / f64::from(other_count);
                    if avg_dist < min_other_dist {
                        min_other_dist = avg_dist;
                    }
                }
            }

            let b_i = if min_other_dist == f64::MAX { 0.0 } else { min_other_dist };

            // Silhouette coefficient for point i
            let max_ab = a_i.max(b_i);
            let s_i = if max_ab > 0.0 { (b_i - a_i) / max_ab } else { 0.0 };

            total_score += s_i;
        }

        total_score / n as f64
    }

    /// Identify anomalous syscalls from flat features
    fn identify_anomalies_from_features(
        &self,
        names: &[String],
        features: &[f32],
        labels: &[usize],
        centers: &[f64],
    ) -> Vec<MlAnomaly> {
        let mut anomalies = Vec::new();

        // Find the cluster with highest average (potential anomalies)
        let max_center = centers.iter().copied().fold(0.0, f64::max);
        let anomaly_threshold = max_center * 0.5; // Syscalls in clusters > 50% of max center

        for (i, name) in names.iter().enumerate() {
            let cluster = labels[i];
            let center = centers[cluster];
            let feature_val = f64::from(features[i]);
            let distance = (feature_val - center).abs();

            // Mark as anomaly if in high-latency cluster
            if center > anomaly_threshold && center == max_center {
                anomalies.push(MlAnomaly {
                    syscall: name.clone(),
                    avg_time_us: feature_val,
                    cluster,
                    distance,
                });
            }
        }

        // Sort by average time descending (handle NaN gracefully)
        anomalies.sort_by(|a, b| {
            b.avg_time_us.partial_cmp(&a.avg_time_us).unwrap_or(std::cmp::Ordering::Equal)
        });

        anomalies
    }

    /// Create report for insufficient data
    fn insufficient_data_report(&self, sample_count: usize) -> MlAnomalyReport {
        MlAnomalyReport {
            silhouette_score: 0.0,
            num_clusters: 0,
            cluster_assignments: HashMap::new(),
            cluster_centers: Vec::new(),
            anomalies: Vec::new(),
            total_samples: sample_count,
        }
    }
}

impl MlAnomalyReport {
    /// Format the report for display
    pub fn format(&self) -> String {
        let mut output = String::new();

        output.push_str("\n=== ML Anomaly Detection Report ===\n");

        if self.num_clusters == 0 {
            output.push_str("Insufficient data for ML analysis\n");
            output.push_str(&format!(
                "(Need at least 3 syscall types, found {})\n",
                self.total_samples
            ));
            return output;
        }

        output.push_str(&format!("Clusters: {}\n", self.num_clusters));
        output.push_str(&format!("Samples: {}\n", self.total_samples));
        output.push_str(&format!("Silhouette Score: {:.3}\n", self.silhouette_score));

        // Cluster centers
        output.push_str("\nCluster Centers (avg time in \u{03bc}s):\n");
        for (i, center) in self.cluster_centers.iter().enumerate() {
            output.push_str(&format!("  Cluster {i}: {center:.2} \u{03bc}s\n"));
        }

        // Anomalies
        if self.anomalies.is_empty() {
            output.push_str("\nNo anomalies detected.\n");
        } else {
            output.push_str(&format!("\nAnomalies Detected: {}\n", self.anomalies.len()));
            for anomaly in &self.anomalies {
                output.push_str(&format!(
                    "  - {} (cluster {}): {:.2} \u{03bc}s (distance: {:.2})\n",
                    anomaly.syscall, anomaly.cluster, anomaly.avg_time_us, anomaly.distance
                ));
            }
        }

        output
    }

    /// Format comparison with z-score results
    pub fn format_comparison(&self, zscore_anomalies: &[(String, f64)]) -> String {
        let mut output = self.format();

        output.push_str("\n=== ML vs Z-Score Comparison ===\n");

        let ml_set: std::collections::HashSet<_> =
            self.anomalies.iter().map(|a| &a.syscall).collect();
        let zscore_set: std::collections::HashSet<_> =
            zscore_anomalies.iter().map(|(name, _)| name).collect();

        // Common anomalies
        let common: Vec<_> = ml_set.intersection(&zscore_set).collect();
        output.push_str(&format!("Common anomalies: {}\n", common.len()));

        // ML-only anomalies
        let ml_only: Vec<_> = ml_set.difference(&zscore_set).collect();
        if !ml_only.is_empty() {
            output.push_str(&format!("ML-only anomalies: {ml_only:?}\n"));
        }

        // Z-score-only anomalies
        let zscore_only: Vec<_> = zscore_set.difference(&ml_set).collect();
        if !zscore_only.is_empty() {
            output.push_str(&format!("Z-score-only anomalies: {zscore_only:?}\n"));
        }

        output
    }
}

// Compile-time thread-safety verification (Sprint 59)
static_assertions::assert_impl_all!(MlAnomalyReport: Send, Sync);
static_assertions::assert_impl_all!(MlAnomaly: Send, Sync);
static_assertions::assert_impl_all!(MlAnomalyAnalyzer: Send, Sync);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ml_analyzer_creation() {
        let analyzer = MlAnomalyAnalyzer::new(3);
        assert_eq!(analyzer.num_clusters, 3);
    }

    #[test]
    fn test_analyze_empty_data() {
        let analyzer = MlAnomalyAnalyzer::new(3);
        let data = HashMap::new();
        let report = analyzer.analyze(&data);
        assert_eq!(report.total_samples, 0);
        assert_eq!(report.num_clusters, 0);
    }

    #[test]
    fn test_analyze_insufficient_data() {
        let analyzer = MlAnomalyAnalyzer::new(3);
        let mut data = HashMap::new();
        data.insert("write".to_string(), (10, 1000));
        data.insert("read".to_string(), (5, 500));

        let report = analyzer.analyze(&data);
        assert_eq!(report.num_clusters, 2); // k = min(3, 2)
    }

    #[test]
    fn test_analyze_with_sufficient_data() {
        let analyzer = MlAnomalyAnalyzer::new(3);
        let mut data = HashMap::new();
        data.insert("write".to_string(), (100, 100000)); // 1 us avg
        data.insert("read".to_string(), (50, 50000)); // 1 us avg
        data.insert("openat".to_string(), (20, 200000)); // 10 us avg
        data.insert("close".to_string(), (80, 80000)); // 1 us avg
        data.insert("mmap".to_string(), (10, 1000000)); // 100 us avg (anomaly)

        let report = analyzer.analyze(&data);
        assert_eq!(report.num_clusters, 3);
        assert!(report.silhouette_score >= -1.0 && report.silhouette_score <= 1.0);
    }

    #[test]
    fn test_report_format() {
        let report = MlAnomalyReport {
            silhouette_score: 0.75,
            num_clusters: 3,
            cluster_assignments: HashMap::new(),
            cluster_centers: vec![1.0, 10.0, 100.0],
            anomalies: vec![],
            total_samples: 5,
        };

        let formatted = report.format();
        assert!(formatted.contains("ML Anomaly Detection"));
        assert!(formatted.contains("Silhouette Score: 0.750"));
        assert!(formatted.contains("Clusters: 3"));
    }

    #[test]
    fn test_calculate_centers() {
        let analyzer = MlAnomalyAnalyzer::new(2);
        let features = vec![1.0_f32, 2.0, 10.0, 11.0];
        let labels = vec![0, 0, 1, 1];

        let centers = analyzer.calculate_centers_from_features(&features, &labels, 2);
        assert_eq!(centers.len(), 2);
        assert!((centers[0] - 1.5).abs() < 0.01); // (1+2)/2
        assert!((centers[1] - 10.5).abs() < 0.01); // (10+11)/2
    }

    #[test]
    fn test_silhouette_calculation() {
        let analyzer = MlAnomalyAnalyzer::new(2);
        let features = vec![1.0_f32, 2.0, 100.0, 101.0];
        let labels = vec![0, 0, 1, 1];

        let score = analyzer.calculate_silhouette_from_features(&features, &labels, 2);
        // Well-separated clusters should have high silhouette
        assert!(score > 0.8);
    }

    #[test]
    fn test_comparison_format() {
        let report = MlAnomalyReport {
            silhouette_score: 0.75,
            num_clusters: 3,
            cluster_assignments: HashMap::new(),
            cluster_centers: vec![1.0, 10.0, 100.0],
            anomalies: vec![MlAnomaly {
                syscall: "mmap".to_string(),
                avg_time_us: 100.0,
                cluster: 2,
                distance: 0.0,
            }],
            total_samples: 5,
        };

        let zscore_anomalies = vec![("mmap".to_string(), 4.5)];
        let formatted = report.format_comparison(&zscore_anomalies);
        assert!(formatted.contains("Comparison"));
        assert!(formatted.contains("Common anomalies: 1"));
    }
}