detcore-model 0.5.5

Core data model and types for Detcore, the Hermit deterministic execution engine.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

//! Explicit slice-boundary protocol. A complete typed stream keeps ordinary
//! events and actual scheduling boundaries in one causal order.
//!
//! A boundary is evidence of an existing scheduling request, not an instruction
//! or permission to introduce a request. Its identity must come from the actual
//! semantic callback, before the completed slice is reset. Replay expectations
//! cannot supply observed identities. The controller must report any error here
//! as a failed replay, irrespective of the legacy `die_on_desync` setting.

use std::collections::BTreeMap;
use std::fmt;

use reverie_syscalls::Sysno;
use serde::Deserialize;
use serde::Serialize;

use super::InstructionPointer;
use super::SchedEvent;
use super::SigWrapper;
use crate::pid::DetTid;
use crate::time::LogicalTime;

/// Stable owner identity. These are logical generations, never host PIDs or
/// addresses. Exec advances the address-space generation; thread creation must
/// allocate a new thread identity even if a deterministic TID is reused.
#[derive(
    Clone,
    Copy,
    Debug,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Serialize,
    Deserialize
)]
#[serde(deny_unknown_fields)]
pub struct Owner {
    /// Current deterministic TID, which Linux can rebind on non-leader exec.
    pub tid: DetTid,
    /// Incarnation of the current TID slot, including an exec replacement.
    pub thread_generation: u64,
    /// Logical thread's creation TID, retained when exec rebinds its current TID.
    pub birth_tid: DetTid,
    /// Creation incarnation; a later thread reusing that TID is distinct.
    pub birth_generation: u64,
    /// Creator component of the actual model address-space identity.
    pub address_space_creator: DetTid,
    /// Exec-generation component of the actual model address-space identity.
    pub address_space_generation: u64,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum Phase {
    Prehook,
    Posthook,
    /// Existing no-RCB maximum body, after its time/ordinary-event update.
    TimerMaximum,
}

/// The mechanism actually delivered to Detcore. RIP availability does not
/// distinguish these causes, and stripping a timestamp must not erase them.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum Cause {
    SoftwareCheckin,
    PmuTimer,
    NoRcbTimer,
    ExplicitSchedYield,
}

/// A semantic entry into Detcore. Hidden backend stops, injected-call retries
/// and dropped host signals are not entries. A signal delivery number comes
/// from the already selected logical delivery, not its host arrival time.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub enum Entry {
    Syscall {
        syscall: Sysno,
        occurrence: u64,
    },
    Rdtsc,
    Cpuid,
    PmuTimer,
    NoRcbTimer,
    ThreadStart,
    Exec,
    SignalEntry {
        signal: SigWrapper,
        occurrence: u64,
        announced: bool,
    },
    Signal {
        signal: SigWrapper,
        occurrence: u64,
        announced: bool,
        delivery: u64,
    },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Identity {
    pub owner: Owner,
    pub cause: Cause,
    pub phase: Phase,
    pub entry: Entry,
    /// Committed guest branch count within this address-space generation.
    pub committed_rcbs: u64,
    /// One-based semantic check-in number, independent of host callbacks.
    pub checkin: u64,
    /// One-based completed slice number. Adjacent zero-RCB slices at the same
    /// check-in have different slice numbers.
    pub slice: u64,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Boundary {
    pub identity: Identity,
    /// Always one. A boundary must never be run-length encoded.
    pub count: u32,
    pub start_rip: Option<InstructionPointer>,
    pub end_rip: Option<InstructionPointer>,
    pub end_time: Option<LogicalTime>,
}

/// The stored stream index includes boundary records. Keeping a single stream
/// prevents an inspector from accidentally indexing only guest operations.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum Record {
    Event(SchedEvent),
    TimesliceEnd(Boundary),
}

impl Record {
    /// Match one complete stored record. Only evidence absent from the stored
    /// record is optional; all semantic identity and count fields are exact.
    pub fn check_observation(&self, index: usize, observed: &Record) -> Result<(), Error> {
        let mut comparable = observed.clone();
        match (self, &mut comparable) {
            (Record::Event(e), Record::Event(o)) => {
                if e.end_time.is_none() {
                    o.end_time = None;
                }
                if e.start_rip.is_none() {
                    o.start_rip = None;
                }
                if e.end_rip.is_none() {
                    o.end_rip = None;
                }
            }
            (Record::TimesliceEnd(e), Record::TimesliceEnd(o)) => {
                if e.end_time.is_none() {
                    o.end_time = None;
                }
                if e.start_rip.is_none() {
                    o.start_rip = None;
                }
                if e.end_rip.is_none() {
                    o.end_rip = None;
                }
            }
            _ => {}
        }
        if self != &comparable {
            return Err(Error::Mismatch {
                index,
                expected: Box::new(self.clone()),
                observed: Box::new(observed.clone()),
            });
        }
        Ok(())
    }
}

/// A separate envelope prevents an old `PreemptionRecord` reader from silently
/// ignoring a new metadata field, including for a recording with zero markers.
/// No implicit conversion to a vector of legacy events is provided.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize)]
#[serde(try_from = "Wire")]
pub struct Schedule {
    records: Vec<Record>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Wire {
    protocol: String,
    version: u32,
    record_count: usize,
    boundary_count: usize,
    records: Vec<Record>,
}

/// A validated borrowed wire view; serialization never copies the record tape.
#[derive(Serialize)]
#[serde(rename = "Wire")]
pub struct BorrowedSchedule<'a> {
    protocol: &'static str,
    version: u32,
    record_count: usize,
    boundary_count: usize,
    records: &'a [Record],
}

const PROTOCOL: &str = "hermit-slice-boundaries";
const VERSION: u32 = 1;

/// Named failures, carrying the stored stream index where applicable. These
/// are errors, not recovery hints, skipped edits or successful partial replays.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
    UnsupportedProtocol {
        protocol: String,
        version: u32,
    },
    RecordCount {
        declared: usize,
        actual: usize,
    },
    BoundaryCount {
        declared: usize,
        actual: usize,
    },
    InvalidBoundary {
        index: usize,
        reason: &'static str,
    },
    InvalidEvent {
        index: usize,
        reason: &'static str,
    },
    Mismatch {
        index: usize,
        expected: Box<Record>,
        observed: Box<Record>,
    },
    Exhausted {
        index: usize,
        observed: Box<Record>,
    },
    Incomplete {
        index: usize,
        remaining: usize,
    },
    UnsupportedEdit {
        operation: &'static str,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "slice-boundary protocol refusal: {self:?}")
    }
}

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

impl TryFrom<Wire> for Schedule {
    type Error = Error;

    fn try_from(wire: Wire) -> Result<Self, Error> {
        if wire.protocol != PROTOCOL || wire.version != VERSION {
            return Err(Error::UnsupportedProtocol {
                protocol: wire.protocol,
                version: wire.version,
            });
        }
        if wire.record_count != wire.records.len() {
            return Err(Error::RecordCount {
                declared: wire.record_count,
                actual: wire.records.len(),
            });
        }
        let actual = boundary_count(&wire.records);
        if wire.boundary_count != actual {
            return Err(Error::BoundaryCount {
                declared: wire.boundary_count,
                actual,
            });
        }
        Self::new(wire.records)
    }
}

impl Serialize for Schedule {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.borrowed().serialize(serializer)
    }
}

fn boundary_count(records: &[Record]) -> usize {
    records
        .iter()
        .filter(|record| matches!(record, Record::TimesliceEnd(_)))
        .count()
}

impl Boundary {
    /// Validate the mandatory shape without imposing a stream position. The
    /// full schedule separately checks ordering and generation transitions.
    pub fn validate_shape(&self, index: usize) -> Result<(), Error> {
        let boundary = self;
        let id = self.identity;
        let invalid = |reason| Error::InvalidBoundary { index, reason };
        if boundary.count != 1 {
            return Err(invalid("boundary count must be one"));
        }
        if id.owner.tid.as_raw() <= 0
            || id.owner.birth_tid.as_raw() <= 0
            || id.owner.address_space_creator.as_raw() <= 0
            || id.checkin == 0
            || id.slice == 0
        {
            return Err(invalid("nonpositive owner or zero occurrence"));
        }
        match id.entry {
            Entry::Syscall { occurrence: 0, .. }
            | Entry::Signal { delivery: 0, .. }
            | Entry::Signal { occurrence: 0, .. }
            | Entry::SignalEntry { occurrence: 0, .. } => {
                return Err(invalid("zero syscall or signal occurrence"));
            }
            Entry::Signal { signal, .. } | Entry::SignalEntry { signal, .. }
                if !(1..=64).contains(&signal.raw()) =>
            {
                return Err(invalid("invalid logical signal"));
            }
            _ => {}
        }
        if (matches!(id.entry, Entry::SignalEntry { .. }) && id.phase != Phase::Prehook)
            || (matches!(id.entry, Entry::Signal { .. }) && id.phase != Phase::Posthook)
        {
            return Err(invalid("signal entry/delivery phase mismatch"));
        }
        if id.phase == Phase::TimerMaximum && id.entry != Entry::NoRcbTimer {
            return Err(invalid(
                "maximum-body phase requires the actual no-RCB timer",
            ));
        }
        let cause_matches = match id.cause {
            Cause::PmuTimer => id.entry == Entry::PmuTimer,
            Cause::NoRcbTimer => id.entry == Entry::NoRcbTimer,
            Cause::ExplicitSchedYield => matches!(
                id.entry,
                Entry::Syscall {
                    syscall: Sysno::sched_yield,
                    ..
                }
            ),
            Cause::SoftwareCheckin => !matches!(id.entry, Entry::PmuTimer | Entry::NoRcbTimer),
        };
        if !cause_matches {
            return Err(invalid("cause disagrees with actual semantic entry"));
        }
        Ok(())
    }
}

impl Schedule {
    pub fn new(records: Vec<Record>) -> Result<Self, Error> {
        Self::validate_records(&records)?;
        Ok(Self { records })
    }

    /// Validate the same tape without cloning it for a recording writer.
    pub fn borrow_records(records: &[Record]) -> Result<BorrowedSchedule<'_>, Error> {
        Self::validate_records(records)?;
        Ok(BorrowedSchedule::new(records))
    }

    fn borrowed(&self) -> BorrowedSchedule<'_> {
        BorrowedSchedule::new(&self.records)
    }

    fn validate_records(records: &[Record]) -> Result<(), Error> {
        let mut previous: BTreeMap<Owner, Identity> = BTreeMap::new();
        let mut latest_owner: BTreeMap<DetTid, Owner> = BTreeMap::new();
        let mut latest_birth: BTreeMap<(DetTid, u64), Owner> = BTreeMap::new();
        for (index, record) in records.iter().enumerate() {
            if let Record::Event(event) = record {
                if event.dettid.as_raw() <= 0 || event.count == 0 {
                    return Err(Error::InvalidEvent {
                        index,
                        reason: "event owner and count must be positive",
                    });
                }
                // A slice-boundary recording stops at every timeslice end: its
                // writer notes no background starts, so it states a format
                // below the one that may mark an end taken without a stop.
                if event.stopless {
                    return Err(Error::InvalidEvent {
                        index,
                        reason: "a slice-boundary recording marks no event `stopless`",
                    });
                }
                continue;
            }
            let Record::TimesliceEnd(boundary) = record else {
                unreachable!();
            };
            let id = boundary.identity;
            let invalid = |reason| Error::InvalidBoundary { index, reason };
            boundary.validate_shape(index)?;
            if let Some(last) = latest_owner.get(&id.owner.tid)
                && (id.owner.thread_generation < last.thread_generation
                    || (id.owner.thread_generation == last.thread_generation
                        && (id.owner.birth_tid != last.birth_tid
                            || id.owner.birth_generation != last.birth_generation
                            || id.owner.address_space_generation < last.address_space_generation
                            || (id.owner.address_space_generation
                                == last.address_space_generation
                                && id.owner.address_space_creator != last.address_space_creator))))
            {
                return Err(invalid("owner generation moved backwards"));
            }
            let birth = (id.owner.birth_tid, id.owner.birth_generation);
            if let Some(last) = latest_birth.get(&birth)
                && (id.owner.address_space_generation < last.address_space_generation
                    || (id.owner.address_space_generation == last.address_space_generation
                        && id.owner != *last))
            {
                return Err(invalid(
                    "logical survivor moved without a new exec incarnation",
                ));
            }
            if let Some(last) = previous.get(&id.owner) {
                if last.slice.checked_add(1) != Some(id.slice) {
                    return Err(invalid("missing, duplicated or reordered slice occurrence"));
                }
                if id.checkin < last.checkin || id.committed_rcbs < last.committed_rcbs {
                    return Err(invalid("check-in or guest RCB count moved backwards"));
                }
                if id.checkin == last.checkin
                    && (id.entry != last.entry
                        || id.phase != last.phase
                        || id.committed_rcbs != last.committed_rcbs)
                {
                    return Err(invalid("one semantic check-in has conflicting identities"));
                }
            } else if id.slice != 1 {
                return Err(invalid("first completed slice must be one"));
            }
            previous.insert(id.owner, id);
            latest_owner.insert(id.owner.tid, id.owner);
            latest_birth.insert(birth, id.owner);
        }
        Ok(())
    }

    pub fn records(&self) -> &[Record] {
        &self.records
    }

    /// Only the optional outer timestamps are stripped. Semantic identity and
    /// both RIP observations remain, including in a boundary-only recording.
    pub fn strip_times(&mut self) {
        for record in &mut self.records {
            match record {
                Record::Event(event) => event.end_time = None,
                Record::TimesliceEnd(boundary) => boundary.end_time = None,
            }
        }
    }

    /// Boundary-aware schedule editing is not implemented. Refuse before an
    /// editor receives mutable output or loses the explicit protocol header.
    /// This applies even when this particular recording contains zero markers.
    pub fn into_legacy_events(self) -> Result<Vec<SchedEvent>, Error> {
        Err(Error::UnsupportedEdit {
            operation: "discard protocol into legacy event vector",
        })
    }

    pub fn edit(&mut self, operation: Edit) -> Result<(), Error> {
        Err(Error::UnsupportedEdit {
            operation: operation.name(),
        })
    }

    pub fn cursor(&self) -> Cursor<'_> {
        Cursor {
            schedule: self,
            next: 0,
        }
    }
}

impl<'a> BorrowedSchedule<'a> {
    fn new(records: &'a [Record]) -> Self {
        Self {
            protocol: PROTOCOL,
            version: VERSION,
            record_count: records.len(),
            boundary_count: boundary_count(records),
            records,
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum Edit {
    Split,
    Search,
    Minimize,
}

impl Edit {
    fn name(self) -> &'static str {
        match self {
            Self::Split => "split boundary schedule",
            Self::Search => "search boundary schedule",
            Self::Minimize => "minimize boundary schedule",
        }
    }
}

/// Strict protocol cursor. A failed observation never advances it. There is
/// deliberately no fast-forward or permissive-desync option. Production
/// integration must route these errors through the controller refusal path.
pub struct Cursor<'a> {
    schedule: &'a Schedule,
    next: usize,
}

impl Cursor<'_> {
    pub fn stored_index(&self) -> usize {
        self.next
    }

    pub fn expected(&self) -> Option<&Record> {
        self.schedule.records.get(self.next)
    }

    pub fn observe(&mut self, observed: &Record) -> Result<(), Error> {
        let Some(expected) = self.expected() else {
            return Err(Error::Exhausted {
                index: self.next,
                observed: Box::new(observed.clone()),
            });
        };
        expected.check_observation(self.next, observed)?;
        self.next += 1;
        Ok(())
    }

    pub fn finish(self) -> Result<(), Error> {
        let remaining = self.schedule.records.len() - self.next;
        if remaining == 0 {
            Ok(())
        } else {
            Err(Error::Incomplete {
                index: self.next,
                remaining,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schedule::Op;

    #[test]
    fn birth_identity_is_required_and_cannot_move_without_exec() {
        let first = boundary();
        let bytes = serde_json::to_value(&first).unwrap();
        for missing in ["birth_tid", "birth_generation"] {
            let mut old = bytes.clone();
            old["identity"]["owner"]
                .as_object_mut()
                .unwrap()
                .remove(missing);
            assert!(serde_json::from_value::<Boundary>(old).is_err());
        }
        let mut changed = first.clone();
        changed.identity.owner.tid = DetTid::from_raw(4);
        assert!(
            Schedule::new(vec![
                Record::TimesliceEnd(first.clone()),
                Record::TimesliceEnd(changed.clone())
            ])
            .is_err()
        );
        changed.identity.owner.address_space_creator = DetTid::from_raw(4);
        changed.identity.owner.address_space_generation += 1;
        assert!(
            Schedule::new(vec![
                Record::TimesliceEnd(first.clone()),
                Record::TimesliceEnd(changed.clone())
            ])
            .is_ok()
        );
        let mut retired = first.clone();
        retired.identity.slice += 1;
        assert!(
            Schedule::new(vec![
                Record::TimesliceEnd(first),
                Record::TimesliceEnd(changed),
                Record::TimesliceEnd(retired)
            ])
            .is_err()
        );
    }

    fn boundary() -> Boundary {
        Boundary {
            identity: Identity {
                owner: Owner {
                    tid: DetTid::from_raw(3),
                    thread_generation: 0,
                    birth_tid: DetTid::from_raw(3),
                    birth_generation: 0,
                    address_space_creator: DetTid::from_raw(3),
                    address_space_generation: 0,
                },
                cause: Cause::SoftwareCheckin,
                phase: Phase::Posthook,
                entry: Entry::Syscall {
                    syscall: Sysno::write,
                    occurrence: 7,
                },
                committed_rcbs: 83,
                checkin: 10,
                slice: 1,
            },
            count: 1,
            start_rip: InstructionPointer::new(0x1000),
            end_rip: InstructionPointer::new(0x1002),
            end_time: Some(LogicalTime::from_rcbs(83)),
        }
    }

    fn one() -> Schedule {
        Schedule::new(vec![Record::TimesliceEnd(boundary())]).unwrap()
    }

    #[test]
    fn borrowed_wire_preserves_owned_bytes_and_the_complete_validator() {
        for schedule in [Schedule::new(vec![]).unwrap(), one()] {
            let wire = Wire {
                protocol: PROTOCOL.into(),
                version: VERSION,
                record_count: schedule.records.len(),
                boundary_count: boundary_count(&schedule.records),
                records: schedule.records.clone(),
            };
            let expected = serde_json::to_vec(&wire).unwrap();
            assert_eq!(serde_json::to_vec(&schedule).unwrap(), expected);
            assert_eq!(
                serde_json::to_vec(&Schedule::borrow_records(schedule.records()).unwrap()).unwrap(),
                expected
            );
        }
        let mut invalid = one().records;
        invalid.extend(invalid.clone());
        assert!(Schedule::new(invalid.clone()).is_err());
        assert!(Schedule::borrow_records(&invalid).is_err());
    }

    #[test]
    fn envelope_round_trip_preserves_every_field() {
        let schedule = one();
        let encoded = serde_json::to_vec(&schedule).unwrap();
        assert_eq!(
            serde_json::from_slice::<Schedule>(&encoded).unwrap(),
            schedule
        );
        assert_eq!(
            serde_json::to_vec(&serde_json::from_slice::<Schedule>(&encoded).unwrap()).unwrap(),
            encoded
        );
    }

    #[test]
    fn empty_schedule_still_requires_supported_protocol() {
        let original = Schedule::new(vec![]).unwrap();
        let value = serde_json::to_value(&original).unwrap();
        assert_eq!(value["protocol"], PROTOCOL);
        assert_eq!(value["version"], VERSION);
        for key in ["protocol", "version", "record_count", "boundary_count"] {
            let mut bad = value.clone();
            bad.as_object_mut().unwrap().remove(key);
            assert!(
                serde_json::from_value::<Schedule>(bad).is_err(),
                "missing {key}"
            );
        }
        for bad_version in [0, 2, u32::MAX] {
            let mut bad = value.clone();
            bad["version"] = bad_version.into();
            assert!(serde_json::from_value::<Schedule>(bad).is_err());
        }
        let mut bad = value;
        bad["protocol"] = "unknown".into();
        assert!(serde_json::from_value::<Schedule>(bad).is_err());
    }

    #[test]
    fn deleting_sole_boundary_cannot_select_legacy_replay() {
        let mut bad = serde_json::to_value(one()).unwrap();
        bad["records"] = serde_json::json!([]);
        assert!(serde_json::from_value::<Schedule>(bad.clone()).is_err());
        bad["record_count"] = 0.into();
        assert!(serde_json::from_value::<Schedule>(bad.clone()).is_err());
        bad["boundary_count"] = 0.into();
        let empty: Schedule = serde_json::from_value(bad).unwrap();
        assert!(matches!(
            empty.clone().into_legacy_events(),
            Err(Error::UnsupportedEdit { .. })
        ));
        assert!(matches!(
            empty.cursor().observe(&Record::TimesliceEnd(boundary())),
            Err(Error::Exhausted { index: 0, .. })
        ));
    }

    #[test]
    fn malformed_fields_are_not_ignored() {
        let value = serde_json::to_value(one()).unwrap();
        for pointer in [
            "",
            "/records/0/TimesliceEnd",
            "/records/0/TimesliceEnd/identity",
            "/records/0/TimesliceEnd/identity/owner",
        ] {
            let mut bad = value.clone();
            bad.pointer_mut(pointer)
                .unwrap()
                .as_object_mut()
                .unwrap()
                .insert("future_field".into(), true.into());
            assert!(
                serde_json::from_value::<Schedule>(bad).is_err(),
                "unknown field at {pointer}"
            );
        }
        let mut bad = value.clone();
        bad["records"][0]["TimesliceEnd"]["identity"]["cause"] = "Unknown".into();
        assert!(serde_json::from_value::<Schedule>(bad).is_err());
        let encoded = serde_json::to_vec(&one()).unwrap();
        assert!(serde_json::from_slice::<Schedule>(&encoded[..encoded.len() - 1]).is_err());
    }

    #[test]
    fn counts_and_cause_are_validated_on_deserialization() {
        for count in [0, 2, u32::MAX] {
            let mut bad = serde_json::to_value(one()).unwrap();
            bad["records"][0]["TimesliceEnd"]["count"] = count.into();
            assert!(serde_json::from_value::<Schedule>(bad).is_err());
        }
        for cause in [
            Cause::PmuTimer,
            Cause::NoRcbTimer,
            Cause::ExplicitSchedYield,
        ] {
            let mut b = boundary();
            b.identity.cause = cause;
            assert!(Schedule::new(vec![Record::TimesliceEnd(b)]).is_err());
        }
        for (cause, entry) in [
            (Cause::PmuTimer, Entry::PmuTimer),
            (Cause::NoRcbTimer, Entry::NoRcbTimer),
            (
                Cause::ExplicitSchedYield,
                Entry::Syscall {
                    syscall: Sysno::sched_yield,
                    occurrence: 1,
                },
            ),
        ] {
            let mut b = boundary();
            b.identity.cause = cause;
            b.identity.entry = entry;
            assert!(Schedule::new(vec![Record::TimesliceEnd(b)]).is_ok());
        }
    }

    #[test]
    fn every_mandatory_identity_field_is_exact_before_cursor_advance() {
        let original = boundary();
        let schedule = one();
        let mut mutations = vec![];
        let mut b = original.clone();
        b.identity.owner.tid = DetTid::from_raw(4);
        mutations.push(b);
        let mut b = original.clone();
        b.identity.owner.thread_generation += 1;
        mutations.push(b);
        let mut b = original.clone();
        b.identity.owner.address_space_generation += 1;
        mutations.push(b);
        let mut b = original.clone();
        b.identity.cause = Cause::ExplicitSchedYield;
        mutations.push(b);
        let mut b = original.clone();
        b.identity.phase = Phase::Prehook;
        mutations.push(b);
        let mut b = original.clone();
        b.identity.committed_rcbs += 1;
        mutations.push(b);
        let mut b = original.clone();
        b.identity.checkin += 1;
        mutations.push(b);
        let mut b = original.clone();
        b.identity.slice += 1;
        mutations.push(b);
        let mut b = original.clone();
        b.identity.entry = Entry::Syscall {
            syscall: Sysno::read,
            occurrence: 7,
        };
        mutations.push(b);
        let mut b = original.clone();
        b.identity.entry = Entry::Syscall {
            syscall: Sysno::write,
            occurrence: 8,
        };
        mutations.push(b);
        let mut b = original.clone();
        b.count = 2;
        mutations.push(b);
        let mut b = original.clone();
        b.end_rip = InstructionPointer::new(0x2000);
        mutations.push(b);
        let mut b = original.clone();
        b.start_rip = None;
        mutations.push(b);
        let mut b = original.clone();
        b.end_time = None;
        mutations.push(b);
        let mut cursor = schedule.cursor();
        for mutation in mutations {
            assert!(matches!(
                cursor.observe(&Record::TimesliceEnd(mutation)),
                Err(Error::Mismatch { index: 0, .. })
            ));
            assert_eq!(cursor.stored_index(), 0);
        }
        cursor.observe(&Record::TimesliceEnd(original)).unwrap();
        cursor.finish().unwrap();
    }

    #[test]
    fn strip_times_retains_payload_and_rip_evidence() {
        let mut schedule = one();
        let before = schedule.clone();
        schedule.strip_times();
        let mut expected = boundary();
        expected.end_time = None;
        assert_eq!(schedule.records(), &[Record::TimesliceEnd(expected)]);
        let mut cursor = schedule.cursor();
        cursor.observe(&before.records()[0]).unwrap();
        cursor.finish().unwrap();
        let mut wrong_rcb = boundary();
        wrong_rcb.identity.committed_rcbs += 1;
        assert!(
            schedule
                .cursor()
                .observe(&Record::TimesliceEnd(wrong_rcb))
                .is_err()
        );
    }

    #[test]
    fn adjacent_zero_rcb_boundaries_are_distinct_and_cannot_be_duplicated() {
        let first = boundary();
        let mut second = first.clone();
        second.identity.slice = 2;
        let schedule = Schedule::new(vec![
            Record::TimesliceEnd(first.clone()),
            Record::TimesliceEnd(second.clone()),
        ])
        .unwrap();
        let mut cursor = schedule.cursor();
        cursor
            .observe(&Record::TimesliceEnd(first.clone()))
            .unwrap();
        assert!(
            cursor
                .observe(&Record::TimesliceEnd(first.clone()))
                .is_err()
        );
        assert_eq!(cursor.stored_index(), 1);
        cursor.observe(&Record::TimesliceEnd(second)).unwrap();
        cursor.finish().unwrap();
        assert!(
            Schedule::new(vec![
                Record::TimesliceEnd(first.clone()),
                Record::TimesliceEnd(first)
            ])
            .is_err()
        );
    }

    #[test]
    fn missing_and_reordered_slices_are_refused_even_with_equal_rcbs() {
        let first = boundary();
        for slice in [0, 1, 3, u64::MAX] {
            let mut second = first.clone();
            second.identity.slice = slice;
            assert!(
                Schedule::new(vec![
                    Record::TimesliceEnd(first.clone()),
                    Record::TimesliceEnd(second)
                ])
                .is_err()
            );
        }
    }

    #[test]
    fn repeated_site_requires_actual_checkin_and_syscall_occurrence() {
        let first = boundary();
        let mut second = first.clone();
        second.identity.slice += 1;
        second.identity.checkin += 1;
        second.identity.entry = Entry::Syscall {
            syscall: Sysno::write,
            occurrence: 8,
        };
        let mut schedule = Schedule::new(vec![
            Record::TimesliceEnd(first.clone()),
            Record::TimesliceEnd(second.clone()),
        ])
        .unwrap();
        schedule.strip_times();
        let mut cursor = schedule.cursor();
        cursor
            .observe(&Record::TimesliceEnd(first.clone()))
            .unwrap();
        let mut stale = first;
        stale.identity.slice = 2;
        assert!(cursor.observe(&Record::TimesliceEnd(stale)).is_err());
        assert_eq!(cursor.stored_index(), 1);
        cursor.observe(&Record::TimesliceEnd(second)).unwrap();
    }

    #[test]
    fn exec_generation_does_not_accept_an_old_boundary() {
        let old = boundary();
        let mut new = old.clone();
        new.identity.owner.address_space_generation += 1;
        new.identity.committed_rcbs = 0;
        new.identity.checkin = 1;
        let schedule = Schedule::new(vec![
            Record::TimesliceEnd(old.clone()),
            Record::TimesliceEnd(new.clone()),
        ])
        .unwrap();
        let mut cursor = schedule.cursor();
        cursor.observe(&Record::TimesliceEnd(old.clone())).unwrap();
        assert!(cursor.observe(&Record::TimesliceEnd(old)).is_err());
        cursor.observe(&Record::TimesliceEnd(new)).unwrap();
        cursor.finish().unwrap();
    }

    #[test]
    fn serialized_stream_cannot_return_to_an_old_generation() {
        let old = boundary();
        let mut new = old.clone();
        new.identity.owner.address_space_generation += 1;
        new.identity.committed_rcbs = 0;
        new.identity.checkin = 1;
        let mut stale = old.clone();
        stale.identity.slice = 2;
        assert!(
            Schedule::new(vec![
                Record::TimesliceEnd(old),
                Record::TimesliceEnd(new),
                Record::TimesliceEnd(stale),
            ])
            .is_err()
        );
    }

    #[test]
    fn recorded_cause_is_not_inferred_from_rip_or_rcb() {
        let mut timer = boundary();
        timer.identity.cause = Cause::PmuTimer;
        timer.identity.entry = Entry::PmuTimer;
        let schedule = Schedule::new(vec![Record::TimesliceEnd(timer)]).unwrap();
        assert!(
            schedule
                .cursor()
                .observe(&Record::TimesliceEnd(boundary()))
                .is_err()
        );
    }

    #[test]
    fn missing_extra_and_skipped_records_cannot_report_completion() {
        assert!(matches!(
            one().cursor().finish(),
            Err(Error::Incomplete {
                index: 0,
                remaining: 1
            })
        ));
        let schedule = one();
        let mut cursor = schedule.cursor();
        cursor.observe(&Record::TimesliceEnd(boundary())).unwrap();
        assert!(matches!(
            cursor.observe(&Record::TimesliceEnd(boundary())),
            Err(Error::Exhausted { index: 1, .. })
        ));
        assert_eq!(cursor.stored_index(), 1);
        let event = Record::Event(SchedEvent::branches(DetTid::from_raw(3), 83));
        let schedule = Schedule::new(vec![
            event.clone(),
            Record::TimesliceEnd(boundary()),
            event.clone(),
        ])
        .unwrap();
        let mut cursor = schedule.cursor();
        cursor.observe(&event).unwrap();
        assert!(cursor.observe(&event).is_err());
        assert_eq!(cursor.stored_index(), 1);
        assert!(cursor.finish().is_err());
    }

    #[test]
    fn stored_indices_include_boundaries() {
        let event = Record::Event(SchedEvent::branches(DetTid::from_raw(3), 83));
        let schedule = Schedule::new(vec![
            event.clone(),
            Record::TimesliceEnd(boundary()),
            event.clone(),
        ])
        .unwrap();
        let mut cursor = schedule.cursor();
        for (index, record) in schedule.records().iter().enumerate() {
            assert_eq!(cursor.stored_index(), index);
            assert_eq!(cursor.expected(), Some(record));
            cursor.observe(record).unwrap();
        }
        assert_eq!(cursor.stored_index(), 3);
        cursor.finish().unwrap();
    }

    #[test]
    fn unsupported_edits_fail_before_output_changes() {
        for mut schedule in [one(), Schedule::new(vec![]).unwrap()] {
            let before = serde_json::to_vec(&schedule).unwrap();
            for edit in [Edit::Split, Edit::Search, Edit::Minimize] {
                assert!(matches!(
                    schedule.edit(edit),
                    Err(Error::UnsupportedEdit { .. })
                ));
                assert_eq!(serde_json::to_vec(&schedule).unwrap(), before);
            }
            assert!(schedule.into_legacy_events().is_err());
        }
    }

    #[test]
    fn legacy_operation_tags_are_unchanged() {
        for (op, json, tag) in [
            (Op::Branch, "\"Branch\"", 0),
            (Op::Rdtsc, "\"Rdtsc\"", 1),
            (Op::Cpuid, "\"Cpuid\"", 2),
            (Op::OtherInstructions, "\"OtherInstructions\"", 4),
        ] {
            assert_eq!(serde_json::to_string(&op).unwrap(), json);
            assert_eq!(
                bincode::serde::encode_to_vec(op, bincode::config::standard()).unwrap(),
                vec![tag]
            );
        }
    }
}