snare 3.5.0

Ergonomic in-process network testing on top of snare-interpose: write the code under test against std, describe the peer with a tester builder
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
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
//! Signals and console control events, per sim. The code under test installs its handlers with
//! the OS's own calls — `sigaction`/`signal` on unix, `SetConsoleCtrlHandler` and the CRT's `signal`
//! on Windows — so `ctrlc` and `signal-hook` run unchanged; the dispositions live
//! in the sim and the real process's are never touched. [`Sim::raise_signal`](crate::Sim) delivers
//! a signal as the host OS would: on unix the handler runs synchronously, as for a signal a process
//! sends itself; on Windows the handlers run on a new thread, last registered first.
//!
//! On unix the sim also delivers `SIGCHLD` for each child process its threads make, once, when the
//! child ends ([`crate::realfd::watch_child`]), with the `siginfo_t` the kernel would fill; the
//! kernel's own `SIGCHLD` for that child reaches no handler (`snare_interpose`'s `children`). A
//! handler the sim delivers interrupts a call its thread is blocked in as the host's would
//! (see [`snare_interpose::interruptible`]).
//!
//! [`SignalTable`] is a sim's dispositions; [`SimSignals`] exposes it to `snare_interpose` as the
//! domain's [`Signals`], which the interposed `sigaction`/`signal`/`raise`/`kill`/`pthread_kill`/
//! `pause`/`sigsuspend` (unix) and `SetConsoleCtrlHandler`/`GenerateConsoleCtrlEvent`/CRT
//! `signal`/`raise` (Windows) consult. [`forward`] optionally routes real signals the process
//! receives into the sims that asked for them. Every table lock is taken inside [`snare_interpose::real`] or from a
//! [`Signals`] method (already in passthrough), and never held while a handler of the code under
//! test runs, since a handler may reinstall itself.

use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::Duration;

use snare_interpose::{SignalOutcome, SignalSource, Signals};

use crate::events::RecordedEvent;
use crate::scope::SimShared;

/// A signal or console control event the code under test can be sent.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Signal {
    /// `SIGINT`; `CTRL_C_EVENT` (0) on Windows.
    Interrupt,
    /// `SIGTERM` (unix only).
    Terminate,
    /// `SIGHUP` (unix only).
    Hangup,
    /// `SIGCHLD` (unix only). The sim sends it itself when a child process its threads made ends.
    Child,
    /// `CTRL_BREAK_EVENT` (1, Windows only).
    Break,
    /// `CTRL_CLOSE_EVENT` (2, Windows only).
    Close,
    /// `CTRL_LOGOFF_EVENT` (5, Windows only).
    Logoff,
    /// `CTRL_SHUTDOWN_EVENT` (6, Windows only).
    ///
    /// The event numbers are wincon.h's
    /// ([Microsoft Learn: HandlerRoutine](https://learn.microsoft.com/en-us/windows/console/handlerroutine)).
    Shutdown,
}

impl Signal {
    /// The host OS's number for this signal — a signal number on unix, a console control event on
    /// Windows — or `None` where the OS has no such signal.
    pub fn raw(self) -> Option<i32> {
        #[cfg(unix)]
        {
            match self {
                Signal::Interrupt => Some(libc::SIGINT),
                Signal::Terminate => Some(libc::SIGTERM),
                Signal::Hangup => Some(libc::SIGHUP),
                Signal::Child => Some(libc::SIGCHLD),
                _ => None,
            }
        }
        #[cfg(windows)]
        {
            use windows_sys::Win32::System::Console::{
                CTRL_BREAK_EVENT, CTRL_C_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT,
                CTRL_SHUTDOWN_EVENT,
            };
            let event = match self {
                Signal::Interrupt => CTRL_C_EVENT,
                Signal::Break => CTRL_BREAK_EVENT,
                Signal::Close => CTRL_CLOSE_EVENT,
                Signal::Logoff => CTRL_LOGOFF_EVENT,
                Signal::Shutdown => CTRL_SHUTDOWN_EVENT,
                _ => return None,
            };
            Some(event as i32)
        }
    }

    /// The signal the host OS numbers `raw`, if it is one the sim models. The inverse of
    /// [`Signal::raw`].
    pub fn from_raw(raw: i32) -> Option<Signal> {
        [
            Signal::Interrupt,
            Signal::Terminate,
            Signal::Hangup,
            Signal::Child,
            Signal::Break,
            Signal::Close,
            Signal::Logoff,
            Signal::Shutdown,
        ]
        .into_iter()
        .find(|signal| signal.raw() == Some(raw))
    }
}

/// What delivering a signal did.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SignalDelivery {
    /// A handler of the code under test ran (on Windows: one returned `TRUE`).
    Handled,
    /// Handlers ran, after which Windows would end the process: a close, logoff or shutdown event.
    HandledThenExit,
    /// The OS default action applies — usually ending the process, which the sim never does.
    DefaultAction,
    /// The signal is ignored.
    Ignored,
    /// Every thread of the sim it could go to blocks it: it stays pending until one unblocks it
    /// or takes it with `sigwait` or a `signalfd`.
    Pending,
    /// A thread waiting for it in `sigwait`, `sigwaitinfo` or `sigtimedwait` takes it; no handler
    /// runs.
    Accepted,
    /// The host OS has no such signal.
    Unavailable,
}

/// Who sent a recorded signal.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SignalOrigin {
    /// The test, through [`SignalHandle`] or the `Sim` methods.
    Sim,
    /// The code under test: `raise`, `kill` of its own process, `pthread_kill`,
    /// `GenerateConsoleCtrlEvent`.
    Process,
    /// A real signal forwarded into the sim (see `SimBuilder::forward_real_signals`).
    Real,
    /// A child process a thread of the sim made has ended.
    Child,
}

impl From<SignalOutcome> for SignalDelivery {
    fn from(outcome: SignalOutcome) -> Self {
        match outcome {
            SignalOutcome::Handled => SignalDelivery::Handled,
            SignalOutcome::HandledThenExit => SignalDelivery::HandledThenExit,
            SignalOutcome::DefaultAction => SignalDelivery::DefaultAction,
            SignalOutcome::Ignored => SignalDelivery::Ignored,
            SignalOutcome::Pending => SignalDelivery::Pending,
            SignalOutcome::Accepted => SignalDelivery::Accepted,
        }
    }
}

impl From<SignalSource> for SignalOrigin {
    fn from(source: SignalSource) -> Self {
        match source {
            SignalSource::Sim => SignalOrigin::Sim,
            SignalSource::Process => SignalOrigin::Process,
            SignalSource::Real => SignalOrigin::Real,
            SignalSource::Child => SignalOrigin::Child,
        }
    }
}

/// Sends signals into one sim, from any thread: one of the sim's own, or one outside it.
#[derive(Clone)]
pub struct SignalHandle {
    /// The sim's shared state: its table, its log, and (weakly) its domain.
    shared: Arc<SimShared>,
}

impl std::fmt::Debug for SignalHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SignalHandle").finish_non_exhaustive()
    }
}

impl SignalHandle {
    /// A handle on the sim behind `shared`.
    pub(crate) fn new(shared: Arc<SimShared>) -> Self {
        SignalHandle { shared }
    }

    /// Delivers `signal` and returns what it did. From a thread of the sim it is delivered there,
    /// as a signal the process sent itself; from any other thread, on a new thread of the sim,
    /// joined before this returns.
    ///
    /// A signal sent this way is sent to the sim's process: one with a handler goes to a root thread
    /// of the sim blocked in a call it interrupts, if there is one, and is reported handled before
    /// it runs there (see [`snare_interpose::deliver_here`]).
    ///
    /// [`SignalDelivery::Unavailable`] when the host has no such signal, the sim's domain is gone,
    /// or the delivery thread could not be started. The result slot is written and read inside
    /// [`snare_interpose::real`] so the hand-off itself never counts as a simulated wait.
    pub fn raise(&self, signal: Signal) -> SignalDelivery {
        let Some(raw) = signal.raw() else {
            return SignalDelivery::Unavailable;
        };
        let Some(domain) = self.shared.domain.get().and_then(|d| d.upgrade()) else {
            return SignalDelivery::Unavailable;
        };
        if domain.is_current() {
            return deliver(&self.shared, raw, SignalSource::Sim);
        }
        let slot = Arc::new(Mutex::new(SignalDelivery::Unavailable));
        let shared = self.shared.clone();
        let out = slot.clone();
        let thread = domain.spawn_injected("snare-signal", move || {
            let delivery = deliver(&shared, raw, SignalSource::Sim);
            snare_interpose::real(|| *out.lock().unwrap() = delivery);
        });
        match thread {
            Ok(thread) => {
                let _ = thread.join();
                snare_interpose::real(|| *slot.lock().unwrap())
            }
            Err(_) => SignalDelivery::Unavailable,
        }
    }

    /// Delivers `signal` once `delay` of sim time has passed, on a thread of the sim that sleeps
    /// it out like any other — a virtual timer the sim can skip to.
    ///
    /// From a thread of the sim the timer thread is spawned with `std::thread` inside
    /// [`snare_interpose::simulated`], so the interposed thread creation adopts it into the
    /// domain; from outside, the domain injects it. With the domain gone the returned
    /// [`PendingSignal`] reports [`SignalDelivery::Unavailable`].
    pub fn raise_after(&self, signal: Signal, delay: Duration) -> PendingSignal {
        let slot = Arc::new(Mutex::new(None));
        let out = slot.clone();
        let handle = self.clone();
        let body = move || {
            std::thread::sleep(delay);
            let delivery = handle.raise(signal);
            snare_interpose::real(|| *out.lock().unwrap() = Some(delivery));
        };
        let domain = self.shared.domain.get().and_then(|d| d.upgrade());
        let thread = match domain {
            Some(domain) if domain.is_current() => snare_interpose::simulated(|| {
                std::thread::Builder::new()
                    .name("snare-signal-timer".into())
                    .spawn(body)
                    .ok()
            }),
            Some(domain) => domain.spawn_injected("snare-signal-timer", body).ok(),
            None => None,
        };
        PendingSignal { thread, slot }
    }
}

/// A signal [`SignalHandle::raise_after`] will deliver.
pub struct PendingSignal {
    /// The timer thread; `None` when it could not be started.
    thread: Option<JoinHandle<()>>,
    /// Where the timer thread leaves the delivery's outcome.
    slot: Arc<Mutex<Option<SignalDelivery>>>,
}

impl std::fmt::Debug for PendingSignal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PendingSignal")
            .field("started", &self.thread.is_some())
            .finish_non_exhaustive()
    }
}

impl PendingSignal {
    /// Waits for the delivery and returns what it did: [`SignalDelivery::Unavailable`] if the
    /// timer thread never ran or died before delivering.
    pub fn wait(self) -> SignalDelivery {
        if let Some(thread) = self.thread {
            let _ = thread.join();
        }
        snare_interpose::real(|| {
            self.slot
                .lock()
                .unwrap()
                .unwrap_or(SignalDelivery::Unavailable)
        })
    }
}

/// Delivers the host-numbered `raw` on the calling thread of the sim: synchronously, its handler
/// run before this returns (see [`snare_interpose::deliver_here`]).
#[cfg(unix)]
fn deliver(_shared: &Arc<SimShared>, raw: i32, source: SignalSource) -> SignalDelivery {
    snare_interpose::deliver_here(raw, source).map_or(SignalDelivery::Unavailable, Into::into)
}

/// Delivers the console event `raw` on a new thread of the sim, as Windows does, and waits for
/// the outcome within the OS's time limit for that event (see [`console::deliver_and_wait`]).
#[cfg(windows)]
fn deliver(shared: &Arc<SimShared>, raw: i32, source: SignalSource) -> SignalDelivery {
    console::deliver_and_wait(shared, raw as u32, source)
}

/// One sim's dispositions.
pub(crate) struct SignalTable {
    /// The `sigaction` installed for each virtual signal, seeded from the process's own (see
    /// [`forward::inherited`]) so an inherited `SIG_IGN` survives, as across exec(2) (man 7
    /// signal, "Signal dispositions": ignored signals stay ignored across execve). `SIGCHLD` is not
    /// seeded: until the sim's code sets it, it follows the process (see [`effective`]).
    #[cfg(unix)]
    actions: Mutex<Actions>,
    /// See `SimBuilder::process_signal_handlers`.
    #[cfg(unix)]
    process_handlers: std::sync::atomic::AtomicBool,
    /// Console control handlers, the ignore-CTRL+C flag and the CRT's signal table.
    #[cfg(windows)]
    console: Mutex<console::Console>,
}

impl SignalTable {
    /// A fresh table: on unix the process's dispositions for `SIGINT`, `SIGTERM` and `SIGHUP` as
    /// they were before any sim forwarded them; on Windows no handlers, as a new console process
    /// starts with only the default handler
    /// ([Microsoft Learn: SetConsoleCtrlHandler](https://learn.microsoft.com/en-us/windows/console/setconsolectrlhandler)).
    pub(crate) fn new() -> Self {
        SignalTable {
            #[cfg(unix)]
            actions: Mutex::new(Actions {
                by_signal: [libc::SIGINT, libc::SIGTERM, libc::SIGHUP]
                    .into_iter()
                    .map(|sig| (sig, forward::inherited(sig)))
                    .collect(),
                set: crate::HashSet::default(),
            }),
            #[cfg(unix)]
            process_handlers: std::sync::atomic::AtomicBool::new(false),
            #[cfg(windows)]
            console: Mutex::default(),
        }
    }
}

/// A sim's `sigaction` table.
#[cfg(unix)]
struct Actions {
    by_signal: crate::HashMap<i32, libc::sigaction>,
    /// The signals the sim's own code has set an action for.
    set: crate::HashSet<i32>,
}

#[cfg(unix)]
type Handlers = std::collections::BTreeMap<i32, libc::sigaction>;

/// The handler the code under test last installed for each signal, in whichever sim: what a sim
/// built with `SimBuilder::process_signal_handlers` falls back to for a signal its own code never
/// set an action for.
#[cfg(unix)]
static PROCESS_HANDLERS: Mutex<Handlers> = Mutex::new(std::collections::BTreeMap::new());

#[cfg(unix)]
fn process_handlers() -> std::sync::MutexGuard<'static, Handlers> {
    PROCESS_HANDLERS.lock().unwrap_or_else(|e| e.into_inner())
}

#[cfg(unix)]
fn is_handler(action: &libc::sigaction) -> bool {
    action.sa_sigaction != libc::SIG_DFL && action.sa_sigaction != libc::SIG_IGN
}

/// The action in force for `sig` in the sim whose table is `table`: the one its code set; else,
/// for a sim built with `SimBuilder::process_signal_handlers` and always for `SIGCHLD`, the handler
/// the code under test last installed in any sim of the process; else the process's own.
/// `SIGCHLD` reaches a sim only for the children its own threads made, so a handler installed
/// once per process (signal-hook-registry's, behind `tokio::process`) is the one the host would
/// run, in whichever sim it was installed.
#[cfg(unix)]
fn effective(table: &SignalTable, actions: &Actions, sig: i32) -> libc::sigaction {
    if let Some(action) = actions
        .set
        .contains(&sig)
        .then(|| actions.by_signal.get(&sig))
        .flatten()
    {
        return *action;
    }
    if (sig == libc::SIGCHLD
        || table
            .process_handlers
            .load(std::sync::atomic::Ordering::Relaxed))
        && let Some(installed) = process_handlers().get(&sig).copied()
    {
        return installed;
    }
    actions
        .by_signal
        .get(&sig)
        .copied()
        .unwrap_or_else(|| forward::inherited(sig))
}

/// How `action` handles `sig`. The default action of `SIGCHLD` is to ignore it (man 7 signal,
/// "Standard signals": `SIGCHLD` "Ign").
#[cfg(unix)]
fn disposition_of(sig: i32, action: &libc::sigaction) -> snare_interpose::Disposition {
    use snare_interpose::Disposition;
    match action.sa_sigaction {
        libc::SIG_DFL if sig == libc::SIGCHLD => Disposition::Ignore,
        libc::SIG_DFL => Disposition::Default,
        libc::SIG_IGN => Disposition::Ignore,
        address => {
            let resethand = action.sa_flags & libc::SA_RESETHAND != 0;
            Disposition::Handler {
                address,
                siginfo: action.sa_flags & libc::SA_SIGINFO != 0,
                // POSIX lets SA_RESETHAND act as if SA_NODEFER were set (IEEE Std 1003.1-2024,
                // sigaction: "sigaction() may behave as if the SA_NODEFER flag were also set");
                // the sim takes that option on Linux only. Linux itself does not
                // (kernel/signal.c signal_delivered blocks the signal unless SA_NODEFER), nor
                // does XNU (bsd/kern/kern_sig.c postsig_locked checks ps_signodefer only).
                nodefer: action.sa_flags & libc::SA_NODEFER != 0
                    || (resethand && cfg!(target_os = "linux")),
                restart: action.sa_flags & libc::SA_RESTART != 0,
                blocks: snare_interpose::virtual_bits(&action.sa_mask),
            }
        }
    }
}

#[cfg(unix)]
impl SignalTable {
    /// Whether a signal the sim's code never set an action for reaches the process's handler.
    pub(crate) fn reach_process_handlers(&self, on: bool) {
        self.process_handlers
            .store(on, std::sync::atomic::Ordering::Relaxed);
    }

    /// The `SIGCHLD` action in force in the sim (see [`effective`]).
    pub(crate) fn child_action(&self) -> libc::sigaction {
        let actions = self.actions.lock().unwrap();
        effective(self, &actions, libc::SIGCHLD)
    }
}

/// The [`Signals`] a sim's domain is built with: its table, and its log for the deliveries.
pub(crate) struct SimSignals(pub(crate) Arc<SimShared>);

impl SimSignals {
    /// The sim's dispositions.
    fn table(&self) -> &SignalTable {
        &self.0.signals
    }
}

impl Signals for SimSignals {
    /// Swaps the stored action; a signal never set reads back as the process's inherited one.
    #[cfg(unix)]
    fn sigaction(&self, sig: i32, act: Option<&libc::sigaction>) -> libc::sigaction {
        let mut actions = self.table().actions.lock().unwrap();
        let previous = actions
            .by_signal
            .get(&sig)
            .copied()
            .unwrap_or_else(|| forward::inherited(sig));
        if let Some(act) = act {
            actions.by_signal.insert(sig, *act);
            actions.set.insert(sig);
            let mut process = process_handlers();
            if is_handler(act) {
                process.insert(sig, *act);
            } else if process
                .get(&sig)
                .is_some_and(|installed| installed.sa_sigaction == previous.sa_sigaction)
            {
                process.remove(&sig);
            }
            if sig == libc::SIGCHLD {
                let installed = process.get(&sig).copied().unwrap_or_else(|| {
                    let mut none = *act;
                    none.sa_sigaction = libc::SIG_DFL;
                    none
                });
                drop(process);
                snare_interpose::set_domains_sigchld_action(&installed);
            }
        }
        previous
    }

    /// Reads the action for delivery. With `SA_RESETHAND` the stored handler is reset to
    /// `SIG_DFL` as it is taken (man 2 sigaction, `SA_RESETHAND`: "Restore the signal action to
    /// the default upon entry to the signal handler").
    #[cfg(unix)]
    fn take_disposition(&self, sig: i32) -> snare_interpose::Disposition {
        let mut actions = self.table().actions.lock().unwrap();
        let action = effective(self.table(), &actions, sig);
        let disposition = disposition_of(sig, &action);
        if action.sa_flags & libc::SA_RESETHAND != 0
            && matches!(disposition, snare_interpose::Disposition::Handler { .. })
        {
            let mut reset = action;
            reset.sa_sigaction = libc::SIG_DFL;
            actions.by_signal.insert(sig, reset);
            actions.set.insert(sig);
        }
        disposition
    }

    #[cfg(unix)]
    fn disposition(&self, sig: i32) -> snare_interpose::Disposition {
        let actions = self.table().actions.lock().unwrap();
        disposition_of(sig, &effective(self.table(), &actions, sig))
    }

    /// Wakes every blocked call of the sim to look for a signal left pending for its thread.
    #[cfg(unix)]
    fn wake_waiters(&self) {
        crate::readiness::readiness().bump(self.0.domain_key());
    }

    /// Waits until a handler interrupts the wait, or the sim is deadlocked with no time left to
    /// pass, where the host would wait for good.
    #[cfg(unix)]
    fn suspend(&self) {
        snare_interpose::interruptible(snare_interpose::Restart::Never, || {
            crate::readiness::readiness().wait_until("pause", None, || false)
        });
    }

    /// Waits on the sim's readiness, which every signal left pending bumps (see
    /// [`wake_waiters`](Self::wake_waiters)), with `timeout` on the sim's clock.
    #[cfg(unix)]
    fn wait(
        &self,
        label: &'static str,
        timeout: Option<Duration>,
        ready: &mut dyn FnMut() -> bool,
    ) -> bool {
        crate::readiness::readiness().wait_until(
            label,
            timeout.map(crate::readiness::Deadline::timeout),
            ready,
        )
    }

    /// See [`console::Console::set_handler`].
    #[cfg(windows)]
    fn set_console_ctrl_handler(&self, handler: usize, add: bool) -> Result<(), u32> {
        self.table()
            .console
            .lock()
            .unwrap()
            .set_handler(handler, add)
    }

    /// Starts the handlers on a new thread of the sim and returns at once:
    /// `GenerateConsoleCtrlEvent` does not wait for them.
    #[cfg(windows)]
    fn console_event(&self, event: u32) {
        console::deliver_async(&self.0, event, SignalSource::Process);
    }

    /// See [`console::Console::crt_signal`].
    #[cfg(windows)]
    fn crt_signal(&self, sig: i32, handler: usize) -> usize {
        self.table()
            .console
            .lock()
            .unwrap()
            .crt_signal(sig, handler)
    }

    /// Runs the CRT handler on the calling thread and returns 0, as UCRT `raise` does after a
    /// handler or `SIG_IGN` (ucrt/misc/signal.cpp `raise`); where the real CRT would `_exit(3)`
    /// for `SIG_DFL`, the sim records the default action and also returns 0.
    #[cfg(windows)]
    fn crt_raise(&self, sig: i32) -> i32 {
        console::crt_raise(&self.0, sig);
        0
    }

    /// Logs the delivery to the sim's event record.
    fn record(&self, raw: i32, source: SignalSource, outcome: SignalOutcome) {
        record(&self.0, raw, source, outcome.into());
    }
}

/// How a blocked socket call goes on after a signal handler runs on its thread; `timeout` is
/// whether the socket has one for it (`SO_RCVTIMEO` for a receive or an accept, `SO_SNDTIMEO`
/// for a send). Restarted under `SA_RESTART`, except that Linux never restarts one with a timeout
/// (man 7 signal: such "input" and "output" socket interfaces "always fail with the error
/// EINTR"); macOS restarts it all the same. Pinned against both hosts by
/// tests/signal_interrupts.rs.
pub(crate) fn socket_restart(timeout: bool) -> snare_interpose::Restart {
    if timeout && cfg!(target_os = "linux") {
        snare_interpose::Restart::Never
    } else {
        snare_interpose::Restart::WithSaRestart
    }
}

/// How a blocking `connect` goes on after a signal handler runs on its thread; `timeout` is
/// whether the socket has an `SO_SNDTIMEO`, the timeout Linux's connect waits with. Linux restarts
/// it under `SA_RESTART`, the restarted call waiting for the same attempt, but not with a timeout
/// (net/ipv4/af_inet.c `__inet_stream_connect` returns `sock_intr_errno(timeo)`, `-ERESTARTSYS`
/// only for an endless wait); XNU always fails it with EINTR (bsd/kern/uipc_syscalls.c
/// `connectit` turns `ERESTART` into `EINTR`). Pinned against both hosts by
/// tests/signal_interrupts.rs.
#[cfg(unix)]
pub(crate) fn connect_restart(timeout: bool) -> snare_interpose::Restart {
    if cfg!(target_os = "linux") {
        socket_restart(timeout)
    } else {
        snare_interpose::Restart::Never
    }
}

/// Logs a delivery of the host-numbered `raw` as a [`RecordedEvent::Signal`]; numbers the sim
/// does not model are dropped. Callers hold no table lock.
fn record(shared: &SimShared, raw: i32, source: SignalSource, delivery: SignalDelivery) {
    if let Some(signal) = Signal::from_raw(raw) {
        shared.record(RecordedEvent::Signal {
            signal,
            origin: source.into(),
            delivery,
        });
    }
}

/// Process-wide forwarding of real signals into the sims that opted in.
///
/// While at least one sim is registered, the process's own actions for `SIGINT`, `SIGTERM` and
/// `SIGHUP` are replaced by `on_signal`, which only writes the signal number to a pipe (the
/// self-pipe trick: write(2) is async-signal-safe, man 7 signal-safety). A reader thread turns
/// each byte into a delivery into every registered sim, outside signal context. The last
/// [`Registration`](forward::Registration) dropped puts the original actions back.
///
/// Lock order: a sim's table lock, then `FORWARDER` (`SimSignals::sigaction` reads
/// `inherited` while holding its table). `FORWARDER` is never held while a table lock is taken
/// or a handler runs: `forward` copies the sims out and releases it first.
#[cfg(unix)]
pub(crate) mod forward {
    use std::sync::atomic::{AtomicI32, Ordering};
    use std::sync::{Arc, Mutex};

    use snare_interpose::{SignalOutcome, SignalSource};

    use crate::HashMap;
    use crate::scope::SimShared;

    /// The signals forwarded: the virtual ones (see `snare_interpose::is_virtual`) but `SIGCHLD`,
    /// which the sim sends itself.
    const SIGNALS: [libc::c_int; 3] = [libc::SIGINT, libc::SIGTERM, libc::SIGHUP];

    /// The forwarding state while any sim is registered.
    #[derive(Default)]
    struct Forwarder {
        /// The id the last [`Registration`] got; ids are never reused.
        next_id: u64,
        /// The registered sims, in registration order.
        sims: Vec<(u64, Arc<SimShared>)>,
        /// The process's actions displaced by [`on_signal`], restored by the last registration out
        /// and borrowed for one delivery when no sim takes a signal.
        originals: HashMap<libc::c_int, libc::sigaction>,
    }

    /// `Some` exactly while a sim is registered and [`on_signal`] is installed.
    static FORWARDER: Mutex<Option<Forwarder>> = Mutex::new(None);
    /// The write end of the pipe the real handlers write to. Opened once and never closed, so a
    /// signal landing while the last sim stops forwarding never writes to a reused descriptor.
    static PIPE_WRITE: AtomicI32 = AtomicI32::new(-1);
    /// Opens the pipe and starts the reader thread, once per process.
    static PIPE: std::sync::OnceLock<()> = std::sync::OnceLock::new();

    /// A sim's place among the forwarding targets; dropping it takes the sim out, and the last one
    /// out puts the process's own dispositions back.
    pub(crate) struct Registration(u64);

    impl Drop for Registration {
        /// Removes the sim; the last one out restores the process's original actions.
        fn drop(&mut self) {
            snare_interpose::real(|| {
                let mut slot = FORWARDER.lock().unwrap();
                let Some(forwarder) = slot.as_mut() else {
                    return;
                };
                forwarder.sims.retain(|(id, _)| *id != self.0);
                if !forwarder.sims.is_empty() {
                    return;
                }
                let forwarder = slot.take().expect("forwarder");
                for (sig, original) in &forwarder.originals {
                    // SAFETY: putting back the action read when forwarding began.
                    unsafe { libc::sigaction(*sig, original, std::ptr::null_mut()) };
                }
            });
        }
    }

    /// The disposition the process had for `sig` before any sim forwarded it: what a new sim's
    /// table starts from, so an inherited `SIG_IGN` reads back. While forwarding, the saved
    /// original; otherwise a query of the live action (man 2 sigaction: a null `act` only reads).
    pub(crate) fn inherited(sig: libc::c_int) -> libc::sigaction {
        if sig == libc::SIGCHLD {
            return snare_interpose::real(snare_interpose::process_sigchld_action);
        }
        snare_interpose::real(|| {
            if let Some(original) = FORWARDER
                .lock()
                .unwrap()
                .as_ref()
                .and_then(|f| f.originals.get(&sig).copied())
            {
                return original;
            }
            // SAFETY: a query only fills `old`.
            unsafe {
                let mut old: libc::sigaction = std::mem::zeroed();
                libc::sigaction(sig, std::ptr::null(), &mut old);
                old
            }
        })
    }

    /// The real handler while forwarding: writes the signal number as one byte to the pipe.
    ///
    /// Only async-signal-safe work is allowed here (man 7 signal-safety): a single write(2) to a
    /// descriptor that is never closed, with `errno` saved and restored so the interrupted code
    /// does not see it change (man 7 signal-safety, "errno"). Signal numbers fit a byte (all
    /// below 32 for the forwarded set).
    extern "C" fn on_signal(sig: libc::c_int, _: *mut libc::siginfo_t, _: *mut libc::c_void) {
        let byte = sig as u8;
        // SAFETY: errno is the interrupted code's and goes back as it was; write(2) is
        // async-signal-safe, and a full pipe only loses this signal.
        unsafe {
            let errno = errno_location();
            let saved = *errno;
            libc::write(
                PIPE_WRITE.load(Ordering::SeqCst),
                (&raw const byte).cast(),
                1,
            );
            *errno = saved;
        }
    }

    /// The calling thread's `errno` (glibc `__errno_location`, csu/errno-loc.c).
    #[cfg(target_os = "linux")]
    unsafe fn errno_location() -> *mut libc::c_int {
        // SAFETY: the calling thread's errno.
        unsafe { libc::__errno_location() }
    }

    /// The calling thread's `errno` (Darwin libc `__error`, <sys/errno.h>).
    #[cfg(not(target_os = "linux"))]
    unsafe fn errno_location() -> *mut libc::c_int {
        // SAFETY: the calling thread's errno.
        unsafe { libc::__error() }
    }

    /// Adds `shared` to the sims real signals are forwarded to, installing the forwarder first if
    /// it is the first.
    pub(crate) fn register(shared: Arc<SimShared>) -> Registration {
        snare_interpose::real(|| {
            let mut slot = FORWARDER.lock().unwrap();
            let forwarder = slot.get_or_insert_with(install);
            forwarder.next_id += 1;
            let id = forwarder.next_id;
            forwarder.sims.push((id, shared));
            Registration(id)
        })
    }

    /// Installs [`on_signal`] for every forwarded signal, saving the actions it displaces, and on
    /// first use opens the pipe and starts the reader thread.
    ///
    /// `SA_RESTART` keeps a real signal from failing the process's blocking calls with `EINTR`
    /// (man 7 signal, "Interruption of system calls and library functions by signal handlers");
    /// an empty `sa_mask` blocks nothing extra while the one-byte write runs. Called with
    /// [`FORWARDER`] held.
    fn install() -> Forwarder {
        PIPE.get_or_init(|| {
            let mut fds = [0; 2];
            // SAFETY: pipe fills `fds`.
            assert_eq!(
                unsafe { libc::pipe(fds.as_mut_ptr()) },
                0,
                "pipe for signal forwarding"
            );
            PIPE_WRITE.store(fds[1], Ordering::SeqCst);
            let read = fds[0];
            let _ = std::thread::Builder::new()
                .name("snare-signal-forwarder".into())
                .spawn(move || {
                    let _service = snare_interpose::service_thread();
                    read_forwarded(read);
                });
        });
        let mut originals = HashMap::default();
        for sig in SIGNALS {
            // SAFETY: installs a handler that only writes to the pipe; reads back the old action.
            unsafe {
                let mut act: libc::sigaction = std::mem::zeroed();
                act.sa_sigaction = on_signal as *const () as libc::sighandler_t;
                act.sa_flags = libc::SA_SIGINFO | libc::SA_RESTART;
                libc::sigemptyset(&mut act.sa_mask);
                let mut old: libc::sigaction = std::mem::zeroed();
                libc::sigaction(sig, &act, &mut old);
                originals.insert(sig, old);
            }
        }
        Forwarder {
            next_id: 0,
            sims: Vec::new(),
            originals,
        }
    }

    /// The reader thread: one byte per real signal, forwarded until the pipe closes or fails.
    /// `EINTR` (a signal landing on this thread mid-read) is retried.
    fn read_forwarded(read: libc::c_int) {
        loop {
            let mut byte = 0u8;
            // SAFETY: reads one byte into `byte`.
            let n = unsafe { libc::read(read, (&raw mut byte).cast(), 1) };
            if n == 0 {
                break;
            }
            if n < 0 {
                if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted {
                    continue;
                }
                break;
            }
            forward(libc::c_int::from(byte));
        }
        // SAFETY: the pipe's read end, owned by this reader.
        unsafe { libc::close(read) };
    }

    /// Delivers a real `sig` into every forwarding sim; if none handled or ignored it, the process
    /// gets it as it would have without the sims.
    ///
    /// Each sim gets it on a thread its domain injects, joined before the next sim, so the
    /// handlers run as managed code. Falling back puts the original action back, re-raises
    /// (raise(3), delivered before it returns), then reinstalls [`on_signal`]; with the original
    /// `SIG_DFL` that ends the process as the signal would have.
    fn forward(sig: libc::c_int) {
        let (sims, original) = {
            let slot = FORWARDER.lock().unwrap();
            let Some(forwarder) = slot.as_ref() else {
                return;
            };
            let sims: Vec<Arc<SimShared>> = forwarder.sims.iter().map(|(_, s)| s.clone()).collect();
            (sims, forwarder.originals.get(&sig).copied())
        };
        let mut taken = false;
        for shared in sims {
            let Some(domain) = shared.domain.get().and_then(|d| d.upgrade()) else {
                continue;
            };
            let outcome = Arc::new(Mutex::new(None));
            let out = outcome.clone();
            let thread = domain.spawn_injected("snare-signal-forward", move || {
                let r = snare_interpose::deliver_here(sig, SignalSource::Real);
                snare_interpose::real(|| *out.lock().unwrap() = r);
            });
            if let Ok(thread) = thread {
                let _ = thread.join();
            }
            taken |= matches!(
                *outcome.lock().unwrap(),
                Some(
                    SignalOutcome::Handled
                        | SignalOutcome::HandledThenExit
                        | SignalOutcome::Ignored
                        | SignalOutcome::Pending
                        | SignalOutcome::Accepted
                )
            );
        }
        if taken {
            return;
        }
        let Some(original) = original else {
            return;
        };
        // SAFETY: the process's own action goes back for one real delivery, then the forwarder's.
        unsafe {
            let mut ours: libc::sigaction = std::mem::zeroed();
            libc::sigaction(sig, &original, &mut ours);
            libc::raise(sig);
            libc::sigaction(sig, &ours, std::ptr::null_mut());
        }
    }
}

/// Process-wide forwarding of real console control events into the sims that opted in.
///
/// While any sim is registered, `on_event` sits in the process's real console handler list.
/// Windows calls it on a thread of its own for each event; it delivers into every registered sim
/// and returns `TRUE` if any of them took the event, `FALSE` to let the next handler (finally the
/// default `ExitProcess`) run
/// ([Microsoft Learn: HandlerRoutine](https://learn.microsoft.com/en-us/windows/console/handlerroutine)).
#[cfg(windows)]
pub(crate) mod forward {
    use std::sync::{Arc, Mutex};

    use snare_interpose::SignalSource;

    use crate::scope::SimShared;

    /// The registered sims.
    #[derive(Default)]
    struct Forwarder {
        /// The id the last [`Registration`] got; ids are never reused.
        next_id: u64,
        /// The registered sims, in registration order.
        sims: Vec<(u64, Arc<SimShared>)>,
    }

    /// `Some` exactly while a sim is registered and [`on_event`] is installed.
    static FORWARDER: Mutex<Option<Forwarder>> = Mutex::new(None);

    /// A sim's place among the forwarding targets; dropping it takes the sim out, and the last one
    /// out removes [`on_event`] from the process's handler list.
    pub(crate) struct Registration(u64);

    impl Drop for Registration {
        /// Removes the sim; the last one out unregisters [`on_event`].
        fn drop(&mut self) {
            snare_interpose::real(|| {
                let mut slot = FORWARDER.lock().unwrap();
                let Some(forwarder) = slot.as_mut() else {
                    return;
                };
                forwarder.sims.retain(|(id, _)| *id != self.0);
                if forwarder.sims.is_empty() {
                    *slot = None;
                    // SAFETY: removes the handler `register` added.
                    unsafe {
                        windows_sys::Win32::System::Console::SetConsoleCtrlHandler(
                            Some(on_event),
                            0,
                        )
                    };
                }
            });
        }
    }

    /// The real console control handler while forwarding. Delivers `event` into each sim in turn,
    /// on a thread its domain injects, waiting for each within the OS's time limit
    /// ([`super::console::deliver_and_wait`]). The sims are copied out before any is delivered
    /// to, so [`FORWARDER`] is not held while handlers run.
    unsafe extern "system" fn on_event(event: u32) -> windows_sys::core::BOOL {
        let sims: Vec<Arc<SimShared>> = snare_interpose::real(|| {
            FORWARDER
                .lock()
                .unwrap()
                .as_ref()
                .map(|f| f.sims.iter().map(|(_, s)| s.clone()).collect())
                .unwrap_or_default()
        });
        let mut taken = false;
        for shared in sims {
            let Some(domain) = shared.domain.get().and_then(|d| d.upgrade()) else {
                continue;
            };
            let out = Arc::new(Mutex::new(None));
            let slot = out.clone();
            let thread = domain.spawn_injected("snare-signal-forward", move || {
                let delivery = super::console::deliver_and_wait(&shared, event, SignalSource::Real);
                snare_interpose::real(|| *slot.lock().unwrap() = Some(delivery));
            });
            if let Ok(thread) = thread {
                let _ = thread.join();
            }
            taken |= matches!(
                *out.lock().unwrap(),
                Some(
                    super::SignalDelivery::Handled
                        | super::SignalDelivery::HandledThenExit
                        | super::SignalDelivery::Ignored
                )
            );
        }
        taken.into()
    }

    /// Adds `shared` to the sims real console events are forwarded to, adding [`on_event`] to
    /// the process's handler list if it is the first.
    pub(crate) fn register(shared: Arc<SimShared>) -> Registration {
        snare_interpose::real(|| {
            let mut slot = FORWARDER.lock().unwrap();
            let forwarder = slot.get_or_insert_with(|| {
                // SAFETY: adds a handler that forwards into the registered sims.
                unsafe {
                    windows_sys::Win32::System::Console::SetConsoleCtrlHandler(Some(on_event), 1)
                };
                Forwarder::default()
            });
            forwarder.next_id += 1;
            let id = forwarder.next_id;
            forwarder.sims.push((id, shared));
            Registration(id)
        })
    }
}

/// Console control handlers and the CRT's signal table, as Windows keeps them.
///
/// The handler list follows SetConsoleCtrlHandler and HandlerRoutine on Microsoft Learn: handlers
/// are called last-registered first until one returns `TRUE`, each event on a new thread, with a
/// time limit for close/logoff/shutdown. The CRT part follows the UCRT's `signal`/`raise`
/// (ucrt/misc/signal.cpp in the Windows SDK sources): the first `signal(SIGINT | SIGBREAK, ..)`
/// registers the CRT's own console handler (`ctrlevent_capture`), which maps `CTRL_C_EVENT` to
/// `SIGINT` and every other event to `SIGBREAK`, resets a function handler to `SIG_DFL` before
/// calling it, and returns `FALSE` for `SIG_DFL` so the next handler runs. The sim passes only
/// `CTRL_BREAK_EVENT` to the `SIGBREAK` handler (see `console::run_handlers`).
///
/// Lock rule: the [`Console`](console::Console) mutex is taken only inside [`snare_interpose::real`] or a
/// [`Signals`] method, and is released before any handler is called.
#[cfg(windows)]
mod console {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU8, Ordering};
    use std::time::Duration;

    use snare_interpose::SignalSource;
    use windows_sys::Win32::System::Console::{
        CTRL_BREAK_EVENT, CTRL_C_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT,
    };

    use super::SignalDelivery;
    use crate::HashMap;
    use crate::readiness::{Deadline, readiness};
    use crate::scope::SimShared;

    // winerror.h: ERROR_INVALID_PARAMETER (87). Microsoft Learn's SetConsoleCtrlHandler page gives
    // no error code for removing a handler never added; 87 is snare's choice, pinned by
    // crates/snare/tests/signals_win.rs `remove_unregistered`.
    const ERROR_INVALID_PARAMETER: u32 = 87;
    // signal.h (UCRT): SIGINT 2, SIGBREAK 21, SIG_DFL ((void (*)(int))0), SIG_IGN ((void (*)(int))1).
    const SIGINT: i32 = 2;
    const SIGBREAK: i32 = 21;
    const SIG_DFL: usize = 0;
    const SIG_IGN: usize = 1;
    /// Where the CRT's own console handler sits in the list once `signal(SIGINT | SIGBREAK, ..)`
    /// installed it. A snare sentinel: no function lives at `usize::MAX`.
    const CRT_HANDLER: usize = usize::MAX;

    /// One sim's console state.
    #[derive(Default)]
    pub(super) struct Console {
        /// Registered `HandlerRoutine` addresses in registration order (called from the back),
        /// with [`CRT_HANDLER`] standing for the CRT's own. A handler added twice appears twice,
        /// as in Windows' list.
        handlers: Vec<usize>,
        /// `SetConsoleCtrlHandler(NULL, TRUE)` is in force: `CTRL_C_EVENT` reaches no handler.
        /// `CTRL_BREAK_EVENT` is unaffected
        /// ([Microsoft Learn: GenerateConsoleCtrlEvent](https://learn.microsoft.com/en-us/windows/console/generateconsolectrlevent):
        /// "CTRL+BREAK signals always cause the handler functions to be called").
        ignore_ctrl_c: bool,
        /// The CRT `signal` table: handler (or `SIG_DFL`/`SIG_IGN`) per signal number; absent is
        /// `SIG_DFL`.
        crt: HashMap<i32, usize>,
    }

    impl Console {
        /// `SetConsoleCtrlHandler(handler, add)`: a null `handler` sets or clears the ignore-CTRL+C
        /// flag; otherwise `add` appends it and removal drops its most recent registration
        /// (Microsoft Learn does not say which of two registrations goes; newest is snare's
        /// choice). Removing one that is not registered fails with `ERROR_INVALID_PARAMETER`.
        pub(super) fn set_handler(&mut self, handler: usize, add: bool) -> Result<(), u32> {
            if handler == 0 {
                self.ignore_ctrl_c = add;
                return Ok(());
            }
            if add {
                self.handlers.push(handler);
                return Ok(());
            }
            match self.handlers.iter().rposition(|&h| h == handler) {
                Some(i) => {
                    self.handlers.remove(i);
                    Ok(())
                }
                None => Err(ERROR_INVALID_PARAMETER),
            }
        }

        /// The CRT's `signal(sig, handler)`: stores `handler`, returning the previous one
        /// (`SIG_DFL` if none), and for `SIGINT`/`SIGBREAK` registers the CRT's console handler
        /// the first time, as UCRT `signal` does with `SetConsoleCtrlHandler(ctrlevent_capture,
        /// TRUE)`. The CRT handler is never unregistered afterwards.
        pub(super) fn crt_signal(&mut self, sig: i32, handler: usize) -> usize {
            if matches!(sig, SIGINT | SIGBREAK) && !self.handlers.contains(&CRT_HANDLER) {
                self.handlers.push(CRT_HANDLER);
            }
            self.crt.insert(sig, handler).unwrap_or(SIG_DFL)
        }

        /// Takes the CRT handler for `sig`, resetting a function to `SIG_DFL` as the CRT does
        /// before calling it ([Microsoft Learn: signal](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/signal):
        /// "Before the specified function is executed, the value of func is set to SIG_DFL").
        fn take_crt(&mut self, sig: i32) -> usize {
            let handler = self.crt.get(&sig).copied().unwrap_or(SIG_DFL);
            if handler != SIG_DFL && handler != SIG_IGN {
                self.crt.insert(sig, SIG_DFL);
            }
            handler
        }
    }

    /// Locks `shared`'s console state. Callers must be in passthrough and must drop the guard
    /// before calling any handler.
    fn table(shared: &SimShared) -> std::sync::MutexGuard<'_, Console> {
        shared.signals.console.lock().unwrap()
    }

    /// The console event the CRT's `sig` stands for, for the log: `SIGINT` is `CTRL_C_EVENT`,
    /// `SIGBREAK` is `CTRL_BREAK_EVENT`. Other CRT signals (`SIGTERM`) are not logged.
    fn event_of(sig: i32) -> Option<i32> {
        match sig {
            SIGINT => Some(CTRL_C_EVENT as i32),
            SIGBREAK => Some(CTRL_BREAK_EVENT as i32),
            _ => None,
        }
    }

    /// Calls CRT handler `handler` for `sig`, returning whether it counts as handled, as
    /// `ctrlevent_capture` returns it: `SIG_DFL` is not (`FALSE`, the next console handler
    /// runs), `SIG_IGN` and a function are (`TRUE`). The function runs as simulated code.
    fn call_crt(handler: usize, sig: i32) -> bool {
        match handler {
            SIG_DFL => false,
            SIG_IGN => true,
            f => {
                // SAFETY: the code under test registered `f` as a CRT signal handler.
                let f: extern "C" fn(i32) = unsafe { std::mem::transmute(f) };
                snare_interpose::simulated(|| f(sig));
                true
            }
        }
    }

    /// The CRT's `raise(sig)`: synchronous, on the calling thread
    /// ([Microsoft Learn: raise](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/raise)).
    /// A `SIG_DFL` would make the real CRT end the process with exit code 3 (Microsoft Learn:
    /// signal, "By default, signal terminates the calling program with exit code 3"; UCRT `raise`
    /// calls `_exit(3)` for every signal, although the raise page lists `SIGTERM` as ignored); the
    /// sim only records [`SignalDelivery::DefaultAction`].
    pub(super) fn crt_raise(shared: &SimShared, sig: i32) {
        let handler = snare_interpose::real(|| table(shared).take_crt(sig));
        let delivery = match handler {
            SIG_DFL => SignalDelivery::DefaultAction,
            SIG_IGN => SignalDelivery::Ignored,
            _ => SignalDelivery::Handled,
        };
        if let Some(event) = event_of(sig) {
            snare_interpose::real(|| super::record(shared, event, SignalSource::Process, delivery));
        }
        call_crt(handler, sig);
    }

    /// Whether this process counts as a Windows (GUI) application, to which the system does not
    /// deliver `CTRL_LOGOFF_EVENT` or `CTRL_SHUTDOWN_EVENT`: "If a console application loads the
    /// gdi32.dll or user32.dll library, the HandlerRoutine function ... does not get called for
    /// the CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT events"
    /// ([Microsoft Learn: SetConsoleCtrlHandler](https://learn.microsoft.com/en-us/windows/console/setconsolectrlhandler)).
    /// Asked of the real process with `GetModuleHandleW`, which does not load anything.
    fn user_session_ignored() -> bool {
        use windows_sys::Win32::System::LibraryLoader::GetModuleHandleW;
        let loaded = |name: &str| {
            let wide: Vec<u16> = name.encode_utf16().chain(Some(0)).collect();
            // SAFETY: a NUL-terminated module name; the refcount is untouched.
            !unsafe { GetModuleHandleW(wide.as_ptr()) }.is_null()
        };
        loaded("user32.dll") || loaded("gdi32.dll")
    }

    /// The outcome slot's value while the handlers are still running.
    const PENDING: u8 = 0;

    /// A [`SignalDelivery`] as a nonzero byte, so the outcome can be handed over in an atomic
    /// that [`deliver_and_wait`] polls from its readiness wait.
    fn encode(delivery: SignalDelivery) -> u8 {
        match delivery {
            SignalDelivery::Handled => 1,
            SignalDelivery::HandledThenExit => 2,
            SignalDelivery::DefaultAction => 3,
            SignalDelivery::Ignored => 4,
            _ => 5,
        }
    }

    /// The inverse of [`encode`]; `None` for [`PENDING`].
    fn decode(code: u8) -> Option<SignalDelivery> {
        Some(match code {
            1 => SignalDelivery::Handled,
            2 => SignalDelivery::HandledThenExit,
            3 => SignalDelivery::DefaultAction,
            4 => SignalDelivery::Ignored,
            5 => SignalDelivery::Unavailable,
            _ => return None,
        })
    }

    /// Runs the handlers for `event`, last registered first, until one returns `TRUE`, as the
    /// thread Windows creates for a console event does
    /// ([Microsoft Learn: HandlerRoutine](https://learn.microsoft.com/en-us/windows/console/handlerroutine)).
    ///
    /// The outcome is decided, and recorded, before any handler runs when it does not depend on
    /// them: `CTRL_C_EVENT` under the ignore flag and logoff/shutdown in a GUI process are
    /// [`Ignored`](SignalDelivery::Ignored) and run nothing; a close, logoff or shutdown ends the
    /// process whatever the handlers return (returning `TRUE` only stops the remaining handlers,
    /// "the system terminates the process"), so it is
    /// [`HandledThenExit`](SignalDelivery::HandledThenExit) with handlers registered and
    /// [`DefaultAction`](SignalDelivery::DefaultAction) without. Otherwise the outcome is
    /// recorded after the handlers: `Handled` if one returned `TRUE`, else `DefaultAction` (the
    /// default handler's `ExitProcess`, which the sim does not carry out). The CRT's handler only
    /// takes `CTRL_C_EVENT` and `CTRL_BREAK_EVENT` here and is skipped for close, logoff and
    /// shutdown, where the real `ctrlevent_capture` would run the `SIGBREAK` handler.
    fn run_handlers(shared: &SimShared, event: u32, source: SignalSource) -> SignalDelivery {
        let closing = matches!(
            event,
            CTRL_CLOSE_EVENT | CTRL_LOGOFF_EVENT | CTRL_SHUTDOWN_EVENT
        );
        let (handlers, ignore) = snare_interpose::real(|| {
            let console = table(shared);
            (console.handlers.clone(), console.ignore_ctrl_c)
        });
        let early = if (event == CTRL_C_EVENT && ignore)
            || (matches!(event, CTRL_LOGOFF_EVENT | CTRL_SHUTDOWN_EVENT) && user_session_ignored())
        {
            Some(SignalDelivery::Ignored)
        } else if closing {
            Some(if handlers.is_empty() {
                SignalDelivery::DefaultAction
            } else {
                SignalDelivery::HandledThenExit
            })
        } else {
            None
        };
        if let Some(delivery) = early {
            snare_interpose::real(|| super::record(shared, event as i32, source, delivery));
            if delivery != SignalDelivery::HandledThenExit {
                return delivery;
            }
        }
        let mut handled = false;
        for &handler in handlers.iter().rev() {
            let taken = if handler == CRT_HANDLER {
                let sig = match event {
                    CTRL_C_EVENT => SIGINT,
                    CTRL_BREAK_EVENT => SIGBREAK,
                    _ => continue,
                };
                let crt = snare_interpose::real(|| table(shared).take_crt(sig));
                call_crt(crt, sig)
            } else {
                // SAFETY: the code under test registered `handler` as a console control handler.
                let f: unsafe extern "system" fn(u32) -> i32 =
                    unsafe { std::mem::transmute(handler) };
                snare_interpose::simulated(|| unsafe { f(event) }) != 0
            };
            if taken {
                handled = true;
                break;
            }
        }
        if let Some(delivery) = early {
            return delivery;
        }
        let delivery = if handled {
            SignalDelivery::Handled
        } else {
            SignalDelivery::DefaultAction
        };
        snare_interpose::real(|| super::record(shared, event as i32, source, delivery));
        delivery
    }

    /// Starts the handlers for `event` on a new thread of the sim, as Windows does, and returns
    /// the slot the outcome lands in.
    ///
    /// From a thread of the sim the thread is spawned inside [`snare_interpose::simulated`] so
    /// the interposed thread creation adopts it; from outside, the domain injects it. The thread
    /// bumps the readiness counter after storing the outcome so a waiter in
    /// [`deliver_and_wait`] re-checks. If no thread could be started the slot holds
    /// [`SignalDelivery::Unavailable`]; with the domain gone it stays [`PENDING`].
    fn spawn_delivery(shared: &Arc<SimShared>, event: u32, source: SignalSource) -> Arc<AtomicU8> {
        let slot = Arc::new(AtomicU8::new(PENDING));
        let out = slot.clone();
        let target = shared.clone();
        let body = move || {
            let delivery = run_handlers(&target, event, source);
            out.store(encode(delivery), Ordering::SeqCst);
            snare_interpose::real(|| target.bump());
        };
        let domain = shared.domain.get().and_then(|d| d.upgrade());
        let spawned = match domain {
            Some(domain) if domain.is_current() => snare_interpose::simulated(|| {
                std::thread::Builder::new()
                    .name("snare-console-event".into())
                    .spawn(body)
                    .map(drop)
            }),
            Some(domain) => domain.spawn_injected("snare-console-event", body).map(drop),
            None => Ok(()),
        };
        if spawned.is_err() {
            slot.store(encode(SignalDelivery::Unavailable), Ordering::SeqCst);
        }
        slot
    }

    /// `GenerateConsoleCtrlEvent`'s delivery: starts the handlers and does not wait.
    pub(super) fn deliver_async(shared: &Arc<SimShared>, event: u32, source: SignalSource) {
        spawn_delivery(shared, event, source);
    }

    /// Delivers `event` and waits for its outcome as long as Windows would let the handlers run
    /// before ending the process: without limit for CTRL+C and CTRL+BREAK, 5 s for a close and
    /// 20 s for a logoff or shutdown, in sim time. A wait that runs out reports
    /// [`SignalDelivery::HandledThenExit`]: Windows would end the process with the handler still
    /// running.
    ///
    /// The limits follow the "Timeouts" table of
    /// [Microsoft Learn: HandlerRoutine](https://learn.microsoft.com/en-us/windows/console/handlerroutine):
    /// `CTRL_CLOSE_EVENT` `SPI_GETHUNGAPPTIMEOUT`, 5000 ms; `CTRL_C`/`CTRL_BREAK` no timeout. For
    /// logoff and shutdown that table gives `SPI_GETWAITTOKILLTIMEOUT`, 5000 ms, for an ordinary
    /// process and `SPI_GETWAITTOKILLSERVICETIMEOUT`, 20000 ms, for a service at shutdown; the sim
    /// uses the 20 s service figure for both.
    pub(super) fn deliver_and_wait(
        shared: &Arc<SimShared>,
        event: u32,
        source: SignalSource,
    ) -> SignalDelivery {
        let slot = spawn_delivery(shared, event, source);
        let limit = match event {
            CTRL_CLOSE_EVENT => Some(Duration::from_secs(5)),
            CTRL_LOGOFF_EVENT | CTRL_SHUTDOWN_EVENT => Some(Duration::from_secs(20)),
            _ => None,
        };
        let deadline = snare_interpose::real(|| limit.map(Deadline::timeout));
        snare_interpose::real(|| {
            readiness().wait_until("signal", deadline, || {
                slot.load(Ordering::SeqCst) != PENDING
            })
        });
        match decode(slot.load(Ordering::SeqCst)) {
            Some(delivery) => delivery,
            None if limit.is_some() => SignalDelivery::HandledThenExit,
            None => SignalDelivery::Handled,
        }
    }
}