Skip to main content

crucible_test_context/coverage/
types.rs

1//! Shared types for coverage analysis and visualization.
2
3use std::collections::HashSet;
4
5/// Function information extracted from BPF ELF
6#[derive(Clone, Debug, Default)]
7pub struct FunctionInfo {
8    pub name: String,
9    pub entry_pc: usize,
10}
11
12/// Result of CFG analysis including reachable sets for filtering visited coverage
13#[derive(Clone, Default)]
14pub struct ReachableAnalysis {
15    pub total_instructions: usize,
16    pub total_branches: usize,
17    pub total_edges: usize,
18    /// All instruction PCs reachable from the entry point
19    pub reachable_pcs: HashSet<usize>,
20    /// All conditional branch PCs reachable from the entry point
21    pub reachable_branch_pcs: HashSet<usize>,
22    /// All edges (pc<<32|target) reachable from the entry point
23    pub reachable_edges: HashSet<u64>,
24}
25
26/// Coverage statistics for a function or program
27#[derive(Clone, Debug, Default)]
28pub struct CoverageStats {
29    pub total_instructions: usize,
30    pub hit_instructions: usize,
31    pub total_branches: usize,
32    pub hit_branches: usize,
33    pub total_blocks: usize,
34    pub hit_blocks: usize,
35}
36
37impl CoverageStats {
38    /// Calculate instruction coverage percentage
39    pub fn instruction_coverage_pct(&self) -> f64 {
40        if self.total_instructions == 0 {
41            0.0
42        } else {
43            100.0 * self.hit_instructions as f64 / self.total_instructions as f64
44        }
45    }
46
47    /// Calculate branch coverage percentage
48    pub fn branch_coverage_pct(&self) -> f64 {
49        if self.total_branches == 0 {
50            0.0
51        } else {
52            100.0 * self.hit_branches as f64 / self.total_branches as f64
53        }
54    }
55
56    /// Calculate block coverage percentage
57    pub fn block_coverage_pct(&self) -> f64 {
58        if self.total_blocks == 0 {
59            0.0
60        } else {
61            100.0 * self.hit_blocks as f64 / self.total_blocks as f64
62        }
63    }
64}
65
66/// Runtime statistics for coverage HTML display
67#[derive(Clone, Debug, Default)]
68pub struct CoverageWriteStats {
69    pub run_time_secs: u64,
70    pub executions: u64,
71    pub edges_hit: usize,
72    pub edges_total: usize,
73    pub branches_hit: usize,
74    pub branches_total: usize,
75    pub instructions_hit: usize,
76    pub instructions_total: usize,
77}
78
79/// Cached per-function data for fast coverage HTML generation
80#[derive(Clone, Debug)]
81pub struct CachedFunctionInfo {
82    pub name: String,
83    pub entry_pc: usize,
84    pub total_instructions: usize,
85    pub total_blocks: usize,
86    /// All instruction PCs in this function (for fast hit counting)
87    pub instruction_pcs: Vec<usize>,
88    /// Block info: (node_id, Vec<instruction_pcs_in_block>)
89    pub blocks: Vec<(usize, Vec<usize>)>,
90}
91
92/// Cached program analysis for fast coverage generation (computed once at startup)
93#[derive(Clone, Debug)]
94pub struct CachedProgramAnalysis {
95    pub program_name: String,
96    /// Pre-computed function list with static stats
97    pub functions: Vec<CachedFunctionInfo>,
98    /// Pre-computed CFG JSON for each function (func_name -> JSON string)
99    pub cfg_json: std::collections::HashMap<String, String>,
100}