Skip to main content

mimir_node_map/
outside.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Machine-entry observations for conditional outside-reach coverage.
3use serde::{Deserialize, Serialize};
4use std::collections::BTreeSet;
5
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct OutsideBody {
8    pub start: u64,
9    pub end: u64,
10    pub symbols: Vec<String>,
11}
12#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13pub struct OutsideGuard {
14    pub version: u32,
15    /// Entire executable address ranges scanned for direct incoming transfers.
16    pub scanned: Vec<(u64, u64)>,
17    /// Candidate out-of-line bodies, including all known aliases.
18    pub bodies: Vec<OutsideBody>,
19    /// All decoded direct transfers into a candidate body, by source and target.
20    pub incoming: Vec<(u64, u64)>,
21}
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub enum OutsideRefusal {
24    Evidence,
25    Identity,
26    Incoming,
27}
28impl OutsideRefusal {
29    pub fn name(self) -> &'static str {
30        match self {
31            Self::Evidence => "outside-machine-evidence",
32            Self::Identity => "outside-machine-identity",
33            Self::Incoming => "outside-machine-incoming",
34        }
35    }
36}
37
38/// Pure shared check. The producer supplies decoded observations, the consumer
39/// checks their shape and the pinned reached rows and executable ranges.
40pub fn proven_bodies(
41    guard: &OutsideGuard,
42    reached: &[(String, bool)],
43    exec: &[(u64, u64)],
44    starts: &[u64],
45) -> Result<BTreeSet<u64>, OutsideRefusal> {
46    let inside =
47        |ranges: &[(u64, u64)], address| ranges.iter().any(|&(a, b)| a <= address && address < b);
48    if guard.version != 1
49        || guard.scanned != exec
50        || exec.is_empty()
51        || exec.iter().any(|&(a, b)| a == 0 || a >= b)
52        || exec.windows(2).any(|w| w[0].1 > w[1].0)
53        || guard.bodies.windows(2).any(|w| w[0].end > w[1].start)
54        || guard.incoming.windows(2).any(|w| w[0] >= w[1])
55    {
56        return Err(OutsideRefusal::Evidence);
57    }
58    for body in &guard.bodies {
59        if body.start >= body.end
60            || !exec.iter().any(|&(a, b)| a <= body.start && body.end <= b)
61            || body.symbols.is_empty()
62            || body.symbols.iter().any(String::is_empty)
63            || body.symbols.windows(2).any(|w| w[0] >= w[1])
64        {
65            return Err(OutsideRefusal::Evidence);
66        }
67        for symbol in &body.symbols {
68            let mut rows = reached.iter().filter(|(s, _)| s == symbol);
69            if !rows.next().is_some_and(|(_, live)| !*live) || rows.next().is_some() {
70                return Err(OutsideRefusal::Identity);
71            }
72        }
73    }
74    if guard.incoming.iter().any(|&(from, to)| {
75        !inside(exec, from) || !guard.bodies.iter().any(|b| b.start <= to && to < b.end)
76    }) {
77        return Err(OutsideRefusal::Evidence);
78    }
79    let mut outside: BTreeSet<_> = guard
80        .bodies
81        .iter()
82        .filter(|b| !starts.iter().any(|&s| b.start <= s && s < b.end))
83        .map(|b| b.start)
84        .collect();
85    loop {
86        let remove: Vec<_> = guard
87            .bodies
88            .iter()
89            .filter(|body| outside.contains(&body.start))
90            .filter(|body| {
91                guard.incoming.iter().any(|&(from, to)| {
92                    body.start <= to
93                        && to < body.end
94                        && !guard.bodies.iter().any(|source| {
95                            outside.contains(&source.start)
96                                && source.start <= from
97                                && from < source.end
98                        })
99                })
100            })
101            .map(|b| b.start)
102            .collect();
103        if remove.is_empty() {
104            break;
105        }
106        for body in remove {
107            outside.remove(&body);
108        }
109    }
110    Ok(outside)
111}