byteflow-actors 0.9.2

Embeddable flow runtime: M:N scheduler, Atomic Hop (Message+Cap), supervisor — use byteflow::
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Runtime-local capability table (Phase 3 — FlowCap + attenuate).
//!
//! A [`CapId`] is an opaque 128-bit token, **not** an authorization.
//! Authorization lives in [`Cap`], owned by one [`crate::Runtime`]:
//!
//! ```text
//! CapId  →  { holder, Cap { target, rights, native_mask, epoch } }
//! ```
//!
//! Every derived grant goes through [`Cap::attenuate`]. `mint` is the
//! trusted-runtime root path (self Cap, spawn addressing, reply_cap).
//!
//! See `docs/security.md` (S6 / S7) and `docs/atomic-hop.md`.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use crate::bytecode::{Cap, CapId, CapTarget, NativeMask, RevocationCell};

pub use crate::bytecode::CapRights;

use super::error::RuntimeError;
use super::process::FlowId;
use super::sync_lock;

const MINT_ATTEMPTS: u32 = 32;

/// One live capability entry. The [`CapId`] is the map key, not a field.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Capability {
    /// Flow allowed to *use* this token.
    pub holder: FlowId,
    pub cap: Cap,
}

impl Capability {
    /// Flow this Cap addresses, if the target is a flow.
    pub fn target(&self) -> Option<FlowId> {
        self.cap.target.flow_id().map(FlowId)
    }

    pub fn rights(&self) -> CapRights {
        self.cap.rights
    }
}

/// Why [`CapTable::resolve`] / [`CapTable::delegate`] refused.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CapError {
    Unknown,
    NotHolder,
    InsufficientRights,
    WrongTarget,
    /// Mutex poison — fail closed (never `into_inner`).
    Unavailable,
}

impl std::fmt::Display for CapError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CapError::Unknown => f.write_str("unknown or revoked capability"),
            CapError::NotHolder => f.write_str("calling flow does not hold this capability"),
            CapError::InsufficientRights => f.write_str("capability lacks required rights"),
            CapError::WrongTarget => f.write_str("capability target is not a flow"),
            CapError::Unavailable => f.write_str("capability table unavailable (poisoned lock)"),
        }
    }
}

impl std::error::Error for CapError {}

impl From<RuntimeError> for CapError {
    fn from(_err: RuntimeError) -> Self {
        CapError::Unavailable
    }
}

struct CapTableInner {
    entries: HashMap<CapId, Capability>,
    flow_cells: HashMap<u64, Arc<RevocationCell>>,
}

/// Registry `CapId → Capability`, owned by one runtime's [`super::runtime::Shared`].
pub struct CapTable {
    inner: Mutex<CapTableInner>,
    native_cell: Arc<RevocationCell>,
    scheduler_cell: Arc<RevocationCell>,
}

impl CapTable {
    pub fn new() -> Self {
        Self {
            inner: Mutex::new(CapTableInner {
                entries: HashMap::new(),
                flow_cells: HashMap::new(),
            }),
            native_cell: Arc::new(RevocationCell::new()),
            scheduler_cell: Arc::new(RevocationCell::new()),
        }
    }

    pub fn native_cell(&self) -> Arc<RevocationCell> {
        Arc::clone(&self.native_cell)
    }

    fn lock(&self, where_: &'static str) -> Result<std::sync::MutexGuard<'_, CapTableInner>, RuntimeError> {
        sync_lock::lock(&self.inner, where_)
    }

    /// Get-or-create the revocation cell for `flow`.
    pub fn bind_flow(&self, flow: FlowId) -> Result<Arc<RevocationCell>, RuntimeError> {
        let mut g = self.lock("CapTable::bind_flow")?;
        Ok(g.flow_cells
            .entry(flow.as_u64())
            .or_insert_with(|| Arc::new(RevocationCell::new()))
            .clone())
    }

    pub fn flow_cell(&self, flow: FlowId) -> Result<Option<Arc<RevocationCell>>, RuntimeError> {
        Ok(self.lock("CapTable::flow_cell")?.flow_cells.get(&flow.as_u64()).cloned())
    }

    pub fn scheduler_cell(&self) -> Arc<RevocationCell> {
        Arc::clone(&self.scheduler_cell)
    }

    fn insert_fresh(
        table: &mut HashMap<CapId, Capability>,
        entry: Capability,
    ) -> Result<CapId, RuntimeError> {
        for _ in 0..MINT_ATTEMPTS {
            let id = CapId::random().map_err(|_| RuntimeError::EntropyFailed)?;
            if id.is_none() || table.contains_key(&id) {
                continue;
            }
            table.insert(id, entry);
            return Ok(id);
        }
        Err(RuntimeError::CapIdCollision)
    }

    fn insert(&self, holder: FlowId, cap: Cap) -> Result<CapId, RuntimeError> {
        let mut table = self.lock("CapTable::insert")?;
        Self::insert_fresh(
            &mut table.entries,
            Capability { holder, cap },
        )
    }

    /// Trusted root mint: `holder` may address `target` with `rights`.
    ///
    /// Used for: self Cap (`SelfPid`), child addressing Cap (`Spawn`),
    /// and per-hop `reply_cap` (holder = recipient, SEND-only back to sender).
    pub fn mint(
        &self,
        holder: FlowId,
        target: FlowId,
        rights: CapRights,
    ) -> Result<CapId, RuntimeError> {
        let cell = self.bind_flow(target)?;
        let cap = Cap::root(CapTarget::Flow(target.as_u64()), rights, None, cell.as_ref());
        self.grant(holder, cap)
    }

    /// Insert a Cap that was already produced by [`Cap::attenuate`] or
    /// [`Cap::root`] (trusted).
    pub fn grant(&self, holder: FlowId, cap: Cap) -> Result<CapId, RuntimeError> {
        self.insert(holder, cap)
    }

    /// Host / registry lookup: existence only, no holder check.
    pub fn lookup(&self, id: CapId) -> Result<Option<Capability>, RuntimeError> {
        if id.is_none() {
            return Ok(None);
        }
        Ok(self.lock("CapTable::lookup")?.entries.get(&id).cloned())
    }

    /// Bytecode path: token + holder + rights + live epoch.
    pub fn resolve(
        &self,
        id: CapId,
        holder: FlowId,
        required: CapRights,
    ) -> Result<Capability, CapError> {
        if id.is_none() {
            return Err(CapError::Unknown);
        }
        let table = self.lock("CapTable::resolve")?;
        let entry = table.entries.get(&id).cloned().ok_or(CapError::Unknown)?;
        if entry.holder != holder {
            return Err(CapError::NotHolder);
        }
        if !entry.cap.rights.contains(required) {
            return Err(CapError::InsufficientRights);
        }
        let valid = match entry.cap.target {
            CapTarget::Flow(fid) => match table.flow_cells.get(&fid) {
                Some(cell) => entry.cap.is_valid(cell.as_ref()),
                None => false,
            },
            CapTarget::NativeTable => entry.cap.is_valid(self.native_cell.as_ref()),
            CapTarget::Scheduler => entry.cap.is_valid(self.scheduler_cell.as_ref()),
        };
        if !valid {
            return Err(CapError::Unknown);
        }
        Ok(entry)
    }

    /// New token, attenuated rights, `to_holder` becomes the holder.
    /// Sole bytecode derivation path — calls [`Cap::attenuate`].
    pub fn attenuate(
        &self,
        id: CapId,
        from_holder: FlowId,
        to_holder: FlowId,
        want_rights: CapRights,
        want_native: Option<&NativeMask>,
    ) -> Result<CapId, CapError> {
        let src = self.resolve(id, from_holder, CapRights::empty())?;
        let cell = match src.cap.target {
            CapTarget::Flow(fid) => self
                .lock("CapTable::attenuate")?
                .flow_cells
                .get(&fid)
                .cloned()
                .ok_or(CapError::Unknown)?,
            CapTarget::NativeTable | CapTarget::Scheduler => {
                return Err(CapError::WrongTarget);
            }
        };
        let narrowed = match super::delegate::exec_delegate(
            &src.cap,
            cell.as_ref(),
            want_rights,
            want_native,
        ) {
            Ok(c) => c,
            Err(super::delegate::DelegateError::SourceRevoked) => {
                return Err(CapError::Unknown)
            }
            Err(super::delegate::DelegateError::SourceLacksNative) => {
                return Err(CapError::InsufficientRights)
            }
        };
        if !narrowed.is_valid(cell.as_ref()) {
            return Err(CapError::Unknown);
        }
        self.insert(to_holder, narrowed).map_err(CapError::from)
    }

    /// New token, same target/rights (attenuation with `want = src.rights`).
    pub fn delegate(
        &self,
        id: CapId,
        from_holder: FlowId,
        to_holder: FlowId,
    ) -> Result<CapId, CapError> {
        let src = self.resolve(id, from_holder, CapRights::empty())?;
        self.attenuate(id, from_holder, to_holder, src.cap.rights, src.cap.native_mask.as_ref())
    }

    /// Host spawn: re-issue a live Cap so `new_holder` can use it.
    /// Does not require the caller to be the current holder (trusted host).
    /// Still goes through [`Cap::attenuate`] (identity attenuation).
    pub fn reissue_for(&self, id: CapId, new_holder: FlowId) -> Result<CapId, CapError> {
        let cap = self.lookup(id)?.ok_or(CapError::Unknown)?;
        let narrowed = cap.cap.attenuate(cap.cap.rights, cap.cap.native_mask.as_ref());
        self.insert(new_holder, narrowed).map_err(CapError::from)
    }

    /// Drop every capability held by or targeting `flow` (flow exited).
    /// Bumps the flow's epoch first so leaked tokens fail in O(1).
    pub fn revoke_flow(&self, flow: FlowId) -> Result<usize, RuntimeError> {
        let mut g = self.lock("CapTable::revoke_flow")?;
        if let Some(cell) = g.flow_cells.get(&flow.as_u64()) {
            cell.revoke();
        }
        g.flow_cells.remove(&flow.as_u64());
        let before = g.entries.len();
        let fid = flow.as_u64();
        g.entries.retain(|_, e| {
            e.holder != flow && e.cap.target != CapTarget::Flow(fid)
        });
        Ok(before - g.entries.len())
    }
}

impl Default for CapTable {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::scheduler::process::next_flow_id;

    #[test]
    fn random_ids_are_not_sequential() -> Result<(), Box<dyn std::error::Error>> {
        let table = CapTable::new();
        let h = next_flow_id();
        let t = next_flow_id();
        let a = table.mint(h, t, CapRights::SEND)?;
        let b = table.mint(h, t, CapRights::SEND)?;
        assert_ne!(a, b);
        assert!(!a.is_none());
        assert_eq!(
            table.resolve(CapId::from_raw(1), h, CapRights::SEND),
            Err(CapError::Unknown)
        );
        Ok(())
    }

    #[test]
    fn resolve_requires_holder_and_rights() -> Result<(), Box<dyn std::error::Error>> {
        let table = CapTable::new();
        let holder = next_flow_id();
        let other = next_flow_id();
        let target = next_flow_id();
        let cap = table.mint(holder, target, CapRights::SEND)?;
        assert_eq!(
            table.resolve(cap, holder, CapRights::SEND)?.target(),
            Some(target)
        );
        assert_eq!(
            table.resolve(cap, other, CapRights::SEND),
            Err(CapError::NotHolder)
        );
        assert_eq!(
            table.resolve(cap, holder, CapRights::ASK),
            Err(CapError::InsufficientRights)
        );
        Ok(())
    }

    #[test]
    fn capability_isolation_is_per_table() -> Result<(), Box<dyn std::error::Error>> {
        let a = CapTable::new();
        let b = CapTable::new();
        let holder = next_flow_id();
        let target = next_flow_id();
        let cap = a.mint(holder, target, CapRights::SEND)?;
        assert_eq!(
            b.resolve(cap, holder, CapRights::SEND),
            Err(CapError::Unknown)
        );
        Ok(())
    }

    #[test]
    fn finalizing_flow_revokes_held_and_targeted_caps() -> Result<(), Box<dyn std::error::Error>> {
        let table = CapTable::new();
        let dead = next_flow_id();
        let alive = next_flow_id();
        let target = next_flow_id();

        let held_by_dead = table.mint(dead, target, CapRights::SEND)?;
        let targeting_dead = table.mint(alive, dead, CapRights::SEND)?;
        let unrelated = table.mint(alive, target, CapRights::SEND)?;

        let removed = table.revoke_flow(dead)?;
        assert_eq!(removed, 2);
        assert_eq!(
            table.resolve(held_by_dead, dead, CapRights::SEND),
            Err(CapError::Unknown)
        );
        assert_eq!(
            table.resolve(targeting_dead, alive, CapRights::SEND),
            Err(CapError::Unknown)
        );
        assert!(table.resolve(unrelated, alive, CapRights::SEND).is_ok());
        Ok(())
    }

    #[test]
    fn delegate_issues_a_new_id_for_the_child() -> Result<(), Box<dyn std::error::Error>> {
        let table = CapTable::new();
        let parent = next_flow_id();
        let child = next_flow_id();
        let target = next_flow_id();
        let original = table.mint(parent, target, CapRights::SEND_ASK)?;
        let granted = table.delegate(original, parent, child)?;
        assert_ne!(granted, original);
        assert_eq!(
            table.resolve(granted, child, CapRights::SEND)?.target(),
            Some(target)
        );
        assert_eq!(
            table.resolve(original, child, CapRights::SEND),
            Err(CapError::NotHolder)
        );
        assert!(table.resolve(original, parent, CapRights::ASK).is_ok());
        Ok(())
    }

    #[test]
    fn attenuate_cannot_escalate() -> Result<(), Box<dyn std::error::Error>> {
        let table = CapTable::new();
        let parent = next_flow_id();
        let child = next_flow_id();
        let target = next_flow_id();
        let original = table.mint(parent, target, CapRights::SEND)?;
        let granted = table.attenuate(
            original,
            parent,
            child,
            CapRights::SEND.union(CapRights::ADMIN),
            None,
        )?;
        let got = table.resolve(granted, child, CapRights::SEND)?;
        assert!(!got.rights().contains(CapRights::ADMIN));
        assert!(got.rights().contains(CapRights::SEND));
        Ok(())
    }
}