hermit-detcore 0.4.0

Detcore: the deterministic scheduler and syscall determinization core of the Hermit 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
/*
 * 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.
 */

use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::task::Context;
use std::task::Poll;
use std::time::SystemTime;

use futures::Future;
use reverie::ExitStatus;
use reverie::Tool;

use super::*;

fn failure(tid: DetTid) -> reverie::BackendFailure {
    reverie::BackendFailure {
        pid: Tid::from_raw(tid.as_raw()),
        tid: Tid::from_raw(tid.as_raw()),
        phase: "native consuming cleanup control",
    }
}

struct WakeCount(AtomicUsize);

impl futures::task::ArcWake for WakeCount {
    fn wake_by_ref(this: &Arc<Self>) {
        this.0.fetch_add(1, Ordering::SeqCst);
    }
}

#[test]
fn backend_failure_wakes_each_global_subscriber_and_late_arrivals() {
    let (_, state, tid, _) = cancellation_test_state();
    let mut first = std::pin::pin!(state.wait_for_backend_failure());
    let mut second = std::pin::pin!(state.wait_for_backend_failure());
    let first_wakes = Arc::new(WakeCount(AtomicUsize::new(0)));
    let second_wakes = Arc::new(WakeCount(AtomicUsize::new(0)));
    let first_waker = futures::task::waker(first_wakes.clone());
    let second_waker = futures::task::waker(second_wakes.clone());
    let mut first_context = Context::from_waker(&first_waker);
    let mut second_context = Context::from_waker(&second_waker);
    assert!(first.as_mut().poll(&mut first_context).is_pending());
    assert!(second.as_mut().poll(&mut second_context).is_pending());
    state.report_backend_failure(failure(tid));
    assert!(first_wakes.0.load(Ordering::SeqCst) > 0);
    assert!(second_wakes.0.load(Ordering::SeqCst) > 0);
    assert!(first.as_mut().poll(&mut first_context).is_ready());
    assert!(second.as_mut().poll(&mut second_context).is_ready());
    let mut late = std::pin::pin!(state.wait_for_backend_failure());
    assert!(late.as_mut().poll(&mut first_context).is_ready());
    state.report_backend_failure(failure(DetTid::from_raw(99)));
    assert!(state.sched.lock().unwrap().backend_failed());
}

// The transport only binds the real sender. The production GlobalState owns
// replies, clock updates, registration checks and consuming deregistration.
struct CleanupRpc<'a> {
    state: &'a GlobalState,
    tid: DetTid,
    calls: AtomicUsize,
}

#[reverie::tool]
impl GlobalRPC<GlobalState> for CleanupRpc<'_> {
    async fn send_rpc(
        &self,
        request: <GlobalState as GlobalTool>::Request,
    ) -> <GlobalState as GlobalTool>::Response {
        assert!(matches!(request.2, GlobalRequest::DeregisterThread(_)));
        self.calls.fetch_add(1, Ordering::SeqCst);
        self.state
            .receive_rpc(Tid::from_raw(self.tid.as_raw()), request)
            .await
    }

    fn config(&self) -> &Config {
        &self.state.cfg
    }
}

async fn consume_thread(state: &GlobalState, tool: &Detcore, thread: crate::ThreadState<()>) {
    let rpc = CleanupRpc {
        state,
        tid: thread.dettid,
        calls: AtomicUsize::new(0),
    };
    let mut completion = std::pin::pin!(tool.on_exit_thread(
        Tid::from_raw(thread.dettid.as_raw()),
        &rpc,
        thread,
        ExitStatus::Exited(0),
    ));
    assert!(matches!(
        futures::poll!(completion.as_mut()),
        Poll::Ready(Ok(()))
    ));
    assert_eq!(rpc.calls.load(Ordering::SeqCst), 1);
}

async fn selected_cleanup(daemon_first: bool) {
    let (config, state, leader, _) = cancellation_test_state();
    let tool = Detcore::new(Tid::from_raw(leader.as_raw()), &config);
    let mut thread = tool.init_thread_state(Tid::from_raw(leader.as_raw()), None);
    thread.detpid = Some(leader);
    thread.thread_logical_time.add_syscall_with_cost(37);
    let waiter = DetTid::from_raw(19);
    let selected = DetTid::from_raw(23);
    let address = 0x404100;
    let futex = FutexID::private(thread.mm_id, address);
    let waiter_response;
    {
        let mut sched = state.sched.lock().unwrap();
        for tid in [waiter, selected] {
            sched.thread_tree.add_child(leader, tid, false);
        }
    }
    install_test_registration(&state, leader, Ivar::new());
    install_test_registration(&state, selected, Ivar::new());
    {
        let mut sched = state.sched.lock().unwrap();
        sched.next_turns.get_mut(&leader).unwrap().child_tid_addr = address;
        sched.next_turns.insert(
            waiter,
            ThreadNextTurn {
                dettid: waiter,
                child_tid_addr: 0,
                req: Ivar::new(),
                resp: Ivar::new(),
                protocol: Default::default(),
            },
        );
        sched.priorities.insert(waiter, DEFAULT_PRIORITY);
        sched.sleep_futex_waiter(&waiter, futex, None, u32::MAX);
        waiter_response = sched.next_turns[&waiter].resp.clone();
    }
    let (chosen, request, response) = state.sched.lock().unwrap().select_test_turn().unwrap();
    let mut daemon = std::pin::pin!(crate::scheduler::finish_selected_turn(
        state.sched.clone(),
        state.global_time.clone(),
        chosen,
        request,
        response.clone(),
    ));
    assert!(futures::poll!(daemon.as_mut()).is_pending());
    assert!(
        state
            .sched
            .lock()
            .unwrap()
            .run_queue
            .tentative_pop_in_progress()
    );
    let before = state.global_time.lock().unwrap().as_nanos();
    state.report_backend_failure(failure(selected));
    assert!(
        !state
            .sched
            .lock()
            .unwrap()
            .run_queue
            .tentative_pop_in_progress()
    );
    if daemon_first {
        assert!(matches!(
            futures::poll!(daemon.as_mut()),
            Poll::Ready(Err(_))
        ));
    }
    // In the other ordering, this real consuming hook performs the clear-TID
    // admission before the daemon is allowed another poll.
    consume_thread(&state, &tool, thread).await;
    if !daemon_first {
        assert!(matches!(
            futures::poll!(daemon.as_mut()),
            Poll::Ready(Err(_))
        ));
    }
    assert!(
        response.try_read().is_none(),
        "failure must not fabricate a response"
    );
    assert!(
        waiter_response.try_read().is_none(),
        "cleanup must not grant the waiter"
    );
    let after = state.global_time.lock().unwrap().as_nanos();
    assert_eq!(after, before + LogicalTime::from_nanos(37));
    {
        let mut sched = state.sched.lock().unwrap();
        assert_eq!(sched.turn, 0);
        assert!(sched.child_tid_was_cleared(futex, leader.as_raw()));
        assert_eq!(
            sched.run_queue.tids().filter(|tid| **tid == waiter).count(),
            1
        );
        assert!(!sched.next_turns.contains_key(&leader));
        assert!(
            !sched.note_deregistration_accounted(leader),
            "one accounting owner"
        );
    }
    assert!(
        crate::scheduler::do_a_turn_blocking(
            state.sched.clone(),
            state.global_time.clone(),
            &Err(crate::scheduler::SkipTurn),
        )
        .await
        .is_err()
    );
    assert_eq!(state.global_time.lock().unwrap().as_nanos(), after);
    assert!(waiter_response.try_read().is_none());
}

#[tokio::test]
async fn backend_failure_closes_selection_before_consuming_clear_tid_cleanup() {
    selected_cleanup(false).await;
    selected_cleanup(true).await;
}

#[tokio::test]
async fn backend_failure_ends_quiescence_without_another_request() {
    let (_, state, tid, _) = cancellation_test_state();
    let request = Ivar::new();
    install_test_registration(&state, tid, request.clone());
    let before = state.global_time.lock().unwrap().as_nanos();
    let last = Err(crate::scheduler::SkipTurn);
    let mut turn = std::pin::pin!(crate::scheduler::do_a_turn_blocking(
        state.sched.clone(),
        state.global_time.clone(),
        &last,
    ));
    assert!(futures::poll!(turn.as_mut()).is_pending());
    state.report_backend_failure(failure(tid));
    assert!(matches!(futures::poll!(turn.as_mut()), Poll::Ready(Err(_))));
    assert!(request.try_read().is_none());
    assert_eq!(state.sched.lock().unwrap().turn, 0);
    assert_eq!(state.global_time.lock().unwrap().as_nanos(), before);
}

#[tokio::test]
async fn backend_failure_completes_unstarted_daemon_without_aborting_it() {
    let config = Config {
        sequentialize_threads: true,
        ..Config::default()
    };
    let mut state = GlobalState::initialize(&config, true);
    tokio::task::yield_now().await;
    assert!(!state.sched_handle.as_ref().unwrap().is_finished());
    state.report_backend_failure(failure(DetTid::from_raw(17)));
    tokio::time::timeout(
        Duration::from_millis(100),
        state.sched_handle.take().unwrap(),
    )
    .await
    .expect("terminal startup must complete")
    .expect("daemon must not panic or be aborted");
    assert!(state.sched.lock().unwrap().started_up.try_read().is_none());
    state.clean_up(false, &None).await;
}

#[tokio::test]
async fn backend_failure_completes_registered_daemon_without_another_request() {
    let config = Config {
        sequentialize_threads: true,
        ..Config::default()
    };
    let mut state = GlobalState::initialize(&config, true);
    let tid = DetTid::from_raw(17);
    state
        .sched
        .lock()
        .unwrap()
        .thread_tree
        .add_child(tid, tid, true);
    let request = Ivar::new();
    install_test_registration(&state, tid, request.clone());
    state.sched.lock().unwrap().started_up.put(());
    tokio::task::yield_now().await;
    assert!(!state.sched_handle.as_ref().unwrap().is_finished());
    state.report_backend_failure(failure(tid));
    tokio::time::timeout(
        Duration::from_millis(100),
        state.sched_handle.take().unwrap(),
    )
    .await
    .expect("terminal quiescence must complete")
    .expect("daemon must not panic or be aborted");
    assert!(request.try_read().is_none());
    assert_eq!(state.sched.lock().unwrap().turn, 0);
    state.clean_up(false, &None).await;
}

#[tokio::test]
async fn backend_failure_precedes_ready_request_without_grant_or_timer_processing() {
    let (_, state, tid, process) = cancellation_test_state();
    let request = Ivar::full(Ok(Resources::new(tid)));
    install_test_registration(&state, tid, request);
    let before = state.global_time.lock().unwrap().as_nanos();
    let deadline = before + LogicalTime::from_nanos(37);
    state.sched.lock().unwrap().register_alarm(
        process,
        tid,
        before,
        LogicalTime::from_nanos(37),
        LogicalTime::ZERO,
        Signal::SIGALRM,
    );
    let (selected, request, response) = state.sched.lock().unwrap().select_test_turn().unwrap();
    state.report_backend_failure(failure(tid));
    assert!(
        crate::scheduler::finish_selected_turn(
            state.sched.clone(),
            state.global_time.clone(),
            selected,
            request,
            response.clone(),
        )
        .await
        .is_err()
    );
    assert!(
        crate::scheduler::do_a_turn_blocking(
            state.sched.clone(),
            state.global_time.clone(),
            &Err(crate::scheduler::SkipTurn),
        )
        .await
        .is_err()
    );
    assert!(response.try_read().is_none());
    assert_eq!(state.global_time.lock().unwrap().as_nanos(), before);
    let sched = state.sched.lock().unwrap();
    assert_eq!(sched.turn, 0);
    assert_eq!(sched.blocked.timed_waiters.next_deadline(), Some(deadline));
}

async fn cancelled_constructed_child(register: bool, terminal: bool) {
    let (config, state, parent, process) = cancellation_test_state();
    install_test_registration(&state, parent, Ivar::new());
    let tool = Detcore::new(Tid::from_raw(process.as_raw()), &config);
    let mut parent_thread = tool.init_thread_state(Tid::from_raw(parent.as_raw()), None);
    parent_thread.detpid = Some(process);
    parent_thread.thread_start_entered = true;
    let flags = CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM | CloneFlags::CLONE_CHILD_CLEARTID;
    parent_thread.clone_flags = Some(flags);
    let child = DetTid::from_raw(18);
    let child_thread = tool.init_thread_state(
        Tid::from_raw(child.as_raw()),
        Some((Tid::from_raw(parent.as_raw()), &parent_thread)),
    );
    assert_eq!(
        child_thread.detpid, None,
        "exercise the real delayed initialization"
    );
    assert!(
        !child_thread.thread_start_entered,
        "child construction resets the parent's marker"
    );
    parent_thread.clone_flags = None;
    let mut guest = ExternalRegistrationGuest {
        global: &state,
        config: &config,
        thread: parent_thread,
        requests: Mutex::new(Vec::new()),
        post_exec: false,
    };
    if register {
        let mut registration = std::pin::pin!(tool.register_external_child(
            &mut guest,
            Tid::from_raw(child.as_raw()),
            0x404100,
            flags,
            0,
            None,
        ));
        assert!(futures::poll!(registration.as_mut()).is_pending());
        assert!(state.sched.lock().unwrap().next_turns.contains_key(&child));
        if terminal {
            state.report_backend_failure(failure(parent));
        }
        // Drop the actual parked parent callback, as the KVM driver does on
        // terminal notification. No ParentContinue reply or child start occurs.
    } else {
        if terminal {
            state.report_backend_failure(failure(parent));
        }
    }
    let before = serde_json::to_value(&*state.global_time.lock().unwrap()).unwrap();
    consume_thread(&state, &tool, child_thread).await;
    let sched = state.sched.lock().unwrap();
    assert_eq!(sched.backend_failed(), terminal);
    assert_eq!(sched.thread_was_registered(child), register);
    assert!(!sched.next_turns.contains_key(&child));
    assert_eq!(sched.turn, 0);
    assert!(sched.next_turns[&parent].resp.try_read().is_none());
    if !register {
        assert_eq!(
            serde_json::to_value(&*state.global_time.lock().unwrap()).unwrap(),
            before
        );
        assert!(!state.global_time.lock().unwrap().contains_thread(child));
    } else {
        assert!(sched.thread_is_logically_killed(child));
    }
}

#[tokio::test]
async fn backend_failure_consumes_registered_child_before_thread_start() {
    cancelled_constructed_child(true, true).await;
}

#[tokio::test]
async fn backend_failure_consumes_unregistered_child_without_admission_or_clock() {
    cancelled_constructed_child(false, true).await;
}

#[tokio::test]
async fn normal_cancellation_consumes_unstarted_child_without_registration() {
    cancelled_constructed_child(false, false).await;
}

#[tokio::test]
async fn backend_failure_blocks_rpc_clock_and_child_registration_at_admission() {
    let (_, state, parent, process) = cancellation_test_state();
    install_test_registration(&state, parent, Ivar::new());
    let child = DetTid::from_raw(18);
    let before = serde_json::to_value(&*state.global_time.lock().unwrap()).unwrap();
    state.report_backend_failure(failure(parent));
    let mut time = DetTime::new(&state.cfg);
    time.add_syscall_with_cost(37);
    let mut rpc = std::pin::pin!(state.receive_rpc(
        Tid::from_raw(parent.as_raw()),
        (
            time,
            MmId::initial(process),
            GlobalRequest::CreateChildThread(
                child,
                process,
                0,
                Some(CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM),
                0,
                None,
                Some(DEFAULT_PRIORITY),
            )
        ),
    ));
    assert!(
        futures::poll!(rpc.as_mut()).is_pending(),
        "no fabricated normal reply"
    );
    assert_eq!(
        serde_json::to_value(&*state.global_time.lock().unwrap()).unwrap(),
        before
    );
    let sched = state.sched.lock().unwrap();
    assert!(!sched.thread_was_registered(child));
    assert!(!sched.next_turns.contains_key(&child));
    assert!(sched.next_turns[&parent].req.try_read().is_none());
    assert_eq!(sched.turn, 0);
}

#[tokio::test]
async fn backend_failure_blocks_registration_that_passed_earlier_header_admission() {
    let (_, state, parent, process) = cancellation_test_state();
    install_test_registration(&state, parent, Ivar::new());
    let child = DetTid::from_raw(18);
    state.report_backend_failure(failure(parent));
    // Enter the actual second mutation boundary directly, representing an RPC
    // whose header won the mutex before failure but whose registration did not.
    let mut rpc = std::pin::pin!(state.recv_create_child_thread(
        Tid::from_raw(parent.as_raw()),
        MmId::initial(process),
        super::super::ChildRegistration {
            parent_dettid: parent,
            parent_detpid: process,
            child_dettid: child,
            child_tid_addr: 0,
            flags: Some(CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM),
            exit_signal: 0,
            physical_ids: None,
            maybe_priority: Some(DEFAULT_PRIORITY),
            parent_is_kernel_blocked: false,
        },
    ));
    assert!(futures::poll!(rpc.as_mut()).is_pending());
    let sched = state.sched.lock().unwrap();
    assert!(!sched.thread_was_registered(child));
    assert!(!sched.next_turns.contains_key(&child));
    assert!(sched.next_turns[&parent].req.try_read().is_none());
}

async fn unknown_deregistration(
    sender: i32,
    owner: i32,
    header_mm: i32,
    owner_mm: i32,
    started: bool,
) {
    let (_, state, _, _) = cancellation_test_state();
    let _ = state
        .receive_rpc(
            Tid::from_raw(sender),
            (
                DetTime::new(&state.cfg),
                MmId::initial(DetPid::from_raw(header_mm)),
                GlobalRequest::DeregisterThread(ThreadDeregistration {
                    dettid: DetTid::from_raw(owner),
                    detpid: DetPid::from_raw(owner_mm),
                    mm: MmId::initial(DetPid::from_raw(owner_mm)),
                    thread_start_entered: started,
                    timeslice_stats: TimesliceStats::default(),
                    syscall_count: 0,
                    chaos_epochs: Vec::new(),
                }),
            ),
        )
        .await;
}

#[tokio::test]
#[should_panic(expected = "a started thread must have a scheduler registration")]
async fn ordinary_unknown_started_owner_is_not_acknowledged_as_unstarted() {
    unknown_deregistration(19, 19, 17, 17, true).await;
}

#[tokio::test]
async fn dbt_missing_physical_id_start_preserves_tombstone_deregistration_accounting() {
    let config = Config {
        sequentialize_threads: true,
        cancel_killed_thread_rpcs: true,
        backend_requires_thread_directed_process_signals: true,
        ..Config::default()
    };
    let state = GlobalState::initialize(&config, false);
    let tid = DetTid::from_raw(19);
    let process = DetPid::from_raw(17);
    let mm = MmId::initial(process);
    // The parent process is already running while its new child's Start RPC
    // races the parent's CreateChildThread. Only the child is unregistered.
    state
        .sched
        .lock()
        .unwrap()
        .thread_tree
        .add_child(process, process, true);
    install_test_registration(&state, process, Ivar::new());
    {
        let sched = state.sched.lock().unwrap();
        assert!(sched.thread_was_registered(process));
        assert!(!sched.thread_was_registered(tid));
    }
    // Exercise the real DBT defensive refusal before CreateChildThread. Do
    // not install the child registration or fabricate its logical tombstone.
    let response = tokio::time::timeout(
        Duration::from_millis(100),
        state.receive_rpc(
            Tid::from_raw(tid.as_raw()),
            (
                DetTime::new(&config),
                mm,
                GlobalRequest::StartNewThread(tid, process, None, None),
            ),
        ),
    )
    .await
    .expect("a missing physical ID must not wait for parent registration");
    assert_eq!(response, (None, GlobalResponse::ThreadExited));
    {
        let sched = state.sched.lock().unwrap();
        assert!(!sched.backend_failed());
        assert!(!sched.thread_was_registered(tid));
        assert!(sched.thread_is_logically_killed(tid));
    }
    let before = serde_json::to_value(&*state.global_time.lock().unwrap()).unwrap();
    let mut stats = TimesliceStats::default();
    stats.record(7);
    // Distinct final and duplicate payloads prove that the consuming RPC
    // reaches existing accounting exactly once, rather than an early reply.
    for count in [17, 99] {
        let response = state
            .receive_rpc(
                Tid::from_raw(tid.as_raw()),
                (
                    DetTime::new(&config),
                    mm,
                    GlobalRequest::DeregisterThread(ThreadDeregistration {
                        dettid: tid,
                        detpid: process,
                        mm,
                        thread_start_entered: true,
                        timeslice_stats: stats,
                        syscall_count: count,
                        chaos_epochs: Vec::new(),
                    }),
                ),
            )
            .await;
        assert_eq!(response, (None, GlobalResponse::DeregisterThread(())));
        let mut sched = state.sched.lock().unwrap();
        assert!(!sched.note_deregistration_accounted(tid));
        assert_eq!(sched.per_thread_timeslice.get(&tid), Some(&stats));
        assert_eq!(sched.per_thread_syscalls.get(&tid), Some(&17));
        assert!(!sched.next_turns.contains_key(&tid));
        assert_eq!(sched.turn, 0);
    }
    assert_eq!(
        serde_json::to_value(&*state.global_time.lock().unwrap()).unwrap(),
        before
    );
}

#[tokio::test]
async fn backend_failure_cleanup_does_not_build_an_invalid_run_summary() {
    let config = Config {
        sequentialize_threads: true,
        ..Config::default()
    };
    let future = SystemTime::now() + Duration::from_secs(60);
    let mut premise = GlobalState::initialize(&config, false);
    premise.realtime_start = future;
    assert!(premise.into_run_summary().is_err());
    let directory = tempfile::tempdir().unwrap();
    let recording = directory.path().join("partial-preemptions.json");
    let config = Config {
        record_preemptions: true,
        record_preemptions_to: Some(recording.clone()),
        ..config
    };
    let mut state = GlobalState::initialize(&config, true);
    state.realtime_start = future;
    let tid = DetTid::from_raw(17);
    let time = LogicalTime::from_nanos(37);
    {
        let mut sched = state.sched.lock().unwrap();
        let writer = sched.preemption_writer.as_mut().unwrap();
        writer.register_thread(tid, DEFAULT_PRIORITY);
        writer.insert_reprioritization(tid, time, 23, DEFAULT_PRIORITY, DEFAULT_PRIORITY + 1);
    }
    assert!(!recording.exists());
    state.report_backend_failure(failure(tid));
    let cleanup = tokio::time::timeout(
        Duration::from_millis(100),
        state.clean_up_after_backend_failure(),
    )
    .await
    .expect("the failed daemon must finish naturally");
    assert!(cleanup.scheduler.is_ok());
    assert!(cleanup.preemption_recording.is_ok());
    let actual: crate::preemptions::PreemptionRecord =
        serde_json::from_slice(&std::fs::read(recording).unwrap()).unwrap();
    actual.validate().unwrap();
    let mut expected = crate::preemptions::ThreadHistory::new()
        .with_prio_changes(vec![(time, DEFAULT_PRIORITY)])
        .with_preemption_rcbs(vec![23]);
    expected.final_prio = DEFAULT_PRIORITY + 1;
    assert_eq!(
        actual.extract_all(),
        std::collections::BTreeMap::from([(tid, expected)])
    );
    assert!(actual.schedevents().is_empty());
}

#[tokio::test]
async fn backend_failure_cleanup_retains_panicked_scheduler_and_recording_error() {
    let directory = tempfile::tempdir().unwrap();
    let config = Config {
        sequentialize_threads: true,
        record_preemptions: true,
        record_preemptions_to: Some(directory.path().to_path_buf()),
        ..Config::default()
    };
    let mut state = GlobalState::initialize(&config, false);
    let sched = state.sched.clone();
    state.sched_handle = Some(tokio::spawn(async move {
        let _lock = sched.lock().unwrap();
        panic!("scheduler cleanup control");
    }));
    let cleanup = tokio::time::timeout(
        Duration::from_millis(100),
        state.clean_up_after_backend_failure(),
    )
    .await
    .expect("the owned task must finish without an abort");
    assert!(cleanup.scheduler.unwrap_err().is_panic());
    assert!(cleanup.preemption_recording.is_err());
    assert!(directory.path().is_dir());
}

#[tokio::test]
#[should_panic(expected = "deregistration must belong to its sender")]
async fn unstarted_cleanup_rejects_another_senders_identity() {
    unknown_deregistration(19, 23, 17, 17, false).await;
}

#[tokio::test]
#[should_panic(expected = "deregistration must retain its MmId")]
async fn unstarted_cleanup_rejects_a_different_payload_mm() {
    unknown_deregistration(19, 19, 17, 23, false).await;
}

#[tokio::test]
async fn thread_start_marker_is_set_before_the_first_registration_wait() {
    let config = Config {
        sequentialize_threads: true,
        ..Config::default()
    };
    let state = GlobalState::initialize(&config, false);
    let tid = Tid::from_raw(17);
    let tool = Detcore::new(tid, &config);
    let thread = tool.init_thread_state(tid, None);
    assert!(!thread.thread_start_entered);
    let mut guest = ExternalRegistrationGuest {
        global: &state,
        config: &config,
        thread,
        requests: Mutex::new(Vec::new()),
        post_exec: false,
    };
    // This test Guest's pid accessor expects the normal process identity.
    guest.thread.detpid = Some(DetPid::from_raw(17));
    {
        let mut start = std::pin::pin!(tool.handle_thread_start(&mut guest));
        assert!(futures::poll!(start.as_mut()).is_pending());
    }
    assert!(guest.thread.thread_start_entered);
    assert!(
        state
            .sched
            .lock()
            .unwrap()
            .thread_was_registered(DetTid::from_raw(17))
    );
    state.report_backend_failure(failure(DetTid::from_raw(17)));
    consume_thread(&state, &tool, guest.thread).await;
}

#[test]
fn both_fork_and_thread_construction_reset_the_parent_start_marker() {
    let config = Config::default();
    let parent = Tid::from_raw(17);
    let tool: Detcore = Detcore::new(parent, &config);
    let mut state = tool.init_thread_state(parent, None);
    state.thread_start_entered = true;
    for flags in [
        CloneFlags::empty(),
        CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM,
    ] {
        state.clone_flags = Some(flags);
        let child = tool.init_thread_state(Tid::from_raw(19), Some((parent, &state)));
        assert!(!child.thread_start_entered);
        assert!(state.thread_start_entered);
    }
}