dope-uring 0.27.0

Owned io_uring backend for dope
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
pub mod common;

use std::{fs, mem, os::fd::AsFd as _, process, slice, time::Duration};

use dope_sys::{
    Errno,
    io::{
        file::{self, Mode},
        request,
        socket::{Domain, Socket as _, addr},
    },
    kernel::{self, errno::Source as _},
    ring::{Source, enter::Enter},
};
use dope_uring::Uring;

use crate::common::{
    Directory,
    layout::{Aligned, encoded},
};

type Borrowed<'a> = <Uring as file::File>::Request<'a>;
type Offset = file::Offset;
type Timeout = <<Uring as Source>::Enter<'static, ()> as Enter>::Timeout;

#[test]
fn cpu_bounds() {
    use dope_sys::thread::Thread as _;

    let expected: Vec<_> = Uring::current().expect("available CPUs").collect();
    let mut cpus = Uring::current().expect("same thread affinity");
    let mut expected = expected.into_iter();
    let mut front = true;
    while expected.len() != 0 {
        assert_eq!(cpus.len(), expected.len());
        assert_eq!(cpus.size_hint(), (expected.len(), Some(expected.len())));
        let actual = if front { cpus.next() } else { cpus.next_back() };
        let wanted = if front {
            expected.next()
        } else {
            expected.next_back()
        };
        assert_eq!(actual, wanted);
        front = !front;
    }
    assert_eq!(cpus.len(), 0);
    assert_eq!(cpus.size_hint(), (0, Some(0)));
    assert_eq!(cpus.next(), None);
    assert_eq!(cpus.next_back(), None);
    assert_eq!(cpus.next(), None);
}

#[test]
fn mapping_layouts() {
    use dope_sys::memory::Error;

    for count in [0, isize::MAX as usize + 1] {
        assert_eq!(
            Uring::layout::<u8>(count),
            Err(Error::Length { element: 1, count })
        );
    }
    assert_eq!(
        Uring::layout::<()>(usize::MAX),
        Err(Error::Length {
            element: 0,
            count: usize::MAX
        })
    );
    for count in [isize::MAX as usize / mem::size_of::<u64>() + 1, usize::MAX] {
        assert_eq!(
            Uring::layout::<u64>(count),
            Err(Error::Length { element: 8, count })
        );
    }
    for count in [0, usize::MAX] {
        assert_eq!(
            Uring::layout::<Aligned>(count),
            Err(Error::Length {
                element: 8192,
                count
            })
        );
    }
    assert_eq!(
        Uring::layout::<Aligned>(1),
        Err(Error::Alignment {
            element: 8192,
            supported: 4096
        })
    );
}

#[test]
fn mapping_bounds() {
    for count in [1, 4096, isize::MAX as usize] {
        assert_eq!(
            Uring::layout::<u8>(count).expect("native mapping").get(),
            count
        );
    }
    for count in [1, 512, isize::MAX as usize / mem::size_of::<u64>()] {
        assert_eq!(
            Uring::layout::<u64>(count).expect("native mapping").get(),
            count * mem::size_of::<u64>()
        );
    }
}

#[test]
fn timeout_accepts_duration() {
    let result = Timeout::try_from(Duration::new(1, 999_999_999));
    assert!(result.is_ok());
    let edge = Duration::new(i64::MAX as u64, 999_999_999);
    assert!(Timeout::try_from(edge).is_ok());
}

#[test]
fn timeout_overflow() {
    let value = Duration::from_secs(i64::MAX as u64 + 1);
    let overflow = Timeout::try_from(value)
        .err()
        .expect("out-of-range timeout was accepted");
    assert_eq!(overflow.requested(), value);
}

#[test]
fn pair_keeps_shutdown() {
    use std::os::fd::AsRawFd as _;

    use dope_sys::io::socket;

    let (left, right) = Uring::pair().expect("socket pair");
    for file in [left.as_fd(), right.as_fd()] {
        // SAFETY: F_GETFD reads flags from this live descriptor.
        let flags = unsafe { libc::fcntl(file.as_raw_fd(), libc::F_GETFD) };
        assert!(flags >= 0);
        assert_ne!(flags & libc::FD_CLOEXEC, 0);
    }
    assert_eq!(
        Uring::send_quiet::<Errno>(left.as_fd(), b"x").expect("send byte"),
        b"x"
    );
    let mut output = [0];
    assert_eq!(
        Uring::recv::<{ libc::MSG_DONTWAIT }, Errno>(right.as_fd(), &mut output)
            .expect("receive byte"),
        b"x"
    );
    Uring::shutdown::<Errno>(left.as_fd(), socket::How::WRITE).expect("shutdown writer");
    assert!(
        Uring::recv::<{ libc::MSG_DONTWAIT }, Errno>(right.as_fd(), &mut output)
            .expect("closed socket")
            .is_empty()
    );
}

#[test]
fn pipe_keeps_nonblocking() {
    use std::os::fd::AsRawFd as _;

    use dope_sys::io::{fd::Fd as _, pipe::Pipe as _};

    let (read, write) = Uring::nonblocking().expect("nonblocking pipe");
    for file in [read.as_fd(), write.as_fd()] {
        // SAFETY: both commands read flags from this live descriptor.
        let (flags, status) = unsafe {
            (
                libc::fcntl(file.as_raw_fd(), libc::F_GETFD),
                libc::fcntl(file.as_raw_fd(), libc::F_GETFL),
            )
        };
        assert!(flags >= 0 && status >= 0);
        assert_ne!(flags & libc::FD_CLOEXEC, 0);
        assert_ne!(status & libc::O_NONBLOCK, 0);
    }
    let mut output = [0];
    assert!(matches!(
        Uring::read::<Errno>(read.as_fd(), &mut output),
        Err(error) if error.is_block()
    ));
    assert_eq!(
        Uring::write::<Errno>(write.as_fd(), b"x").expect("write byte"),
        b"x"
    );
    assert_eq!(
        Uring::read::<Errno>(read.as_fd(), &mut output).expect("read byte"),
        b"x"
    );
    drop(write);
    assert!(
        Uring::read::<Errno>(read.as_fd(), &mut output)
            .expect("closed pipe")
            .is_empty()
    );
}

#[test]
fn open_keeps_paths() {
    use std::{fs::File, os::fd, ptr};

    use request::Request as _;

    let dir = File::open(env!("CARGO_MANIFEST_DIR")).expect("package directory");
    let options = Borrowed::options(file::Open::READ, file::Mode::new(0));
    let path = c"entry";
    let entry = Borrowed::open(fd::AsFd::as_fd(&dir), path, &options);
    let data = encoded(&entry);

    assert_eq!(data[0], 28, "IORING_OP_OPENAT2");
    assert_eq!(data[1], 0);
    assert_eq!(&data[4..8], &fd::AsRawFd::as_raw_fd(&dir).to_ne_bytes());
    assert_eq!(
        &data[8..16],
        &(ptr::from_ref(&options) as u64).to_ne_bytes()
    );
    assert_eq!(&data[16..24], &(path.as_ptr() as u64).to_ne_bytes());
    assert_eq!(&data[24..28], &24_u32.to_ne_bytes(), "open_how size");
    assert_eq!(&data[28..], &[0; 36]);
}

#[test]
fn metadata_keeps_descriptor() {
    use std::{os::fd::AsRawFd as _, ptr};

    use request::Request as _;

    type Metadata = <Uring as file::File>::Metadata;
    let file = fs::File::open(env!("CARGO_MANIFEST_DIR")).expect("package directory");
    let mut output = Metadata::UNINIT;
    let address = ptr::from_mut(&mut output).cast::<libc::statx>();
    let entry = Borrowed::metadata(file.as_fd(), &mut output);
    let data = encoded(&entry);
    assert_eq!(data[0], 21, "IORING_OP_STATX");
    assert_eq!(data[1], 1 << 4, "IOSQE_ASYNC");
    assert_eq!(&data[4..8], &file.as_raw_fd().to_ne_bytes());
    assert_eq!(&data[8..16], &(address as u64).to_ne_bytes());
    let path = u64::from_ne_bytes(data[16..24].try_into().expect("path address"));
    // SAFETY: the request retains its static, terminated empty path.
    assert!(unsafe { std::ffi::CStr::from_ptr(path as *const _) }.is_empty());
    assert_eq!(&data[24..28], &0x201_u32.to_ne_bytes());
    assert_eq!(&data[28..32], &0x1000_u32.to_ne_bytes());
    assert_eq!(&data[32..], &[0; 32]);
    let address = ptr::from_mut(&mut output).cast::<libc::statx>();
    // SAFETY: the live descriptor and native output remain exclusively borrowed.
    assert_eq!(
        unsafe { libc::statx(file.as_raw_fd(), c"".as_ptr(), 0x1000, 0x201, address) },
        0
    );
    // SAFETY: statx succeeded and no longer accesses the output.
    assert!(
        unsafe { output.try_result() }
            .expect("native metadata")
            .0
            .is_dir()
    );
    let size = i64::MAX as u64 + 1;
    // SAFETY: the successful native result is initialized and exclusively owned.
    unsafe { (*ptr::from_mut(&mut output).cast::<libc::statx>()).stx_size = size };
    // SAFETY: the initialized result has no remaining native writer.
    assert_eq!(
        unsafe { output.try_result() },
        Err(file::MetadataError::Overflow(size))
    );
    // SAFETY: the initialized result remains exclusively owned.
    unsafe { (*ptr::from_mut(&mut output).cast::<libc::statx>()).stx_mask &= !libc::STATX_TYPE };
    // SAFETY: the initialized result has no remaining native writer.
    assert!(
        matches!(unsafe { output.try_result() }, Err(file::MetadataError::Missing(mask)) if mask.get() == libc::STATX_TYPE)
    );
}

#[test]
fn names_keep_async() {
    use std::{fs::File, os::fd};

    use request::Request as _;

    let dir = File::open(env!("CARGO_MANIFEST_DIR")).expect("package directory");
    let raw = fd::AsRawFd::as_raw_fd(&dir).to_ne_bytes();
    let old = c"old";
    let new = c"new";
    let rename = Borrowed::rename(fd::AsFd::as_fd(&dir), old, new);
    let unlink = Borrowed::unlink(fd::AsFd::as_fd(&dir), old);
    let rename = encoded(&rename);
    let unlink = encoded(&unlink);

    assert_eq!(rename[0], 35, "IORING_OP_RENAMEAT");
    assert_eq!(unlink[0], 36, "IORING_OP_UNLINKAT");
    for entry in [&rename, &unlink] {
        assert_eq!(entry[1], 1 << 4, "IOSQE_ASYNC");
        assert_eq!(&entry[4..8], &raw);
        assert_eq!(&entry[16..24], &(old.as_ptr() as u64).to_ne_bytes());
        assert_eq!(&entry[28..], &[0; 36]);
    }
    assert_eq!(&rename[8..16], &(new.as_ptr() as u64).to_ne_bytes());
    assert_eq!(&rename[24..28], &raw);
    assert_eq!(&unlink[8..16], &[0; 8]);
    assert_eq!(&unlink[24..28], &[0; 4]);
}

#[test]
fn offset_keeps_position() {
    use std::{fs::File, os::fd};

    use o3::buffer::bytes;
    use request::Request as _;

    let file = File::open(env!("CARGO_MANIFEST_DIR")).expect("package directory");
    let input = bytes::Prefix::try_new(b"data".as_slice()).expect("bounded input");
    let mut output = [std::mem::MaybeUninit::uninit(); 4];
    for offset in [0, i64::MAX as u64] {
        let entry = Borrowed::write(
            fd::AsFd::as_fd(&file),
            input,
            Offset::new(offset).expect("absolute position"),
        );
        let data = encoded(&entry);
        assert_eq!(data[0], 23, "IORING_OP_WRITE");
        assert_eq!(data[1], 1 << 4, "IOSQE_ASYNC");
        assert_eq!(&data[8..16], &offset.to_ne_bytes());
        assert_eq!(&data[24..28], &4_u32.to_ne_bytes());

        let address = output.as_mut_ptr() as u64;
        let entry = Borrowed::read(
            fd::AsFd::as_fd(&file),
            bytes::bounded::Value::try_from(output.as_mut_slice()).expect("bounded output"),
            Offset::new(offset).expect("absolute position"),
        );
        let data = encoded(&entry);
        assert_eq!(data[0], 22, "IORING_OP_READ");
        assert_eq!(data[1], 1 << 4, "IOSQE_ASYNC");
        assert_eq!(&data[4..8], &fd::AsRawFd::as_raw_fd(&file).to_ne_bytes());
        assert_eq!(&data[8..16], &offset.to_ne_bytes());
        assert_eq!(&data[16..24], &address.to_ne_bytes());
        assert_eq!(&data[24..28], &4_u32.to_ne_bytes());
    }
}

#[test]
fn captures_before_call() {
    let closed = unsafe { libc::close(-1) };
    let fd_fault = Uring::try_get().expect("errno slot").expect("failed close");
    Uring::clear();
    assert_eq!(Uring::try_get(), Ok(None));
    let missing = unsafe { libc::access(c"".as_ptr(), libc::F_OK) };
    let path = Uring::try_get()
        .expect("errno slot")
        .expect("failed access");

    assert_eq!(closed, -1);
    assert_eq!(missing, -1);
    assert_eq!(fd_fault, Errno::BAD_DESCRIPTOR);
    assert_eq!(path.code().get(), libc::ENOENT);
}

#[test]
fn unix_path_extent() {
    let offset = mem::offset_of!(libc::sockaddr_un, sun_path);
    let capacity = mem::size_of::<libc::sockaddr_un>() - offset;
    let maximum = capacity - usize::from(<Uring as addr::Source>::UNIX_TERMINATED);
    let bytes = [b'p'; mem::size_of::<libc::sockaddr_un>()];
    let address = addr::Unix::from_path::<Uring>(&bytes[..maximum]).expect("native path limit");
    let (raw, length) = address.as_addr().parts();
    assert_eq!(
        length.get(),
        offset + maximum + usize::from(maximum < capacity),
    );
    // SAFETY: the borrowed address retains its full initialized native payload.
    let encoded = unsafe { slice::from_raw_parts(raw.cast::<u8>().add(offset), maximum) };
    assert_eq!(encoded, &bytes[..maximum]);
    assert!(matches!(
        addr::Unix::from_path::<Uring>(&bytes[..maximum + 1]),
        Err(addr::PathError::Length { actual, maximum: limit })
            if actual == maximum + 1 && limit == maximum
    ));
}

#[test]
fn unix_abstract_extent() {
    let offset = mem::offset_of!(libc::sockaddr_un, sun_path);
    let maximum = mem::size_of::<libc::sockaddr_un>() - offset - 1;
    let mut bytes = [b'n'; mem::size_of::<libc::sockaddr_un>()];
    bytes[..4].copy_from_slice(&process::id().to_ne_bytes());
    bytes[4] = 0;
    let address = addr::Unix::from_name::<Uring>(&bytes[..maximum]);
    let address = address.expect("native abstract name limit");
    let socket = Uring::datagram::<Errno>(Domain::Unix).expect("abstract datagram socket");
    address
        .as_addr()
        .try_bind::<Uring, Errno>(socket.as_fd())
        .expect("bind binary abstract name");
    let mut storage = addr::Storage::empty();
    let local = addr::Addr::local::<Uring, Errno>(socket.as_fd(), &mut storage)
        .expect("native abstract address");
    let (raw, length) = local.parts();
    assert_eq!(length.get(), offset + 1 + maximum);
    // SAFETY: getsockname admitted this exact initialized address extent.
    let encoded = unsafe { slice::from_raw_parts(raw.cast::<u8>().add(offset), maximum + 1) };
    assert_eq!(encoded[0], 0);
    assert_eq!(&encoded[1..], &bytes[..maximum]);
    assert!(matches!(
        addr::Unix::from_name::<Uring>(&bytes[..maximum + 1]),
        Err(addr::PathError::Length { actual, maximum: limit })
            if actual == maximum + 1 && limit == maximum
    ));
}

#[test]
fn mkdir_keeps_failure() {
    use dope_sys::io::file::File as _;

    let temp = Directory::try_new("mkdir").expect("isolated directory");
    let mut path = temp.c_path().to_bytes().to_vec();
    path.extend_from_slice(b"/child");
    let path = std::ffi::CString::new(path).expect("child path");
    Uring::mkdir::<Errno>(&path, Mode::new(0o700)).expect("create directory");
    assert!(
        Uring::stat::<Errno>(&path)
            .expect("directory metadata")
            .kind()
            .is_dir()
    );
    let failure = Uring::mkdir::<kernel::Errno<()>>(&path, Mode::new(0o700));
    Uring::clear();
    assert_eq!(failure, Err(kernel::Errno::from(Errno::ALREADY_EXISTS)));
    fs::remove_dir(temp.path().join("child")).expect("child directory cleanup");
}

#[test]
fn keeps_buffered_entries() {
    use dope_sys::io::file::Dir;

    let temp = Directory::try_new("directory_entries").expect("isolated directory");
    fs::write(temp.path().join("data"), b"").expect("entry fixture");
    let mut dir = Dir::<Uring>::open::<Errno>(temp.c_path()).expect("open directory");
    let first = {
        let (_, mut entries) = dir.entries();
        entries
            .try_next::<Errno>()
            .expect("first read")
            .expect("first entry")
            .name
            .to_owned()
    };
    let mut names = vec![first];
    let (_, mut entries) = dir.entries();
    while let Some(entry) = entries.try_next::<Errno>().expect("remaining entries") {
        if let Some(kind) = entry.kind {
            assert_eq!(kind.is_regular(), entry.name == c"data");
        }
        names.push(entry.name.to_owned());
    }
    names.sort_unstable();
    assert_eq!(names, [c".", c"..", c"data"]);
    drop(dir);
    fs::remove_file(temp.path().join("data")).expect("entry fixture cleanup");
}