diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 21df08597..6ee7d792d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -158,6 +158,23 @@ jobs: - run: cargo check --workspace --release -vv --target=aarch64-apple-ios --features=all-apis --all-targets - run: cargo check --workspace --release -vv --target=aarch64-linux-android --features=use-libc,net,std,alloc,event,fs,mm,param,pipe,use-libc-auxv,libc_errno --all-targets - run: cargo check --workspace --release -vv --target=i686-pc-windows-msvc --features=all-apis --all-targets + - name: Check Android and musl builds without linux-raw-sys to protect AOSP Rustix use + env: + RUSTFLAGS: >- + -D warnings -D elided-lifetimes-in-paths + --cfg criterion --cfg rustix_no_linux_raw + run: | + FEATURES="use-libc,net,std,alloc,event,fs,mm,param,pipe,thread,use-libc-auxv,libc_errno" + cargo check --workspace --release -vv --all-targets --features=$FEATURES \ + --target=x86_64-unknown-linux-musl + cargo check --workspace --release -vv --all-targets --features=$FEATURES \ + --target=x86_64-linux-android + cargo check --workspace --release -vv --all-targets --features=$FEATURES \ + --target=i686-linux-android + cargo check --workspace --release -vv --all-targets --features=$FEATURES \ + --target=arm-linux-androideabi + cargo check --workspace --release -vv --all-targets --features=$FEATURES \ + --target=aarch64-linux-android check_no_default_features: name: Check --no-default-features diff --git a/Cargo.toml b/Cargo.toml index 0943a6cec..98f7cd1bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ rustc-std-workspace-alloc = { version = "1.0.1", optional = true } # not aliased # addition to the libc backend. The linux_raw backend is used by default. The # libc backend can be selected via adding `--cfg=rustix_use_libc` to # `RUSTFLAGS` or enabling the `use-libc` cargo feature. -[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies] +[target.'cfg(all(not(rustix_use_libc), not(rustix_no_linux_raw), not(miri), target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies] linux-raw-sys = { version = "0.12", default-features = false, features = ["auxvec", "general", "errno", "ioctl", "no_std", "elf"] } libc_errno = { package = "errno", version = "0.3.10", default-features = false, optional = true } libc = { version = "0.2.191", default-features = false, optional = true } @@ -41,7 +41,7 @@ libc = { version = "0.2.191", default-features = false, optional = true } # # On all other Unix-family platforms, and under Miri, we always use the libc # backend, so enable its dependencies unconditionally. -[target.'cfg(all(not(windows), any(rustix_use_libc, miri, not(all(target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies] +[target.'cfg(all(not(windows), any(rustix_use_libc, rustix_no_linux_raw, miri, not(all(target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies] libc_errno = { package = "errno", version = "0.3.10", default-features = false } libc = { version = "0.2.191", default-features = false } @@ -49,7 +49,7 @@ libc = { version = "0.2.191", default-features = false } # # Some syscalls do not have libc wrappers, such as in `io_uring`. For these, # the libc backend uses the linux-raw-sys ABI and `libc::syscall`. -[target.'cfg(all(any(target_os = "linux", target_os = "android"), any(rustix_use_libc, miri, not(all(target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies] +[target.'cfg(all(not(rustix_no_linux_raw), any(target_os = "linux", target_os = "android"), any(rustix_use_libc, miri, not(all(target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies] linux-raw-sys = { version = "0.12", default-features = false, features = ["general", "ioctl", "no_std"] } # For the libc backend on Windows, use the Winsock API in windows-sys. diff --git a/build.rs b/build.rs index 0bde91294..319d56fc8 100644 --- a/build.rs +++ b/build.rs @@ -100,6 +100,7 @@ fn main() { // install the toolchain for it. let libc = feature_use_libc || cfg_use_libc + || cfg_no_linux_raw || os != "linux" || !inline_asm_name_present || is_unsupported_abi @@ -182,6 +183,7 @@ fn main() { println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM"); println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_LIBC"); + println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_NO_LINUX_RAW"); // Rerun this script if any of our features or configuration flags change, // or if the toolchain we used for feature detection changes. diff --git a/src/backend/libc/c.rs b/src/backend/libc/c.rs index fb748979f..5ed09a22a 100644 --- a/src/backend/libc/c.rs +++ b/src/backend/libc/c.rs @@ -516,6 +516,14 @@ pub(crate) use linux_raw_sys::general::{ #[cfg(target_os = "android")] pub(crate) use __fsid_t as fsid_t; +// Android's libc crate is missing some constants. +// TODO(https://github.com/bytecodealliance/rustix/issues/1589): try to upstream these +// constants to libc. +#[cfg(target_os = "android")] +pub(crate) const FUTEX_WAITERS: u32 = 0x80000000; +#[cfg(target_os = "android")] +pub(crate) const FUTEX_OWNER_DIED: u32 = 0x40000000; + // FreeBSD added `timerfd_*` in FreeBSD 14. NetBSD added then in NetBSD 10. #[cfg(all(feature = "time", any(target_os = "freebsd", target_os = "netbsd")))] syscall!(pub(crate) fn timerfd_create( diff --git a/src/backend/libc/fs/syscalls.rs b/src/backend/libc/fs/syscalls.rs index 5b8962f98..03cec6936 100644 --- a/src/backend/libc/fs/syscalls.rs +++ b/src/backend/libc/fs/syscalls.rs @@ -2086,10 +2086,10 @@ pub(crate) fn statx( // doesn't represent all the known flags. // // [it's deprecated]: https://patchwork.kernel.org/project/linux-fsdevel/patch/20200505095915.11275-7-mszeredi@redhat.com/ - #[cfg(any(not(linux_raw_dep), not(target_env = "musl")))] + #[cfg(not(target_env = "musl"))] const STATX__RESERVED: u32 = c::STATX__RESERVED as u32; #[cfg(target_env = "musl")] - const STATX__RESERVED: u32 = linux_raw_sys::general::STATX__RESERVED; + const STATX__RESERVED: u32 = 0x80000000; if (mask.bits() & STATX__RESERVED) == STATX__RESERVED { return Err(io::Errno::INVAL); } diff --git a/src/backend/libc/thread/futex.rs b/src/backend/libc/thread/futex.rs index 5e836a9ab..6d2e14955 100644 --- a/src/backend/libc/thread/futex.rs +++ b/src/backend/libc/thread/futex.rs @@ -17,6 +17,16 @@ bitflags::bitflags! { } } +// TODO(https://github.com/bytecodealliance/rustix/issues/1589): try to upstream these +// constants to libc. +const FUTEX2_SIZE_U8: u32 = 0; +const FUTEX2_SIZE_U16: u32 = 1; +const FUTEX2_SIZE_U32: u32 = 2; +const FUTEX2_SIZE_U64: u32 = 3; +const FUTEX2_SIZE_MASK: u32 = 3; +const FUTEX2_NUMA: u32 = 4; +const FUTEX2_PRIVATE: u32 = 128; + bitflags::bitflags! { /// `FUTEX2_*` flags for use with the functions in [`Waitv`]. /// @@ -29,22 +39,19 @@ bitflags::bitflags! { #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] pub struct WaitFlags: u32 { /// `FUTEX_U8` - const SIZE_U8 = linux_raw_sys::general::FUTEX2_SIZE_U8; + const SIZE_U8 = FUTEX2_SIZE_U8; /// `FUTEX_U16` - const SIZE_U16 = linux_raw_sys::general::FUTEX2_SIZE_U16; + const SIZE_U16 = FUTEX2_SIZE_U16; /// `FUTEX_U32` - const SIZE_U32 = linux_raw_sys::general::FUTEX2_SIZE_U32; + const SIZE_U32 = FUTEX2_SIZE_U32; /// `FUTEX_U64` - const SIZE_U64 = linux_raw_sys::general::FUTEX2_SIZE_U64; + const SIZE_U64 = FUTEX2_SIZE_U64; /// `FUTEX_SIZE_MASK` - const SIZE_MASK = linux_raw_sys::general::FUTEX2_SIZE_MASK; - + const SIZE_MASK = FUTEX2_SIZE_MASK; /// `FUTEX2_NUMA` - const NUMA = linux_raw_sys::general::FUTEX2_NUMA; - + const NUMA = FUTEX2_NUMA; /// `FUTEX2_PRIVATE` - const PRIVATE = linux_raw_sys::general::FUTEX2_PRIVATE; - + const PRIVATE = FUTEX2_PRIVATE; /// const _ = !0; } @@ -85,7 +92,7 @@ pub(crate) enum Operation { } /// `FUTEX_WAITERS` -pub const WAITERS: u32 = linux_raw_sys::general::FUTEX_WAITERS; +pub const WAITERS: u32 = c::FUTEX_WAITERS; /// `FUTEX_OWNER_DIED` -pub const OWNER_DIED: u32 = linux_raw_sys::general::FUTEX_OWNER_DIED; +pub const OWNER_DIED: u32 = c::FUTEX_OWNER_DIED; diff --git a/src/backend/libc/thread/syscalls.rs b/src/backend/libc/thread/syscalls.rs index 5d45b428d..ad27ff91c 100644 --- a/src/backend/libc/thread/syscalls.rs +++ b/src/backend/libc/thread/syscalls.rs @@ -338,13 +338,13 @@ pub(crate) fn setns(fd: BorrowedFd<'_>, nstype: c::c_int) -> io::Result io::Result<()> { ret(c::unshare(flags.bits() as i32)) } -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] #[inline] pub(crate) fn capget( header: &mut linux_raw_sys::general::__user_cap_header_struct, @@ -366,7 +366,7 @@ pub(crate) fn capget( } } -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] #[inline] pub(crate) fn capset( header: &mut linux_raw_sys::general::__user_cap_header_struct, @@ -484,6 +484,7 @@ pub(crate) unsafe fn futex_val2( let timeout = val2 as usize as *const Timespec; #[cfg(all( + linux_raw_dep, target_pointer_width = "32", not(any(target_arch = "aarch64", target_arch = "x86_64")) ))] @@ -513,6 +514,16 @@ pub(crate) unsafe fn futex_val2( )) } + #[cfg(all( + not(linux_raw_dep), + target_pointer_width = "32", + not(any(target_arch = "aarch64", target_arch = "x86_64")) + ))] + { + let _ = (uaddr, op, flags, val, timeout, uaddr2, val3); + Err(io::Errno::NOSYS) + } + #[cfg(any( target_pointer_width = "64", target_arch = "aarch64", @@ -555,6 +566,7 @@ pub(crate) unsafe fn futex_timeout( val3: u32, ) -> io::Result { #[cfg(all( + linux_raw_dep, target_pointer_width = "32", not(any(target_arch = "aarch64", target_arch = "x86_64")) ))] @@ -592,6 +604,16 @@ pub(crate) unsafe fn futex_timeout( }) } + #[cfg(all( + not(linux_raw_dep), + target_pointer_width = "32", + not(any(target_arch = "aarch64", target_arch = "x86_64")) + ))] + { + let _ = (uaddr, op, flags, val, timeout, uaddr2, val3); + Err(io::Errno::NOSYS) + } + #[cfg(any( target_pointer_width = "64", target_arch = "aarch64", @@ -625,6 +647,7 @@ pub(crate) unsafe fn futex_timeout( /// The raw pointers must point to valid aligned memory. #[cfg(linux_kernel)] #[cfg(all( + linux_raw_dep, target_pointer_width = "32", not(any(target_arch = "aarch64", target_arch = "x86_64")) ))] @@ -673,8 +696,9 @@ pub(crate) fn futex_waitv( timeout: Option<&Timespec>, clockid: ClockId, ) -> io::Result { + use crate::backend::c::clockid_t; use futex::Wait as FutexWait; - use linux_raw_sys::general::__kernel_clockid_t as clockid_t; + syscall! { fn futex_waitv( waiters: *const FutexWait, diff --git a/src/fs/statx.rs b/src/fs/statx.rs index b03cfac39..eb73ddab1 100644 --- a/src/fs/statx.rs +++ b/src/fs/statx.rs @@ -4,6 +4,7 @@ use crate::fd::AsFd; use crate::fs::AtFlags; use crate::types::Padding; use crate::{backend, io, path}; +#[cfg(any(linux_raw_dep, not(target_env = "musl")))] use backend::c; use bitflags::bitflags; @@ -276,3 +277,36 @@ mod compat { } } } + +// These are the actual values for the constants, needed for the fallback implementation. +// TODO(https://github.com/bytecodealliance/rustix/issues/1589): try to upstream these +// constants to libc. +#[cfg(all(not(linux_raw_dep), target_env = "musl"))] +mod c { + pub const STATX_TYPE: u32 = 0x00000001; + pub const STATX_MODE: u32 = 0x00000002; + pub const STATX_NLINK: u32 = 0x00000004; + pub const STATX_UID: u32 = 0x00000008; + pub const STATX_GID: u32 = 0x00000010; + pub const STATX_ATIME: u32 = 0x00000020; + pub const STATX_MTIME: u32 = 0x00000040; + pub const STATX_CTIME: u32 = 0x00000080; + pub const STATX_INO: u32 = 0x00000100; + pub const STATX_SIZE: u32 = 0x00000200; + pub const STATX_BLOCKS: u32 = 0x00000400; + pub const STATX_BASIC_STATS: u32 = 0x000007ff; + pub const STATX_BTIME: u32 = 0x00000800; + pub const STATX_MNT_ID: u32 = 0x00001000; + pub const STATX_DIOALIGN: u32 = 0x00002000; // Deprecated, but here for completeness + pub const STATX_ALL: u32 = 0x00000fff; // Note: Doesn't include newer flags + + pub const STATX_ATTR_COMPRESSED: u64 = 0x00000004; + pub const STATX_ATTR_IMMUTABLE: u64 = 0x00000010; + pub const STATX_ATTR_APPEND: u64 = 0x00000020; + pub const STATX_ATTR_NODUMP: u64 = 0x00000040; + pub const STATX_ATTR_ENCRYPTED: u64 = 0x00000800; + pub const STATX_ATTR_AUTOMOUNT: u64 = 0x00001000; + pub const STATX_ATTR_MOUNT_ROOT: u64 = 0x00020000; + pub const STATX_ATTR_VERITY: u64 = 0x00100000; + pub const STATX_ATTR_DAX: u64 = 0x00200000; +} diff --git a/src/ioctl/mod.rs b/src/ioctl/mod.rs index 70c690530..b6d8fe423 100644 --- a/src/ioctl/mod.rs +++ b/src/ioctl/mod.rs @@ -344,6 +344,36 @@ type _Opcode = c::c_uint; #[cfg(windows)] type _Opcode = i32; +/// Convenience macro for Rustix's ioctl write +#[macro_export] +macro_rules! ioctl_write_ptr { + ($name:ident, $ioty:expr, $nr:expr, $ty:ty) => { + pub unsafe fn $name(fd: std::os::fd::BorrowedFd, data: &$ty) -> std::io::Result<()> { + const OPCODE: $crate::ioctl::Opcode = + $crate::ioctl::opcode::write::<$ty>($ioty as u8, $nr as u8); + Ok(rustix::ioctl::ioctl( + fd, + $crate::ioctl::Setter::::new(*data), + )?) + } + }; +} + +/// Convenience macro for Rustix's ioctl read +#[macro_export] +macro_rules! ioctl_readwrite { + ($name:ident, $ioty:expr, $nr:expr, $ty:ty) => { + pub unsafe fn $name(fd: std::os::fd::BorrowedFd, data: &mut $ty) -> std::io::Result<()> { + const OPCODE: $crate::ioctl::Opcode = + $crate::ioctl::opcode::read_write::<$ty>($ioty as u8, $nr as u8); + Ok($crate::ioctl::ioctl( + fd, + $crate::ioctl::Updater::::new(data), + )?) + } + }; +} + #[cfg(linux_raw_dep)] #[cfg(not(any(target_arch = "sparc", target_arch = "sparc64")))] #[cfg(test)] diff --git a/src/thread/mod.rs b/src/thread/mod.rs index 0b34b0e59..c2857802d 100644 --- a/src/thread/mod.rs +++ b/src/thread/mod.rs @@ -5,16 +5,16 @@ mod clock; pub mod futex; #[cfg(linux_kernel)] mod id; -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] mod libcap; #[cfg(linux_kernel)] mod membarrier; -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] mod prctl; #[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))] mod sched; mod sched_yield; -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] mod setns; pub use clock::*; @@ -23,13 +23,14 @@ pub use id::*; #[cfg(linux_kernel)] // #[expect(deprecated, reason = "CapabilityFlags is deprecated")] #[allow(deprecated)] +#[cfg(all(linux_kernel, linux_raw_dep))] pub use libcap::{capabilities, set_capabilities, CapabilityFlags, CapabilitySet, CapabilitySets}; #[cfg(linux_kernel)] pub use membarrier::*; -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] pub use prctl::*; #[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))] pub use sched::*; pub use sched_yield::sched_yield; -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] pub use setns::*; diff --git a/tests/thread/main.rs b/tests/thread/main.rs index 9d9d2b6d5..c312e7961 100644 --- a/tests/thread/main.rs +++ b/tests/thread/main.rs @@ -10,12 +10,12 @@ mod cpu_set; mod futex; #[cfg(linux_kernel)] mod id; -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] mod libcap; #[cfg(linux_kernel)] mod membarrier; -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] mod prctl; mod sched; -#[cfg(linux_kernel)] +#[cfg(all(linux_kernel, linux_raw_dep))] mod setns;