Skip to content

Backport fixes to 0.36 #557

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

Merged
merged 5 commits into from
Mar 4, 2023
Merged
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
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
task:
name: stable x86_64-unknown-freebsd-13
freebsd_instance:
image_family: freebsd-13-0-snap
image_family: freebsd-13-1
setup_script:
- pkg install -y curl
- curl https://sh.rustup.rs -sSf --output rustup.sh
Expand Down
12 changes: 12 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ 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");

// Rerun this script if any of our features or configuration flags change,
// or if the toolchain we used for feature detection changes.
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_USE_LIBC");
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_RUSTC_DEP_OF_STD");
println!("cargo:rerun-if-env-changed=CARGO_CFG_MIRI");
println!("cargo:rerun-if-env-changed=CARGO_ENCODED_RUSTFLAGS");
println!("cargo:rerun-if-env-changed=RUSTC");
println!("cargo:rerun-if-env-changed=TARGET");
println!("cargo:rerun-if-env-changed=CARGO_RUSTC_WRAPPER");
println!("cargo:rerun-if-env-changed=PROFILE");
}

/// Link in the desired version of librustix_outline_{arch}.a, containing the
Expand Down
123 changes: 109 additions & 14 deletions src/backend/libc/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,21 @@ pub(super) use c::posix_fadvise64 as libc_posix_fadvise;
pub(super) use c::{pread as libc_pread, pwrite as libc_pwrite};
#[cfg(any(target_os = "android", target_os = "linux", target_os = "emscripten"))]
pub(super) use c::{pread64 as libc_pread, pwrite64 as libc_pwrite};
#[cfg(not(any(
windows,
target_os = "android",
target_os = "emscripten",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "redox",
target_os = "solaris",
)))]
pub(super) use c::{preadv as libc_preadv, pwritev as libc_pwritev};
#[cfg(any(target_os = "linux", target_os = "emscripten"))]
pub(super) use c::{preadv64 as libc_preadv, pwritev64 as libc_pwritev};

#[cfg(target_os = "android")]
mod readwrite_pv64 {
use super::c;
Expand Down Expand Up @@ -302,20 +315,9 @@ mod readwrite_pv64 {
}
}
}
#[cfg(not(any(
windows,
target_os = "android",
target_os = "emscripten",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "redox",
target_os = "solaris",
)))]
pub(super) use c::{preadv as libc_preadv, pwritev as libc_pwritev};
#[cfg(target_os = "android")]
pub(super) use readwrite_pv64::{preadv64 as libc_preadv, pwritev64 as libc_pwritev};

// macOS added preadv and pwritev in version 11.0
#[cfg(any(target_os = "ios", target_os = "macos"))]
mod readwrite_pv {
Expand All @@ -337,11 +339,104 @@ mod readwrite_pv {
) -> c::ssize_t
}
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pub(super) use c::{preadv64v2 as libc_preadv2, pwritev64v2 as libc_pwritev2};
#[cfg(any(target_os = "ios", target_os = "macos"))]
pub(super) use readwrite_pv::{preadv as libc_preadv, pwritev as libc_pwritev};

// GLIBC added `preadv64v2` and `pwritev64v2` in version 2.26.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
mod readwrite_pv64v2 {
use super::c;

// 64-bit offsets on 32-bit platforms are passed in endianness-specific
// lo/hi pairs. See src/backend/linux_raw/conv.rs for details.
#[cfg(all(target_endian = "little", target_pointer_width = "32"))]
fn lo(x: u64) -> usize {
(x >> 32) as usize
}
#[cfg(all(target_endian = "little", target_pointer_width = "32"))]
fn hi(x: u64) -> usize {
(x & 0xffff_ffff) as usize
}
#[cfg(all(target_endian = "big", target_pointer_width = "32"))]
fn lo(x: u64) -> usize {
(x & 0xffff_ffff) as usize
}
#[cfg(all(target_endian = "big", target_pointer_width = "32"))]
fn hi(x: u64) -> usize {
(x >> 32) as usize
}

pub(in super::super) unsafe fn preadv64v2(
fd: c::c_int,
iov: *const c::iovec,
iovcnt: c::c_int,
offset: c::off64_t,
flags: c::c_int,
) -> c::ssize_t {
// Older GLIBC lacks `preadv64v2`, so use the `weak!` mechanism to
// test for it, and call back to `c::syscall`. We don't use
// `weak_or_syscall` here because we need to pass the 64-bit offset
// specially.
weak! {
fn preadv64v2(c::c_int, *const c::iovec, c::c_int, c::off64_t, c::c_int) -> c::ssize_t
}
if let Some(fun) = preadv64v2.get() {
fun(fd, iov, iovcnt, offset, flags)
} else {
#[cfg(target_pointer_width = "32")]
{
c::syscall(
c::SYS_preadv,
fd,
iov,
iovcnt,
hi(offset as u64),
lo(offset as u64),
flags,
) as c::ssize_t
}
#[cfg(target_pointer_width = "64")]
{
c::syscall(c::SYS_preadv2, fd, iov, iovcnt, offset, flags) as c::ssize_t
}
}
}
pub(in super::super) unsafe fn pwritev64v2(
fd: c::c_int,
iov: *const c::iovec,
iovcnt: c::c_int,
offset: c::off64_t,
flags: c::c_int,
) -> c::ssize_t {
// See the comments in `preadv64v2`.
weak! {
fn pwritev64v2(c::c_int, *const c::iovec, c::c_int, c::off64_t, c::c_int) -> c::ssize_t
}
if let Some(fun) = pwritev64v2.get() {
fun(fd, iov, iovcnt, offset, flags)
} else {
#[cfg(target_pointer_width = "32")]
{
c::syscall(
c::SYS_pwritev,
fd,
iov,
iovcnt,
hi(offset as u64),
lo(offset as u64),
flags,
) as c::ssize_t
}
#[cfg(target_pointer_width = "64")]
{
c::syscall(c::SYS_pwritev2, fd, iov, iovcnt, offset, flags) as c::ssize_t
}
}
}
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pub(super) use readwrite_pv64v2::{preadv64v2 as libc_preadv2, pwritev64v2 as libc_pwritev2};

#[cfg(not(any(
windows,
target_os = "aix",
Expand Down
8 changes: 7 additions & 1 deletion src/backend/libc/thread/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,13 @@ pub(crate) fn gettid() -> Pid {
#[cfg(any(target_os = "android", target_os = "linux"))]
#[inline]
pub(crate) fn setns(fd: BorrowedFd, nstype: c::c_int) -> io::Result<c::c_int> {
unsafe { ret_c_int(c::setns(borrowed_fd(fd), nstype)) }
// `setns` wasn't supported in glibc until 2.14, and musl until 0.9.5,
// so use `syscall`.
weak_or_syscall! {
fn setns(fd: c::c_int, nstype: c::c_int) via SYS_setns -> c::c_int
}

unsafe { ret_c_int(setns(borrowed_fd(fd), nstype)) }
}

#[cfg(any(target_os = "android", target_os = "linux"))]
Expand Down
5 changes: 5 additions & 0 deletions src/backend/linux_raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
//! such as which pointers are array slices, out parameters, or in-out
//! parameters, which integers are owned or borrowed file descriptors, etc.

// Weak symbols used by the use-libc-auxv feature for glibc 2.15 support.
#[cfg(feature = "use-libc-auxv")]
#[macro_use]
mod weak;

#[macro_use]
mod arch;
mod conv;
Expand Down
37 changes: 23 additions & 14 deletions src/backend/linux_raw/param/libc_auxv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
//! This uses raw pointers to locate and read the kernel-provided auxv array.
#![allow(unsafe_code)]

#[cfg(any(feature = "param", feature = "runtime"))]
use super::super::c;
use super::super::elf::*;
#[cfg(feature = "param")]
use crate::ffi::CStr;
#[cfg(feature = "runtime")]
use core::slice;

// `getauxval` wasn't supported in glibc until 2.16.
weak!(fn getauxval(libc::c_ulong) -> *mut libc::c_void);

#[cfg(feature = "param")]
#[inline]
pub(crate) fn page_size() -> usize {
Expand All @@ -22,35 +23,39 @@ pub(crate) fn page_size() -> usize {
#[cfg(feature = "param")]
#[inline]
pub(crate) fn clock_ticks_per_second() -> u64 {
unsafe { libc::getauxval(libc::AT_CLKTCK) as u64 }
unsafe { libc::sysconf(libc::_SC_CLK_TCK) as u64 }
}

#[cfg(feature = "param")]
#[inline]
pub(crate) fn linux_hwcap() -> (usize, usize) {
unsafe {
(
libc::getauxval(libc::AT_HWCAP) as usize,
libc::getauxval(libc::AT_HWCAP2) as usize,
)
if let Some(libc_getauxval) = getauxval.get() {
unsafe {
let hwcap = libc_getauxval(libc::AT_HWCAP) as usize;
let hwcap2 = libc_getauxval(libc::AT_HWCAP2) as usize;
(hwcap, hwcap2)
}
} else {
(0, 0)
}
}

#[cfg(feature = "param")]
#[inline]
pub(crate) fn linux_execfn() -> &'static CStr {
unsafe {
let execfn = libc::getauxval(libc::AT_EXECFN) as *const c::c_char;
CStr::from_ptr(execfn.cast())
if let Some(libc_getauxval) = getauxval.get() {
unsafe { CStr::from_ptr(libc_getauxval(libc::AT_EXECFN).cast()) }
} else {
cstr!("")
}
}

#[cfg(feature = "runtime")]
#[inline]
pub(crate) fn exe_phdrs() -> (*const c::c_void, usize) {
pub(crate) fn exe_phdrs() -> (*const libc::c_void, usize) {
unsafe {
(
libc::getauxval(libc::AT_PHDR) as *const c::c_void,
libc::getauxval(libc::AT_PHDR) as *const libc::c_void,
libc::getauxval(libc::AT_PHNUM) as usize,
)
}
Expand All @@ -70,5 +75,9 @@ pub(in super::super) fn exe_phdrs_slice() -> &'static [Elf_Phdr] {
/// so if we don't see it, this function returns a null pointer.
#[inline]
pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr {
unsafe { libc::getauxval(linux_raw_sys::general::AT_SYSINFO_EHDR.into()) as *const Elf_Ehdr }
if let Some(libc_getauxval) = getauxval.get() {
unsafe { libc_getauxval(linux_raw_sys::general::AT_SYSINFO_EHDR.into()) as *const Elf_Ehdr }
} else {
core::ptr::null()
}
}
Loading