diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:44 +0000 |
commit | c23a457e72abe608715ac76f076f47dc42af07a5 (patch) | |
tree | 2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /vendor/rustix/src/backend/linux_raw/param | |
parent | Releasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip |
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustix/src/backend/linux_raw/param')
-rw-r--r-- | vendor/rustix/src/backend/linux_raw/param/auxv.rs | 258 | ||||
-rw-r--r-- | vendor/rustix/src/backend/linux_raw/param/init.rs | 149 | ||||
-rw-r--r-- | vendor/rustix/src/backend/linux_raw/param/libc_auxv.rs | 47 | ||||
-rw-r--r-- | vendor/rustix/src/backend/linux_raw/param/mod.rs | 11 | ||||
-rw-r--r-- | vendor/rustix/src/backend/linux_raw/param/mustang_auxv.rs | 159 |
5 files changed, 317 insertions, 307 deletions
diff --git a/vendor/rustix/src/backend/linux_raw/param/auxv.rs b/vendor/rustix/src/backend/linux_raw/param/auxv.rs index 144ff10cd..8b0423ae4 100644 --- a/vendor/rustix/src/backend/linux_raw/param/auxv.rs +++ b/vendor/rustix/src/backend/linux_raw/param/auxv.rs @@ -6,24 +6,23 @@ #![allow(unsafe_code)] use crate::backend::c; -use crate::backend::elf::*; use crate::fd::OwnedFd; #[cfg(feature = "param")] use crate::ffi::CStr; use crate::fs::{Mode, OFlags}; use crate::utils::{as_ptr, check_raw_pointer}; +#[cfg(feature = "alloc")] use alloc::vec::Vec; -use core::ffi::c_void; use core::mem::size_of; use core::ptr::{null_mut, read_unaligned, NonNull}; -#[cfg(feature = "runtime")] -use core::slice; use core::sync::atomic::Ordering::Relaxed; use core::sync::atomic::{AtomicPtr, AtomicUsize}; +use linux_raw_sys::elf::*; use linux_raw_sys::general::{ - AT_BASE, AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_PHDR, AT_PHENT, - AT_PHNUM, AT_SYSINFO_EHDR, + AT_BASE, AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR, }; +#[cfg(feature = "runtime")] +use linux_raw_sys::general::{AT_ENTRY, AT_PHDR, AT_PHENT, AT_PHNUM}; #[cfg(feature = "param")] #[inline] @@ -83,31 +82,23 @@ pub(crate) fn linux_execfn() -> &'static CStr { #[cfg(feature = "runtime")] #[inline] -pub(crate) fn exe_phdrs() -> (*const c::c_void, usize) { +pub(crate) fn exe_phdrs() -> (*const c::c_void, usize, usize) { let mut phdr = PHDR.load(Relaxed); + let mut phent = PHENT.load(Relaxed); let mut phnum = PHNUM.load(Relaxed); if phdr.is_null() || phnum == 0 { init_auxv(); phdr = PHDR.load(Relaxed); + phent = PHENT.load(Relaxed); phnum = PHNUM.load(Relaxed); } - (phdr.cast(), phnum) + (phdr.cast(), phent, phnum) } -#[cfg(feature = "runtime")] -#[inline] -pub(in super::super) fn exe_phdrs_slice() -> &'static [Elf_Phdr] { - let (phdr, phnum) = exe_phdrs(); - - // SAFETY: We assume the `AT_PHDR` and `AT_PHNUM` values provided by the - // kernel form a valid slice. - unsafe { slice::from_raw_parts(phdr.cast(), phnum) } -} - -/// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, -/// so if we don't see it, this function returns a null pointer. +/// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, so +/// if we don't see it, this function returns a null pointer. #[inline] pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr { let mut ehdr = SYSINFO_EHDR.load(Relaxed); @@ -120,15 +111,35 @@ pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr { ehdr } +#[cfg(feature = "runtime")] +#[inline] +pub(crate) fn entry() -> usize { + let mut entry = ENTRY.load(Relaxed); + + if entry == 0 { + init_auxv(); + entry = ENTRY.load(Relaxed); + } + + entry +} + static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0); static CLOCK_TICKS_PER_SECOND: AtomicUsize = AtomicUsize::new(0); static HWCAP: AtomicUsize = AtomicUsize::new(0); static HWCAP2: AtomicUsize = AtomicUsize::new(0); +static EXECFN: AtomicPtr<c::c_char> = AtomicPtr::new(null_mut()); static SYSINFO_EHDR: AtomicPtr<Elf_Ehdr> = AtomicPtr::new(null_mut()); +#[cfg(feature = "runtime")] static PHDR: AtomicPtr<Elf_Phdr> = AtomicPtr::new(null_mut()); +#[cfg(feature = "runtime")] +static PHENT: AtomicUsize = AtomicUsize::new(0); +#[cfg(feature = "runtime")] static PHNUM: AtomicUsize = AtomicUsize::new(0); -static EXECFN: AtomicPtr<c::c_char> = AtomicPtr::new(null_mut()); +#[cfg(feature = "runtime")] +static ENTRY: AtomicUsize = AtomicUsize::new(0); +#[cfg(feature = "alloc")] fn pr_get_auxv() -> crate::io::Result<Vec<u8>> { use super::super::conv::{c_int, pass_usize, ret_usize}; const PR_GET_AUXV: c::c_int = 0x41555856; @@ -138,7 +149,9 @@ fn pr_get_auxv() -> crate::io::Result<Vec<u8>> { __NR_prctl, c_int(PR_GET_AUXV), buffer.as_ptr(), - pass_usize(buffer.len()) + pass_usize(buffer.len()), + pass_usize(0), + pass_usize(0) ))? }; if len <= buffer.len() { @@ -151,28 +164,33 @@ fn pr_get_auxv() -> crate::io::Result<Vec<u8>> { __NR_prctl, c_int(PR_GET_AUXV), buffer.as_ptr(), - pass_usize(buffer.len()) + pass_usize(buffer.len()), + pass_usize(0), + pass_usize(0) ))? }; assert_eq!(len, buffer.len()); return Ok(buffer); } -/// On non-Mustang platforms, we read the aux vector via the `prctl` -/// `PR_GET_AUXV`, with a fallback to /proc/self/auxv for kernels that don't -/// support `PR_GET_AUXV`. +/// If we don't have "use-explicitly-provided-auxv" or "use-libc-auxv", we +/// read the aux vector via the `prctl` `PR_GET_AUXV`, with a fallback to +/// /proc/self/auxv for kernels that don't support `PR_GET_AUXV`. #[cold] fn init_auxv() { - match pr_get_auxv() { - Ok(buffer) => { - // SAFETY: We assume the kernel returns a valid auxv. - unsafe { - init_from_auxp(buffer.as_ptr().cast()); + #[cfg(feature = "alloc")] + { + match pr_get_auxv() { + Ok(buffer) => { + // SAFETY: We assume the kernel returns a valid auxv. + unsafe { + init_from_aux_iter(AuxPointer(buffer.as_ptr().cast())); + } + return; + } + Err(_) => { + // Fall back to /proc/self/auxv on error. } - return; - } - Err(_) => { - // Fall back to /proc/self/auxv on error. } } @@ -182,10 +200,17 @@ fn init_auxv() { // auxv records. let file = crate::fs::open("/proc/self/auxv", OFlags::RDONLY, Mode::empty()).unwrap(); - let _ = init_from_auxv_file(file); + #[cfg(feature = "alloc")] + init_from_auxv_file(file).unwrap(); + + #[cfg(not(feature = "alloc"))] + unsafe { + init_from_aux_iter(AuxFile(file)).unwrap(); + } } /// Process auxv entries from the open file `auxv`. +#[cfg(feature = "alloc")] #[cold] fn init_from_auxv_file(auxv: OwnedFd) -> Option<()> { let mut buffer = Vec::<u8>::with_capacity(512); @@ -211,7 +236,7 @@ fn init_from_auxv_file(auxv: OwnedFd) -> Option<()> { } // SAFETY: We loaded from an auxv file into the buffer. - unsafe { init_from_auxp(buffer.as_ptr().cast()) } + unsafe { init_from_aux_iter(AuxPointer(buffer.as_ptr().cast())) } } /// Process auxv entries from the auxv array pointed to by `auxp`. @@ -223,37 +248,50 @@ fn init_from_auxv_file(auxv: OwnedFd) -> Option<()> { /// The buffer contains `Elf_aux_t` elements, though it need not be aligned; /// function uses `read_unaligned` to read from it. #[cold] -unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) -> Option<()> { +unsafe fn init_from_aux_iter(aux_iter: impl Iterator<Item = Elf_auxv_t>) -> Option<()> { let mut pagesz = 0; let mut clktck = 0; let mut hwcap = 0; let mut hwcap2 = 0; - let mut phdr = null_mut(); - let mut phnum = 0; let mut execfn = null_mut(); let mut sysinfo_ehdr = null_mut(); + #[cfg(feature = "runtime")] + let mut phdr = null_mut(); + #[cfg(feature = "runtime")] + let mut phnum = 0; + #[cfg(feature = "runtime")] let mut phent = 0; + #[cfg(feature = "runtime")] + let mut entry = 0; - loop { - let Elf_auxv_t { a_type, a_val } = read_unaligned(auxp); - + for Elf_auxv_t { a_type, a_val } in aux_iter { match a_type as _ { AT_PAGESZ => pagesz = a_val as usize, AT_CLKTCK => clktck = a_val as usize, AT_HWCAP => hwcap = a_val as usize, AT_HWCAP2 => hwcap2 = a_val as usize, + AT_EXECFN => execfn = check_raw_pointer::<c::c_char>(a_val as *mut _)?.as_ptr(), + AT_SYSINFO_EHDR => sysinfo_ehdr = check_elf_base(a_val as *mut _)?.as_ptr(), + + AT_BASE => { + let _ = check_elf_base(a_val.cast())?; + } + + #[cfg(feature = "runtime")] AT_PHDR => phdr = check_raw_pointer::<Elf_Phdr>(a_val as *mut _)?.as_ptr(), + #[cfg(feature = "runtime")] AT_PHNUM => phnum = a_val as usize, + #[cfg(feature = "runtime")] AT_PHENT => phent = a_val as usize, - AT_EXECFN => execfn = check_raw_pointer::<c::c_char>(a_val as *mut _)?.as_ptr(), - AT_BASE => check_interpreter_base(a_val.cast())?, - AT_SYSINFO_EHDR => sysinfo_ehdr = check_vdso_base(a_val as *mut _)?.as_ptr(), + #[cfg(feature = "runtime")] + AT_ENTRY => entry = a_val as usize, + AT_NULL => break, _ => (), } - auxp = auxp.add(1); } + #[cfg(feature = "runtime")] assert_eq!(phent, size_of::<Elf_Phdr>()); // The base and sysinfo_ehdr (if present) matches our platform. Accept @@ -262,86 +300,29 @@ unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) -> Option<()> { CLOCK_TICKS_PER_SECOND.store(clktck, Relaxed); HWCAP.store(hwcap, Relaxed); HWCAP2.store(hwcap2, Relaxed); - PHDR.store(phdr, Relaxed); - PHNUM.store(phnum, Relaxed); EXECFN.store(execfn, Relaxed); SYSINFO_EHDR.store(sysinfo_ehdr, Relaxed); + #[cfg(feature = "runtime")] + PHDR.store(phdr, Relaxed); + #[cfg(feature = "runtime")] + PHNUM.store(phnum, Relaxed); + #[cfg(feature = "runtime")] + ENTRY.store(entry, Relaxed); Some(()) } -/// Check that `base` is a valid pointer to the program interpreter. -/// -/// `base` is some value we got from a `AT_BASE` aux record somewhere, -/// which hopefully holds the value of the program interpreter in memory. Do a -/// series of checks to be as sure as we can that it's safe to use. -#[cold] -unsafe fn check_interpreter_base(base: *const Elf_Ehdr) -> Option<()> { - check_elf_base(base)?; - Some(()) -} - /// Check that `base` is a valid pointer to the kernel-provided vDSO. /// /// `base` is some value we got from a `AT_SYSINFO_EHDR` aux record somewhere, /// which hopefully holds the value of the kernel-provided vDSO in memory. Do a /// series of checks to be as sure as we can that it's safe to use. #[cold] -unsafe fn check_vdso_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> { - // In theory, we could check that we're not attempting to parse our own ELF - // image, as an additional check. However, older Linux toolchains don't - // support this, and Rust's `#[linkage = "extern_weak"]` isn't stable yet, - // so just disable this for now. - /* - { - extern "C" { - static __ehdr_start: c::c_void; - } - - let ehdr_start: *const c::c_void = &__ehdr_start; - if base == ehdr_start { - return None; - } - } - */ - - let hdr = check_elf_base(base)?; - - // Check that the ELF is not writable, since that would indicate that this - // isn't the ELF we think it is. Here we're just using `clock_getres` just - // as an arbitrary system call which writes to a buffer and fails with - // `EFAULT` if the buffer is not writable. - { - use crate::backend::conv::{c_uint, ret}; - if ret(syscall!( - __NR_clock_getres, - c_uint(linux_raw_sys::general::CLOCK_MONOTONIC), - base - )) != Err(crate::io::Errno::FAULT) - { - // We can't gracefully fail here because we would seem to have just - // mutated some unknown memory. - #[cfg(feature = "std")] - { - std::process::abort(); - } - #[cfg(all(not(feature = "std"), feature = "rustc-dep-of-std"))] - { - core::intrinsics::abort(); - } - } - } - - Some(hdr) -} - -/// Check that `base` is a valid pointer to an ELF image. -#[cold] unsafe fn check_elf_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> { - // If we're reading a 64-bit auxv on a 32-bit platform, we'll see - // a zero `a_val` because `AT_*` values are never greater than - // `u32::MAX`. Zero is used by libc's `getauxval` to indicate - // errors, so it should never be a valid value. + // If we're reading a 64-bit auxv on a 32-bit platform, we'll see a zero + // `a_val` because `AT_*` values are never greater than `u32::MAX`. Zero is + // used by libc's `getauxval` to indicate errors, so it should never be a + // valid value. if base.is_null() { return None; } @@ -400,15 +381,46 @@ unsafe fn check_elf_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> { Some(NonNull::new_unchecked(as_ptr(hdr) as *mut _)) } -// ELF ABI +// Aux reading utilities + +// Read auxv records from an array in memory. +struct AuxPointer(*const Elf_auxv_t); -#[repr(C)] -#[derive(Copy, Clone)] -struct Elf_auxv_t { - a_type: usize, +impl Iterator for AuxPointer { + type Item = Elf_auxv_t; - // Some of the values in the auxv array are pointers, so we make `a_val` a - // pointer, in order to preserve their provenance. For the values which are - // integers, we cast this to `usize`. - a_val: *const c_void, + #[cold] + fn next(&mut self) -> Option<Self::Item> { + unsafe { + let value = read_unaligned(self.0); + self.0 = self.0.add(1); + Some(value) + } + } +} + +// Read auxv records from a file. +#[cfg(not(feature = "alloc"))] +struct AuxFile(OwnedFd); + +#[cfg(not(feature = "alloc"))] +impl Iterator for AuxFile { + type Item = Elf_auxv_t; + + // This implementation does lots of `read`s and it isn't amazing, but + // hopefully we won't use it often. + #[cold] + fn next(&mut self) -> Option<Self::Item> { + let mut buf = [0_u8; size_of::<Self::Item>()]; + let mut slice = &mut buf[..]; + while !slice.is_empty() { + match crate::io::read(&self.0, slice) { + Ok(0) => panic!("unexpected end of auxv file"), + Ok(n) => slice = &mut slice[n..], + Err(crate::io::Errno::INTR) => continue, + Err(err) => Err(err).unwrap(), + } + } + Some(unsafe { read_unaligned(buf.as_ptr().cast()) }) + } } diff --git a/vendor/rustix/src/backend/linux_raw/param/init.rs b/vendor/rustix/src/backend/linux_raw/param/init.rs new file mode 100644 index 000000000..46aae009a --- /dev/null +++ b/vendor/rustix/src/backend/linux_raw/param/init.rs @@ -0,0 +1,149 @@ +//! Linux auxv `init` function, for "use-explicitly-provided-auxv" mode. +//! +//! # Safety +//! +//! This uses raw pointers to locate and read the kernel-provided auxv array. +#![allow(unsafe_code)] + +use crate::backend::c; +#[cfg(feature = "param")] +use crate::ffi::CStr; +use core::ffi::c_void; +use core::ptr::{null_mut, read, NonNull}; +use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; +use linux_raw_sys::elf::*; +use linux_raw_sys::general::{ + AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR, +}; +#[cfg(feature = "runtime")] +use linux_raw_sys::general::{AT_ENTRY, AT_PHDR, AT_PHENT, AT_PHNUM}; + +#[cfg(feature = "param")] +#[inline] +pub(crate) fn page_size() -> usize { + unsafe { PAGE_SIZE.load(Ordering::Relaxed) } +} + +#[cfg(feature = "param")] +#[inline] +pub(crate) fn clock_ticks_per_second() -> u64 { + unsafe { CLOCK_TICKS_PER_SECOND.load(Ordering::Relaxed) as u64 } +} + +#[cfg(feature = "param")] +#[inline] +pub(crate) fn linux_hwcap() -> (usize, usize) { + unsafe { + ( + HWCAP.load(Ordering::Relaxed), + HWCAP2.load(Ordering::Relaxed), + ) + } +} + +#[cfg(feature = "param")] +#[inline] +pub(crate) fn linux_execfn() -> &'static CStr { + let execfn = unsafe { EXECFN.load(Ordering::Relaxed) }; + + // SAFETY: We initialize `EXECFN` to a valid `CStr` pointer, and we assume + // the `AT_EXECFN` value provided by the kernel points to a valid C string. + unsafe { CStr::from_ptr(execfn.cast()) } +} + +#[cfg(feature = "runtime")] +#[inline] +pub(crate) fn exe_phdrs() -> (*const c_void, usize, usize) { + unsafe { + ( + PHDR.load(Ordering::Relaxed).cast(), + PHENT.load(Ordering::Relaxed), + PHNUM.load(Ordering::Relaxed), + ) + } +} + +/// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, 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 { SYSINFO_EHDR.load(Ordering::Relaxed) } +} + +#[cfg(feature = "runtime")] +#[inline] +pub(crate) fn entry() -> usize { + unsafe { ENTRY.load(Ordering::Relaxed) } +} + +static mut PAGE_SIZE: AtomicUsize = AtomicUsize::new(0); +static mut CLOCK_TICKS_PER_SECOND: AtomicUsize = AtomicUsize::new(0); +static mut HWCAP: AtomicUsize = AtomicUsize::new(0); +static mut HWCAP2: AtomicUsize = AtomicUsize::new(0); +static mut SYSINFO_EHDR: AtomicPtr<Elf_Ehdr> = AtomicPtr::new(null_mut()); +// Initialize `EXECFN` to a valid `CStr` pointer so that we don't need to check +// for null on every `execfn` call. +static mut EXECFN: AtomicPtr<c::c_char> = AtomicPtr::new(b"\0".as_ptr() as _); +// Use `dangling` so that we can always treat it like an empty slice. +#[cfg(feature = "runtime")] +static mut PHDR: AtomicPtr<Elf_Phdr> = AtomicPtr::new(NonNull::dangling().as_ptr()); +#[cfg(feature = "runtime")] +static mut PHENT: AtomicUsize = AtomicUsize::new(0); +#[cfg(feature = "runtime")] +static mut PHNUM: AtomicUsize = AtomicUsize::new(0); +#[cfg(feature = "runtime")] +static mut ENTRY: AtomicUsize = AtomicUsize::new(0); + +/// When "use-explicitly-provided-auxv" is enabled, we export a function to be +/// called during initialization, and passed a pointer to the original +/// environment variable block set up by the OS. +pub(crate) unsafe fn init(envp: *mut *mut u8) { + init_from_envp(envp); +} + +/// # Safety +/// +/// This must be passed a pointer to the environment variable buffer +/// provided by the kernel, which is followed in memory by the auxv array. +unsafe fn init_from_envp(mut envp: *mut *mut u8) { + while !(*envp).is_null() { + envp = envp.add(1); + } + init_from_auxp(envp.add(1).cast()) +} + +/// Process auxv entries from the auxv array pointed to by `auxp`. +/// +/// # Safety +/// +/// This must be passed a pointer to an auxv array. +/// +/// The buffer contains `Elf_aux_t` elements, though it need not be aligned; +/// function uses `read_unaligned` to read from it. +unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) { + loop { + let Elf_auxv_t { a_type, a_val } = read(auxp); + + match a_type as _ { + AT_PAGESZ => PAGE_SIZE.store(a_val as usize, Ordering::Relaxed), + AT_CLKTCK => CLOCK_TICKS_PER_SECOND.store(a_val as usize, Ordering::Relaxed), + AT_HWCAP => HWCAP.store(a_val as usize, Ordering::Relaxed), + AT_HWCAP2 => HWCAP2.store(a_val as usize, Ordering::Relaxed), + AT_EXECFN => EXECFN.store(a_val.cast::<c::c_char>(), Ordering::Relaxed), + AT_SYSINFO_EHDR => SYSINFO_EHDR.store(a_val.cast::<Elf_Ehdr>(), Ordering::Relaxed), + + #[cfg(feature = "runtime")] + AT_PHDR => PHDR.store(a_val.cast::<Elf_Phdr>(), Ordering::Relaxed), + #[cfg(feature = "runtime")] + AT_PHNUM => PHNUM.store(a_val as usize, Ordering::Relaxed), + #[cfg(feature = "runtime")] + AT_PHENT => PHENT.store(a_val as usize, Ordering::Relaxed), + #[cfg(feature = "runtime")] + AT_ENTRY => ENTRY.store(a_val as usize, Ordering::Relaxed), + + AT_NULL => break, + _ => (), + } + auxp = auxp.add(1); + } +} diff --git a/vendor/rustix/src/backend/linux_raw/param/libc_auxv.rs b/vendor/rustix/src/backend/linux_raw/param/libc_auxv.rs index 97739fcb5..0e6ca6ed5 100644 --- a/vendor/rustix/src/backend/linux_raw/param/libc_auxv.rs +++ b/vendor/rustix/src/backend/linux_raw/param/libc_auxv.rs @@ -6,35 +6,39 @@ #![allow(unsafe_code)] use crate::backend::c; -use crate::backend::elf::*; #[cfg(feature = "param")] use crate::ffi::CStr; #[cfg(not(feature = "runtime"))] use core::ptr::null; -#[cfg(feature = "runtime")] -use core::slice; +use linux_raw_sys::elf::*; // `getauxval` wasn't supported in glibc until 2.16. Also this lets us use // `*mut` as the return type to preserve strict provenance. #[cfg(not(feature = "runtime"))] weak!(fn getauxval(c::c_ulong) -> *mut c::c_void); -// With the "runtime" feature, go ahead and depend on `getauxval` existing -// so that we never fail. +// With the "runtime" feature, go ahead and depend on `getauxval` existing so +// that we never fail. #[cfg(feature = "runtime")] extern "C" { fn getauxval(type_: c::c_ulong) -> *mut c::c_void; } +#[cfg(feature = "runtime")] const AT_PHDR: c::c_ulong = 3; +#[cfg(feature = "runtime")] +const AT_PHENT: c::c_ulong = 4; +#[cfg(feature = "runtime")] const AT_PHNUM: c::c_ulong = 5; +#[cfg(feature = "runtime")] +const AT_ENTRY: c::c_ulong = 9; const AT_HWCAP: c::c_ulong = 16; const AT_HWCAP2: c::c_ulong = 26; const AT_EXECFN: c::c_ulong = 31; const AT_SYSINFO_EHDR: c::c_ulong = 33; -// Declare `sysconf` ourselves so that we don't depend on all of libc -// just for this. +// Declare `sysconf` ourselves so that we don't depend on all of libc just for +// this. extern "C" { fn sysconf(name: c::c_int) -> c::c_long; } @@ -52,12 +56,16 @@ const _SC_CLK_TCK: c::c_int = 2; fn test_abi() { const_assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE); const_assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK); - const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR); - const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM); const_assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP); const_assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2); const_assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN); const_assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR); + #[cfg(feature = "runtime")] + const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR); + #[cfg(feature = "runtime")] + const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM); + #[cfg(feature = "runtime")] + const_assert_eq!(self::AT_ENTRY, ::libc::AT_ENTRY); } #[cfg(feature = "param")] @@ -114,24 +122,15 @@ pub(crate) fn linux_execfn() -> &'static CStr { #[cfg(feature = "runtime")] #[inline] -pub(crate) fn exe_phdrs() -> (*const c::c_void, usize) { +pub(crate) fn exe_phdrs() -> (*const c::c_void, usize, usize) { unsafe { let phdr = getauxval(AT_PHDR) as *const c::c_void; + let phent = getauxval(AT_PHENT) as usize; let phnum = getauxval(AT_PHNUM) as usize; - (phdr, phnum) + (phdr, phent, phnum) } } -#[cfg(feature = "runtime")] -#[inline] -pub(in super::super) fn exe_phdrs_slice() -> &'static [Elf_Phdr] { - let (phdr, phnum) = exe_phdrs(); - - // SAFETY: We assume the `AT_PHDR` and `AT_PHNUM` values provided by the - // kernel form a valid slice. - unsafe { slice::from_raw_parts(phdr.cast(), phnum) } -} - /// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, /// so if we don't see it, this function returns a null pointer. #[inline] @@ -150,3 +149,9 @@ pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr { getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr } } + +#[cfg(feature = "runtime")] +#[inline] +pub(crate) fn entry() -> usize { + unsafe { getauxval(AT_ENTRY) as usize } +} diff --git a/vendor/rustix/src/backend/linux_raw/param/mod.rs b/vendor/rustix/src/backend/linux_raw/param/mod.rs index 956282074..365f01604 100644 --- a/vendor/rustix/src/backend/linux_raw/param/mod.rs +++ b/vendor/rustix/src/backend/linux_raw/param/mod.rs @@ -1,12 +1,15 @@ -// On Mustang, origin is in control of program startup and can access the -// incoming aux values on the stack. +// With "use-explicitly-provided-auxv" enabled, we expect to be initialized +// with an explicit `rustix::param::init` call. // // With "use-libc-auxv" enabled, use libc's `getauxval`. // // Otherwise, we read aux values from /proc/self/auxv. -#[cfg_attr(target_vendor = "mustang", path = "mustang_auxv.rs")] +#[cfg_attr(feature = "use-explicitly-provided-auxv", path = "init.rs")] #[cfg_attr( - all(not(target_vendor = "mustang"), feature = "use-libc-auxv"), + all( + not(feature = "use-explicitly-provided-auxv"), + feature = "use-libc-auxv" + ), path = "libc_auxv.rs" )] pub(crate) mod auxv; diff --git a/vendor/rustix/src/backend/linux_raw/param/mustang_auxv.rs b/vendor/rustix/src/backend/linux_raw/param/mustang_auxv.rs deleted file mode 100644 index f45a25faf..000000000 --- a/vendor/rustix/src/backend/linux_raw/param/mustang_auxv.rs +++ /dev/null @@ -1,159 +0,0 @@ -//! Linux auxv support, for Mustang. -//! -//! # Safety -//! -//! This uses raw pointers to locate and read the kernel-provided auxv array. -#![allow(unsafe_code)] - -use crate::backend::c; -use crate::backend::elf::*; -#[cfg(feature = "param")] -use crate::ffi::CStr; -use core::ffi::c_void; -use core::mem::size_of; -use core::ptr::{null, read}; -#[cfg(feature = "runtime")] -use core::slice; -use linux_raw_sys::general::{ - AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_PHDR, AT_PHENT, AT_PHNUM, - AT_SYSINFO_EHDR, -}; - -#[cfg(feature = "param")] -#[inline] -pub(crate) fn page_size() -> usize { - // SAFETY: This is initialized during program startup. - unsafe { PAGE_SIZE } -} - -#[cfg(feature = "param")] -#[inline] -pub(crate) fn clock_ticks_per_second() -> u64 { - // SAFETY: This is initialized during program startup. - unsafe { CLOCK_TICKS_PER_SECOND as u64 } -} - -#[cfg(feature = "param")] -#[inline] -pub(crate) fn linux_hwcap() -> (usize, usize) { - // SAFETY: This is initialized during program startup. - unsafe { (HWCAP, HWCAP2) } -} - -#[cfg(feature = "param")] -#[inline] -pub(crate) fn linux_execfn() -> &'static CStr { - // SAFETY: This is initialized during program startup. And we - // assume it's a valid pointer to a NUL-terminated string. - unsafe { CStr::from_ptr(EXECFN.0.cast()) } -} - -#[cfg(feature = "runtime")] -#[inline] -pub(crate) fn exe_phdrs() -> (*const c_void, usize) { - // SAFETY: This is initialized during program startup. - unsafe { (PHDR.0.cast(), PHNUM) } -} - -#[cfg(feature = "runtime")] -#[inline] -pub(in super::super) fn exe_phdrs_slice() -> &'static [Elf_Phdr] { - let (phdr, phnum) = exe_phdrs(); - - // SAFETY: We assume the `AT_PHDR` and `AT_PHNUM` values provided by the - // kernel form a valid slice. - unsafe { slice::from_raw_parts(phdr.cast(), phnum) } -} - -/// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, -/// so if we don't see it, this function returns a null pointer. -#[inline] -pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr { - // SAFETY: This is initialized during program startup. - unsafe { SYSINFO_EHDR.0 } -} - -/// A const pointer to `T` that implements [`Sync`]. -#[repr(transparent)] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct SyncConstPtr<T>(*const T); -unsafe impl<T> Sync for SyncConstPtr<T> {} - -impl<T> SyncConstPtr<T> { - /// Creates a `SyncConstPointer` from a raw pointer. - /// - /// Behavior is undefined if `ptr` is actually not - /// safe to share across threads. - pub const unsafe fn new(ptr: *const T) -> Self { - Self(ptr) - } -} - -static mut PAGE_SIZE: usize = 0; -static mut CLOCK_TICKS_PER_SECOND: usize = 0; -static mut HWCAP: usize = 0; -static mut HWCAP2: usize = 0; -static mut SYSINFO_EHDR: SyncConstPtr<Elf_Ehdr> = unsafe { SyncConstPtr::new(null()) }; -static mut PHDR: SyncConstPtr<Elf_Phdr> = unsafe { SyncConstPtr::new(null()) }; -static mut PHNUM: usize = 0; -static mut EXECFN: SyncConstPtr<c::c_char> = unsafe { SyncConstPtr::new(null()) }; - -/// On mustang, we export a function to be called during initialization, and -/// passed a pointer to the original environment variable block set up by the -/// OS. -pub(crate) unsafe fn init(envp: *mut *mut u8) { - init_from_envp(envp); -} - -/// # Safety -/// -/// This must be passed a pointer to the environment variable buffer -/// provided by the kernel, which is followed in memory by the auxv array. -unsafe fn init_from_envp(mut envp: *mut *mut u8) { - while !(*envp).is_null() { - envp = envp.add(1); - } - init_from_auxp(envp.add(1).cast()) -} - -/// Process auxv entries from the auxv array pointed to by `auxp`. -/// -/// # Safety -/// -/// This must be passed a pointer to an auxv array. -/// -/// The buffer contains `Elf_aux_t` elements, though it need not be aligned; -/// function uses `read_unaligned` to read from it. -unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) { - loop { - let Elf_auxv_t { a_type, a_val } = read(auxp); - - match a_type as _ { - AT_PAGESZ => PAGE_SIZE = a_val as usize, - AT_CLKTCK => CLOCK_TICKS_PER_SECOND = a_val as usize, - AT_HWCAP => HWCAP = a_val as usize, - AT_HWCAP2 => HWCAP2 = a_val as usize, - AT_PHDR => PHDR = SyncConstPtr::new(a_val.cast::<Elf_Phdr>()), - AT_PHNUM => PHNUM = a_val as usize, - AT_PHENT => assert_eq!(a_val as usize, size_of::<Elf_Phdr>()), - AT_EXECFN => EXECFN = SyncConstPtr::new(a_val.cast::<c::c_char>()), - AT_SYSINFO_EHDR => SYSINFO_EHDR = SyncConstPtr::new(a_val.cast::<Elf_Ehdr>()), - AT_NULL => break, - _ => (), - } - auxp = auxp.add(1); - } -} - -// ELF ABI - -#[repr(C)] -#[derive(Copy, Clone)] -struct Elf_auxv_t { - a_type: usize, - - // Some of the values in the auxv array are pointers, so we make `a_val` a - // pointer, in order to preserve their provenance. For the values which are - // integers, we cast this to `usize`. - a_val: *const c_void, -} |