Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the Buffer trait #908

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions src/backend/libc/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2213,16 +2213,14 @@ struct Attrlist {
}

#[cfg(any(apple, linux_kernel))]
pub(crate) fn getxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Result<usize> {
let value_ptr = value.as_mut_ptr();

pub(crate) unsafe fn getxattr(path: &CStr, name: &CStr, value_ptr: *mut u8, cap: usize) -> io::Result<usize> {
#[cfg(not(apple))]
unsafe {
ret_usize(c::getxattr(
path.as_ptr(),
name.as_ptr(),
value_ptr.cast::<c::c_void>(),
value.len(),
cap,
))
}

Expand All @@ -2232,24 +2230,22 @@ pub(crate) fn getxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Result
path.as_ptr(),
name.as_ptr(),
value_ptr.cast::<c::c_void>(),
value.len(),
cap,
0,
0,
))
}
}

#[cfg(any(apple, linux_kernel))]
pub(crate) fn lgetxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Result<usize> {
let value_ptr = value.as_mut_ptr();

pub(crate) unsafe fn lgetxattr(path: &CStr, name: &CStr, value_ptr: *mut u8, cap: usize) -> io::Result<usize> {
#[cfg(not(apple))]
unsafe {
ret_usize(c::lgetxattr(
path.as_ptr(),
name.as_ptr(),
value_ptr.cast::<c::c_void>(),
value.len(),
cap,
))
}

Expand All @@ -2259,24 +2255,22 @@ pub(crate) fn lgetxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Resul
path.as_ptr(),
name.as_ptr(),
value_ptr.cast::<c::c_void>(),
value.len(),
cap,
0,
c::XATTR_NOFOLLOW,
))
}
}

#[cfg(any(apple, linux_kernel))]
pub(crate) fn fgetxattr(fd: BorrowedFd<'_>, name: &CStr, value: &mut [u8]) -> io::Result<usize> {
let value_ptr = value.as_mut_ptr();

pub(crate) unsafe fn fgetxattr(fd: BorrowedFd<'_>, name: &CStr, value_ptr: *mut u8, cap: usize) -> io::Result<usize> {
#[cfg(not(apple))]
unsafe {
ret_usize(c::fgetxattr(
borrowed_fd(fd),
name.as_ptr(),
value_ptr.cast::<c::c_void>(),
value.len(),
cap,
))
}

Expand All @@ -2286,7 +2280,7 @@ pub(crate) fn fgetxattr(fd: BorrowedFd<'_>, name: &CStr, value: &mut [u8]) -> io
borrowed_fd(fd),
name.as_ptr(),
value_ptr.cast::<c::c_void>(),
value.len(),
cap,
0,
0,
))
Expand Down
12 changes: 6 additions & 6 deletions src/backend/libc/io/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ use {
crate::io::{IoSlice, IoSliceMut},
};

pub(crate) fn read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> io::Result<usize> {
pub(crate) unsafe fn read(fd: BorrowedFd<'_>, buf: *mut u8, cap: usize) -> io::Result<usize> {
unsafe {
ret_usize(c::read(
borrowed_fd(fd),
buf.as_mut_ptr().cast(),
min(buf.len(), READ_LIMIT),
buf.cast(),
min(cap, READ_LIMIT),
))
}
}
Expand All @@ -46,8 +46,8 @@ pub(crate) fn write(fd: BorrowedFd<'_>, buf: &[u8]) -> io::Result<usize> {
}
}

pub(crate) fn pread(fd: BorrowedFd<'_>, buf: &mut [u8], offset: u64) -> io::Result<usize> {
let len = min(buf.len(), READ_LIMIT);
pub(crate) fn pread(fd: BorrowedFd<'_>, buf: *mut u8, cap: usize, offset: u64) -> io::Result<usize> {
let len = min(cap, READ_LIMIT);

// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
Expand All @@ -59,7 +59,7 @@ pub(crate) fn pread(fd: BorrowedFd<'_>, buf: &mut [u8], offset: u64) -> io::Resu
unsafe {
ret_usize(c::pread(
borrowed_fd(fd),
buf.as_mut_ptr().cast(),
buf.cast(),
len,
offset,
))
Expand Down
15 changes: 8 additions & 7 deletions src/backend/libc/net/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ use {
};

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
pub(crate) fn recv(fd: BorrowedFd<'_>, buf: &mut [u8], flags: RecvFlags) -> io::Result<usize> {
pub(crate) unsafe fn recv(fd: BorrowedFd<'_>, buf: *mut u8, cap: usize, flags: RecvFlags) -> io::Result<usize> {
unsafe {
ret_send_recv(c::recv(
borrowed_fd(fd),
buf.as_mut_ptr().cast(),
send_recv_len(buf.len()),
buf.cast(),
send_recv_len(cap),
bitflags_bits!(flags),
))
}
Expand All @@ -55,9 +55,10 @@ pub(crate) fn send(fd: BorrowedFd<'_>, buf: &[u8], flags: SendFlags) -> io::Resu
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
pub(crate) fn recvfrom(
pub(crate) unsafe fn recvfrom(
fd: BorrowedFd<'_>,
buf: &mut [u8],
buf: *mut u8,
cap: usize,
flags: RecvFlags,
) -> io::Result<(usize, Option<SocketAddrAny>)> {
unsafe {
Expand All @@ -71,8 +72,8 @@ pub(crate) fn recvfrom(

ret_send_recv(c::recvfrom(
borrowed_fd(fd),
buf.as_mut_ptr().cast(),
send_recv_len(buf.len()),
buf.cast(),
send_recv_len(cap),
bitflags_bits!(flags),
storage.as_mut_ptr().cast(),
&mut len,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/libc/rand/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
use {crate::backend::c, crate::backend::conv::ret_usize, crate::io, crate::rand::GetRandomFlags};

#[cfg(linux_kernel)]
pub(crate) fn getrandom(buf: &mut [u8], flags: GetRandomFlags) -> io::Result<usize> {
pub(crate) unsafe fn getrandom(buf: *mut u8, cap: usize, flags: GetRandomFlags) -> io::Result<usize> {
// `getrandom` wasn't supported in glibc until 2.25.
weak_or_syscall! {
fn getrandom(buf: *mut c::c_void, buflen: c::size_t, flags: c::c_uint) via SYS_getrandom -> c::ssize_t
}

unsafe { ret_usize(getrandom(buf.as_mut_ptr().cast(), buf.len(), flags.bits())) }
unsafe { ret_usize(getrandom(buf.cast(), cap, flags.bits())) }
}
26 changes: 12 additions & 14 deletions src/backend/linux_raw/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
use crate::backend::c;
use crate::backend::conv::fs::oflags_for_open_how;
use crate::backend::conv::{
by_ref, c_int, c_uint, dev_t, opt_mut, pass_usize, raw_fd, ret, ret_c_int, ret_c_uint,
ret_infallible, ret_owned_fd, ret_usize, size_of, slice, slice_mut, zero,
by_ref, c_int, c_uint, dev_t, opt_mut, raw_fd, ret, ret_c_int, ret_c_uint,
ret_infallible, ret_owned_fd, ret_usize, size_of, slice, pass_usize, zero,
slice_mut
};
#[cfg(target_pointer_width = "64")]
use crate::backend::conv::{loff_t, loff_t_from_u64, ret_u64};
Expand Down Expand Up @@ -1518,43 +1519,40 @@ pub(crate) fn inotify_rm_watch(infd: BorrowedFd<'_>, wfd: i32) -> io::Result<()>
}

#[inline]
pub(crate) fn getxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Result<usize> {
let (value_addr_mut, value_len) = slice_mut(value);
pub(crate) unsafe fn getxattr(path: &CStr, name: &CStr, value: *mut u8, cap: usize) -> io::Result<usize> {
unsafe {
ret_usize(syscall!(
__NR_getxattr,
path,
name,
value_addr_mut,
value_len
value,
pass_usize(cap)
))
}
}

#[inline]
pub(crate) fn lgetxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Result<usize> {
let (value_addr_mut, value_len) = slice_mut(value);
pub(crate) unsafe fn lgetxattr(path: &CStr, name: &CStr, value: *mut u8, cap: usize) -> io::Result<usize> {
unsafe {
ret_usize(syscall!(
__NR_lgetxattr,
path,
name,
value_addr_mut,
value_len
value,
pass_usize(cap)
))
}
}

#[inline]
pub(crate) fn fgetxattr(fd: BorrowedFd<'_>, name: &CStr, value: &mut [u8]) -> io::Result<usize> {
let (value_addr_mut, value_len) = slice_mut(value);
pub(crate) unsafe fn fgetxattr(fd: BorrowedFd<'_>, name: &CStr, value: *mut u8, cap: usize) -> io::Result<usize> {
unsafe {
ret_usize(syscall!(
__NR_fgetxattr,
fd,
name,
value_addr_mut,
value_len
value,
pass_usize(cap)
))
}
}
Expand Down
59 changes: 28 additions & 31 deletions src/backend/linux_raw/io/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::backend::conv::loff_t_from_u64;
use crate::backend::conv::zero;
use crate::backend::conv::{
c_uint, raw_fd, ret, ret_c_int, ret_c_uint, ret_discarded_fd, ret_owned_fd, ret_usize, slice,
slice_mut,
pass_usize
};
#[cfg(target_pointer_width = "32")]
use crate::backend::conv::{hi, lo};
Expand All @@ -29,16 +29,12 @@ use core::cmp;
use linux_raw_sys::general::{F_DUPFD_CLOEXEC, F_GETFD, F_SETFD};

#[inline]
pub(crate) fn read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> io::Result<usize> {
let (buf_addr_mut, buf_len) = slice_mut(buf);

unsafe { ret_usize(syscall!(__NR_read, fd, buf_addr_mut, buf_len)) }
pub(crate) unsafe fn read(fd: BorrowedFd<'_>, buf: *mut u8, len: usize) -> io::Result<usize> {
unsafe { ret_usize(syscall!(__NR_read, fd, buf, pass_usize(len))) }
}

#[inline]
pub(crate) fn pread(fd: BorrowedFd<'_>, buf: &mut [u8], pos: u64) -> io::Result<usize> {
let (buf_addr_mut, buf_len) = slice_mut(buf);

pub(crate) unsafe fn pread(fd: BorrowedFd<'_>, buf: *mut u8, cap: usize, pos: u64) -> io::Result<usize> {
// <https://github.com/torvalds/linux/blob/fcadab740480e0e0e9fa9bd272acd409884d431a/arch/arm64/kernel/sys32.c#L75>
#[cfg(all(
target_pointer_width = "32",
Expand All @@ -48,8 +44,8 @@ pub(crate) fn pread(fd: BorrowedFd<'_>, buf: &mut [u8], pos: u64) -> io::Result<
ret_usize(syscall!(
__NR_pread64,
fd,
buf_addr_mut,
buf_len,
buf,
pass_usize(cap),
zero(),
hi(pos),
lo(pos)
Expand All @@ -63,8 +59,8 @@ pub(crate) fn pread(fd: BorrowedFd<'_>, buf: &mut [u8], pos: u64) -> io::Result<
ret_usize(syscall!(
__NR_pread64,
fd,
buf_addr_mut,
buf_len,
buf,
pass_usize(cap),
hi(pos),
lo(pos)
))
Expand All @@ -74,8 +70,8 @@ pub(crate) fn pread(fd: BorrowedFd<'_>, buf: &mut [u8], pos: u64) -> io::Result<
ret_usize(syscall!(
__NR_pread64,
fd,
buf_addr_mut,
buf_len,
buf,
pass_usize(cap),
loff_t_from_u64(pos)
))
}
Expand Down Expand Up @@ -306,25 +302,26 @@ pub(crate) fn is_read_write(fd: BorrowedFd<'_>) -> io::Result<(bool, bool)> {
// Do a `recv` with `PEEK` and `DONTWAIT` for 1 byte. A 0 indicates
// the read side is shut down; an `EWOULDBLOCK` indicates the read
// side is still open.
//
// TODO: This code would benefit from having a better way to read into
// uninitialized memory.
let mut buf = [0];
match crate::backend::net::syscalls::recv(
fd,
&mut buf,
RecvFlags::PEEK | RecvFlags::DONTWAIT,
) {
Ok(0) => read = false,
Err(err) => {
#[allow(unreachable_patterns)] // `EAGAIN` may equal `EWOULDBLOCK`
match err {
io::Errno::AGAIN | io::Errno::WOULDBLOCK => (),
io::Errno::NOTSOCK => not_socket = true,
_ => return Err(err),
let mut buf = core::mem::MaybeUninit::<u8>::uninit();

unsafe {
match crate::backend::net::syscalls::recv(
fd,
buf.as_mut_ptr(),
1,
RecvFlags::PEEK | RecvFlags::DONTWAIT,
) {
Ok(0) => read = false,
Err(err) => {
#[allow(unreachable_patterns)] // `EAGAIN` may equal `EWOULDBLOCK`
match err {
io::Errno::AGAIN | io::Errno::WOULDBLOCK => (),
io::Errno::NOTSOCK => not_socket = true,
_ => return Err(err),
}
}
Ok(_) => (),
}
Ok(_) => (),
}
}
if write && !not_socket {
Expand Down
Loading