Skip to main content

fic/wfcheck/
recv_head.rs

1//! Where the notified graph takes a message. The kernel invokes `notified`
2//! for one message, so:
3//!
4//! - **Receive-headed** (`recvHeaded`): every edge out of `init` is an
5//!   unguarded receive (`c?x`) or IRQ await (`irq?N`).
6//! - **One per channel**: no two head edges receive on one channel or await
7//!   one IRQ, since which one takes the message is undecided.
8//! - **Only at the head**: no other edge receives or awaits an IRQ.
9//!
10//! A PD then waits only at its head, where nothing but a message enables a
11//! branch. Deadlock freedom is proved of such programs, so each finding ends
12//! with [`VOIDS`].
13
14use crate::ast::IrqKind;
15use crate::base::{ChanId, ProcId};
16use crate::ccfg::graph::{Edge, Label};
17use crate::error::Diagnostic;
18use crate::tast::{Comm, Event};
19
20use super::wellformed::Graph;
21
22/// What a finding costs: the end of each message.
23const VOIDS: &str =
24    "a program that breaks this rule is outside the deadlock freedom fic guarantees";
25
26/// What a receive takes from: a channel, or an IRQ.
27#[derive(Clone, Copy, PartialEq)]
28pub(super) enum Receive {
29    Chan(ChanId),
30    Irq(u32),
31}
32
33impl Receive {
34    /// What `edge` receives from, when it receives or awaits an IRQ.
35    fn of(edge: &Edge) -> Option<Receive> {
36        match &edge.label {
37            Some(Label::Event(event)) => Receive::of_event(event),
38            _ => None,
39        }
40    }
41
42    /// What `event` receives from, when it is a receive or an IRQ await.
43    pub(super) fn of_event(event: &Event) -> Option<Receive> {
44        match event {
45            Event::Comm(Comm::Recv { chan, .. }) => Some(Receive::Chan(*chan)),
46            Event::Irq {
47                irq,
48                kind: IrqKind::Await,
49            } => Some(Receive::Irq(*irq)),
50            _ => None,
51        }
52    }
53}
54
55impl Graph<'_> {
56    /// The head, the out-edges of `init` in the graph of `notified`:
57    /// receive-headed, one receive per channel or IRQ.
58    pub(super) fn head(&mut self, notified: ProcId, head: &[&Edge]) {
59        if head.iter().any(|e| !e.blocks() || e.guard.is_some()) {
60            let decl = &self.view.body().processes[notified].sig;
61            self.found.push(Diagnostic::at(
62                format!(
63                    "process `{}` bound to `microkit.notified` is not receive-headed: each path at its head must begin with an unguarded receive (`c?x`) or IRQ await (`irq?N`) — the kernel invokes it for a message; {VOIDS}",
64                    decl.name
65                ),
66                decl.span.clone(),
67                self.file,
68            ));
69        }
70        for (i, edge) in head.iter().enumerate() {
71            let Some(on) = Receive::of(edge) else {
72                continue;
73            };
74            if head[..i].iter().any(|e| Receive::of(e) == Some(on)) {
75                let name = self.receive_name(on);
76                self.report(
77                    edge.process,
78                    self.event_span(edge),
79                    &format!(
80                        "two branches of the notified head receive on `{name}`: which one takes the \
81                         message is not decided — receive once, and decide by guards after; {VOIDS}"
82                    ),
83                );
84            }
85        }
86    }
87
88    /// A receive or IRQ await off the head.
89    pub(super) fn stray_receive(&mut self, edge: &Edge) {
90        let Some(on) = Receive::of(edge) else {
91            return;
92        };
93        let name = self.receive_name(on);
94        self.report(
95            edge.process,
96            self.event_span(edge),
97            &format!(
98                "a receive (`{name}…`) outside the head of the notified body: a message is taken only \
99                 where the kernel's invocation starts — receive there, and decide by guards after; {VOIDS}"
100            ),
101        );
102    }
103
104    /// `c?` or `irq?N`, for the reader.
105    fn receive_name(&self, on: Receive) -> String {
106        match on {
107            Receive::Chan(c) => format!("{}?", self.view.chan(c).name),
108            Receive::Irq(n) => format!("irq?{n}"),
109        }
110    }
111}