hermit-run 0.4.0

Hermit: a deterministic Linux execution engine for reproducible runs, record and replay, and concurrency-race search.
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
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
/*
 * 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::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::os::fd::RawFd;
use std::os::unix::fs::FileExt;

use reverie::Errno;
use reverie::Error;
use reverie::Guest;
use reverie::Stack;
use reverie::syscalls::Addr;
use reverie::syscalls::AddrMut;
use reverie::syscalls::Fcntl;
use reverie::syscalls::FcntlCmd;
use reverie::syscalls::Ftruncate;
use reverie::syscalls::Getcwd;
use reverie::syscalls::Getdents;
use reverie::syscalls::Getdents64;
use reverie::syscalls::Getuid;
use reverie::syscalls::Ioctl;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::Pread64;
use reverie::syscalls::Read;
use reverie::syscalls::Readlink;
use reverie::syscalls::RtSigtimedwait;
use reverie::syscalls::RtTgsigqueueinfo;
use reverie::syscalls::Statx;
use reverie::syscalls::Syscall;
use reverie::syscalls::Timespec;
use reverie::syscalls::family::StatFamily;
use reverie::syscalls::family::WriteFamily;
use reverie::syscalls::ioctl;

use super::Replayer;
use crate::event::FileCloneImage;
use crate::event::ReplayFdKind;
use crate::event::deterministic_ioctl_error;
use crate::event::xattr_query_output;
use crate::vectored_offset;

#[repr(C)]
struct UserSignalInfoHead {
    signo: libc::c_int,
    errno: libc::c_int,
    code: libc::c_int,
    padding: libc::c_int,
    pid: libc::pid_t,
    uid: libc::uid_t,
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-696): Review lossless replay-output backpressure handling.
async fn wait_for_replay_output(output_fd: RawFd) -> std::io::Result<bool> {
    // F_DUPFD_CLOEXEC keeps the endpoint alive while the bounded blocking task
    // polls it, without changing the shared open-file-description flags.
    let duplicate = unsafe { libc::fcntl(output_fd, libc::F_DUPFD_CLOEXEC, 0) };
    if duplicate == -1 {
        return Err(std::io::Error::last_os_error());
    }
    // SAFETY: F_DUPFD_CLOEXEC returned a new descriptor owned by this task.
    let duplicate = unsafe { OwnedFd::from_raw_fd(duplicate) };
    let readiness = tokio::task::spawn_blocking(move || {
        let mut pollfd = libc::pollfd {
            fd: duplicate.as_raw_fd(),
            events: libc::POLLOUT,
            revents: 0,
        };
        // A finite timeout keeps a cancelled replay from leaving an unbounded
        // blocking-pool task behind. Timeout or EINTR asks the caller to retry
        // its nonblocking write in a new bounded task.
        let ready = unsafe { libc::poll(&mut pollfd, 1, 100) };
        if ready > 0 {
            return Ok(pollfd.revents & libc::POLLOUT != 0);
        }
        if ready == 0 {
            return Ok(true);
        }
        let error = std::io::Error::last_os_error();
        if error.kind() == std::io::ErrorKind::Interrupted {
            Ok(true)
        } else {
            Err(error)
        }
    })
    .await;
    match readiness {
        Ok(result) => result,
        Err(error) => Err(std::io::Error::other(format!(
            "replay output readiness task failed: {error}"
        ))),
    }
}

const CLONE_COPY_CHUNK_BYTES: usize = 1024 * 1024;

fn restore_sparse_clone_sidecar(
    source: &std::fs::File,
    destination: &std::fs::File,
    length: u64,
    destination_offset: u64,
) -> std::io::Result<()> {
    let mut cursor = 0u64;
    let mut buffer = vec![0; CLONE_COPY_CHUNK_BYTES];
    while cursor < length {
        // SAFETY: source is owned and cursor fits off_t on x86_64.
        let data_offset = unsafe {
            libc::lseek(
                source.as_raw_fd(),
                cursor.try_into().unwrap(),
                libc::SEEK_DATA,
            )
        };
        let (data_offset, hole) = if data_offset == -1 {
            let error = std::io::Error::last_os_error();
            match error.raw_os_error() {
                Some(libc::ENXIO) => break,
                Some(libc::EINVAL) => (0, length),
                _ => return Err(error),
            }
        } else {
            // SAFETY: source is owned and data_offset came from lseek.
            let hole = unsafe { libc::lseek(source.as_raw_fd(), data_offset, libc::SEEK_HOLE) };
            if hole == -1 {
                let error = std::io::Error::last_os_error();
                if error.raw_os_error() != Some(libc::ENXIO) {
                    return Err(error);
                }
            }
            (
                data_offset as u64,
                if hole == -1 {
                    length
                } else {
                    (hole as u64).min(length)
                },
            )
        };

        let mut offset = data_offset;
        while offset < hole {
            let count = usize::try_from((hole - offset).min(buffer.len() as u64)).unwrap();
            source.read_exact_at(&mut buffer[..count], offset)?;
            destination.write_all_at(
                &buffer[..count],
                destination_offset.checked_add(offset).ok_or_else(|| {
                    std::io::Error::new(std::io::ErrorKind::InvalidInput, "clone offset overflow")
                })?,
            )?;
            offset += count as u64;
        }
        cursor = hole;
    }
    Ok(())
}

fn clear_clone_destination_range(
    file: &std::fs::File,
    offset: u64,
    length: u64,
) -> std::io::Result<()> {
    if length == 0 {
        return Ok(());
    }
    // SAFETY: file is an owned regular-file descriptor and the range was
    // accepted by the recorded FICLONERANGE operation.
    let result = unsafe {
        libc::fallocate(
            file.as_raw_fd(),
            libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE,
            offset.try_into().map_err(|_| {
                std::io::Error::new(std::io::ErrorKind::InvalidInput, "clone offset overflow")
            })?,
            length.try_into().map_err(|_| {
                std::io::Error::new(std::io::ErrorKind::InvalidInput, "clone length overflow")
            })?,
        )
    };
    if result == 0 {
        return Ok(());
    }
    let error = std::io::Error::last_os_error();
    if !matches!(
        error.raw_os_error(),
        Some(libc::EOPNOTSUPP | libc::ENOSYS | libc::EINVAL)
    ) {
        return Err(error);
    }

    tracing::warn!(%error, "hole punching unavailable; zeroing cloned replay range");
    let zeros = vec![0; CLONE_COPY_CHUNK_BYTES];
    let mut written = 0u64;
    while written < length {
        let count = usize::try_from((length - written).min(zeros.len() as u64)).unwrap();
        let write_offset = offset.checked_add(written).ok_or_else(|| {
            std::io::Error::new(std::io::ErrorKind::InvalidInput, "clone offset overflow")
        })?;
        file.write_all_at(&zeros[..count], write_offset)?;
        written += count as u64;
    }
    Ok(())
}

fn set_replay_output_flags(output_fd: RawFd, flags: libc::c_int) -> std::io::Result<()> {
    // SAFETY: the caller owns output_fd for the duration of the operation.
    if unsafe { libc::fcntl(output_fd, libc::F_SETFL, flags) } == -1 {
        Err(std::io::Error::last_os_error())
    } else {
        Ok(())
    }
}

fn write_replay_output_once_with<F>(
    output_fd: RawFd,
    bytes: &[u8],
    file_offset: Option<i64>,
    set_flags: &mut F,
) -> std::io::Result<usize>
where
    F: FnMut(RawFd, libc::c_int) -> std::io::Result<()>,
{
    // Nonblocking mode is temporary and is restored before this function
    // returns. In particular, no async suspension may expose it through the
    // shared open-file description.
    // SAFETY: fcntl only inspects this valid, Replayer-owned duplicate.
    let flags = unsafe { libc::fcntl(output_fd, libc::F_GETFL) };
    if flags == -1 {
        return Err(std::io::Error::last_os_error());
    }
    let temporary_flags = match file_offset {
        Some(_) => flags & !libc::O_APPEND,
        None => flags | libc::O_NONBLOCK,
    };
    let changed_flags = temporary_flags != flags;
    if changed_flags {
        set_flags(output_fd, temporary_flags)?;
    }

    let written = match file_offset {
        Some(position) => {
            // SAFETY: bytes points to readable memory and output_fd is open.
            unsafe { libc::pwrite(output_fd, bytes.as_ptr().cast(), bytes.len(), position) }
        }
        None => {
            // SAFETY: bytes points to readable memory and output_fd is open.
            unsafe { libc::write(output_fd, bytes.as_ptr().cast(), bytes.len()) }
        }
    };
    let result = if written == -1 {
        Err(std::io::Error::last_os_error())
    } else {
        Ok(written as usize)
    };

    if changed_flags && let Err(restore_error) = set_flags(output_fd, flags) {
        let write_detail = match result {
            Ok(written) => format!("write completed with {written} byte(s)"),
            Err(ref write_error) => format!("write failed: {write_error}"),
        };
        return Err(std::io::Error::other(format!(
            "failed to restore replay output flags after {write_detail}: {restore_error}"
        )));
    }
    result
}

fn write_replay_output_once(
    output_fd: RawFd,
    bytes: &[u8],
    file_offset: Option<i64>,
) -> std::io::Result<usize> {
    let mut set_flags = set_replay_output_flags;
    write_replay_output_once_with(output_fd, bytes, file_offset, &mut set_flags)
}

async fn emit_replay_output_with<W>(
    output_fd: RawFd,
    bytes: &[u8],
    file_offset: Option<i64>,
    advances_output_offset: bool,
    write_once: &mut W,
) -> std::io::Result<()>
where
    W: FnMut(RawFd, &[u8], Option<i64>) -> std::io::Result<usize>,
{
    if bytes.is_empty() {
        return Ok(());
    }

    let mut offset = 0;
    while offset < bytes.len() {
        let remaining = &bytes[offset..];
        let written = if let Some(file_offset) = file_offset {
            let offset = i64::try_from(offset).map_err(|_| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "recorded replay output offset does not fit in i64",
                )
            })?;
            let position = file_offset.checked_add(offset).ok_or_else(|| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "recorded replay output offset overflow",
                )
            })?;
            write_once(output_fd, remaining, Some(position))
        } else {
            // send with MSG_NOSIGNAL handles sockets without risking a tracer
            // SIGPIPE. MSG_DONTWAIT is per-call and does not modify the shared
            // open-file description. Pipes reject send with ENOTSOCK, so use
            // a write whose O_NONBLOCK window ends before any async wait.
            let sent = unsafe {
                libc::send(
                    output_fd,
                    remaining.as_ptr().cast(),
                    remaining.len(),
                    libc::MSG_DONTWAIT | libc::MSG_NOSIGNAL,
                )
            };
            if sent == -1 {
                let error = std::io::Error::last_os_error();
                if error.raw_os_error() == Some(libc::ENOTSOCK) {
                    write_once(output_fd, remaining, None)
                } else {
                    Err(error)
                }
            } else {
                Ok(sent as usize)
            }
        };
        match written {
            Ok(written) if written > 0 => {
                offset += written;
                continue;
            }
            Err(error) => {
                if error.kind() == std::io::ErrorKind::Interrupted {
                    continue;
                }
                if error.kind() == std::io::ErrorKind::WouldBlock {
                    match wait_for_replay_output(output_fd).await {
                        Ok(true) => continue,
                        Ok(false) => {
                            return Err(std::io::Error::other(format!(
                                "replay output fd {output_fd} became unavailable after {offset}/{} bytes",
                                bytes.len()
                            )));
                        }
                        Err(wait_error) => {
                            return Err(std::io::Error::other(format!(
                                "replay output fd {output_fd} remained blocked after {offset}/{} bytes: {error}; readiness check failed: {wait_error}",
                                bytes.len()
                            )));
                        }
                    }
                }
                return Err(error);
            }
            Ok(_) => {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::WriteZero,
                    format!(
                        "replay output fd {output_fd} wrote zero bytes after {offset}/{} bytes",
                        bytes.len()
                    ),
                ));
            }
        }
    }

    if advances_output_offset {
        let file_offset = file_offset.ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "advancing captured output requires a recorded file offset",
            )
        })?;
        let length = i64::try_from(bytes.len()).map_err(|_| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "recorded replay output length does not fit in i64",
            )
        })?;
        let final_offset = file_offset.checked_add(length).ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "recorded replay output offset overflow",
            )
        })?;
        // SAFETY: output_fd is an owned seekable output duplicate.
        let positioned = unsafe { libc::lseek(output_fd, final_offset, libc::SEEK_SET) };
        if positioned == -1 {
            return Err(std::io::Error::last_os_error());
        }
        if positioned != final_offset {
            return Err(std::io::Error::other(format!(
                "failed to advance captured output fd position to {final_offset}: positioned at {positioned}"
            )));
        }
    }
    Ok(())
}

async fn emit_replay_output(
    output_fd: RawFd,
    bytes: &[u8],
    file_offset: Option<i64>,
    advances_output_offset: bool,
) -> std::io::Result<()> {
    let mut write_once = write_replay_output_once;
    emit_replay_output_with(
        output_fd,
        bytes,
        file_offset,
        advances_output_offset,
        &mut write_once,
    )
    .await
}

fn truncate_replay_output(output_fd: RawFd, length: i64) -> Result<(), Error> {
    // SAFETY: output_fd is an owned duplicate and length was accepted by the
    // kernel while recording.
    if unsafe { libc::ftruncate(output_fd, length) } == -1 {
        Err(Error::Io(std::io::Error::last_os_error()))
    } else {
        Ok(())
    }
}

fn successful_replay_write_count(result: Result<i64, Errno>) -> Result<Option<usize>, Error> {
    match result {
        Ok(count) => usize::try_from(count).map(Some).map_err(|_| {
            Error::Tool(anyhow::anyhow!(
                "recording contains successful write with invalid count {count}"
            ))
        }),
        Err(_) => Ok(None),
    }
}

/// Scatter the recorded flat output `bytes` of a vectored read back into the
/// guest's `iovec` array, filling each buffer in order until the bytes are
/// exhausted. Returns the number of bytes written (the syscall return value).
fn scatter_iovec_output<M: MemoryAccess>(
    memory: &mut M,
    iov_addr: Option<usize>,
    iovcnt: usize,
    bytes: &[u8],
) -> Result<usize, Errno> {
    if bytes.is_empty() {
        return Ok(0);
    }
    let addr = iov_addr
        .and_then(Addr::<libc::iovec>::from_raw)
        .ok_or(Errno::EFAULT)?;
    let mut iovecs = vec![
        libc::iovec {
            iov_base: std::ptr::null_mut(),
            iov_len: 0,
        };
        iovcnt
    ];
    memory.read_values(addr, &mut iovecs)?;

    let mut written = 0;
    for iovec in iovecs {
        if written == bytes.len() {
            break;
        }
        let take = (bytes.len() - written).min(iovec.iov_len);
        if take == 0 {
            continue;
        }
        let dst = AddrMut::<u8>::from_raw(iovec.iov_base as usize).ok_or(Errno::EFAULT)?;
        memory.write_exact(dst, &bytes[written..written + take])?;
        written += take;
    }
    // The recorded byte count must fit within the guest's provided iovecs.
    assert_eq!(written, bytes.len());
    Ok(written)
}

fn read_iovec_input<M: MemoryAccess>(
    memory: &M,
    iov_addr: Option<usize>,
    iovcnt: usize,
    length: usize,
) -> Result<Vec<u8>, Errno> {
    let mut bytes = vec![0; length];
    if length == 0 {
        return Ok(bytes);
    }
    let addr = iov_addr
        .and_then(Addr::<libc::iovec>::from_raw)
        .ok_or(Errno::EFAULT)?;
    let mut iovecs = vec![
        libc::iovec {
            iov_base: std::ptr::null_mut(),
            iov_len: 0,
        };
        iovcnt
    ];
    memory.read_values(addr, &mut iovecs)?;

    let mut filled = 0;
    for iovec in iovecs {
        if filled == length {
            break;
        }
        let take = (length - filled).min(iovec.iov_len);
        if take == 0 {
            continue;
        }
        let src = Addr::<u8>::from_raw(iovec.iov_base as usize).ok_or(Errno::EFAULT)?;
        memory.read_exact(src, &mut bytes[filled..filled + take])?;
        filled += take;
    }
    assert_eq!(filled, length, "recorded write exceeds its iovec capacity");
    Ok(bytes)
}

fn read_write_bytes<M: MemoryAccess>(
    memory: &M,
    syscall: WriteFamily,
    length: usize,
) -> Result<Vec<u8>, Errno> {
    match syscall {
        WriteFamily::Write(call) => {
            let mut bytes = vec![0; length];
            memory.read_exact(call.buf().ok_or(Errno::EFAULT)?, &mut bytes)?;
            Ok(bytes)
        }
        WriteFamily::Pwrite64(call) => {
            let mut bytes = vec![0; length];
            memory.read_exact(call.buf().ok_or(Errno::EFAULT)?, &mut bytes)?;
            Ok(bytes)
        }
        WriteFamily::Writev(call) => read_iovec_input(
            memory,
            call.iov().map(|addr| addr.as_raw()),
            call.len(),
            length,
        ),
        WriteFamily::Pwritev(call) => read_iovec_input(
            memory,
            call.iov().map(|addr| addr.as_raw()),
            call.iov_len(),
            length,
        ),
        WriteFamily::Pwritev2(call) => read_iovec_input(
            memory,
            call.iov().map(|addr| addr.as_raw()),
            call.iov_len() as usize,
            length,
        ),
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#662): Audit temporary blocking and restoration for replay side effects.
fn replay_side_effect_tool_error(
    fd: libc::c_int,
    operation: &str,
    outcome: impl std::fmt::Display,
) -> Error {
    Error::Tool(anyhow::anyhow!(
        "failed to {operation} for replay fd {fd}: {outcome}"
    ))
}

fn require_replay_fcntl_success(
    fd: libc::c_int,
    operation: &str,
    result: Result<i64, Errno>,
) -> Result<(), Error> {
    match result {
        Ok(0) => Ok(()),
        Ok(value) => Err(replay_side_effect_tool_error(
            fd,
            operation,
            format!("F_SETFL returned {value}"),
        )),
        Err(error) => Err(replay_side_effect_tool_error(fd, operation, error)),
    }
}

fn require_replay_fcntl_value(
    fd: libc::c_int,
    operation: &str,
    result: Result<i64, Errno>,
) -> Result<i64, Error> {
    result.map_err(|error| replay_side_effect_tool_error(fd, operation, error))
}

fn finish_kernel_side_effect(
    fd: libc::c_int,
    result: Result<i64, Errno>,
    restored: Result<i64, Errno>,
) -> Result<Result<i64, Errno>, Error> {
    require_replay_fcntl_success(fd, "restore descriptor flags", restored)?;
    // Keep the injected syscall result nested so callers can still compare it
    // exactly with the recording after the descriptor was restored.
    Ok(result)
}

async fn inject_kernel_side_effect<G: Guest<Replayer>>(
    guest: &mut G,
    fd: libc::c_int,
    syscall: Syscall,
) -> Result<Result<i64, Errno>, Error> {
    let result = guest.inject(syscall).await;
    if result != Err(Errno::EAGAIN) {
        return Ok(result);
    }

    let flags = require_replay_fcntl_value(
        fd,
        "read descriptor flags",
        guest
            .inject(Fcntl::new().with_fd(fd).with_cmd(FcntlCmd::F_GETFL))
            .await,
    )? as libc::c_int;
    if flags & libc::O_NONBLOCK == 0 {
        return Ok(result);
    }
    let cleared = guest
        .inject(
            Fcntl::new()
                .with_fd(fd)
                .with_cmd(FcntlCmd::F_SETFL(flags & !libc::O_NONBLOCK)),
        )
        .await;
    require_replay_fcntl_success(fd, "temporarily clear O_NONBLOCK", cleared)?;
    let result = guest.inject(syscall).await;
    let restored = guest
        .inject(Fcntl::new().with_fd(fd).with_cmd(FcntlCmd::F_SETFL(flags)))
        .await;
    finish_kernel_side_effect(fd, result, restored)
}

impl Replayer {
    fn advance_regular_file_position(&self, pid: reverie::Pid, fd: libc::c_int, length: usize) {
        if !self.fd_is_in_replay_root(pid, fd) {
            return;
        }
        let duplicate = crate::fd::duplicate_guest_fd(pid, fd)
            .unwrap_or_else(|error| panic!("failed to duplicate replay file for read: {error}"));
        let offset = libc::off_t::try_from(length).expect("recorded read length exceeds off_t");
        // SAFETY: duplicate is an owned descriptor and the recorded read succeeded.
        let result = unsafe { libc::lseek(duplicate.as_raw_fd(), offset, libc::SEEK_CUR) };
        assert_ne!(
            result,
            -1,
            "failed to advance replay file after read: {}",
            std::io::Error::last_os_error()
        );
    }

    fn replay_regular_file_write<G: Guest<Self>>(
        &self,
        guest: &G,
        syscall: WriteFamily,
        count: usize,
        offset: Option<i64>,
        advances_offset: bool,
    ) -> Result<(), Error> {
        if !self.fd_is_in_replay_root(guest.pid(), syscall.fd()) {
            return Ok(());
        }
        let offset = offset.ok_or_else(|| {
            Error::Tool(anyhow::anyhow!(
                "recorded regular-file write is missing its offset"
            ))
        })?;
        let offset_u64 = u64::try_from(offset).map_err(|_| {
            Error::Tool(anyhow::anyhow!(
                "recorded regular-file write used negative offset {offset}"
            ))
        })?;
        let bytes = read_write_bytes(&guest.memory(), syscall, count)?;
        let duplicate =
            crate::fd::duplicate_guest_fd(guest.pid(), syscall.fd()).map_err(Error::Io)?;
        let file = std::fs::File::from(duplicate);
        file.write_all_at(&bytes, offset_u64).map_err(Error::Io)?;

        if advances_offset {
            let next = offset
                .checked_add(i64::try_from(count).map_err(|_| {
                    Error::Tool(anyhow::anyhow!(
                        "recorded regular-file write length {count} exceeds i64"
                    ))
                })?)
                .ok_or_else(|| {
                    Error::Tool(anyhow::anyhow!(
                        "recorded regular-file write offset overflow"
                    ))
                })?;
            // SAFETY: file owns a duplicate of the guest open-file description.
            let result = unsafe { libc::lseek(file.as_raw_fd(), next, libc::SEEK_SET) };
            if result == -1 {
                return Err(Error::Io(std::io::Error::last_os_error()));
            }
            if result != next {
                return Err(Error::Tool(anyhow::anyhow!(
                    "failed to advance replay file after write to {next}: positioned at {result}"
                )));
            }
        }
        Ok(())
    }

    /// Replays the vectored read family (`readv`/`preadv`/`preadv2`) by
    /// scattering the recorded flattened output bytes across the guest's current
    /// `iovec` buffers. Guest-created regular files and eventfds are also read
    /// live so their kernel state remains aligned with the recording.
    pub(super) async fn handle_readv_family<G: Guest<Self>>(
        &self,
        guest: &mut G,
        iov_addr: Option<usize>,
        iovcnt: usize,
        syscall: Syscall,
    ) -> Result<i64, Error> {
        let event = next_event!(guest, ReadvV2)?;
        let (fd, advances_offset) = match syscall {
            Syscall::Readv(call) => (call.fd(), true),
            Syscall::Preadv(call) => (call.fd(), false),
            Syscall::Preadv2(call) => {
                (call.fd(), vectored_offset(call.pos_l(), call.pos_h()) == -1)
            }
            _ => unreachable!("readv-family handler received {syscall:?}"),
        };
        match event.replay_fd_kind {
            ReplayFdKind::Eventfd => {
                let actual = inject_kernel_side_effect(guest, fd, syscall).await?;
                assert_eq!(
                    actual,
                    Ok(event.bytes.len() as i64),
                    "replayed readv eventfd side effect diverged"
                );
            }
            ReplayFdKind::RegularFile if advances_offset => {
                self.advance_regular_file_position(guest.pid(), fd, event.bytes.len());
            }
            ReplayFdKind::None | ReplayFdKind::RegularFile => {}
        }
        for _ in 0..event.consumed_sigpipe_count {
            self.consume_pending_sigpipe(guest).await?;
        }
        let written = scatter_iovec_output(&mut guest.memory(), iov_addr, iovcnt, &event.bytes)?;
        Ok(written as i64)
    }

    // FIXME: Generalize the read-family of syscalls with `ReadFamily`.
    pub(super) async fn handle_read<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Read,
    ) -> Result<i64, Error> {
        let event = next_event!(guest, ReadV2)?;
        match event.replay_fd_kind {
            ReplayFdKind::Eventfd => {
                let actual = inject_kernel_side_effect(guest, syscall.fd(), syscall.into()).await?;
                assert_eq!(
                    actual,
                    Ok(event.bytes.len() as i64),
                    "replayed read eventfd side effect diverged"
                );
            }
            ReplayFdKind::RegularFile => {
                self.advance_regular_file_position(guest.pid(), syscall.fd(), event.bytes.len());
            }
            ReplayFdKind::None => {}
        }
        for _ in 0..event.consumed_sigpipe_count {
            self.consume_pending_sigpipe(guest).await?;
        }

        assert!(event.bytes.len() <= syscall.len());

        // A read that transferred nothing never touched the buffer, and it may
        // be NULL.
        if !event.bytes.is_empty() {
            guest
                .memory()
                .write_exact(syscall.buf().unwrap(), &event.bytes)
                .unwrap();
        }
        Ok(event.bytes.len() as i64)
    }

    pub(super) async fn handle_pread64<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Pread64,
    ) -> Result<i64, Errno> {
        let buf = next_event!(guest, Bytes)?;

        assert!(buf.len() <= syscall.len());

        // Write out the buffer. A read that transferred nothing never touched
        // it, and it may be NULL.
        if !buf.is_empty() {
            guest
                .memory()
                .write_exact(syscall.buf().unwrap(), &buf)
                .unwrap();
        }
        Ok(buf.len() as i64)
    }

    async fn consume_pending_sigpipe<G: Guest<Self>>(&self, guest: &mut G) -> Result<(), Errno> {
        let mut set: libc::sigset_t = unsafe { std::mem::zeroed() };
        // SAFETY: set is initialized and remains valid for both libc calls.
        assert_eq!(unsafe { libc::sigemptyset(&mut set) }, 0);
        assert_eq!(unsafe { libc::sigaddset(&mut set, libc::SIGPIPE) }, 0);

        let mut stack = guest.stack().await;
        let set_addr = stack.reserve::<libc::sigset_t>();
        let info_addr = stack.reserve::<libc::siginfo_t>();
        let timeout_addr = stack.push(Timespec {
            tv_sec: 0,
            tv_nsec: 0,
        });
        let _guard = stack.commit()?;
        guest.memory().write_value(set_addr, &set)?;
        let consumed = guest
            .inject(
                RtSigtimedwait::new()
                    .with_set(Some(set_addr))
                    .with_info(Some(info_addr))
                    .with_timeout(Some(timeout_addr))
                    .with_sigsetsize(std::mem::size_of::<u64>()),
            )
            .await;
        assert_eq!(
            consumed,
            Ok(libc::SIGPIPE as i64),
            "failed to consume replayed SIGPIPE after signalfd read: {consumed:?}"
        );
        Ok(())
    }
    async fn replay_sigpipe<G: Guest<Self>>(&self, guest: &mut G) -> Result<(), Errno> {
        let uid = guest.inject(Getuid::new()).await? as libc::uid_t;

        let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
        assert!(std::mem::size_of::<UserSignalInfoHead>() <= std::mem::size_of_val(&info));
        let head =
            unsafe { &mut *(&mut info as *mut libc::siginfo_t).cast::<UserSignalInfoHead>() };
        *head = UserSignalInfoHead {
            signo: libc::SIGPIPE,
            errno: 0,
            code: libc::SI_USER,
            padding: 0,
            pid: guest.pid().as_raw(),
            uid,
        };

        let mut stack = guest.stack().await;
        let info_addr = stack.reserve::<libc::siginfo_t>();
        let _guard = stack.commit()?;
        guest.memory().write_value(info_addr, &info)?;
        let delivered = guest
            .inject(
                RtTgsigqueueinfo::new()
                    .with_tgid(guest.pid().as_raw())
                    .with_tid(guest.tid().as_raw())
                    .with_sig(libc::SIGPIPE)
                    .with_siginfo(Some(info_addr)),
            )
            .await;
        assert_eq!(
            delivered,
            Ok(0),
            "failed to reproduce recorded SIGPIPE: {delivered:?}"
        );
        Ok(())
    }

    fn output_endpoint(
        &self,
        output_fd: libc::c_int,
    ) -> Result<(&tokio::sync::Mutex<()>, RawFd), Error> {
        let (lock, output, error) = match output_fd {
            libc::STDOUT_FILENO => (&self.stdout_output_lock, &self.stdout, &self.stdout_error),
            libc::STDERR_FILENO => (&self.stderr_output_lock, &self.stderr, &self.stderr_error),
            _ => {
                return Err(Error::Tool(anyhow::anyhow!(
                    "recording names invalid captured output descriptor {output_fd}"
                )));
            }
        };
        let output = output.as_ref().ok_or_else(|| {
            let reason = error.as_deref().unwrap_or("descriptor was closed");
            Error::Tool(anyhow::anyhow!(
                "recording requires output fd {output_fd}, but replay could not duplicate it: {reason}"
            ))
        })?;
        Ok((lock, output.as_raw_fd()))
    }

    async fn replay_output<G: Guest<Self>>(
        &self,
        guest: &mut G,
        advances_output_offset: bool,
        syscall: WriteFamily,
        output_fd: i32,
        count: usize,
        output_offset: Option<i64>,
    ) -> Result<(), Error> {
        // Faults while reading the guest's syscall arguments are Linux-visible
        // syscall errors. Failures after that boundary are failures of Replay's
        // host-side output machinery and must abort the tool instead.
        let bytes = read_write_bytes(&guest.memory(), syscall, count)?;
        let (output_lock, output) = self.output_endpoint(output_fd)?;
        let _guard = output_lock.lock().await;
        emit_replay_output(output, &bytes, output_offset, advances_output_offset)
            .await
            .map_err(Error::Io)?;
        Ok(())
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#557): Audit recorded write side effects and signal fidelity.
    pub(super) async fn handle_write_family<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: WriteFamily,
    ) -> Result<i64, Error> {
        let event = next_event!(guest, WriteV2)?;
        let successful_count = successful_replay_write_count(event.result)?;
        match event.replay_fd_kind {
            ReplayFdKind::Eventfd => {
                let actual =
                    inject_kernel_side_effect(guest, syscall.fd(), Syscall::from(syscall)).await?;
                assert_eq!(
                    actual, event.result,
                    "replayed eventfd write side effect diverged"
                );
            }
            ReplayFdKind::RegularFile => {
                if let Some(count) = successful_count {
                    self.replay_regular_file_write(
                        guest,
                        syscall,
                        count,
                        event.replay_file_offset,
                        event.replay_file_advances_offset,
                    )?;
                }
            }
            ReplayFdKind::None => {}
        }
        if event.generated_sigpipe {
            self.replay_sigpipe(guest).await?;
        }
        if let (Some(count), Some(output_fd)) = (successful_count, event.output_fd) {
            self.replay_output(
                guest,
                event.advances_output_offset,
                syscall,
                output_fd,
                count,
                event.output_offset,
            )
            .await?;
        }
        event.result.map_err(Error::from)
    }

    // TODO-HUMAN-REVIEW(#557): Audit captured-output ftruncate replay.
    pub(super) async fn handle_ftruncate<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Ftruncate,
    ) -> Result<i64, Error> {
        let event = next_event!(guest, FtruncateV2)?;
        if event.result.is_ok() {
            if event.length != syscall.length() {
                return Err(Error::Tool(anyhow::anyhow!(
                    "replayed ftruncate length diverged: recorded {}, requested {}",
                    event.length,
                    syscall.length()
                )));
            }
            if let Some(output_fd) = event.output_fd {
                let (output_lock, output) = self.output_endpoint(output_fd)?;
                let _guard = output_lock.lock().await;
                truncate_replay_output(output, event.length)?;
            }
            if event.replay_regular_file
                && event.output_fd.is_none()
                && self.fd_is_in_replay_root(guest.pid(), syscall.fd())
            {
                let duplicate =
                    crate::fd::duplicate_guest_fd(guest.pid(), syscall.fd()).map_err(Error::Io)?;
                let file = std::fs::File::from(duplicate);
                let length = u64::try_from(event.length).map_err(|_| {
                    Error::Tool(anyhow::anyhow!(
                        "recording contains successful ftruncate with negative length {}",
                        event.length
                    ))
                })?;
                file.set_len(length).map_err(Error::Io)?;
            }
        }
        event.result.map_err(Error::from)
    }

    pub(super) async fn handle_stat_family<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: StatFamily,
    ) -> Result<i64, Errno> {
        next_event!(guest, Stat).and_then(|event| {
            let addr = syscall.stat().ok_or(Errno::EFAULT)?;
            guest.memory().write_value(addr.0, &event.statbuf)?;
            // stat calls always return 0 on success.
            Ok(0)
        })
    }

    pub(super) async fn handle_statfs<G: Guest<Self>>(
        &self,
        guest: &mut G,
        buf: Option<AddrMut<'_, libc::statfs>>,
    ) -> Result<i64, Errno> {
        let bytes = next_event!(guest, Statfs)?;
        assert_eq!(bytes.len(), std::mem::size_of::<libc::statfs>());
        guest
            .memory()
            .write_exact(buf.ok_or(Errno::EFAULT)?.cast(), &bytes)?;
        Ok(0)
    }

    pub(super) async fn handle_statx<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Statx,
    ) -> Result<i64, Errno> {
        next_event!(guest, Statx).and_then(|buf| {
            let addr = syscall.statx().ok_or(Errno::EFAULT)?;
            guest.memory().write_value(addr.0, &buf.into())?;
            // statx calls always return 0 on success.
            Ok(0)
        })
    }

    pub(super) async fn handle_ioctl<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Ioctl,
    ) -> Result<i64, Errno> {
        let request = syscall.request();

        if deterministic_ioctl_error(&request).is_some() {
            return next_event!(guest, Return);
        }

        if matches!(
            request,
            ioctl::Request::FICLONE(_) | ioctl::Request::FICLONERANGE(_)
        ) {
            let snapshot = next_event!(guest, FileClone)?;
            let destination_is_internal = self.fd_is_in_replay_root(guest.pid(), syscall.fd());
            if destination_is_internal {
                let path = format!("/proc/{}/fd/{}", guest.pid().as_raw(), syscall.fd());
                let file = std::fs::OpenOptions::new()
                    .write(true)
                    .open(&path)
                    .unwrap_or_else(|error| {
                        panic!("failed to open cloned replay destination {path}: {error}")
                    });
                let prior_length = file
                    .metadata()
                    .unwrap_or_else(|error| panic!("failed to stat clone destination: {error}"))
                    .len();
                if snapshot.truncate_destination {
                    file.set_len(0).unwrap_or_else(|error| {
                        panic!("failed to truncate cloned replay file: {error}")
                    });
                }
                file.set_len(snapshot.length)
                    .unwrap_or_else(|error| panic!("failed to size cloned replay file: {error}"));
                if !snapshot.truncate_destination && snapshot.destination_offset < prior_length {
                    let overlap = snapshot
                        .replacement_length
                        .min(prior_length - snapshot.destination_offset);
                    clear_clone_destination_range(&file, snapshot.destination_offset, overlap)
                        .unwrap_or_else(|error| {
                            panic!("failed to clear cloned replay range: {error}")
                        });
                }
                match snapshot.image {
                    FileCloneImage::Extents(extents) => {
                        for extent in extents {
                            let offset = snapshot
                                .destination_offset
                                .checked_add(extent.offset)
                                .expect("clone destination offset overflow");
                            file.write_all_at(&extent.bytes, offset)
                                .unwrap_or_else(|error| {
                                    panic!("failed to materialize cloned replay extent: {error}")
                                });
                        }
                    }
                    FileCloneImage::Sidecar(relative) => {
                        let relative = std::path::Path::new(&relative);
                        assert!(
                            !relative.is_absolute()
                                && !relative.components().any(|component| matches!(
                                    component,
                                    std::path::Component::ParentDir | std::path::Component::RootDir
                                )),
                            "invalid clone sidecar path {relative:?}"
                        );
                        let sidecar = self.data.join(relative);
                        let source = std::fs::File::open(&sidecar).unwrap_or_else(|error| {
                            panic!("failed to open clone sidecar {sidecar:?}: {error}")
                        });
                        let sidecar_length = source
                            .metadata()
                            .unwrap_or_else(|error| {
                                panic!("failed to stat clone sidecar {sidecar:?}: {error}")
                            })
                            .len();
                        assert_eq!(
                            sidecar_length, snapshot.replacement_length,
                            "clone sidecar length changed"
                        );
                        restore_sparse_clone_sidecar(
                            &source,
                            &file,
                            snapshot.replacement_length,
                            snapshot.destination_offset,
                        )
                        .unwrap_or_else(|error| {
                            panic!("failed to restore clone sidecar {sidecar:?}: {error}")
                        });
                    }
                }
            }
            Ok(0)
        } else if matches!(
            request,
            ioctl::Request::FIOCLEX | ioctl::Request::FIONCLEX | ioctl::Request::FIONBIO(_)
        ) {
            self.handle_replayed_side_effect(guest, Syscall::from(syscall), "ioctl")
                .await
        } else if request.direction() == ioctl::Direction::Read {
            let output = next_event!(guest, Ioctl)?;
            request.write_output(&mut guest.memory(), &output)?;
            Ok(0)
        } else {
            let ret = next_event!(guest, Return)?;
            Ok(ret)
        }
    }

    pub(super) async fn handle_readlink<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Readlink,
    ) -> Result<i64, Errno> {
        let buf = next_event!(guest, Bytes)?;

        debug_assert!(buf.len() <= syscall.bufsize());

        // Write out the buffer.
        guest
            .memory()
            .write_exact(syscall.buf().unwrap().cast::<u8>(), &buf)?;
        Ok(buf.len() as i64)
    }

    // TODO-HUMAN-REVIEW(PR-3601)
    /// Replays an xattr query from the recording without asking the replay
    /// process: its descriptor may be a placeholder and its paths live in the
    /// replay chroot. A sized query writes back the recorded value bytes; a
    /// zero-size query returns the recorded length the value needs.
    pub(super) async fn handle_xattr_query<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Syscall,
    ) -> Result<i64, Errno> {
        let (buf, size) = xattr_query_output(&syscall)
            .unwrap_or_else(|| unreachable!("xattr query replay called for {syscall:?}"));
        if size == 0 {
            return next_event!(guest, Return);
        }
        let bytes = next_event!(guest, Bytes)?;
        assert!(
            bytes.len() <= size,
            "recorded xattr value of {} bytes exceeds the {size}-byte buffer",
            bytes.len()
        );
        if !bytes.is_empty() {
            guest.memory().write_exact(
                buf.expect("a recorded non-empty xattr value had an output buffer"),
                &bytes,
            )?;
        }
        Ok(bytes.len() as i64)
    }

    // TODO-HUMAN-REVIEW(#3598)
    /// Writes back the recorded working directory, including its NUL, without
    /// asking the replay process, whose working directory lives in the replay
    /// chroot rather than the recording's namespace.
    pub(super) async fn handle_getcwd<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Getcwd,
    ) -> Result<i64, Errno> {
        let buf = next_event!(guest, Bytes)?;

        debug_assert!(buf.len() <= syscall.size());

        guest
            .memory()
            .write_exact(syscall.buf().ok_or(Errno::EFAULT)?.cast::<u8>(), &buf)?;
        Ok(buf.len() as i64)
    }

    pub(super) async fn handle_getdents<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Getdents,
    ) -> Result<i64, Errno> {
        let buf = next_event!(guest, Bytes)?;

        // Make sure we don't overflow the buffer.
        debug_assert!(buf.len() <= syscall.count() as usize);

        // Write out the buffer.
        guest
            .memory()
            .write_exact(syscall.dirent().unwrap().cast::<u8>(), &buf)?;
        Ok(buf.len() as i64)
    }

    pub(super) async fn handle_getdents64<G: Guest<Self>>(
        &self,
        guest: &mut G,
        syscall: Getdents64,
    ) -> Result<i64, Errno> {
        let buf = next_event!(guest, Bytes)?;

        // Make sure we don't overflow the buffer.
        debug_assert!(buf.len() <= syscall.count() as usize);

        // Write out the buffer.
        guest
            .memory()
            .write_exact(syscall.dirent().unwrap().cast::<u8>(), &buf)?;
        Ok(buf.len() as i64)
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::fs::OpenOptions;
    use std::io::Read as _;
    use std::io::Seek as _;
    use std::io::Write as _;
    use std::os::fd::OwnedFd;
    use std::os::unix::net::UnixStream;
    use std::time::Duration;

    use tokio::io::unix::AsyncFd;

    use super::*;

    #[test]
    fn successful_kernel_side_effect_restore_preserves_the_injected_result() {
        assert_eq!(finish_kernel_side_effect(4, Ok(7), Ok(0)).unwrap(), Ok(7));
        assert_eq!(
            finish_kernel_side_effect(4, Err(Errno::EAGAIN), Ok(0)).unwrap(),
            Err(Errno::EAGAIN)
        );
    }

    #[test]
    fn kernel_side_effect_restore_failure_is_not_a_guest_errno() {
        for restored in [Err(Errno::EBADF), Ok(1)] {
            let failure = finish_kernel_side_effect(4, Ok(7), restored)
                .expect_err("restoration failure must abort replay");
            assert!(
                matches!(failure.into_errno(), Err(Error::Tool(_))),
                "restoration failure became guest-visible errno"
            );
        }
    }

    #[test]
    fn temporary_flag_change_failure_is_not_a_guest_errno() {
        for result in [Err(Errno::EBADF), Ok(1)] {
            let failure = require_replay_fcntl_success(4, "temporarily clear O_NONBLOCK", result)
                .expect_err("flag setup failure must abort replay");
            assert!(
                matches!(failure.into_errno(), Err(Error::Tool(_))),
                "flag setup failure became guest-visible errno"
            );
        }
    }

    #[test]
    fn descriptor_flag_read_failure_is_not_a_guest_errno() {
        let failure = require_replay_fcntl_value(4, "read descriptor flags", Err(Errno::EBADF))
            .expect_err("flag read failure must abort replay");
        assert!(
            matches!(failure.into_errno(), Err(Error::Tool(_))),
            "flag read failure became guest-visible errno"
        );
    }

    #[test]
    fn missing_or_invalid_output_endpoint_is_an_outer_tool_error() {
        let replayer = Replayer {
            stdout_error: Some("Too many open files".to_owned()),
            ..Replayer::default()
        };
        for output_fd in [libc::STDOUT_FILENO, libc::STDERR_FILENO, 9] {
            let failure = match replayer.output_endpoint(output_fd) {
                Ok(_) => panic!("unavailable replay output fd {output_fd} unexpectedly resolved"),
                Err(error) => error,
            };
            if output_fd == libc::STDOUT_FILENO {
                assert!(
                    failure.to_string().contains("Too many open files"),
                    "endpoint acquisition failure lost its recorded cause: {failure}"
                );
            }
            assert!(
                matches!(failure.into_errno(), Err(Error::Tool(_))),
                "endpoint acquisition failure became guest-visible errno"
            );
        }
    }

    #[tokio::test]
    async fn replay_output_sink_failure_is_an_outer_io_error() {
        let output = OpenOptions::new().write(true).open("/dev/full").unwrap();

        let failure = emit_replay_output(output.as_raw_fd(), b"LOST", None, false)
            .await
            .map_err(Error::Io)
            .expect_err("full replay output device must abort replay");
        assert!(
            matches!(failure.into_errno(), Err(Error::Io(error)) if error.raw_os_error() == Some(libc::ENOSPC)),
            "replay output sink failure became guest-visible errno"
        );
    }

    #[test]
    fn replay_output_ftruncate_failure_is_an_outer_io_error() {
        let failure = truncate_replay_output(-1, 0)
            .expect_err("invalid captured output endpoint must abort replay");
        assert!(
            matches!(failure.into_errno(), Err(Error::Io(error)) if error.raw_os_error() == Some(libc::EBADF)),
            "captured-output ftruncate failure became guest-visible errno"
        );
    }

    #[tokio::test]
    async fn replay_output_readiness_failure_is_an_outer_io_error() {
        let failure = wait_for_replay_output(-1)
            .await
            .map_err(Error::Io)
            .expect_err("invalid replay output must fail readiness acquisition");
        assert!(
            matches!(failure.into_errno(), Err(Error::Io(error)) if error.raw_os_error() == Some(libc::EBADF)),
            "readiness failure became guest-visible errno"
        );
    }

    #[test]
    fn malformed_successful_write_count_is_an_outer_tool_error() {
        let failure = successful_replay_write_count(Ok(-1))
            .expect_err("negative successful count must invalidate the recording");
        assert!(
            matches!(failure.into_errno(), Err(Error::Tool(_))),
            "invalid recorded count became guest-visible errno"
        );
        assert_eq!(
            successful_replay_write_count(Err(Errno::EFAULT)).unwrap(),
            None
        );
    }

    #[tokio::test]
    async fn replay_output_preserves_regular_file_offset() {
        let mut file = tempfile::tempfile().unwrap();
        emit_replay_output(file.as_raw_fd(), b"ONE", None, false)
            .await
            .unwrap();
        emit_replay_output(file.as_raw_fd(), b"TWO", None, false)
            .await
            .unwrap();
        file.rewind().unwrap();

        let mut output = String::new();
        file.read_to_string(&mut output).unwrap();
        assert_eq!(output, "ONETWO");
    }

    #[tokio::test]
    async fn replay_output_preserves_positioned_file_writes() {
        let mut file = tempfile::tempfile().unwrap();
        emit_replay_output(file.as_raw_fd(), b"X", Some(5), false)
            .await
            .unwrap();
        file.rewind().unwrap();

        let mut output = Vec::new();
        file.read_to_end(&mut output).unwrap();
        assert_eq!(output, b"\0\0\0\0\0X");
    }

    #[tokio::test]
    async fn positioned_replay_advances_shared_offset_for_write() {
        let mut file = tempfile::tempfile().unwrap();
        emit_replay_output(file.as_raw_fd(), b"X", Some(5), true)
            .await
            .unwrap();
        assert_eq!(file.stream_position().unwrap(), 6);
        file.rewind().unwrap();

        let mut output = Vec::new();
        file.read_to_end(&mut output).unwrap();
        assert_eq!(output, b"\0\0\0\0\0X");
    }

    #[tokio::test]
    async fn positioned_replay_temporarily_clears_append() {
        let mut file = tempfile::tempfile().unwrap();
        file.write_all(b"ABC").unwrap();
        let fd = file.as_raw_fd();
        // SAFETY: fd is open and F_GETFL does not mutate memory.
        let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
        assert_ne!(flags, -1);
        // SAFETY: fd is open for the duration of the test.
        assert_ne!(
            unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_APPEND) },
            -1
        );

        emit_replay_output(fd, b"X", Some(1), false).await.unwrap();
        file.rewind().unwrap();
        let mut output = String::new();
        file.read_to_string(&mut output).unwrap();
        assert_eq!(output, "AXC");
        // SAFETY: fd remains open and F_GETFL does not mutate memory.
        assert_ne!(
            unsafe { libc::fcntl(fd, libc::F_GETFL) } & libc::O_APPEND,
            0
        );
    }

    #[tokio::test]
    async fn replay_output_supports_sockets() {
        let (output, mut peer) = UnixStream::pair().unwrap();
        emit_replay_output(output.as_raw_fd(), b"SOCKET_OUT", None, false)
            .await
            .unwrap();

        let mut received = [0; 10];
        peer.read_exact(&mut received).unwrap();
        assert_eq!(&received, b"SOCKET_OUT");
    }

    #[tokio::test]
    async fn replay_output_reports_closed_socket_failure() {
        let (output, peer) = UnixStream::pair().unwrap();
        // A concurrent fork can keep the peer alive after drop(peer). Hold a
        // duplicate to exercise that case, and shut down their shared socket.
        let inherited_peer = peer.try_clone().unwrap();
        peer.shutdown(std::net::Shutdown::Both).unwrap();
        drop(peer);

        let error = emit_replay_output(output.as_raw_fd(), b"LOST", None, false)
            .await
            .expect_err("closed replay output socket must fail");
        assert_eq!(error.raw_os_error(), Some(libc::EPIPE));
        drop(inherited_peer);
    }

    #[tokio::test]
    async fn replay_output_reports_full_device_failure() {
        let output = OpenOptions::new().write(true).open("/dev/full").unwrap();

        let error = emit_replay_output(output.as_raw_fd(), b"LOST", None, false)
            .await
            .expect_err("full replay output device must fail");
        assert_eq!(error.raw_os_error(), Some(libc::ENOSPC));
    }

    #[tokio::test(flavor = "current_thread")]
    async fn replay_output_does_not_retry_after_flag_restoration_failure() {
        let mut pipe = [0; 2];
        // SAFETY: pipe points to two writable integers.
        assert_eq!(
            unsafe { libc::pipe2(pipe.as_mut_ptr(), libc::O_CLOEXEC) },
            0
        );
        // SAFETY: ownership of each open pipe descriptor transfers exactly once.
        let _input = unsafe { OwnedFd::from_raw_fd(pipe[0]) };
        let output = unsafe { OwnedFd::from_raw_fd(pipe[1]) };

        // Fill the pipe while it is nonblocking, then restore blocking mode so
        // replay has to change and restore the shared open-file-description flags.
        let flags = unsafe { libc::fcntl(output.as_raw_fd(), libc::F_GETFL) };
        assert_ne!(flags, -1);
        set_replay_output_flags(output.as_raw_fd(), flags | libc::O_NONBLOCK).unwrap();
        let fill = [0_u8; 4096];
        loop {
            let written =
                unsafe { libc::write(output.as_raw_fd(), fill.as_ptr().cast(), fill.len()) };
            if written >= 0 {
                continue;
            }
            assert_eq!(
                std::io::Error::last_os_error().kind(),
                std::io::ErrorKind::WouldBlock
            );
            break;
        }
        set_replay_output_flags(output.as_raw_fd(), flags).unwrap();

        let set_flags_calls = Cell::new(0);
        let mut set_flags = |fd, requested_flags| {
            let call = set_flags_calls.get() + 1;
            set_flags_calls.set(call);
            if call == 2 {
                Err(std::io::Error::from_raw_os_error(libc::EBADF))
            } else {
                set_replay_output_flags(fd, requested_flags)
            }
        };
        let mut write_once = |fd, bytes: &[u8], offset| {
            write_replay_output_once_with(fd, bytes, offset, &mut set_flags)
        };

        let error = tokio::time::timeout(
            Duration::from_millis(250),
            emit_replay_output_with(output.as_raw_fd(), b"x", None, false, &mut write_once),
        )
        .await
        .expect("flag restoration failure was incorrectly retried")
        .expect_err("flag restoration failure must fail replay output");
        assert_eq!(set_flags_calls.get(), 2);
        assert_eq!(error.kind(), std::io::ErrorKind::Other);
        let rendered = error.to_string();
        assert!(
            rendered.contains("failed to restore replay output flags"),
            "missing restoration context: {rendered}"
        );
        assert!(
            rendered.contains(&format!("os error {}", libc::EBADF)),
            "missing EBADF cause: {rendered}"
        );
        let failure = Error::Io(error);
        assert!(
            matches!(failure.into_errno(), Err(Error::Io(_))),
            "flag-restoration failure became guest-visible errno"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn replay_output_retries_after_blocking_pipe_backpressure_on_same_executor() {
        let mut pipe = [0; 2];
        // SAFETY: pipe points to two writable integers.
        assert_eq!(
            unsafe { libc::pipe2(pipe.as_mut_ptr(), libc::O_CLOEXEC) },
            0
        );
        // SAFETY: ownership of each open pipe descriptor transfers exactly once.
        let input = AsyncFd::new(unsafe { OwnedFd::from_raw_fd(pipe[0]) }).unwrap();
        let output = unsafe { OwnedFd::from_raw_fd(pipe[1]) };
        // AsyncFd readers must use a nonblocking descriptor. The write end
        // deliberately remains blocking to exercise temporary flag handling.
        let input_flags = unsafe { libc::fcntl(input.as_raw_fd(), libc::F_GETFL) };
        assert_ne!(input_flags, -1);
        assert_ne!(
            unsafe {
                libc::fcntl(
                    input.as_raw_fd(),
                    libc::F_SETFL,
                    input_flags | libc::O_NONBLOCK,
                )
            },
            -1
        );
        let output_flags = unsafe { libc::fcntl(output.as_raw_fd(), libc::F_GETFL) };
        assert_eq!(output_flags & libc::O_NONBLOCK, 0);

        let expected = vec![b'x'; 256 * 1024];
        let expected_for_reader = expected.clone();
        let reader = tokio::spawn(async move {
            let mut actual = vec![0; expected_for_reader.len()];
            let mut offset = 0;
            while offset < actual.len() {
                let mut readiness = input.readable().await.unwrap();
                match readiness.try_io(|input| {
                    // SAFETY: actual's unwritten suffix is valid and the descriptor is open.
                    let read = unsafe {
                        libc::read(
                            input.get_ref().as_raw_fd(),
                            actual[offset..].as_mut_ptr().cast(),
                            actual.len() - offset,
                        )
                    };
                    if read == -1 {
                        Err(std::io::Error::last_os_error())
                    } else {
                        Ok(read as usize)
                    }
                }) {
                    Ok(Ok(0)) => break,
                    Ok(Ok(read)) => offset += read,
                    Ok(Err(error)) => panic!("pipe read failed: {error}"),
                    Err(_) => continue,
                }
            }
            actual.truncate(offset);
            actual
        });

        let actual = tokio::time::timeout(Duration::from_secs(2), async {
            emit_replay_output(output.as_raw_fd(), &expected, None, false)
                .await
                .unwrap();
            assert_eq!(
                unsafe { libc::fcntl(output.as_raw_fd(), libc::F_GETFL) } & libc::O_NONBLOCK,
                0,
                "replay output left the caller's pipe nonblocking"
            );
            drop(output);
            reader.await.unwrap()
        })
        .await
        .expect("replay output deadlocked its pipe reader");
        assert_eq!(actual, expected);
    }

    #[tokio::test(flavor = "current_thread")]
    async fn replay_output_cancellation_restores_blocking_pipe_flags() {
        let mut pipe = [0; 2];
        // SAFETY: pipe points to two writable integers.
        assert_eq!(
            unsafe { libc::pipe2(pipe.as_mut_ptr(), libc::O_CLOEXEC) },
            0
        );
        // SAFETY: ownership of each open pipe descriptor transfers exactly once.
        let _input = unsafe { OwnedFd::from_raw_fd(pipe[0]) };
        let output = unsafe { OwnedFd::from_raw_fd(pipe[1]) };

        // Fill the pipe without blocking, then restore its blocking mode before
        // calling the async replay path.
        let flags = unsafe { libc::fcntl(output.as_raw_fd(), libc::F_GETFL) };
        assert_ne!(flags, -1);
        assert_ne!(
            unsafe { libc::fcntl(output.as_raw_fd(), libc::F_SETFL, flags | libc::O_NONBLOCK) },
            -1
        );
        let fill = [0_u8; 4096];
        loop {
            let written =
                unsafe { libc::write(output.as_raw_fd(), fill.as_ptr().cast(), fill.len()) };
            if written >= 0 {
                continue;
            }
            assert_eq!(
                std::io::Error::last_os_error().kind(),
                std::io::ErrorKind::WouldBlock
            );
            break;
        }
        assert_ne!(
            unsafe { libc::fcntl(output.as_raw_fd(), libc::F_SETFL, flags) },
            -1
        );

        let result = tokio::time::timeout(
            Duration::from_millis(25),
            emit_replay_output(output.as_raw_fd(), b"x", None, false),
        )
        .await;
        assert!(
            result.is_err(),
            "full pipe unexpectedly accepted replay output"
        );
        assert_eq!(
            unsafe { libc::fcntl(output.as_raw_fd(), libc::F_GETFL) } & libc::O_NONBLOCK,
            0,
            "cancelled replay output left the caller's pipe nonblocking"
        );
    }
}