synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-wire / src / probe.rs
| 1 | //! Coverage probes for the frame decoder. |
| 2 | //! |
| 3 | //! The decoder is generic over a [`Probe`]. Production code passes [`NoProbe`], whose |
| 4 | //! methods are empty and inline away, so the decoder carries no instrumentation cost. The |
| 5 | //! in-repository fuzzer ([`crate::fuzz`]) passes an [`EdgeProbe`], which records every |
| 6 | //! transition between instrumented decoder states (an AFL-style edge map: the edge index is |
| 7 | //! `prev ^ cur` over hashed site ids) and the highest input offset the decoder touched. |
| 8 | //! |
| 9 | //! A site id is the source line of the probe call (`line!()`), so every instrumented branch |
| 10 | //! of `frame.rs` is a distinct state. |
| 11 | |
| 12 | /// Receives decoder state transitions and input reads. |
| 13 | pub trait Probe { |
| 14 | /// The decoder reached instrumented state `site`. |
| 15 | fn site(&mut self, site: u32); |
| 16 | /// The decoder read input bytes up to (not including) absolute offset `end`. |
| 17 | fn touched(&mut self, end: usize); |
| 18 | } |
| 19 | |
| 20 | /// The production probe: records nothing. |
| 21 | #[derive(Debug, Default, Clone, Copy)] |
| 22 | pub struct NoProbe; |
| 23 | |
| 24 | impl Probe for NoProbe { |
| 25 | #[inline(always)] |
| 26 | fn site(&mut self, _site: u32) {} |
| 27 | #[inline(always)] |
| 28 | fn touched(&mut self, _end: usize) {} |
| 29 | } |
| 30 | |
| 31 | /// Size of the edge map (a power of two). |
| 32 | pub const EDGE_MAP_LEN: usize = 1 << 16; |
| 33 | |
| 34 | /// Most distinct edges one execution records in the touched list before falling back |
| 35 | /// to a full scan of the map. |
| 36 | pub const TOUCHED_CAP: usize = 4096; |
| 37 | |
| 38 | /// Source lines tracked for site reach (sites on later lines share slots modulo this). |
| 39 | pub const SITE_LINES: usize = 1 << 13; |
| 40 | |
| 41 | /// Edge-coverage probe used by the fuzzer. |
| 42 | #[derive(Clone)] |
| 43 | pub struct EdgeProbe { |
| 44 | prev: u32, |
| 45 | /// Hit count per edge index (saturating). |
| 46 | pub map: Box<[u8]>, |
| 47 | /// Indices of the edges hit in this execution (preallocated, never grows), unless |
| 48 | /// `overflow` is set. |
| 49 | touched: Vec<u32>, |
| 50 | overflow: bool, |
| 51 | /// Every site id (source line) reached since creation; not cleared by `reset`. |
| 52 | pub sites_seen: Box<[bool]>, |
| 53 | /// Highest absolute input offset read (exclusive end). |
| 54 | pub high_water: usize, |
| 55 | } |
| 56 | |
| 57 | impl Default for EdgeProbe { |
| 58 | fn default() -> Self { |
| 59 | Self::new() |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl EdgeProbe { |
| 64 | pub fn new() -> Self { |
| 65 | Self { |
| 66 | prev: 0, |
| 67 | map: vec![0u8; EDGE_MAP_LEN].into_boxed_slice(), |
| 68 | touched: Vec::with_capacity(TOUCHED_CAP), |
| 69 | overflow: false, |
| 70 | sites_seen: vec![false; SITE_LINES].into_boxed_slice(), |
| 71 | high_water: 0, |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// Clear the map and high-water mark for the next execution (no allocation). |
| 76 | pub fn reset(&mut self) { |
| 77 | self.prev = 0; |
| 78 | if self.overflow { |
| 79 | self.map.fill(0); |
| 80 | } else { |
| 81 | for &i in &self.touched { |
| 82 | self.map[i as usize] = 0; |
| 83 | } |
| 84 | } |
| 85 | self.touched.clear(); |
| 86 | self.overflow = false; |
| 87 | self.high_water = 0; |
| 88 | } |
| 89 | |
| 90 | /// Call `f(index, count)` for every edge hit in this execution. |
| 91 | pub fn for_each_hit(&self, mut f: impl FnMut(usize, u8)) { |
| 92 | if self.overflow { |
| 93 | for (i, &c) in self.map.iter().enumerate() { |
| 94 | if c != 0 { |
| 95 | f(i, c); |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | for &i in &self.touched { |
| 100 | f(i as usize, self.map[i as usize]); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// Spread a site id over the edge map (Fibonacci hashing). |
| 107 | fn hash_site(site: u32) -> u32 { |
| 108 | (site.wrapping_mul(0x9E37_79B1) >> 16) & (EDGE_MAP_LEN as u32 - 1) |
| 109 | } |
| 110 | |
| 111 | impl Probe for EdgeProbe { |
| 112 | fn site(&mut self, site: u32) { |
| 113 | self.sites_seen[site as usize & (SITE_LINES - 1)] = true; |
| 114 | let cur = hash_site(site); |
| 115 | let idx = ((self.prev ^ cur) as usize) & (EDGE_MAP_LEN - 1); |
| 116 | if self.map[idx] == 0 { |
| 117 | if self.touched.len() < TOUCHED_CAP { |
| 118 | self.touched.push(idx as u32); |
| 119 | } else { |
| 120 | self.overflow = true; |
| 121 | } |
| 122 | } |
| 123 | self.map[idx] = self.map[idx].saturating_add(1); |
| 124 | self.prev = cur >> 1; |
| 125 | } |
| 126 | |
| 127 | fn touched(&mut self, end: usize) { |
| 128 | self.high_water = self.high_water.max(end); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// AFL-style hit-count bucket of one edge (bit per bucket), so a loop that runs more often |
| 133 | /// counts as new behaviour without every extra iteration counting. |
| 134 | pub fn bucket(count: u8) -> u8 { |
| 135 | match count { |
| 136 | 0 => 0, |
| 137 | 1 => 1, |
| 138 | 2 => 2, |
| 139 | 3 => 4, |
| 140 | 4..=7 => 8, |
| 141 | 8..=15 => 16, |
| 142 | 16..=31 => 32, |
| 143 | 32..=127 => 64, |
| 144 | _ => 128, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// Source lines of `frame.rs` that hold an instrumented site (`cov!` or |
| 149 | /// `probe.site(line!())`), so a run can report which sites it never reached. |
| 150 | pub fn instrumented_lines() -> Vec<u32> { |
| 151 | include_str!("frame.rs") |
| 152 | .lines() |
| 153 | .enumerate() |
| 154 | .filter(|(_, l)| { |
| 155 | let t = l.trim_start(); |
| 156 | !t.starts_with("//") |
| 157 | && !t.starts_with("$r.") |
| 158 | && (t.starts_with("cov!(") || t.starts_with("probe.site(line!())")) |
| 159 | }) |
| 160 | .map(|(i, _)| i as u32 + 1) |
| 161 | .collect() |
| 162 | } |