Skip to main content

dope_sys/probe/
mod.rs

1use crate::{io::socket::addr, memory};
2pub mod accept;
3mod check;
4pub mod cleanup;
5mod failure;
6mod outcome;
7pub mod raw;
8pub mod receive;
9pub mod session;
10pub use check::Check;
11pub use failure::Failure;
12pub use outcome::Outcome;
13
14/// Exact syscall or local-address failure while constructing probe sockets.
15#[derive(Clone, Copy, Debug, Eq, PartialEq)]
16pub enum Creation {
17    Syscall(crate::Errno),
18    Local(addr::Local<crate::Errno>),
19}
20
21impl From<crate::Errno> for Creation {
22    fn from(error: crate::Errno) -> Self {
23        Self::Syscall(error)
24    }
25}
26
27impl From<addr::Local<crate::Errno>> for Creation {
28    fn from(error: addr::Local<crate::Errno>) -> Self {
29        Self::Local(error)
30    }
31}
32
33/// A probe operation and its exact syscall failure.
34#[derive(Clone, Copy, Debug, Eq, PartialEq)]
35pub struct Syscall<P, E> {
36    pub probe: P,
37    pub error: E,
38}
39use crate::{
40    io::{
41        Close,
42        reactor::{complete, submit},
43    },
44    ring,
45    ring::{Access, map},
46    table::{token, token::host},
47};
48
49/// Stable host identities used by native capability checks.
50pub trait Context: host::Context {
51    const RING: Self::Probe;
52    const SOCKET: Self::Probe;
53    const OPTION: Self::Probe;
54    const CLOSE: Self::Probe;
55    const RECEIVE: Self::Probe;
56    const MESSAGE: Self::Probe;
57    const LISTENER: Self::Probe;
58    const ACCEPT: Self::Probe;
59    const MULTISHOT: Self::Probe;
60    const ACCEPT_CLOSE: Self::Probe;
61    const MULTI_CLOSE: Self::Probe;
62    const RELEASE: Self::Probe;
63}
64
65/// Classifies native fixed-file removal failures without erasing their phase.
66pub trait Build<E>: Context {
67    type Primary: Copy;
68    type Removed: Copy;
69
70    fn remove(mode: receive::Mode, error: E) -> Self::Removed;
71    fn release(probe: Self::Probe, error: E) -> Self::Removed;
72}
73
74/// Selects native canary checks without exposing native completion layouts.
75pub trait Source: ring::Source + submit::Control {
76    type Range: accept::Range<Target = Self::Target>;
77    type Payload: receive::Receive;
78
79    type Release<T: token::Codec, L: memory::raw::Canary, P: Copy>: cleanup::Release<
80            P,
81            Ring = Self::Ring<T>,
82            File = <Self::Ring<T> as Access>::File,
83            Session = <Self::Session<T, L> as session::Empty>::Occupied,
84            Target = <Self::Ring<T> as Access>::Target,
85            Error = map::Storage<<<Self::Ring<T> as Close>::State as map::Unmap>::Error>,
86        >
87    where
88        Self: complete::Observe<T>;
89
90    type Cleanup<T: token::Codec, L: memory::raw::Canary, P: Copy>: memory::Cleanup<
91            P,
92            Error = cleanup::Failure<
93                map::Storage<<<Self::Ring<T> as Close>::State as map::Unmap>::Error>,
94                Cause<
95                    <T::Context as host::Context>::Probe,
96                    <Self as complete::Observe<T>>::Observation,
97                    <Self::Accept<T> as accept::Accept>::Error,
98                >,
99            >,
100        >
101    where
102        Self: complete::Observe<T>;
103
104    type Accept<T: token::Codec>: accept::Accept<
105            Ring = Self::Ring<T>,
106            Owned = Self::Owned<T>,
107            Probe = <T::Context as host::Context>::Probe,
108            Outcome = Self::Outcome<T, <T::Context as host::Context>::Probe>,
109        >
110    where
111        Self: complete::Observe<T>;
112
113    type Session<T: token::Codec, L: memory::raw::Canary>: session::Empty<
114            Ring = Self::Ring<T>,
115            Layout = L,
116            Owned = Self::Owned<T>,
117            Probe = <T::Context as host::Context>::Probe,
118            Outcome = Self::Outcome<T, <T::Context as host::Context>::Probe>,
119        >
120    where
121        Self: complete::Observe<T>;
122
123    type Outcome<T: complete::raw::Token, P: Copy>: Outcome<
124            Probe = P,
125            Observation = <Self as complete::Observe<T>>::Observation,
126            Value = <Self::Range as accept::Range>::Value,
127        >
128    where
129        Self: complete::Observe<T>;
130
131    type Waiting<T: token::Codec, S>: raw::Wait<
132            S,
133            Ring = Self::Ring<T>,
134            Owned = Self::Owned<T>,
135            Probe = <T::Context as host::Context>::Probe,
136            Outcome = Self::Outcome<T, <T::Context as host::Context>::Probe>,
137        >
138    where
139        Self: complete::Observe<T>;
140}
141
142/// Validates an owned ring before its resources are exposed to a driver.
143pub trait Validate<L: memory::raw::Canary, P: Copy>: Sized {
144    type Probe: Copy;
145    type Cleanup;
146
147    fn support(self) -> Result<Self, Self::Cleanup>;
148    fn empty(self, probe: Self::Probe) -> Result<Self, Self::Cleanup>;
149}
150use o3::base::num::bounded;
151
152/// Exact kernel or completion evidence that stopped a capability canary.
153#[derive(Clone, Copy, Debug, Eq, PartialEq)]
154pub enum Cause<P, O, E> {
155    Enter {
156        probe: P,
157        error: E,
158    },
159    Missing(P),
160    Unexpected {
161        probe: P,
162        observed: O,
163    },
164    Residual {
165        probe: P,
166        submit: bounded::raw::U32<0, { u32::MAX }>,
167        complete: bounded::raw::U32<0, { u32::MAX }>,
168    },
169    Check(Failure<P, O>),
170}
171
172impl<P, O: Copy, E> Cause<P, O, E> {
173    /// Returns only evidence whose completion remains at the queue head.
174    pub fn unread(&self) -> Option<O> {
175        match self {
176            Self::Unexpected { observed, .. } => Some(*observed),
177            Self::Check(failure) => failure.unread(),
178            Self::Enter { .. } | Self::Missing(_) | Self::Residual { .. } => None,
179        }
180    }
181}
182
183/// The owner is returned only after the kernel reports terminal completion.
184#[derive(Debug)]
185#[must_use = "a submitted operation must be completed or canceled"]
186pub enum State<S, W> {
187    Idle(S),
188    Active(W),
189}
190
191/// A stopped transition retains its current owner and the exact cause.
192/// Unlike a rejected input, the owner may already contain committed progress.
193#[derive(Debug)]
194#[must_use = "resume or close the retained owner and report its cause"]
195pub struct Stopped<S, E> {
196    pub state: S,
197    pub cause: E,
198}
199
200impl<S, E> Stopped<S, E> {
201    pub fn attach<R>(state: S, result: Result<R, E>) -> Result<(S, R), Self> {
202        let cause = match result {
203            Ok(value) => return Ok((state, value)),
204            Err(cause) => cause,
205        };
206        Err(Self { state, cause })
207    }
208
209    pub fn map<T>(self, map: impl FnOnce(S) -> T) -> Stopped<T, E> {
210        Stopped {
211            state: map(self.state),
212            cause: self.cause,
213        }
214    }
215
216    pub fn idle<W>(self) -> Stopped<State<S, W>, E> {
217        self.map(State::Idle)
218    }
219
220    pub fn active<I>(self) -> Stopped<State<I, S>, E> {
221        self.map(State::Active)
222    }
223
224    pub fn convert<F: From<E>>(self) -> Stopped<S, F> {
225        Stopped {
226            state: self.state,
227            cause: F::from(self.cause),
228        }
229    }
230
231    pub fn into_parts(self) -> (S, E) {
232        (self.state, self.cause)
233    }
234}