Skip to content
Open
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
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions crates/host_env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ rustix = { workspace = true }
[target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies]
num_cpus = "1.17.0"

[target.'cfg(not(any(target_os = "ios", target_os = "android", target_os = "windows", target_arch = "wasm32", target_os = "redox")))'.dependencies]
mac_address = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
dns-lookup = { workspace = true }
gethostname = { workspace = true }
rustyline = { workspace = true }
socket2 = { workspace = true, features = ["all"] }
which = { workspace = true }

[target.'cfg(all(unix, not(target_os = "ios"), not(target_os = "redox")))'.dependencies]
termios = { workspace = true }

Expand All @@ -35,6 +45,9 @@ libloading = "0.9"
[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]
libffi = { workspace = true, features = ["system"] }

[target.'cfg(target_os = "macos")'.dependencies]
system-configuration = { workspace = true }

[target.'cfg(windows)'.dependencies]
junction = { workspace = true }
schannel = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/host_env/src/faulthandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
use alloc::vec::Vec;
#[cfg(unix)]
use parking_lot::Mutex;

#[cfg(unix)]
pub use libc::{SA_NODEFER, c_int};
pub use libc::{SIGFPE, SIGSEGV};
#[cfg(windows)]
use windows_sys::Win32::System::{
Diagnostics::Debug::{
Expand Down
26 changes: 26 additions & 0 deletions crates/host_env/src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ use std::os::fd::BorrowedFd;

use crate::os::CheckLibcResult;

pub use libc::{F_GETFD, F_GETFL, F_SETFD, F_SETFL, FD_CLOEXEC};

#[cfg(not(target_os = "wasi"))]
pub use libc::{F_DUPFD, F_DUPFD_CLOEXEC, F_GETLK, F_SETLK, F_SETLKW};

#[cfg(not(any(target_os = "wasi", target_os = "redox")))]
pub use libc::{F_GETOWN, F_RDLCK, F_SETOWN, F_UNLCK, F_WRLCK, LOCK_EX, LOCK_NB, LOCK_SH, LOCK_UN};

#[cfg(target_vendor = "apple")]
pub use libc::{F_FULLFSYNC, F_NOCACHE};

#[cfg(target_os = "freebsd")]
pub use libc::{F_DUP2FD, F_DUP2FD_CLOEXEC};

#[cfg(any(target_os = "android", target_os = "linux"))]
pub use libc::{F_OFD_GETLK, F_OFD_SETLK, F_OFD_SETLKW};

#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
pub use libc::{
F_ADD_SEALS, F_GET_SEALS, F_GETLEASE, F_GETPIPE_SZ, F_NOTIFY, F_SEAL_GROW, F_SEAL_SEAL,
F_SEAL_SHRINK, F_SEAL_WRITE, F_SETLEASE, F_SETPIPE_SZ,
};

#[cfg(any(target_os = "dragonfly", target_os = "netbsd", target_vendor = "apple"))]
pub use libc::F_GETPATH;

pub fn normalize_ioctl_request(request: i64) -> libc::c_ulong {
(request as u32) as libc::c_ulong
}
Expand Down
14 changes: 13 additions & 1 deletion crates/host_env/src/fileutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,22 @@ pub mod windows {
}
}

/// C `FILE *` handle as returned by [`fopen`] and consumed by [`fclose`].
pub type CFile = libc::FILE;

/// Close a file opened with [`fopen`].
///
/// # Safety
/// `fp` must be a non-null pointer returned by [`fopen`] and must not have been
/// closed already.
pub unsafe fn fclose(fp: *mut CFile) -> core::ffi::c_int {
unsafe { libc::fclose(fp) }
}

// _Py_fopen_obj in cpython (Python/fileutils.c:1757-1835)
// Open a file using std::fs::File and convert to FILE*
// Automatically handles path encoding and EINTR retries
pub fn fopen(path: &std::path::Path, mode: &str) -> std::io::Result<*mut libc::FILE> {
pub fn fopen(path: &std::path::Path, mode: &str) -> std::io::Result<*mut CFile> {
use alloc::ffi::CString;
use std::fs::File;

Expand Down
10 changes: 8 additions & 2 deletions crates/host_env/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
fs::{self, File, Metadata, ReadDir},
io,
path::Path,
path::{Path, PathBuf},
};

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

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

/// Resolve `binary_name` to an absolute path by searching `PATH` (and `PATHEXT` on Windows).
#[cfg(not(target_arch = "wasm32"))]
pub fn which<T: AsRef<std::ffi::OsStr>>(binary_name: T) -> Option<PathBuf> {
::which::which(binary_name).ok()
}

#[cfg(windows)]
pub fn open_write_with_custom_flags(path: impl AsRef<Path>, flags: u32) -> io::Result<File> {
use std::os::windows::fs::OpenOptionsExt;
Expand Down
2 changes: 2 additions & 0 deletions crates/host_env/src/grp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io;

pub use libc::gid_t;

pub struct Group {
pub name: String,
pub passwd: String,
Expand Down
5 changes: 5 additions & 0 deletions crates/host_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod fileutils;
pub mod fs;
#[cfg(any(unix, windows))]
pub mod locale;
pub mod readline;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Gate readline export by target to avoid cfg/dependency mismatch.

Line 33 exports readline on all targets, but its dependency surface is target-gated in Cargo.toml (non-wasm32). This can break unsupported targets at compile time.

🔧 Proposed fix
- pub mod readline;
+ #[cfg(not(target_arch = "wasm32"))]
+ pub mod readline;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub mod readline;
#[cfg(not(target_arch = "wasm32"))]
pub mod readline;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/host_env/src/lib.rs` at line 33, The module export `pub mod readline;`
is currently unconditional but its crate dependency is only available for
non-wasm32 targets; wrap the export with a target cfg to match Cargo.toml (e.g.
change to `#[cfg(not(target_arch = "wasm32"))] pub mod readline;`) so the module
is only compiled on supported targets; locate the `pub mod readline` line in
lib.rs and apply the cfg attribute (or an equivalent cfg_if) to avoid the
cfg/dependency mismatch.


#[cfg(windows)]
pub mod windows;
Expand Down Expand Up @@ -64,6 +65,10 @@ pub mod time;

#[cfg(windows)]
pub mod cert_store;
#[cfg(target_os = "macos")]
pub mod system_configuration {
pub use ::system_configuration::*;
}
#[cfg(any(unix, windows))]
pub mod faulthandler;
#[cfg(any(unix, windows))]
Expand Down
17 changes: 17 additions & 0 deletions crates/host_env/src/locale.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
use alloc::vec::Vec;
use core::{ffi::CStr, ptr};

pub use libc::{LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME};

#[cfg(all(unix, not(any(target_os = "ios", target_os = "redox"))))]
pub use libc::LC_MESSAGES;

#[cfg(all(
unix,
not(any(target_os = "ios", target_os = "android", target_os = "redox"))
))]
pub use libc::{
ABDAY_1, ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABMON_1, ABMON_2, ABMON_3,
ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12, ALT_DIGITS,
AM_STR, CODESET, CRNCYSTR, D_FMT, D_T_FMT, DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7,
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,
MON_9, MON_10, MON_11, MON_12, NOEXPR, PM_STR, RADIXCHAR, T_FMT, T_FMT_AMPM, THOUSEP, YESEXPR,
};

#[cfg(windows)]
#[repr(C)]
struct RawLconv {
Expand Down
56 changes: 56 additions & 0 deletions crates/host_env/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,62 @@

use std::io;

#[cfg(unix)]
pub use libc::{
MADV_DONTNEED, MADV_NORMAL, MADV_RANDOM, MADV_SEQUENTIAL, MADV_WILLNEED, MAP_ANON,
MAP_ANONYMOUS, MAP_PRIVATE, MAP_SHARED, PROT_EXEC, PROT_READ, PROT_WRITE,
};

#[cfg(target_os = "macos")]
pub use libc::{MADV_FREE_REUSABLE, MADV_FREE_REUSE};

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "fuchsia",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_vendor = "apple"
))]
pub use libc::MADV_FREE;

#[cfg(target_os = "linux")]
pub use libc::{
MADV_DODUMP, MADV_DOFORK, MADV_DONTDUMP, MADV_DONTFORK, MADV_HUGEPAGE, MADV_HWPOISON,
MADV_MERGEABLE, MADV_NOHUGEPAGE, MADV_REMOVE, MADV_UNMERGEABLE,
};

#[cfg(any(
target_os = "android",
all(
target_os = "linux",
any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "s390x",
target_arch = "x86",
target_arch = "x86_64",
target_arch = "sparc64"
)
)
))]
pub use libc::MADV_SOFT_OFFLINE;

#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))]
pub use libc::{MAP_DENYWRITE, MAP_EXECUTABLE, MAP_POPULATE};

#[cfg(any(target_os = "linux", target_os = "openbsd", target_os = "netbsd"))]
pub use libc::MAP_STACK;

#[cfg(target_os = "freebsd")]
pub use libc::{MADV_AUTOSYNC, MADV_CORE, MADV_NOCORE, MADV_NOSYNC, MADV_PROTECT};

pub use libc::EOVERFLOW;

#[cfg(windows)]
use crate::windows::{CheckWin32Bool, HandleToOwned};
#[cfg(unix)]
Expand Down
2 changes: 1 addition & 1 deletion crates/host_env/src/multiprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use alloc::ffi::CString;
use std::io;

#[cfg(unix)]
use libc::sem_t;
pub use libc::{sem_t, timespec};
#[cfg(unix)]
use nix::errno::Errno;

Expand Down
2 changes: 2 additions & 0 deletions crates/host_env/src/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::os::fd::FromRawFd;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, OwnedFd};
use std::path::Path;

pub use libc::{c_char, pid_t};

pub struct UnameInfo {
pub sysname: String,
pub nodename: String,
Expand Down
30 changes: 7 additions & 23 deletions crates/vm/src/readline.rs → crates/host_env/src/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,34 +106,18 @@ pub mod rustyline_readline {
}

pub fn load_history(&mut self, path: &Path) -> OtherResult<()> {
#[cfg(not(feature = "host_env"))]
{
let _ = path;
Err(io::Error::other("history requires the `host_env` feature").into())
}
#[cfg(feature = "host_env")]
{
self.repl.load_history(path)?;
Ok(())
}
self.repl.load_history(path)?;
Ok(())
}

pub fn save_history(&mut self, path: &Path) -> OtherResult<()> {
#[cfg(not(feature = "host_env"))]
{
let _ = path;
Err(io::Error::other("history requires the `host_env` feature").into())
}
#[cfg(feature = "host_env")]
if !path.exists()
&& let Some(parent) = path.parent()
{
if !path.exists()
&& let Some(parent) = path.parent()
{
crate::host_env::fs::create_dir_all(parent)?;
}
self.repl.save_history(path)?;
Ok(())
crate::fs::create_dir_all(parent)?;
}
self.repl.save_history(path)?;
Ok(())
}

pub fn add_history_entry(&mut self, entry: &str) -> OtherResult<()> {
Expand Down
Loading
Loading