
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1201 · main · 9 min 57 s
1Étoiles
0Forks
0Issues
| 1 | //! The signal frames of links, unlinks, monitors and exit signals across |
| 2 | //! nodes (`docs/wire-format.md`, *Signals*): their bytes only. `node.rs` |
| 3 | //! sends them from the hooks and applies what arrives through |
| 4 | //! `dist::inbound`. |
| 5 | //! |
| 6 | //! Every one names a pid of each node of the connection, by serial: the |
| 7 | //! sender's first, then the receiver's. An exit reason is |
| 8 | //! `std/process.ExitReason`'s constructor in one byte, and a crash's text. |
| 9 | |
| 10 | use louise_rt_prims::wire::{MAX_PID_SERIAL, MAX_TAG}; |
| 11 | |
| 12 | use crate::sched::Exit; |
| 13 | |
| 14 | /// The ops of [`Tie`] frames: the signal ops `0x10` to `0x1f` are theirs. |
| 15 | pub(super) const LINK: u8 = 0x10; |
| 16 | pub(super) const EXIT: u8 = 0x11; |
| 17 | pub(super) const MONITOR: u8 = 0x12; |
| 18 | pub(super) const DOWN: u8 = 0x13; |
| 19 | pub(super) const KILL: u8 = 0x14; |
| 20 | pub(super) const STOP: u8 = 0x15; |
| 21 | pub(super) const DEMONITOR: u8 = 0x16; |
| 22 | pub(super) const UNLINK: u8 = 0x17; |
| 23 | pub(super) const UNLINK_ACK: u8 = 0x18; |
| 24 | |
| 25 | /// Whether a signal frame's op is a [`Tie`]'s. |
| 26 | pub(super) fn is_tie(op: u8) -> bool { |
| 27 | (LINK..=0x1f).contains(&op) |
| 28 | } |
| 29 | |
| 30 | /// The longest crash text a frame carries, in bytes: a longer one is cut |
| 31 | /// at a character boundary, so every tie frame fits the smallest frame |
| 32 | /// limit a node may announce (1 KiB). |
| 33 | pub(super) const MAX_REASON: usize = 960; |
| 34 | |
| 35 | /// What a [`Tie`] frame asks of its receiver. |
| 36 | #[derive(Clone, Debug, PartialEq)] |
| 37 | pub(super) enum Op { |
| 38 | /// `from` linked to `to`. |
| 39 | Link, |
| 40 | /// `from`, linked to `to`, exited. |
| 41 | Exit(Exit), |
| 42 | /// `from` monitors `to`; its `Down` is due on `tag`. |
| 43 | Monitor { |
| 44 | tag: u64, |
| 45 | }, |
| 46 | /// `from`, monitored by `to` on `tag`, exited. |
| 47 | Down { |
| 48 | tag: u64, |
| 49 | reason: Exit, |
| 50 | }, |
| 51 | Kill, |
| 52 | Stop, |
| 53 | /// `from` no longer monitors `to` on `tag`. |
| 54 | Demonitor { |
| 55 | tag: u64, |
| 56 | }, |
| 57 | /// `from` unlinks `to`, as its unlink `id` (any value: the receiver |
| 58 | /// only echoes it). |
| 59 | Unlink { |
| 60 | id: u64, |
| 61 | }, |
| 62 | /// `from` removed its half of the link that `to` unlinked as `id`, or |
| 63 | /// had none. |
| 64 | UnlinkAck { |
| 65 | id: u64, |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | /// A link, monitor or exit signal frame: from the sender's actor `from` to |
| 70 | /// the receiver's actor `to` (serials below 2^46). |
| 71 | #[derive(Clone, Debug, PartialEq)] |
| 72 | pub(super) struct Tie { |
| 73 | pub(super) from: u64, |
| 74 | pub(super) to: u64, |
| 75 | pub(super) op: Op, |
| 76 | } |
| 77 | |
| 78 | impl Tie { |
| 79 | /// The frame's body: its op, then its fields. |
| 80 | pub(super) fn encode(&self) -> Vec<u8> { |
| 81 | let mut out = Vec::with_capacity(25); |
| 82 | out.push(match self.op { |
| 83 | Op::Link => LINK, |
| 84 | Op::Exit(_) => EXIT, |
| 85 | Op::Monitor { .. } => MONITOR, |
| 86 | Op::Down { .. } => DOWN, |
| 87 | Op::Kill => KILL, |
| 88 | Op::Stop => STOP, |
| 89 | Op::Demonitor { .. } => DEMONITOR, |
| 90 | Op::Unlink { .. } => UNLINK, |
| 91 | Op::UnlinkAck { .. } => UNLINK_ACK, |
| 92 | }); |
| 93 | out.extend_from_slice(&self.from.to_le_bytes()); |
| 94 | out.extend_from_slice(&self.to.to_le_bytes()); |
| 95 | match &self.op { |
| 96 | Op::Link | Op::Kill | Op::Stop => {} |
| 97 | Op::Exit(reason) => encode_reason(&mut out, reason), |
| 98 | Op::Monitor { tag } | Op::Demonitor { tag } => { |
| 99 | out.extend_from_slice(&tag.to_le_bytes()) |
| 100 | } |
| 101 | Op::Unlink { id } | Op::UnlinkAck { id } => out.extend_from_slice(&id.to_le_bytes()), |
| 102 | Op::Down { tag, reason } => { |
| 103 | out.extend_from_slice(&tag.to_le_bytes()); |
| 104 | encode_reason(&mut out, reason); |
| 105 | } |
| 106 | } |
| 107 | out |
| 108 | } |
| 109 | |
| 110 | /// Op `op`'s frame from `body`, the bytes after the op: `None` if it is |
| 111 | /// malformed, of another length, or out of range. |
| 112 | pub(super) fn decode(op: u8, body: &[u8]) -> Option<Tie> { |
| 113 | let (from, rest) = word(body)?; |
| 114 | let (to, rest) = word(rest)?; |
| 115 | if from >= MAX_PID_SERIAL || to >= MAX_PID_SERIAL { |
| 116 | return None; |
| 117 | } |
| 118 | let op = match op { |
| 119 | LINK if rest.is_empty() => Op::Link, |
| 120 | KILL if rest.is_empty() => Op::Kill, |
| 121 | STOP if rest.is_empty() => Op::Stop, |
| 122 | EXIT => Op::Exit(decode_reason(rest)?), |
| 123 | MONITOR => Op::Monitor { |
| 124 | tag: only_tag(rest)?, |
| 125 | }, |
| 126 | DEMONITOR => Op::Demonitor { |
| 127 | tag: only_tag(rest)?, |
| 128 | }, |
| 129 | UNLINK => Op::Unlink { |
| 130 | id: only_word(rest)?, |
| 131 | }, |
| 132 | UNLINK_ACK => Op::UnlinkAck { |
| 133 | id: only_word(rest)?, |
| 134 | }, |
| 135 | DOWN => { |
| 136 | let (tag, rest) = tag(rest)?; |
| 137 | Op::Down { |
| 138 | tag, |
| 139 | reason: decode_reason(rest)?, |
| 140 | } |
| 141 | } |
| 142 | _ => return None, |
| 143 | }; |
| 144 | Some(Tie { from, to, op }) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// A little-endian `u64`, and the bytes after it. |
| 149 | fn word(bytes: &[u8]) -> Option<(u64, &[u8])> { |
| 150 | let (w, rest) = bytes.split_first_chunk::<8>()?; |
| 151 | Some((u64::from_le_bytes(*w), rest)) |
| 152 | } |
| 153 | |
| 154 | /// A subject's tag (below 2^62), and the bytes after it. |
| 155 | fn tag(bytes: &[u8]) -> Option<(u64, &[u8])> { |
| 156 | word(bytes).filter(|(t, _)| *t < MAX_TAG) |
| 157 | } |
| 158 | |
| 159 | /// A tag that is all of `bytes`. |
| 160 | fn only_tag(bytes: &[u8]) -> Option<u64> { |
| 161 | match tag(bytes)? { |
| 162 | (tag, []) => Some(tag), |
| 163 | _ => None, |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /// A `u64` that is all of `bytes`. |
| 168 | fn only_word(bytes: &[u8]) -> Option<u64> { |
| 169 | match word(bytes)? { |
| 170 | (w, []) => Some(w), |
| 171 | _ => None, |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// `reason = kind (1) [, len (2), text]`: `Crashed` only has the text. |
| 176 | fn encode_reason(out: &mut Vec<u8>, reason: &Exit) { |
| 177 | match reason { |
| 178 | Exit::Normal => out.push(0), |
| 179 | Exit::Killed => out.push(1), |
| 180 | Exit::Shutdown => out.push(3), |
| 181 | Exit::Crashed(text) => { |
| 182 | let mut end = text.len().min(MAX_REASON); |
| 183 | while !text.is_char_boundary(end) { |
| 184 | end -= 1; |
| 185 | } |
| 186 | out.push(2); |
| 187 | out.extend_from_slice(&(end as u16).to_le_bytes()); |
| 188 | out.extend_from_slice(&text.as_bytes()[..end]); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /// A reason that is all of `bytes`. |
| 194 | fn decode_reason(bytes: &[u8]) -> Option<Exit> { |
| 195 | match bytes { |
| 196 | [0] => Some(Exit::Normal), |
| 197 | [1] => Some(Exit::Killed), |
| 198 | [3] => Some(Exit::Shutdown), |
| 199 | [2, l0, l1, text @ ..] if usize::from(u16::from_le_bytes([*l0, *l1])) == text.len() => { |
| 200 | std::str::from_utf8(text) |
| 201 | .ok() |
| 202 | .map(|t| Exit::Crashed(t.to_owned())) |
| 203 | } |
| 204 | _ => None, |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | #[cfg(test)] |
| 209 | mod tests { |
| 210 | use super::*; |
| 211 | |
| 212 | /// `docs/wire-format.md`'s example: actor 7, linked to actor 12 of the |
| 213 | /// receiving node, crashed with "boom". |
| 214 | #[test] |
| 215 | fn an_exit_is_the_documented_bytes() { |
| 216 | let exit = Tie { |
| 217 | from: 7, |
| 218 | to: 12, |
| 219 | op: Op::Exit(Exit::Crashed("boom".into())), |
| 220 | }; |
| 221 | let bytes = exit.encode(); |
| 222 | assert_eq!( |
| 223 | bytes, |
| 224 | [ |
| 225 | &[0x11][..], |
| 226 | &[7, 0, 0, 0, 0, 0, 0, 0], |
| 227 | &[12, 0, 0, 0, 0, 0, 0, 0], |
| 228 | &[2, 4, 0], |
| 229 | b"boom", |
| 230 | ] |
| 231 | .concat() |
| 232 | ); |
| 233 | assert_eq!(Tie::decode(bytes[0], &bytes[1..]), Some(exit)); |
| 234 | } |
| 235 | |
| 236 | /// `docs/wire-format.md`'s example: actor 7 unlinks actor 12 of the |
| 237 | /// receiving node as its unlink 1, which that node acknowledges. |
| 238 | #[test] |
| 239 | fn an_unlink_and_its_acknowledgement_are_the_documented_bytes() { |
| 240 | let unlink = Tie { |
| 241 | from: 7, |
| 242 | to: 12, |
| 243 | op: Op::Unlink { id: 1 }, |
| 244 | }; |
| 245 | let ack = Tie { |
| 246 | from: 12, |
| 247 | to: 7, |
| 248 | op: Op::UnlinkAck { id: 1 }, |
| 249 | }; |
| 250 | let (seven, twelve, one) = ( |
| 251 | [7, 0, 0, 0, 0, 0, 0, 0], |
| 252 | [12, 0, 0, 0, 0, 0, 0, 0], |
| 253 | [1, 0, 0, 0, 0, 0, 0, 0], |
| 254 | ); |
| 255 | assert_eq!( |
| 256 | unlink.encode(), |
| 257 | [&[0x17][..], &seven, &twelve, &one].concat() |
| 258 | ); |
| 259 | assert_eq!(ack.encode(), [&[0x18][..], &twelve, &seven, &one].concat()); |
| 260 | for tie in [unlink, ack] { |
| 261 | let bytes = tie.encode(); |
| 262 | assert_eq!(Tie::decode(bytes[0], &bytes[1..]), Some(tie)); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn every_frame_round_trips() { |
| 268 | let reasons = [ |
| 269 | Exit::Normal, |
| 270 | Exit::Killed, |
| 271 | Exit::Shutdown, |
| 272 | Exit::Crashed(String::new()), |
| 273 | Exit::Crashed("café".into()), |
| 274 | ]; |
| 275 | let mut ops = vec![ |
| 276 | Op::Link, |
| 277 | Op::Kill, |
| 278 | Op::Stop, |
| 279 | Op::Monitor { tag: 40 }, |
| 280 | Op::Demonitor { tag: 40 }, |
| 281 | Op::Unlink { id: 1 }, |
| 282 | Op::Unlink { id: u64::MAX }, |
| 283 | Op::UnlinkAck { id: 0 }, |
| 284 | Op::UnlinkAck { id: u64::MAX }, |
| 285 | ]; |
| 286 | for reason in reasons { |
| 287 | ops.push(Op::Exit(reason.clone())); |
| 288 | ops.push(Op::Down { tag: 3, reason }); |
| 289 | } |
| 290 | for op in ops { |
| 291 | let tie = Tie { |
| 292 | from: MAX_PID_SERIAL - 1, |
| 293 | to: 0, |
| 294 | op, |
| 295 | }; |
| 296 | let bytes = tie.encode(); |
| 297 | assert!(is_tie(bytes[0])); |
| 298 | assert_eq!(Tie::decode(bytes[0], &bytes[1..]).as_ref(), Some(&tie)); |
| 299 | // A byte more or less is another length: refused. |
| 300 | assert_eq!(Tie::decode(bytes[0], &bytes[1..bytes.len() - 1]), None); |
| 301 | let mut longer = bytes.clone(); |
| 302 | longer.push(0); |
| 303 | assert_eq!(Tie::decode(bytes[0], &longer[1..]), None, "{tie:?}"); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | #[test] |
| 308 | fn out_of_range_or_malformed_frames_are_refused() { |
| 309 | let link = |from: u64, to: u64| { |
| 310 | Tie { |
| 311 | from, |
| 312 | to, |
| 313 | op: Op::Link, |
| 314 | } |
| 315 | .encode() |
| 316 | }; |
| 317 | assert_eq!(Tie::decode(LINK, &link(MAX_PID_SERIAL, 0)[1..]), None); |
| 318 | assert_eq!(Tie::decode(LINK, &link(0, MAX_PID_SERIAL)[1..]), None); |
| 319 | let monitor = Tie { |
| 320 | from: 1, |
| 321 | to: 2, |
| 322 | op: Op::Monitor { tag: MAX_TAG }, |
| 323 | }; |
| 324 | assert_eq!(Tie::decode(MONITOR, &monitor.encode()[1..]), None); |
| 325 | let demonitor = Tie { |
| 326 | op: Op::Demonitor { tag: MAX_TAG }, |
| 327 | ..monitor |
| 328 | }; |
| 329 | assert_eq!(Tie::decode(DEMONITOR, &demonitor.encode()[1..]), None); |
| 330 | let body = &link(1, 2)[1..]; |
| 331 | let id = [body, &[7; 8]].concat(); |
| 332 | assert_eq!(Tie::decode(UNLINK, body), None, "no id"); |
| 333 | assert_eq!( |
| 334 | Tie::decode(UNLINK_ACK, &id[..id.len() - 1]), |
| 335 | None, |
| 336 | "7 bytes" |
| 337 | ); |
| 338 | assert_eq!(Tie::decode(0x19, &id), None, "no op 0x19 yet"); |
| 339 | let exit = |reason: &[u8]| [body, reason].concat(); |
| 340 | assert_eq!(Tie::decode(EXIT, &exit(&[4])), None, "no reason 4"); |
| 341 | assert_eq!(Tie::decode(EXIT, &exit(&[2, 1, 0, 0xff])), None, "UTF-8"); |
| 342 | assert_eq!(Tie::decode(EXIT, &exit(&[2, 2, 0, b'a'])), None, "length"); |
| 343 | assert_eq!( |
| 344 | Tie::decode(EXIT, &exit(&[2, 1, 0, b'a'])), |
| 345 | Some(Tie { |
| 346 | from: 1, |
| 347 | to: 2, |
| 348 | op: Op::Exit(Exit::Crashed("a".into())) |
| 349 | }) |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | #[test] |
| 354 | fn a_long_crash_text_is_cut_at_a_character_boundary() { |
| 355 | // 'é' is 2 bytes: 481 of them end past the limit, mid-character. |
| 356 | let text = "é".repeat(481); |
| 357 | let down = Tie { |
| 358 | from: 1, |
| 359 | to: 2, |
| 360 | op: Op::Down { |
| 361 | tag: 3, |
| 362 | reason: Exit::Crashed(text.clone()), |
| 363 | }, |
| 364 | }; |
| 365 | let bytes = down.encode(); |
| 366 | // The frame's kind, then this body, fit 1 KiB. |
| 367 | assert!(bytes.len() < 1024, "{}", bytes.len()); |
| 368 | let cut = "é".repeat(480); |
| 369 | assert_eq!(cut.len(), MAX_REASON); |
| 370 | assert_eq!( |
| 371 | Tie::decode(DOWN, &bytes[1..]).map(|t| t.op), |
| 372 | Some(Op::Down { |
| 373 | tag: 3, |
| 374 | reason: Exit::Crashed(cut) |
| 375 | }) |
| 376 | ); |
| 377 | } |
| 378 | } |