nuclei 0.5.2

Proactive IO & runtime system
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
//! The socket handles through their own API, on a runtime: what each call
//! returns, the trait impls on a handle and on a shared reference, and the
//! rules that keep the descriptor the handle's.

use core::future::{Future, poll_fn};
use core::pin::Pin;
use core::task::Poll;
use std::io::{self, Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream, UdpSocket};
#[cfg(unix)]
use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd};
#[cfg(unix)]
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
use std::time::Duration;

use futures_core::Stream;
use futures_io::{AsyncRead, AsyncWrite};

use crate::alone::alone;
use crate::io::Handle;
use crate::runtime::{Builder, Runtime, spawn};

fn runtime() -> Runtime {
    Builder::new()
        .worker_threads(2)
        .pin_workers(false)
        .build()
        .unwrap()
}

async fn within<F: Future>(fut: F) -> F::Output {
    crate::time::timeout(Duration::from_secs(10), fut)
        .await
        .expect("the operation never completed")
}

async fn read_some<R: AsyncRead + Unpin>(r: &mut R, buf: &mut [u8]) -> io::Result<usize> {
    poll_fn(|cx| Pin::new(&mut *r).poll_read(cx, buf)).await
}

async fn read_exact<R: AsyncRead + Unpin>(r: &mut R, mut buf: &mut [u8]) -> io::Result<()> {
    while !buf.is_empty() {
        match read_some(r, buf).await? {
            0 => return Err(io::ErrorKind::UnexpectedEof.into()),
            n => buf = &mut buf[n..],
        }
    }
    Ok(())
}

async fn write_all<W: AsyncWrite + Unpin>(w: &mut W, mut buf: &[u8]) -> io::Result<()> {
    while !buf.is_empty() {
        let n = poll_fn(|cx| Pin::new(&mut *w).poll_write(cx, buf)).await?;
        buf = &buf[n..];
    }
    poll_fn(|cx| Pin::new(&mut *w).poll_flush(cx)).await
}

async fn close<W: AsyncWrite + Unpin>(w: &mut W) -> io::Result<()> {
    poll_fn(|cx| Pin::new(&mut *w).poll_close(cx)).await
}

async fn next<S: Stream + Unpin>(s: &mut S) -> Option<S::Item> {
    poll_fn(|cx| Pin::new(&mut *s).poll_next(cx)).await
}

/// A minimal executor of a thread that runs no shard: park until woken.
fn plain_block_on<F: Future>(fut: F) -> F::Output {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::task::{Context, Wake, Waker};
    struct Unpark(std::thread::Thread, AtomicBool);
    impl Wake for Unpark {
        fn wake(self: Arc<Self>) {
            self.1.store(true, Ordering::SeqCst);
            self.0.unpark();
        }
    }
    let unpark = Arc::new(Unpark(std::thread::current(), AtomicBool::new(false)));
    let waker = Waker::from(Arc::clone(&unpark));
    let mut cx = Context::from_waker(&waker);
    let mut fut = core::pin::pin!(fut);
    let deadline = std::time::Instant::now() + Duration::from_secs(10);
    loop {
        if let Poll::Ready(out) = fut.as_mut().poll(&mut cx) {
            return out;
        }
        while !unpark.1.swap(false, Ordering::SeqCst) {
            assert!(std::time::Instant::now() < deadline, "never woken");
            std::thread::park_timeout(Duration::from_millis(100));
        }
    }
}

fn pattern(len: usize) -> Vec<u8> {
    (0..len).map(|i| (i * 13 + i / 241) as u8).collect()
}

/// The connected stream sockets the tests pair: Unix ones where the system
/// has them, else TCP over loopback.
#[cfg(unix)]
type Pair = UnixStream;
#[cfg(windows)]
type Pair = TcpStream;

fn stream_pair() -> (Pair, Pair) {
    #[cfg(unix)]
    return UnixStream::pair().unwrap();
    #[cfg(windows)]
    {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let a = TcpStream::connect(listener.local_addr().unwrap()).unwrap();
        let (b, _) = listener.accept().unwrap();
        (a, b)
    }
}

fn handle_pair() -> (Handle<Pair>, Handle<Pair>) {
    let (a, b) = stream_pair();
    (Handle::new(a).unwrap(), Handle::new(b).unwrap())
}

#[cfg(unix)]
fn tempdir(tag: &str) -> std::path::PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "nuclei-net-{tag}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

/// Where a seccomp filter ends any thread that calls `io_uring_setup`, the
/// process is not ended: `auto` falls back to epoll, and a TCP exchange runs.
/// Alone in its process: the filter cannot be taken off again.
#[cfg(all(target_os = "linux", feature = "iouring"))]
#[test]
fn auto_falls_back_to_epoll_where_a_filter_kills_io_uring() {
    if !alone("net::tests::auto_falls_back_to_epoll_where_a_filter_kills_io_uring") {
        return;
    }
    if std::env::var("NUCLEI_BACKEND").is_ok_and(|b| !b.is_empty() && b != "auto") {
        return;
    }
    use crate::syscore::linux::probe::tests::{answer, install};
    // Every thread the runtime starts from here on carries the filter.
    if let Err(e) = install(answer(
        libc::SYS_io_uring_setup,
        libc::SECCOMP_RET_KILL_THREAD,
    )) {
        eprintln!("skipping: cannot install a seccomp filter here: {e}");
        return;
    }
    assert!(
        !crate::rt::backend::chosen().unwrap().rings(),
        "auto took a ring this process cannot have"
    );
    let rt = runtime();
    rt.block_on(async {
        let listener = Handle::<TcpListener>::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let server = spawn(async move {
            let (mut conn, _) = within(listener.accept()).await.unwrap();
            let mut buf = [0u8; 5];
            within(read_exact(&mut conn, &mut buf)).await.unwrap();
            within(write_all(&mut conn, &buf)).await.unwrap();
        });
        let mut client = within(Handle::<TcpStream>::connect(addr)).await.unwrap();
        within(write_all(&mut client, b"hello")).await.unwrap();
        let mut back = [0u8; 5];
        within(read_exact(&mut client, &mut back)).await.unwrap();
        assert_eq!(&back, b"hello");
        server.await;
    });
}

#[test]
fn a_tcp_echo_carries_a_large_message_both_ways() {
    let rt = runtime();
    rt.block_on(async {
        let listener = Handle::<TcpListener>::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let server = spawn(async move {
            let (mut conn, peer) = within(listener.accept()).await.unwrap();
            assert!(peer.ip().is_loopback());
            let mut buf = vec![0u8; 64 * 1024];
            loop {
                let n = within(read_some(&mut conn, &mut buf)).await.unwrap();
                if n == 0 {
                    break;
                }
                within(write_all(&mut conn, &buf[..n])).await.unwrap();
            }
            close(&mut conn).await.unwrap();
        });
        let mut client = within(Handle::<TcpStream>::connect(addr)).await.unwrap();
        let data = pattern(3 << 20);
        let (reader, writer) = (&client, &client);
        let expected = data.clone();
        let mut r = reader;
        let read_back = async move {
            let mut got = vec![0u8; expected.len()];
            read_exact(&mut r, &mut got).await.unwrap();
            got == expected
        };
        let mut w = writer;
        let send = async move {
            write_all(&mut w, &data).await.unwrap();
            close(&mut w).await.unwrap();
        };
        // Both halves at once, through shared references.
        let (same, ()) = within(join(read_back, send)).await;
        assert!(same, "the echo came back changed");
        within(server).await;
        let mut rest = [0u8; 1];
        assert_eq!(within(read_some(&mut client, &mut rest)).await.unwrap(), 0);
    });
}

/// Runs two futures together on the calling task.
async fn join<A: Future, B: Future>(a: A, b: B) -> (A::Output, B::Output) {
    let (mut a, mut b) = (Box::pin(a), Box::pin(b));
    let (mut ra, mut rb) = (None, None);
    poll_fn(|cx| {
        if ra.is_none()
            && let Poll::Ready(v) = a.as_mut().poll(cx)
        {
            ra = Some(v);
        }
        if rb.is_none()
            && let Poll::Ready(v) = b.as_mut().poll(cx)
        {
            rb = Some(v);
        }
        if ra.is_some() && rb.is_some() {
            Poll::Ready((ra.take().unwrap(), rb.take().unwrap()))
        } else {
            Poll::Pending
        }
    })
    .await
}

#[test]
fn send_recv_and_peek_on_tcp() {
    let rt = runtime();
    rt.block_on(async {
        let listener = Handle::<TcpListener>::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let client = spawn(async move { Handle::<TcpStream>::connect(addr).await.unwrap() });
        let (conn, _) = within(listener.accept()).await.unwrap();
        let client = within(client).await;
        assert_eq!(within(client.send(b"ping")).await.unwrap(), 4);
        let mut buf = [0u8; 8];
        assert_eq!(within(conn.peek(&mut buf)).await.unwrap(), 4);
        assert_eq!(within(conn.recv(&mut buf)).await.unwrap(), 4);
        assert_eq!(&buf[..4], b"ping");
    });
}

#[test]
fn connect_tries_each_address_and_reports_the_last_error() {
    let rt = runtime();
    rt.block_on(async {
        let (_held, closed) = crate::net::refused_addr();
        let err = within(Handle::<TcpStream>::connect(closed))
            .await
            .unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::ConnectionRefused);
        let listener = Handle::<TcpListener>::bind("127.0.0.1:0").await.unwrap();
        let open = listener.local_addr().unwrap();
        let both: &[SocketAddr] = &[closed, open];
        let stream = within(Handle::<TcpStream>::connect(both)).await.unwrap();
        assert_eq!(stream.peer_addr().unwrap(), open);
        let none: &[SocketAddr] = &[];
        let err = within(Handle::<TcpStream>::connect(none))
            .await
            .unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
    });
}

#[test]
fn incoming_and_accept_multi_yield_every_connection() {
    let rt = runtime();
    rt.block_on(async {
        let listener = Handle::<TcpListener>::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let clients: Vec<_> = (0..6).map(|_| TcpStream::connect(addr).unwrap()).collect();
        let mut incoming = listener.incoming();
        for _ in 0..3 {
            within(next(&mut incoming)).await.unwrap().unwrap();
        }
        drop(incoming);
        let mut multi = within(listener.accept_multi()).await.unwrap();
        // The generator owns its own copy of the listener.
        drop(listener);
        for _ in 0..3 {
            let conn = within(next(&mut multi)).await.unwrap();
            assert!(conn.peer_addr().is_ok());
        }
        assert!(multi.take_error().is_none());
        drop(clients);
    });
}

#[test]
fn udp_sends_receives_peeks_and_connects() {
    let rt = runtime();
    rt.block_on(async {
        let a = Handle::<UdpSocket>::bind("127.0.0.1:0").await.unwrap();
        let b = Handle::<UdpSocket>::bind("127.0.0.1:0").await.unwrap();
        let (a_addr, b_addr) = (a.local_addr().unwrap(), b.local_addr().unwrap());
        let mut buf = [0u8; 64];
        // A receive that waits, completed by the driver.
        let sender = async {
            crate::time::sleep(Duration::from_millis(5)).await;
            a.send_to(b"hello", b_addr).await.unwrap()
        };
        let (got, sent) = within(join(b.peek_from(&mut buf), sender)).await;
        let (n, from) = got.unwrap();
        assert_eq!((n, from, sent), (5, a_addr, 5));
        let (n, from) = within(b.recv_from(&mut buf)).await.unwrap();
        assert_eq!((&buf[..n], from), (&b"hello"[..], a_addr));
        // A connected socket talks to its peer only.
        let c = within(Handle::<UdpSocket>::connect(b_addr)).await.unwrap();
        assert_eq!(within(c.send(b"conn")).await.unwrap(), 4);
        let (n, from) = within(b.recv_from(&mut buf)).await.unwrap();
        assert_eq!((&buf[..n], from), (&b"conn"[..], c.local_addr().unwrap()));
        within(b.send_to(b"back", c.local_addr().unwrap()))
            .await
            .unwrap();
        assert_eq!(within(c.peek(&mut buf)).await.unwrap(), 4);
        assert_eq!(within(c.recv(&mut buf)).await.unwrap(), 4);
        assert_eq!(&buf[..4], b"back");
    });
}

#[cfg(unix)]
#[test]
fn unix_streams_connect_accept_and_pair() {
    let rt = runtime();
    let dir = tempdir("stream");
    let path = dir.join("s.sock");
    rt.block_on(async {
        let listener = Handle::<UnixListener>::bind(&path).unwrap();
        let p = path.clone();
        let client = spawn(async move {
            let mut c = Handle::<UnixStream>::connect(&p).await.unwrap();
            write_all(&mut c, b"over unix").await.unwrap();
            c
        });
        let mut incoming = listener.incoming();
        let mut conn = within(next(&mut incoming)).await.unwrap().unwrap();
        let mut buf = [0u8; 9];
        within(read_exact(&mut conn, &mut buf)).await.unwrap();
        assert_eq!(&buf, b"over unix");
        drop(within(client).await);
        let (a, b) = Handle::<UnixStream>::pair().unwrap();
        assert_eq!(within(a.send(b"x")).await.unwrap(), 1);
        assert_eq!(within(b.peek(&mut buf)).await.unwrap(), 1);
        assert_eq!(within(b.recv(&mut buf)).await.unwrap(), 1);
        let (accepted, _) = {
            let p = path.clone();
            let c = spawn(async move { Handle::<UnixStream>::connect(&p).await.unwrap() });
            let accepted = within(listener.accept()).await.unwrap();
            drop(within(c).await);
            accepted
        };
        drop(accepted);
    });
    std::fs::remove_dir_all(dir).unwrap();
}

#[cfg(unix)]
#[test]
fn unix_datagrams_send_to_paths_and_pairs() {
    let rt = runtime();
    let dir = tempdir("dgram");
    let (pa, pb) = (dir.join("a.sock"), dir.join("b.sock"));
    rt.block_on(async {
        let a = Handle::<UnixDatagram>::bind(&pa).unwrap();
        let b = Handle::<UnixDatagram>::bind(&pb).unwrap();
        assert_eq!(within(a.send_to(b"to b", &pb)).await.unwrap(), 4);
        let mut buf = [0u8; 16];
        let (n, from) = within(b.peek_from(&mut buf)).await.unwrap();
        assert_eq!(n, 4);
        assert_eq!(from.as_pathname(), Some(pa.as_path()));
        let (n, _) = within(b.recv_from(&mut buf)).await.unwrap();
        assert_eq!(&buf[..n], b"to b");
        let (x, y) = Handle::<UnixDatagram>::pair().unwrap();
        assert_eq!(within(x.send(b"pair")).await.unwrap(), 4);
        assert_eq!(within(y.peek(&mut buf)).await.unwrap(), 4);
        assert_eq!(within(y.recv(&mut buf)).await.unwrap(), 4);
        // An unnamed sender.
        let (n, from) = {
            within(x.send(b"anon")).await.unwrap();
            within(y.recv_from(&mut buf)).await.unwrap()
        };
        assert_eq!(n, 4);
        assert!(from.is_unnamed());
    });
    std::fs::remove_dir_all(dir).unwrap();
}

#[test]
fn a_handle_made_by_new_finds_out_what_it_wraps() {
    let rt = runtime();
    rt.block_on(async {
        let std_listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = std_listener.local_addr().unwrap();
        let listener = Handle::new(std_listener).unwrap();
        let client = Handle::new(TcpStream::connect(addr).unwrap()).unwrap();
        let (conn, _) = within(listener.accept()).await.unwrap();
        within(client.send(b"new")).await.unwrap();
        let mut buf = [0u8; 3];
        assert_eq!(within(conn.recv(&mut buf)).await.unwrap(), 3);
        #[cfg(unix)]
        let (a, b) = UnixDatagram::pair().unwrap();
        #[cfg(windows)]
        let (a, b) = {
            let (a, b) = (
                UdpSocket::bind("127.0.0.1:0").unwrap(),
                UdpSocket::bind("127.0.0.1:0").unwrap(),
            );
            a.connect(b.local_addr().unwrap()).unwrap();
            (a, b)
        };
        let (a, b) = (Handle::new(a).unwrap(), Handle::new(b).unwrap());
        within(a.send(b"dg")).await.unwrap();
        assert_eq!(within(b.recv(&mut buf)).await.unwrap(), 2);
    });
}

/// `get_mut` hands the object out; swapping it for another makes the next
/// operation use the new one, and nothing the old one left shows up.
#[test]
fn a_get_mut_swap_is_seen_by_the_next_operation() {
    let rt = runtime();
    rt.block_on(async {
        let (old_peer, mine) = stream_pair();
        let (new_peer, other) = stream_pair();
        let mut handle = Handle::new(mine).unwrap();
        let mut buf = [0u8; 8];
        // A receive that waits, then is dropped, and the old peer's bytes
        // that the driver reads ahead for it.
        {
            let mut waiting = Box::pin(handle.recv(&mut buf));
            let polled = poll_fn(|cx| Poll::Ready(waiting.as_mut().poll(cx))).await;
            assert!(polled.is_pending());
        }
        (&old_peer).write_all(b"old").unwrap();
        crate::time::sleep(Duration::from_millis(10)).await;
        other.set_nonblocking(true).unwrap();
        let old = std::mem::replace(handle.get_mut(), other);
        drop(old);
        (&new_peer).write_all(b"new").unwrap();
        let n = within(handle.recv(&mut buf)).await.unwrap();
        assert_eq!(&buf[..n], b"new");
    });
}

/// The handle's number changes hands while the old file lives on through a
/// dup: the old file's bytes never show up on the new socket, and the new
/// socket's bytes are all its own.
#[cfg(unix)]
#[test]
fn a_dup_and_close_never_lets_the_driver_read_a_reused_number() {
    let rt = runtime();
    rt.block_on(async {
        let (old_peer, mine) = UnixStream::pair().unwrap();
        let keep = mine.try_clone().unwrap();
        let handle = Handle::new(mine).unwrap();
        let mut buf = [0u8; 8];
        {
            let mut waiting = Box::pin(handle.recv(&mut buf));
            let polled = poll_fn(|cx| Poll::Ready(waiting.as_mut().poll(cx))).await;
            assert!(polled.is_pending());
        }
        // The handle goes; its number is taken over at once by another socket.
        let number = handle.into_inner().into_raw_fd();
        let (new_peer, other) = UnixStream::pair().unwrap();
        // SAFETY: `number` is this test's own descriptor; dup2 replaces it.
        assert_eq!(unsafe { libc::dup2(other.as_raw_fd(), number) }, number);
        drop(other);
        // SAFETY: `number` is this test's own descriptor.
        let reused = UnixStream::from(unsafe { OwnedFd::from_raw_fd(number) });
        let reused = Handle::new(reused).unwrap();
        (&old_peer).write_all(b"old").unwrap();
        (&new_peer).write_all(b"mine").unwrap();
        let n = within(reused.recv(&mut buf)).await.unwrap();
        assert_eq!(&buf[..n], b"mine");
        keep.set_nonblocking(true).unwrap();
        assert_eq!((&keep).read(&mut buf).unwrap(), 3);
        assert_eq!(&buf[..3], b"old");
    });
}

/// A handle dropped while an operation waits: the operation's future goes
/// with it, the descriptor closes after the registration, and the peer sees
/// the end.
#[test]
fn a_handle_dropped_with_an_operation_waiting_closes_cleanly() {
    let rt = runtime();
    rt.block_on(async {
        let (a, b) = handle_pair();
        let mut buf = [0u8; 8];
        {
            let mut waiting = Box::pin(a.recv(&mut buf));
            let polled = poll_fn(|cx| Poll::Ready(waiting.as_mut().poll(cx))).await;
            assert!(polled.is_pending());
        }
        drop(a);
        let mut b = b;
        assert_eq!(within(read_some(&mut b, &mut buf)).await.unwrap(), 0);
    });
}

/// The same where the ring keeps no fixed-file table, as on a kernel that
/// lets go of a file out of the table only late: the dropped handle's file
/// goes once its own operation ended, whatever else the ring has out.
#[cfg(ring)]
#[test]
fn a_handle_dropped_with_an_operation_waiting_closes_cleanly_on_copies() {
    crate::rt::shard::ring::TABLE_FOR_TEST.with(|t| t.set(Some(0)));
    a_handle_dropped_with_an_operation_waiting_closes_cleanly();
    crate::rt::shard::ring::TABLE_FOR_TEST.with(|t| t.set(None));
}

#[test]
fn a_handle_used_from_another_executor_works() {
    // Polled by a minimal executor on a plain thread: a runtime shard
    // serves the source.
    let (a, b) = handle_pair();
    let echo = std::thread::spawn(move || {
        let mut buf = [0u8; 5];
        plain_block_on(async {
            let mut b = b;
            read_exact(&mut b, &mut buf).await.unwrap();
            write_all(&mut b, &buf).await.unwrap();
        });
        buf
    });
    let got = plain_block_on(async {
        let mut a = a;
        write_all(&mut a, b"plain").await.unwrap();
        let mut buf = [0u8; 5];
        read_exact(&mut a, &mut buf).await.unwrap();
        buf
    });
    assert_eq!(&got, b"plain");
    assert_eq!(&echo.join().unwrap(), b"plain");
}

#[allow(dead_code)]
fn send_and_sync_follow_the_object() {
    fn send_sync<T: Send + Sync>() {}
    send_sync::<Handle<TcpStream>>();
    send_sync::<Handle<UdpSocket>>();
    send_sync::<Handle<Pair>>();
    fn send<T: Send>() {}
    send::<super::TcpStreamGenerator>();
}

#[allow(dead_code)]
fn stream_impls_are_on_the_handle_and_its_reference(h: &Handle<TcpStream>, u: &Handle<Pair>) {
    fn io<T: AsyncRead + AsyncWrite>(_: T) {}
    io(h);
    io(u);
}

/// Writes until a write goes behind (the socket is full); returns every
/// byte written, in order. Each write is small enough to be copied behind:
/// one of 16 KiB or more waits for room instead.
async fn fill_behind(h: &Handle<Pair>) -> Vec<u8> {
    const CHUNK: usize = 8 * 1024;
    let mut sent = Vec::new();
    let mut seq = 0usize;
    while !h.source.has_unsent() {
        let chunk: Vec<u8> = (0..CHUNK)
            .map(|i| ((seq * CHUNK + i) % 251) as u8)
            .collect();
        seq += 1;
        let n = within(h.send(&chunk)).await.unwrap();
        sent.extend_from_slice(&chunk[..n]);
    }
    sent
}

fn read_to_end_blocking(mut peer: Pair) -> Vec<u8> {
    peer.set_nonblocking(false).unwrap();
    peer.set_read_timeout(Some(Duration::from_secs(10)))
        .unwrap();
    let mut got = Vec::new();
    peer.read_to_end(&mut got).unwrap();
    got
}

/// Reads `peer` to its end, up to a deadline: what came, and whether the
/// end did.
fn read_to_end_within(mut peer: Pair, within: Duration) -> (Vec<u8>, bool) {
    peer.set_nonblocking(false).unwrap();
    peer.set_read_timeout(Some(Duration::from_millis(100)))
        .unwrap();
    let deadline = std::time::Instant::now() + within;
    let mut got = Vec::new();
    let mut buf = vec![0u8; 64 * 1024];
    while std::time::Instant::now() < deadline {
        match peer.read(&mut buf) {
            Ok(0) => return (got, true),
            Ok(n) => got.extend_from_slice(&buf[..n]),
            Err(e)
                if matches!(
                    e.kind(),
                    io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut
                ) => {}
            Err(e) => panic!("{e}"),
        }
    }
    (got, false)
}

#[test]
fn a_handle_dropped_with_bytes_behind_still_delivers_them_then_closes() {
    let rt = runtime();
    let (mine, peer) = stream_pair();
    let (sent, source) = rt.block_on(async {
        let handle = Handle::new(mine).unwrap();
        let sent = fill_behind(&handle).await;
        let source = std::sync::Arc::clone(handle.source());
        drop(handle);
        (sent, source)
    });
    let (got, ended) =
        std::thread::spawn(move || read_to_end_within(peer, Duration::from_secs(10)))
            .join()
            .unwrap();
    // The source's state, should the bytes or the end never come.
    assert!(
        ended,
        "{} of {} bytes came, then nothing: {}",
        got.len(),
        sent.len(),
        source.write_report()
    );
    assert_eq!(got.len(), sent.len(), "bytes accepted were lost");
    assert!(got == sent, "the bytes arrived changed or out of order");
}

#[test]
fn into_inner_hands_the_object_back_after_the_bytes_behind() {
    let rt = runtime();
    let (mine, peer) = stream_pair();
    let (sent, reader) = rt.block_on(async {
        let handle = Handle::new(mine).unwrap();
        let mut sent = fill_behind(&handle).await;
        // The socket is full: a reader starts draining it.
        let reader = std::thread::spawn(move || read_to_end_blocking(peer));
        let mut stream = handle.into_inner();
        // Written with std after the handle's bytes: it must come after them.
        stream.set_nonblocking(false).unwrap();
        stream.write_all(b"after").unwrap();
        sent.extend_from_slice(b"after");
        (sent, reader)
    });
    let got = reader.join().unwrap();
    assert!(got == sent, "{} bytes sent, {} got", sent.len(), got.len());
}

#[test]
fn get_mut_hands_the_object_out_after_the_bytes_behind() {
    let rt = runtime();
    let (mine, peer) = stream_pair();
    let (sent, reader) = rt.block_on(async {
        let mut handle = Handle::new(mine).unwrap();
        let mut sent = fill_behind(&handle).await;
        let reader = std::thread::spawn(move || read_to_end_blocking(peer));
        // Written with std after the handle's bytes: it must come after them.
        let direct = handle.get_mut();
        direct.set_nonblocking(false).unwrap();
        direct.write_all(b"direct").unwrap();
        direct.set_nonblocking(true).unwrap();
        sent.extend_from_slice(b"direct");
        within(handle.send(b"!")).await.unwrap();
        sent.extend_from_slice(b"!");
        (sent, reader)
    });
    let got = reader.join().unwrap();
    assert!(got == sent, "{} bytes sent, {} got", sent.len(), got.len());
}

#[test]
fn vectored_reads_and_writes_scatter_and_gather_in_order() {
    use std::io::{IoSlice, IoSliceMut};
    let rt = runtime();
    rt.block_on(async {
        let (a, b) = handle_pair();
        let (mut a, mut b) = (&a, &b);
        // At once: the socket takes the gathered bytes.
        let n = within(poll_fn(|cx| {
            Pin::new(&mut a).poll_write_vectored(cx, &[IoSlice::new(b"one"), IoSlice::new(b"two")])
        }))
        .await
        .unwrap();
        assert_eq!(n, 6);
        let (mut x, mut y) = ([0u8; 2], [0u8; 8]);
        let n = within(poll_fn(|cx| {
            let mut bufs = [IoSliceMut::new(&mut x), IoSliceMut::new(&mut y)];
            Pin::new(&mut b).poll_read_vectored(cx, &mut bufs)
        }))
        .await
        .unwrap();
        assert_eq!(n, 6);
        assert_eq!((&x, &y[..4]), (b"on", &b"etwo"[..]));
        // A scattered read that waits, completed by the driver.
        let writer = async {
            crate::time::sleep(Duration::from_millis(5)).await;
            write_all(&mut a, b"late bytes").await.unwrap();
        };
        let (mut p, mut q) = ([0u8; 4], [0u8; 16]);
        let reader = poll_fn(|cx| {
            let mut bufs = [IoSliceMut::new(&mut p), IoSliceMut::new(&mut q)];
            Pin::new(&mut b).poll_read_vectored(cx, &mut bufs)
        });
        let (n, ()) = within(join(reader, writer)).await;
        let n = n.unwrap();
        let mut got = p[..n.min(4)].to_vec();
        got.extend_from_slice(&q[..n.saturating_sub(4)]);
        assert_eq!(got, b"late bytes"[..n]);
    });
}

/// Bytes and the peer's FIN come in one event: after a short read the end
/// of the stream is still seen.
#[test]
fn tcp_bytes_then_the_peers_close_read_whole_then_end() {
    let rt = runtime();
    rt.block_on(async {
        for size in [1usize, 100, 70_000] {
            let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
            let addr = listener.local_addr().unwrap();
            let mut client = within(Handle::<TcpStream>::connect(addr)).await.unwrap();
            let (mut peer, _) = listener.accept().unwrap();
            let data = pattern(size);
            // Written and closed at once: the bytes and the FIN arrive together.
            peer.write_all(&data).unwrap();
            drop(peer);
            crate::time::sleep(Duration::from_millis(5)).await;
            let mut got = Vec::new();
            let mut buf = vec![0u8; 4096];
            loop {
                match within(read_some(&mut client, &mut buf)).await.unwrap() {
                    0 => break,
                    n => got.extend_from_slice(&buf[..n]),
                }
            }
            assert!(got == data, "{size} bytes: got {}", got.len());
        }
    });
}

/// Urgent data stops a read at its mark with bytes still queued behind it:
/// those bytes are still read, without new ones having to come.
#[test]
fn tcp_bytes_behind_urgent_data_are_still_read() {
    let rt = runtime();
    rt.block_on(async {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let mut client = within(Handle::<TcpStream>::connect(addr)).await.unwrap();
        let (peer, _) = listener.accept().unwrap();
        peer.set_nodelay(true).unwrap();
        // One normal read first, so the socket reads short and drained.
        (&peer).write_all(b"first").unwrap();
        let mut buf = [0u8; 64];
        assert_eq!(within(read_some(&mut client, &mut buf)).await.unwrap(), 5);
        // Bytes, one urgent byte, more bytes: the read stops at the mark.
        (&peer).write_all(b"before").unwrap();
        #[cfg(unix)]
        // SAFETY: a plain send of one byte from our own buffer.
        let sent = unsafe { libc::send(peer.as_raw_fd(), b"!".as_ptr().cast(), 1, libc::MSG_OOB) };
        #[cfg(windows)]
        // SAFETY: as above.
        let sent = unsafe {
            use std::os::windows::io::AsRawSocket;
            crate::syscore::windows::send(
                peer.as_raw_socket() as _,
                b"!".as_ptr(),
                1,
                crate::syscore::windows::MSG_OOB,
            )
        };
        assert_eq!(sent, 1);
        (&peer).write_all(b"after").unwrap();
        crate::time::sleep(Duration::from_millis(5)).await;
        let mut got = Vec::new();
        while got.len() < b"beforeafter".len() {
            let n = within(read_some(&mut client, &mut buf)).await.unwrap();
            assert!(n > 0);
            got.extend_from_slice(&buf[..n]);
        }
        // The urgent byte is not in the stream (no SO_OOBINLINE).
        assert_eq!(got, b"beforeafter");
        drop(peer);
    });
}

/// Many short round trips: every reply is read, none waits for a later one.
#[test]
fn tcp_short_round_trips_never_wait_for_the_next_message() {
    let rt = runtime();
    rt.block_on(async {
        let listener = Handle::<TcpListener>::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let server = spawn(async move {
            let (mut s, _) = listener.accept().await.unwrap();
            let mut buf = [0u8; 16];
            loop {
                match read_some(&mut s, &mut buf).await.unwrap() {
                    0 => break,
                    n => write_all(&mut s, &buf[..n]).await.unwrap(),
                }
            }
        });
        let mut c = within(Handle::<TcpStream>::connect(addr)).await.unwrap();
        c.set_nodelay(true).unwrap();
        for i in 0..2000u32 {
            let msg = i.to_le_bytes();
            within(write_all(&mut c, &msg)).await.unwrap();
            let mut back = [0u8; 4];
            within(read_exact(&mut c, &mut back)).await.unwrap();
            assert_eq!(back, msg);
        }
        drop(c);
        within(server).await;
    });
}

/// The TCP form of the reuse case: a connection's number taken over by
/// another socket while a dup keeps the old one open.
#[cfg(unix)]
#[test]
fn a_tcp_dup_and_close_never_lets_the_driver_touch_a_reused_number() {
    let rt = runtime();
    rt.block_on(async {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let handle = within(Handle::<TcpStream>::connect(addr)).await.unwrap();
        let (old_peer, _) = listener.accept().unwrap();
        let keep = handle.get_ref().try_clone().unwrap();
        let mut buf = [0u8; 8];
        {
            let mut waiting = Box::pin(handle.recv(&mut buf));
            let polled = poll_fn(|cx| Poll::Ready(waiting.as_mut().poll(cx))).await;
            assert!(polled.is_pending());
        }
        let number = handle.into_inner().into_raw_fd();
        let (new_peer, other) = UnixStream::pair().unwrap();
        // SAFETY: `number` is this test's own descriptor; dup2 replaces it.
        assert_eq!(unsafe { libc::dup2(other.as_raw_fd(), number) }, number);
        drop(other);
        // SAFETY: `number` is this test's own descriptor.
        let reused =
            Handle::new(UnixStream::from(unsafe { OwnedFd::from_raw_fd(number) })).unwrap();
        (&old_peer).write_all(b"old").unwrap();
        (&new_peer).write_all(b"mine").unwrap();
        let n = within(reused.recv(&mut buf)).await.unwrap();
        assert_eq!(&buf[..n], b"mine");
        // The old connection still has its bytes. TCP may hand them over a
        // little after the write (macOS does on loopback), so the read waits
        // for them, for a bounded time.
        keep.set_nonblocking(false).unwrap();
        keep.set_read_timeout(Some(Duration::from_secs(10)))
            .unwrap();
        let mut old = [0u8; 3];
        (&keep)
            .read_exact(&mut old)
            .expect("the old connection's bytes never came");
        assert_eq!(&old, b"old");
    });
}

/// A handle dropped with bytes still to send keeps its object, and so its
/// number, until the bytes are out: no socket made meanwhile takes the
/// number, the bytes go to the old peer whole, and once they did the number
/// is free again.
#[cfg(unix)]
#[test]
fn a_handle_dropped_with_bytes_behind_keeps_its_number_until_they_are_out() {
    if !alone("net::tests::a_handle_dropped_with_bytes_behind_keeps_its_number_until_they_are_out")
    {
        return;
    }
    let rt = runtime();
    let (mine, peer) = UnixStream::pair().unwrap();
    let (sent, number) = rt.block_on(async {
        let handle = Handle::new(mine).unwrap();
        let sent = fill_behind(&handle).await;
        let number = handle.as_raw_fd();
        drop(handle);
        (sent, number)
    });
    // Sockets made while the bytes are out never take the number: the
    // lowest free numbers are taken, and the dropped handle's is not free.
    let mut made = Vec::new();
    for _ in 0..64 {
        let (a, b) = UnixStream::pair().unwrap();
        assert_ne!(
            a.as_raw_fd(),
            number,
            "a socket took the number with bytes out"
        );
        assert_ne!(
            b.as_raw_fd(),
            number,
            "a socket took the number with bytes out"
        );
        made.push((a, b));
    }
    drop(made);
    let got = std::thread::spawn(move || read_to_end_blocking(peer))
        .join()
        .unwrap();
    assert!(
        got == sent,
        "{} of {} bytes reached the old peer",
        got.len(),
        sent.len()
    );
    // Delivered and closed: the number comes free.
    let deadline = std::time::Instant::now() + Duration::from_secs(10);
    let mut kept = Vec::new();
    loop {
        let (a, b) = UnixStream::pair().unwrap();
        if a.as_raw_fd() == number || b.as_raw_fd() == number {
            break;
        }
        if a.as_raw_fd() < number {
            kept.push((a, b));
        }
        assert!(
            std::time::Instant::now() < deadline,
            "the dropped handle's number never came free once its bytes were out"
        );
        std::thread::sleep(Duration::from_millis(1));
    }
}

/// Many handles dropped with bytes still to send, under a lowered limit on
/// descriptors: each cycle's descriptors (the handle's, kept by its linger,
/// and the peer's) are all closed again, so the count returns to where it
/// began and the limit is never met.
#[cfg(unix)]
#[test]
fn dropped_handles_with_unsent_bytes_leave_no_descriptor_open() {
    if !alone("net::tests::dropped_handles_with_unsent_bytes_leave_no_descriptor_open") {
        return;
    }
    let open = || crate::alone::open_descriptors().len();
    let rt = runtime();
    let before = open();
    // A limit is a descriptor number: the room is above the highest open.
    let top = crate::alone::open_descriptors()
        .into_iter()
        .max()
        .unwrap_or(0);
    let limit = libc::rlimit {
        rlim_cur: top as libc::rlim_t + 64,
        rlim_max: top as libc::rlim_t + 64,
    };
    // SAFETY: lowers this process's own limit with a valid struct.
    assert_eq!(unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &limit) }, 0);
    for _ in 0..1500 {
        let (mine, peer) = UnixStream::pair().unwrap();
        rt.block_on(async {
            let handle = Handle::new(mine).unwrap();
            fill_behind(&handle).await;
            drop(handle);
        });
        drop(peer);
    }
    let deadline = std::time::Instant::now() + Duration::from_secs(20);
    while open() > before + 4 {
        assert!(
            std::time::Instant::now() < deadline,
            "{} descriptors open, {before} before",
            open()
        );
        std::thread::sleep(Duration::from_millis(10));
    }
}

#[cfg(windows)]
mod windows;

mod datagram_owned;

#[cfg(unix)]
mod emfile;

mod send_file;

mod zero_copy;