Visitar URL original
more hostenv isolation by youknowone · Pull Request #7886 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a5ac299
Convert host_env Windows path/argv params from raw *const u16 to &Wid…
youknowone May 16, 2026
cea48cc
Migrate remaining winapi raw u16 pointer signatures to typed references
youknowone May 16, 2026
25a9d40
Migrate winreg pub unsafe fn string parameters to typed references
youknowone May 16, 2026
0acdcad
Add ToPyException impls for host_env error types (PyPy wrap_oserror a…
youknowone May 16, 2026
dc57418
Add CheckLibcResult helper and apply to socket/fcntl/shm/posix_wasi
youknowone May 16, 2026
2d4be2c
Add Win32 BOOL/HANDLE check helpers; apply check helpers across host_env
youknowone May 16, 2026
592e8b4
Apply Win32/libc check helpers to overlapped/testconsole/os.rs
youknowone May 16, 2026
86055d2
Apply Win32 check helpers to winapi.rs (partial)
youknowone May 16, 2026
fe71c58
Apply Win32 check helpers across more winapi.rs functions
youknowone May 16, 2026
a7c567a
Apply Win32 check helpers to nt.rs (partial)
youknowone May 16, 2026
5329cc5
Add CheckWin32Sentinel helper; apply to nt.rs INVALID_HANDLE_VALUE/IN…
youknowone May 16, 2026
f1532e2
Add OwnedHandle / HandleToOwned helper; apply to mmap create_named_ma…
youknowone May 16, 2026
e768278
Use OwnedHandle RAII in nt::pipe to eliminate manual cleanup on error…
youknowone May 16, 2026
b730f7e
Use OwnedHandle in nt::chmod_follow; hoist HandleToOwned import
youknowone May 16, 2026
67ece5b
Drop rustix dependency from vm crate
youknowone May 17, 2026
a61d9bd
Fix CI failures: cross-platform regressions
youknowone May 19, 2026
3320c2a
Fix CI failures: cfg gates and unused imports
youknowone May 19, 2026
e9ec6e8
Fix CI failures: rustfmt and windows unused import
youknowone May 19, 2026
ba3f4fa
Push remaining libc/extern callsites from vm into host_env
youknowone May 19, 2026
45c83dd
Move time tz state and winsound FFI into host_env
youknowone May 19, 2026
49c13f8
Migrate unsetenv to host_env::nt::wputenv; rustfmt
youknowone May 20, 2026
61e0fd4
Address PR review comments
youknowone May 20, 2026
783c6b5
Fix CI failures and address review follow-ups
youknowone May 20, 2026
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
Prev Previous commit
Next Next commit
Add CheckLibcResult helper and apply to socket/fcntl/shm/posix_wasi
  • Loading branch information
youknowone committed May 20, 2026
commit dc57418c921d0f60a879ec6ef91f3a9356509ae5
46 changes: 10 additions & 36 deletions crates/host_env/src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ use std::io;
#[cfg(unix)]
use std::os::fd::BorrowedFd;

use crate::os::CheckLibcResult;

pub fn normalize_ioctl_request(request: i64) -> libc::c_ulong {
(request as u32) as libc::c_ulong
}

pub fn fcntl_int(fd: i32, cmd: i32, arg: i32) -> io::Result<i32> {
let ret = unsafe { libc::fcntl(fd, cmd, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::fcntl(fd, cmd, arg) }.check_libc_neg()
}

pub fn validate_fd(fd: i32) -> io::Result<()> {
Expand Down Expand Up @@ -56,12 +53,7 @@ pub fn set_blocking(fd: BorrowedFd<'_>, blocking: bool) -> io::Result<()> {
}

pub fn fcntl_with_bytes(fd: i32, cmd: i32, arg: &mut [u8]) -> io::Result<i32> {
let ret = unsafe { libc::fcntl(fd, cmd, arg.as_mut_ptr()) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::fcntl(fd, cmd, arg.as_mut_ptr()) }.check_libc_neg()
}

/// # Safety
Expand All @@ -73,31 +65,16 @@ pub unsafe fn ioctl_ptr(
request: libc::c_ulong,
arg: *mut libc::c_void,
) -> io::Result<i32> {
let ret = unsafe { libc::ioctl(fd, request as _, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::ioctl(fd, request as _, arg) }.check_libc_neg()
}

pub fn ioctl_int(fd: i32, request: libc::c_ulong, arg: i32) -> io::Result<i32> {
let ret = unsafe { libc::ioctl(fd, request as _, arg) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::ioctl(fd, request as _, arg) }.check_libc_neg()
}

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
pub fn flock(fd: i32, operation: i32) -> io::Result<i32> {
let ret = unsafe { libc::flock(fd, operation) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret)
}
unsafe { libc::flock(fd, operation) }.check_libc_neg()
}

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
Expand Down Expand Up @@ -137,7 +114,7 @@ pub fn lockf(fd: i32, cmd: i32, len: i64, start: i64, whence: i32) -> Result<i32
..unsafe { core::mem::zeroed() }
};

let ret = unsafe {
unsafe {
libc::fcntl(
fd,
if (cmd & libc::LOCK_NB) != 0 {
Expand All @@ -147,10 +124,7 @@ pub fn lockf(fd: i32, cmd: i32, len: i64, start: i64, whence: i32) -> Result<i32
},
&lock,
)
};
if ret < 0 {
Err(LockfError::Io(io::Error::last_os_error()))
} else {
Ok(ret)
}
.check_libc_neg()
.map_err(LockfError::Io)
}
24 changes: 24 additions & 0 deletions crates/host_env/src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,30 @@ pub fn errno_io_error() -> io::Error {
std::io::Error::last_os_error()
}

/// Convert a libc-style return value into an `io::Result`.
///
/// Negative values are treated as errors; errno is read via [`errno_io_error`].
/// Modeled after PyPy's `rposix.handle_posix_error`.
pub trait CheckLibcResult: Sized {
/// Returns `Ok(self)` if non-negative, otherwise `Err` with the current errno.
fn check_libc_neg(self) -> io::Result<Self>;
}

macro_rules! impl_check_libc_result {
($($ty:ty),* $(,)?) => {
$(
impl CheckLibcResult for $ty {
#[inline]
fn check_libc_neg(self) -> io::Result<Self> {
if self < 0 { Err(errno_io_error()) } else { Ok(self) }
}
}
)*
};
}

impl_check_libc_result!(i16, i32, i64, isize);

#[cfg(windows)]
pub fn get_errno() -> i32 {
unsafe extern "C" {
Expand Down
48 changes: 15 additions & 33 deletions crates/host_env/src/posix_wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@ use alloc::ffi::CString;
use core::{ffi::CStr, time::Duration};
use std::{ffi::OsStr, io};

use crate::os::CheckLibcResult;

pub fn make_dir(path: &CStr, mode: u32) -> io::Result<()> {
let ret = unsafe { libc::mkdir(path.as_ptr(), mode as _) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
unsafe { libc::mkdir(path.as_ptr(), mode as _) }.check_libc_neg()?;
Ok(())
}

pub fn make_dir_at(dir_fd: i32, path: &CStr, mode: u32) -> io::Result<()> {
let ret = unsafe { libc::mkdirat(dir_fd, path.as_ptr(), mode as _) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
unsafe { libc::mkdirat(dir_fd, path.as_ptr(), mode as _) }.check_libc_neg()?;
Ok(())
}

pub fn remove_dir_at(dir_fd: i32, path: &CStr) -> io::Result<()> {
let ret = unsafe { libc::unlinkat(dir_fd, path.as_ptr(), libc::AT_REMOVEDIR) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
unsafe { libc::unlinkat(dir_fd, path.as_ptr(), libc::AT_REMOVEDIR) }.check_libc_neg()?;
Ok(())
}

pub fn stat_path(
Expand All @@ -48,10 +38,8 @@ pub fn stat_path(
} else {
libc::AT_SYMLINK_NOFOLLOW
};
let ret = unsafe { libc::fstatat(dir_fd, path.as_ptr(), stat.as_mut_ptr(), flags) };
if ret < 0 {
return Err(io::Error::last_os_error());
}
unsafe { libc::fstatat(dir_fd, path.as_ptr(), stat.as_mut_ptr(), flags) }
.check_libc_neg()?;
return Ok(Some(unsafe { stat.assume_init() }));
}

Expand All @@ -60,11 +48,8 @@ pub fn stat_path(
} else {
unsafe { libc::lstat(path.as_ptr(), stat.as_mut_ptr()) }
};
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(Some(unsafe { stat.assume_init() }))
}
ret.check_libc_neg()?;
Ok(Some(unsafe { stat.assume_init() }))
}

pub fn stat_fd(fd: crate::crt_fd::Borrowed<'_>) -> io::Result<crate::fileutils::StatStruct> {
Expand All @@ -83,7 +68,7 @@ pub fn set_file_times_at(
tv_nsec: d.subsec_nanos() as _,
};
let times = [ts(access), ts(modified)];
let ret = unsafe {
unsafe {
libc::utimensat(
dir_fd,
path.as_ptr(),
Expand All @@ -94,10 +79,7 @@ pub fn set_file_times_at(
libc::AT_SYMLINK_NOFOLLOW
},
)
};
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
.check_libc_neg()?;
Ok(())
}
17 changes: 5 additions & 12 deletions crates/host_env/src/shm.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use core::ffi::CStr;
use std::io;

use crate::os::CheckLibcResult;

pub fn shm_open(name: &CStr, flags: libc::c_int, mode: libc::c_uint) -> io::Result<libc::c_int> {
#[cfg(target_os = "freebsd")]
let mode = mode.try_into().unwrap();

let fd = unsafe { libc::shm_open(name.as_ptr(), flags, mode) };
if fd == -1 {
Err(io::Error::last_os_error())
} else {
Ok(fd)
}
unsafe { libc::shm_open(name.as_ptr(), flags, mode) }.check_libc_neg()
}

pub fn shm_unlink(name: &CStr) -> io::Result<()> {
let ret = unsafe { libc::shm_unlink(name.as_ptr()) };
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
unsafe { libc::shm_unlink(name.as_ptr()) }.check_libc_neg()?;
Ok(())
}
62 changes: 21 additions & 41 deletions crates/host_env/src/socket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(unix)]
use crate::os::CheckLibcResult;
#[cfg(unix)]
use core::ffi::CStr;
#[cfg(unix)]
use core::time::Duration;
Expand Down Expand Up @@ -36,20 +38,17 @@ pub fn close_socket_ignore_connreset(socket: libc::c_int) -> io::Result<()> {
pub fn getsockopt_int(fd: libc::c_int, level: i32, name: i32) -> io::Result<i32> {
let mut flag: libc::c_int = 0;
let mut flagsize = core::mem::size_of::<libc::c_int>() as libc::socklen_t;
let ret = unsafe {
unsafe {
libc::getsockopt(
fd,
level,
name,
&mut flag as *mut libc::c_int as *mut _,
&mut flagsize,
)
};
if ret < 0 {
Err(crate::os::errno_io_error())
} else {
Ok(flag)
}
.check_libc_neg()?;
Ok(flag)
}

#[cfg(unix)]
Expand All @@ -61,67 +60,55 @@ pub fn getsockopt_bytes(
) -> io::Result<Vec<u8>> {
let mut buf = vec![0u8; buflen];
let mut optlen = buflen as libc::socklen_t;
let ret = unsafe { libc::getsockopt(fd, level, name, buf.as_mut_ptr() as *mut _, &mut optlen) };
if ret < 0 {
Err(crate::os::errno_io_error())
} else {
buf.truncate(optlen as usize);
Ok(buf)
}
unsafe { libc::getsockopt(fd, level, name, buf.as_mut_ptr() as *mut _, &mut optlen) }
.check_libc_neg()?;
buf.truncate(optlen as usize);
Ok(buf)
}

#[cfg(unix)]
pub fn setsockopt_bytes(fd: libc::c_int, level: i32, name: i32, value: &[u8]) -> io::Result<()> {
let ret = unsafe {
unsafe {
libc::setsockopt(
fd,
level,
name,
value.as_ptr() as *const _,
value.len() as libc::socklen_t,
)
};
if ret < 0 {
Err(crate::os::errno_io_error())
} else {
Ok(())
}
.check_libc_neg()?;
Ok(())
}

#[cfg(unix)]
pub fn setsockopt_int(fd: libc::c_int, level: i32, name: i32, value: i32) -> io::Result<()> {
let ret = unsafe {
unsafe {
libc::setsockopt(
fd,
level,
name,
&value as *const i32 as *const _,
core::mem::size_of::<i32>() as libc::socklen_t,
)
};
if ret < 0 {
Err(crate::os::errno_io_error())
} else {
Ok(())
}
.check_libc_neg()?;
Ok(())
}

#[cfg(unix)]
pub fn setsockopt_none(fd: libc::c_int, level: i32, name: i32, optlen: u32) -> io::Result<()> {
let ret = unsafe {
unsafe {
libc::setsockopt(
fd,
level,
name,
core::ptr::null(),
optlen as libc::socklen_t,
)
};
if ret < 0 {
Err(crate::os::errno_io_error())
} else {
Ok(())
}
.check_libc_neg()?;
Ok(())
}

#[cfg(unix)]
Expand Down Expand Up @@ -367,10 +354,7 @@ pub fn recvmsg(
msg.msg_controllen = ancbufsize as _;
}

let ret = unsafe { libc::recvmsg(fd.as_raw_fd(), &mut msg, flags) };
if ret < 0 {
return Err(io::Error::last_os_error());
}
let ret = unsafe { libc::recvmsg(fd.as_raw_fd(), &mut msg, flags) }.check_libc_neg()?;

let data = unsafe {
data_buf.set_len(ret as usize);
Expand Down Expand Up @@ -477,12 +461,8 @@ pub fn sendmsg_afalg(
msghdr.msg_controllen = control_buf.len() as _;
}

let ret = unsafe { libc::sendmsg(fd.as_raw_fd(), &msghdr, flags) };
if ret < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ret as usize)
}
let ret = unsafe { libc::sendmsg(fd.as_raw_fd(), &msghdr, flags) }.check_libc_neg()?;
Ok(ret as usize)
}

#[cfg(windows)]
Expand Down