Skip to main content

dope_core/driver/life/native/create/
failure.rs

1use dope_sys::{
2    io::{reactor::complete, socket::addr},
3    kernel, memory, probe,
4    probe::{accept, receive, session},
5    ring::{create, inspect, map, prepare::Rejection},
6};
7
8use crate::{
9    Engine,
10    driver::{
11        life::native::create::{Kernel, Mapping},
12        result,
13        result::{IoOperation, poll::fault},
14    },
15    io::socket,
16};
17#[cfg(target_os = "linux")]
18use crate::{driver::life::native::host::Receive, io::transfer};
19
20pub(crate) use crate::driver::storage::Failure;
21
22impl<E> From<probe::Syscall<result::Probe, E>> for Failure
23where
24    Kernel: From<E>,
25{
26    fn from(cause: probe::Syscall<result::Probe, E>) -> Self {
27        Self::Kernel {
28            operation: IoOperation::Probe(cause.probe),
29            cause: Kernel::from(cause.error),
30        }
31    }
32}
33
34impl From<probe::Syscall<result::Probe, probe::Creation>> for Failure {
35    fn from(cause: probe::Syscall<result::Probe, probe::Creation>) -> Self {
36        match cause.error {
37            probe::Creation::Syscall(error)
38            | probe::Creation::Local(addr::Local::Syscall(error)) => probe::Syscall {
39                probe: cause.probe,
40                error,
41            }
42            .into(),
43            probe::Creation::Local(addr::Local::Length { actual, capacity }) => {
44                Self::Probe(result::ProbeError::new(
45                    cause.probe,
46                    result::Violation::InvalidAddress(socket::Error::Storage { actual, capacity }),
47                ))
48            }
49        }
50    }
51}
52
53impl From<accept::Check<result::Probe>> for Failure {
54    fn from(check: accept::Check<result::Probe>) -> Self {
55        let (probe, cause) = match check {
56            accept::Check::Early(probe) => (probe, result::Violation::EarlyTermination),
57            accept::Check::Outside { probe, cause } => {
58                (probe, result::Violation::EscapedSlot(cause.into()))
59            }
60        };
61        Self::Probe(result::ProbeError::new(probe, cause))
62    }
63}
64
65impl From<memory::Error> for Failure {
66    fn from(error: memory::Error) -> Self {
67        Self::Mapping(Mapping::from(error))
68    }
69}
70
71impl<R, U> From<session::Primary<R, U>> for Failure
72where
73    Kernel: From<R> + From<U>,
74{
75    fn from(primary: session::Primary<R, U>) -> Self {
76        let (op, cause) = match primary {
77            session::Primary::Register(error) => {
78                (IoOperation::RegisterCanaryBuffers, Kernel::from(error))
79            }
80            session::Primary::Unregister(error) => {
81                (IoOperation::UnregisterCanaryBuffers, Kernel::from(error))
82            }
83        };
84        Self::Kernel {
85            operation: op,
86            cause,
87        }
88    }
89}
90
91impl<E> From<receive::Install<E>> for Failure
92where
93    Kernel: From<E>,
94{
95    fn from(failure: receive::Install<E>) -> Self {
96        let op = match failure.mode {
97            receive::Mode::Data => IoOperation::InstallReceiveFile,
98            receive::Mode::Message => IoOperation::InstallReceiveMessageFile,
99        };
100        Self::Kernel {
101            operation: op,
102            cause: Kernel::from(failure.error),
103        }
104    }
105}
106
107#[cfg(target_os = "linux")]
108impl<O, E, L, C>
109    From<
110        session::Checked<
111            result::Probe,
112            O,
113            E,
114            <crate::Backend as memory::buffer::provided::Source>::Index,
115            L,
116            C,
117            result::ProbeError,
118        >,
119    > for Failure
120where
121    Engine: complete::Observe<Observation = O>,
122    Kernel: From<E>,
123    transfer::Len: From<L>,
124    transfer::NonZero: From<C>,
125{
126    fn from(
127        cause: session::Checked<
128            result::Probe,
129            O,
130            E,
131            <crate::Backend as memory::buffer::provided::Source>::Index,
132            L,
133            C,
134            result::ProbeError,
135        >,
136    ) -> Self {
137        use dope_sys::memory::buffer::canary;
138        use session::Checked;
139
140        let (probe, fault) = match cause {
141            Checked::Wait(cause) => return cause.into(),
142            Checked::MissingBuffer(probe) => (probe, result::Violation::MissingBuffer),
143            Checked::Terminated(probe) => (probe, result::Violation::EarlyTermination),
144            Checked::Buffer { probe, failure } => {
145                let fault = match failure {
146                    canary::Failure::Inspect(error) => {
147                        return Self::Probe(error);
148                    }
149                    canary::Failure::Index(index) => {
150                        let layout = <Receive as memory::raw::Canary>::LAYOUT;
151                        result::Violation::InvalidBuffer(
152                            crate::driver::receive::Selection::from_native(
153                                o3::buffer::storage::provided::native::Index {
154                                    index: index.0,
155                                    capacity: layout.0.count().into(),
156                                },
157                            ),
158                        )
159                    }
160                    canary::Failure::Length { actual, capacity } => {
161                        result::Violation::BufferLength {
162                            actual: actual.into(),
163                            capacity: capacity.into(),
164                        }
165                    }
166                };
167                (probe, fault)
168            }
169        };
170        Self::Probe(result::ProbeError::new(probe, fault))
171    }
172}
173
174impl<C, E> From<inspect::Unsupported<C, E>> for Failure
175where
176    C: Into<<crate::Backend as kernel::Source>::Code>,
177    Kernel: From<E>,
178{
179    fn from(error: inspect::Unsupported<C, E>) -> Self {
180        match error {
181            inspect::Unsupported::Opcode(code) => {
182                Self::Capability(result::Capability::Opcode(result::Opcode::native(code)))
183            }
184            inspect::Unsupported::Register(error) => Self::Kernel {
185                operation: IoOperation::RegisterOpcodes,
186                cause: Kernel::from(error),
187            },
188        }
189    }
190}
191
192impl From<(result::Probe, receive::Send)> for Failure {
193    fn from((probe, error): (result::Probe, receive::Send)) -> Self {
194        match error {
195            receive::Send::Syscall(error) => Self::Kernel {
196                operation: IoOperation::Probe(probe),
197                cause: Kernel::from(error),
198            },
199            receive::Send::Partial(partial) => Self::Probe(result::ProbeError::new(
200                probe,
201                result::Violation::Send(fault::Send::from_partial(partial)),
202            )),
203        }
204    }
205}
206
207impl<C, F, R> From<Rejection<C, F, R>> for Failure
208where
209    Kernel: From<C> + From<F> + From<R>,
210{
211    fn from(cause: Rejection<C, F, R>) -> Self {
212        let (op, cause) = match cause {
213            Rejection::Feature(feature) => {
214                return Self::Capability(result::Capability::Feature(feature));
215            }
216            Rejection::Cancel(cause) => (IoOperation::CancelRegistrations, Kernel::from(cause)),
217            Rejection::Files(cause) => (IoOperation::RegisterFileTable, Kernel::from(cause)),
218            Rejection::Range(cause) => (IoOperation::RegisterFileRange, Kernel::from(cause)),
219        };
220        Self::Kernel {
221            operation: op,
222            cause,
223        }
224    }
225}
226
227impl<S, M> From<create::Cause<S, M>> for Failure
228where
229    Kernel: From<S>,
230    Mapping: From<map::Map<M>>,
231{
232    fn from(cause: create::Cause<S, M>) -> Self {
233        match cause {
234            create::Cause::Setup(cause) => Self::Kernel {
235                operation: IoOperation::ReactorCreate,
236                cause: Kernel::from(cause),
237            },
238            create::Cause::Layout(cause) => Self::RingLayout(cause),
239            create::Cause::Map(cause) => Self::Mapping(Mapping::from(cause)),
240        }
241    }
242}
243
244impl<M, R, B> From<create::Primary<M, R, B>> for Failure
245where
246    Self: From<M>,
247    Kernel: From<R> + From<B>,
248{
249    fn from(primary: create::Primary<M, R, B>) -> Self {
250        match primary {
251            create::Primary::Ring(error) => Self::from(error),
252            create::Primary::Memory(error) => {
253                Self::Mapping(<Mapping as From<memory::Error>>::from(error))
254            }
255            create::Primary::Register(cause) => Self::Kernel {
256                operation: IoOperation::RegisterReactor,
257                cause: Kernel::from(cause),
258            },
259            create::Primary::Provided(cause) => Self::Kernel {
260                operation: IoOperation::RegisterProvidedBuffers,
261                cause: Kernel::from(cause),
262            },
263        }
264    }
265}
266
267impl<S, W> From<create::Failure<S, W>> for Failure
268where
269    Mapping: From<S>,
270    Failure: From<W>,
271{
272    fn from(failure: create::Failure<S, W>) -> Self {
273        match failure {
274            create::Failure::Storage(error) => Self::Mapping(Mapping::from(error)),
275            create::Failure::Memory(error) => {
276                Self::Mapping(<Mapping as From<memory::Error>>::from(error))
277            }
278            create::Failure::Wait(cause) => Self::from(cause),
279        }
280    }
281}
282
283impl<O, E> From<probe::Cause<result::Probe, O, E>> for Failure
284where
285    Engine: complete::Observe<Observation = O>,
286    Kernel: From<E>,
287{
288    fn from(cause: probe::Cause<result::Probe, O, E>) -> Self {
289        use probe::Cause;
290
291        let (probe, fault) = match cause {
292            Cause::Enter { probe, error } => {
293                return Self::Kernel {
294                    operation: IoOperation::Probe(probe),
295                    cause: Kernel::from(error),
296                };
297            }
298            Cause::Check(error) => {
299                return Self::Probe(result::ProbeError::decode(error));
300            }
301            Cause::Missing(probe) => (probe, result::Violation::MissingCompletion),
302            Cause::Unexpected { probe, observed } => (
303                probe,
304                result::Violation::UnexpectedCompletion {
305                    expected: probe,
306                    observed: fault::Observation::new(observed),
307                },
308            ),
309            Cause::Residual {
310                probe,
311                submit,
312                complete,
313            } => (
314                probe,
315                result::Violation::ResidualWork {
316                    submissions: fault::Pending::from(submit),
317                    completions: fault::Available::from(complete),
318                },
319            ),
320        };
321        Self::Probe(result::ProbeError::new(probe, fault))
322    }
323}