Skip to content

Commit 3f33073

Browse files
authored
more hostenv isolation (#7932)
* Move which crate dependency from vm to host_env Add host_env::fs::which wrapper and update getpath.rs call site. * Move readline module from vm to host_env Move crates/vm/src/readline.rs to crates/host_env/src/readline.rs and re-export it from rustpython_vm::readline. Move rustyline dependency from vm to host_env. Drop the host_env feature gates on history I/O inside the moved module. * Move gethostname and mac_address dependencies from stdlib to host_env Add host_env::socket::hostname and host_env::socket::mac_address wrappers. Update _socket.gethostname and _uuid.get_node_id call sites. * Re-export libc constants and types through host_env Move direct libc constant and type references in stdlib (faulthandler, fcntl, multiprocessing, posixshmem, select, syslog) to use host_env re-exports. Replace libc::c_int and libc::c_uint in stdlib with core::ffi equivalents. * Re-export mmap libc constants through host_env Move direct libc MADV_*, MAP_*, PROT_*, EOVERFLOW references in stdlib::mmap to host_env::mmap re-exports. Replace libc::c_int with core::ffi::c_int. * Re-export locale and termios libc constants through host_env Move direct libc LC_*, ABDAY_*, ABMON_*, TIOC*, FIO* and related constants in stdlib::locale and stdlib::termios to host_env re-exports. Replace libc::c_char in stdlib::locale and stdlib::openssl::cert with core::ffi::c_char. * Replace libc c-types and FILE wrappers in openssl with host_env Replace libc::c_int, c_long, c_uchar, c_uint, c_ulong, c_void, c_char in stdlib::openssl with core::ffi equivalents. Replace libc::ENOENT with host_env::errno::errors::ENOENT. Add host_env::fileutils::{CFile, fclose} and route the load_dh_params fclose call and PEM_read_DHparams FILE pointer type through them. * Fix Windows build: expose EOVERFLOW, SIGSEGV, SIGFPE on Windows host_mmap::EOVERFLOW and host_faulthandler::{SIGSEGV, SIGFPE} re-exports were gated to cfg(unix) but stdlib uses them from Windows and platform-agnostic code paths. * Re-export remaining libc types and constants through host_env Move direct libc references in stdlib (resource, posixsubprocess, grp, socket) to host_env re-exports. Replace libc::c_long, libc::c_longlong, libc::c_char in stdlib::socket with core::ffi equivalents. Add resource::{RLIMIT_*, RUSAGE_*, c_long, rlim_t, rlimit, timeval}, posix::{c_char, pid_t}, grp::gid_t, and socket::{AF_*, SOCK_STREAM, sa_family_t, socklen_t, sockaddr_*} re-exports. * Move socket2 and dns-lookup dependencies from stdlib to host_env Re-export socket2 as host_env::socket::raw and dns-lookup as host_env::socket::dns. Update _socket call sites to route through host_env aliases. * Move system-configuration dependency from stdlib to host_env Re-export the macOS system-configuration crate as host_env::system_configuration and update _scproxy call sites. * Restrict CAN/ALG sockaddr structs to target_os linux libc on Android (bionic) does not define sockaddr_can, so importing it for target_os = "android" fails to compile. Keep AF_ALG/AF_CAN for both linux and android, and gate sockaddr_alg/sockaddr_can to linux only. Assisted-by: Claude
1 parent 984cb50 commit 3f33073

39 files changed

Lines changed: 837 additions & 520 deletions

Cargo.lock

Lines changed: 300 additions & 201 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/host_env/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ rustix = { workspace = true }
2727
[target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies]
2828
num_cpus = "1.17.0"
2929

30+
[target.'cfg(not(any(target_os = "ios", target_os = "android", target_os = "windows", target_arch = "wasm32", target_os = "redox")))'.dependencies]
31+
mac_address = { workspace = true }
32+
33+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
34+
dns-lookup = { workspace = true }
35+
gethostname = { workspace = true }
36+
rustyline = { workspace = true }
37+
socket2 = { workspace = true, features = ["all"] }
38+
which = { workspace = true }
39+
3040
[target.'cfg(all(unix, not(target_os = "ios"), not(target_os = "redox")))'.dependencies]
3141
termios = { workspace = true }
3242

@@ -37,6 +47,9 @@ libloading = "0.9"
3747
[target.'cfg(all(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"), not(any(target_env = "musl", target_env = "sgx"))))'.dependencies]
3848
libffi = { workspace = true, features = ["system"] }
3949

50+
[target.'cfg(target_os = "macos")'.dependencies]
51+
system-configuration = { workspace = true }
52+
4053
[target.'cfg(windows)'.dependencies]
4154
junction = { workspace = true }
4255
schannel = { workspace = true }

crates/host_env/src/faulthandler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use alloc::vec::Vec;
1313
#[cfg(unix)]
1414
use parking_lot::Mutex;
15+
16+
#[cfg(unix)]
17+
pub use libc::{SA_NODEFER, c_int};
18+
pub use libc::{SIGFPE, SIGSEGV};
1519
#[cfg(windows)]
1620
use windows_sys::Win32::System::{
1721
Diagnostics::Debug::{

crates/host_env/src/fcntl.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ use std::os::fd::BorrowedFd;
55

66
use crate::os::CheckLibcResult;
77

8+
pub use libc::{F_GETFD, F_GETFL, F_SETFD, F_SETFL, FD_CLOEXEC};
9+
10+
#[cfg(not(target_os = "wasi"))]
11+
pub use libc::{F_DUPFD, F_DUPFD_CLOEXEC, F_GETLK, F_SETLK, F_SETLKW};
12+
13+
#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
14+
pub use libc::{F_GETOWN, F_RDLCK, F_SETOWN, F_UNLCK, F_WRLCK, LOCK_EX, LOCK_NB, LOCK_SH, LOCK_UN};
15+
16+
#[cfg(target_vendor = "apple")]
17+
pub use libc::{F_FULLFSYNC, F_NOCACHE};
18+
19+
#[cfg(target_os = "freebsd")]
20+
pub use libc::{F_DUP2FD, F_DUP2FD_CLOEXEC};
21+
22+
#[cfg(any(target_os = "android", target_os = "linux"))]
23+
pub use libc::{F_OFD_GETLK, F_OFD_SETLK, F_OFD_SETLKW};
24+
25+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
26+
pub use libc::{
27+
F_ADD_SEALS, F_GET_SEALS, F_GETLEASE, F_GETPIPE_SZ, F_NOTIFY, F_SEAL_GROW, F_SEAL_SEAL,
28+
F_SEAL_SHRINK, F_SEAL_WRITE, F_SETLEASE, F_SETPIPE_SZ,
29+
};
30+
31+
#[cfg(any(target_os = "dragonfly", target_os = "netbsd", target_vendor = "apple"))]
32+
pub use libc::F_GETPATH;
33+
834
pub fn normalize_ioctl_request(request: i64) -> libc::c_ulong {
935
(request as u32) as libc::c_ulong
1036
}

crates/host_env/src/fileutils.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,22 @@ pub mod windows {
442442
}
443443
}
444444

445+
/// C `FILE *` handle as returned by [`fopen`] and consumed by [`fclose`].
446+
pub type CFile = libc::FILE;
447+
448+
/// Close a file opened with [`fopen`].
449+
///
450+
/// # Safety
451+
/// `fp` must be a non-null pointer returned by [`fopen`] and must not have been
452+
/// closed already.
453+
pub unsafe fn fclose(fp: *mut CFile) -> core::ffi::c_int {
454+
unsafe { libc::fclose(fp) }
455+
}
456+
445457
// _Py_fopen_obj in cpython (Python/fileutils.c:1757-1835)
446458
// Open a file using std::fs::File and convert to FILE*
447459
// Automatically handles path encoding and EINTR retries
448-
pub fn fopen(path: &std::path::Path, mode: &str) -> std::io::Result<*mut libc::FILE> {
460+
pub fn fopen(path: &std::path::Path, mode: &str) -> std::io::Result<*mut CFile> {
449461
use alloc::ffi::CString;
450462
use std::fs::File;
451463

crates/host_env/src/fs.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
fs::{self, File, Metadata, ReadDir},
33
io,
4-
path::Path,
4+
path::{Path, PathBuf},
55
};
66

77
pub fn open(path: impl AsRef<Path>) -> io::Result<File> {
@@ -44,10 +44,16 @@ pub fn open_write(path: impl AsRef<Path>) -> io::Result<File> {
4444
fs::OpenOptions::new().write(true).open(path)
4545
}
4646

47-
pub fn canonicalize(path: impl AsRef<Path>) -> io::Result<std::path::PathBuf> {
47+
pub fn canonicalize(path: impl AsRef<Path>) -> io::Result<PathBuf> {
4848
fs::canonicalize(path)
4949
}
5050

51+
/// Resolve `binary_name` to an absolute path by searching `PATH` (and `PATHEXT` on Windows).
52+
#[cfg(not(target_arch = "wasm32"))]
53+
pub fn which<T: AsRef<std::ffi::OsStr>>(binary_name: T) -> Option<PathBuf> {
54+
::which::which(binary_name).ok()
55+
}
56+
5157
#[cfg(windows)]
5258
pub fn open_write_with_custom_flags(path: impl AsRef<Path>, flags: u32) -> io::Result<File> {
5359
use std::os::windows::fs::OpenOptionsExt;

crates/host_env/src/grp.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::io;
22

3+
pub use libc::gid_t;
4+
35
pub struct Group {
46
pub name: String,
57
pub passwd: String,

crates/host_env/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod fileutils;
3030
pub mod fs;
3131
#[cfg(any(unix, windows))]
3232
pub mod locale;
33+
pub mod readline;
3334

3435
#[cfg(windows)]
3536
pub mod windows;
@@ -67,6 +68,10 @@ pub mod time;
6768

6869
#[cfg(windows)]
6970
pub mod cert_store;
71+
#[cfg(target_os = "macos")]
72+
pub mod system_configuration {
73+
pub use ::system_configuration::*;
74+
}
7075
#[cfg(any(unix, windows))]
7176
pub mod faulthandler;
7277
#[cfg(any(unix, windows))]

crates/host_env/src/locale.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
use alloc::vec::Vec;
22
use core::{ffi::CStr, ptr};
33

4+
pub use libc::{LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME};
5+
6+
#[cfg(all(unix, not(any(target_os = "ios", target_os = "redox"))))]
7+
pub use libc::LC_MESSAGES;
8+
9+
#[cfg(all(
10+
unix,
11+
not(any(target_os = "ios", target_os = "android", target_os = "redox"))
12+
))]
13+
pub use libc::{
14+
ABDAY_1, ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABMON_1, ABMON_2, ABMON_3,
15+
ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12, ALT_DIGITS,
16+
AM_STR, CODESET, CRNCYSTR, D_FMT, D_T_FMT, DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7,
17+
ERA, ERA_D_FMT, ERA_D_T_FMT, ERA_T_FMT, MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8,
18+
MON_9, MON_10, MON_11, MON_12, NOEXPR, PM_STR, RADIXCHAR, T_FMT, T_FMT_AMPM, THOUSEP, YESEXPR,
19+
};
20+
421
#[cfg(windows)]
522
#[repr(C)]
623
struct RawLconv {

crates/host_env/src/mmap.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ pub use libc::MAP_STACK;
5959
#[cfg(target_os = "freebsd")]
6060
pub use libc::{MADV_AUTOSYNC, MADV_CORE, MADV_NOCORE, MADV_NOSYNC, MADV_PROTECT};
6161

62-
#[cfg(windows)]
6362
pub use libc::EOVERFLOW;
6463

6564
#[cfg(windows)]

0 commit comments

Comments
 (0)