Skip to main content

oris_kernel/kernel/
kernel_mode.rs

1//! Kernel execution mode: controls determinism and replay/verify behavior.
2//!
3//! In **Replay** and **Verify** modes, nondeterministic operations (clock, randomness,
4//! thread spawn) must be trapped so the same run yields an identical event stream.
5
6use serde::{Deserialize, Serialize};
7
8/// Runtime mode for the kernel: determines whether nondeterministic operations are allowed.
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
10pub enum KernelMode {
11    /// Normal execution: no restrictions.
12    Normal,
13    /// Recording a run for later replay; event stream is the source of truth.
14    Record,
15    /// Replaying from the event log; no live side effects, traps on clock/random/spawn.
16    Replay,
17    /// Verifying: same as Replay but also check event stream hash matches expected.
18    Verify,
19}
20
21impl Default for KernelMode {
22    fn default() -> Self {
23        KernelMode::Normal
24    }
25}
26
27impl KernelMode {
28    /// Returns true if clock access, hardware randomness, and thread spawn must be trapped.
29    pub fn traps_nondeterminism(self) -> bool {
30        matches!(self, KernelMode::Replay | KernelMode::Verify)
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn normal_does_not_trap() {
40        assert!(!KernelMode::Normal.traps_nondeterminism());
41    }
42
43    #[test]
44    fn record_does_not_trap() {
45        assert!(!KernelMode::Record.traps_nondeterminism());
46    }
47
48    #[test]
49    fn replay_traps() {
50        assert!(KernelMode::Replay.traps_nondeterminism());
51    }
52
53    #[test]
54    fn verify_traps() {
55        assert!(KernelMode::Verify.traps_nondeterminism());
56    }
57}