Improve error messages of auxv loading

This commit is contained in:
Jakub Beránek
2025-07-18 08:49:28 +02:00
parent 5e52677a82
commit 81d90d8257
2 changed files with 17 additions and 10 deletions

View File

@@ -119,7 +119,7 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
{ {
// If calling getauxval fails, try to read the auxiliary vector from // If calling getauxval fails, try to read the auxiliary vector from
// its file: // its file:
auxv_from_file("/proc/self/auxv") auxv_from_file("/proc/self/auxv").map_err(|_| ())
} }
#[cfg(not(feature = "std_detect_file_io"))] #[cfg(not(feature = "std_detect_file_io"))]
{ {
@@ -157,17 +157,22 @@ fn getauxval(key: usize) -> Result<usize, ()> {
/// Tries to read the auxiliary vector from the `file`. If this fails, this /// Tries to read the auxiliary vector from the `file`. If this fails, this
/// function returns `Err`. /// function returns `Err`.
#[cfg(feature = "std_detect_file_io")] #[cfg(feature = "std_detect_file_io")]
pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, ()> { pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, alloc::string::String> {
let file = super::read_file(file)?; let file = super::read_file(file)?;
auxv_from_file_bytes(&file)
}
/// Read auxiliary vector from a slice of bytes.
#[cfg(feature = "std_detect_file_io")]
pub(super) fn auxv_from_file_bytes(bytes: &[u8]) -> Result<AuxVec, alloc::string::String> {
// See <https://github.com/torvalds/linux/blob/v5.15/include/uapi/linux/auxvec.h>. // See <https://github.com/torvalds/linux/blob/v5.15/include/uapi/linux/auxvec.h>.
// //
// The auxiliary vector contains at most 34 (key,value) fields: from // The auxiliary vector contains at most 34 (key,value) fields: from
// `AT_MINSIGSTKSZ` to `AT_NULL`, but its number may increase. // `AT_MINSIGSTKSZ` to `AT_NULL`, but its number may increase.
let len = file.len(); let len = bytes.len();
let mut buf = alloc::vec![0_usize; 1 + len / core::mem::size_of::<usize>()]; let mut buf = alloc::vec![0_usize; 1 + len / core::mem::size_of::<usize>()];
unsafe { unsafe {
core::ptr::copy_nonoverlapping(file.as_ptr(), buf.as_mut_ptr() as *mut u8, len); core::ptr::copy_nonoverlapping(bytes.as_ptr(), buf.as_mut_ptr() as *mut u8, len);
} }
auxv_from_buf(&buf) auxv_from_buf(&buf)
@@ -176,7 +181,7 @@ pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, ()> {
/// Tries to interpret the `buffer` as an auxiliary vector. If that fails, this /// Tries to interpret the `buffer` as an auxiliary vector. If that fails, this
/// function returns `Err`. /// function returns `Err`.
#[cfg(feature = "std_detect_file_io")] #[cfg(feature = "std_detect_file_io")]
fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> { fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, alloc::string::String> {
// Targets with only AT_HWCAP: // Targets with only AT_HWCAP:
#[cfg(any( #[cfg(any(
target_arch = "riscv32", target_arch = "riscv32",
@@ -222,7 +227,7 @@ fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> {
} }
// Suppress unused variable // Suppress unused variable
let _ = buf; let _ = buf;
Err(()) Err(alloc::string::String::from("hwcap not found"))
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -6,14 +6,16 @@ use alloc::vec::Vec;
mod auxvec; mod auxvec;
#[cfg(feature = "std_detect_file_io")] #[cfg(feature = "std_detect_file_io")]
fn read_file(path: &str) -> Result<Vec<u8>, ()> { fn read_file(orig_path: &str) -> Result<Vec<u8>, alloc::string::String> {
let mut path = Vec::from(path.as_bytes()); use alloc::format;
let mut path = Vec::from(orig_path.as_bytes());
path.push(0); path.push(0);
unsafe { unsafe {
let file = libc::open(path.as_ptr() as *const libc::c_char, libc::O_RDONLY); let file = libc::open(path.as_ptr() as *const libc::c_char, libc::O_RDONLY);
if file == -1 { if file == -1 {
return Err(()); return Err(format!("Cannot open file at {orig_path}"));
} }
let mut data = Vec::new(); let mut data = Vec::new();
@@ -23,7 +25,7 @@ fn read_file(path: &str) -> Result<Vec<u8>, ()> {
match libc::read(file, spare.as_mut_ptr() as *mut _, spare.len()) { match libc::read(file, spare.as_mut_ptr() as *mut _, spare.len()) {
-1 => { -1 => {
libc::close(file); libc::close(file);
return Err(()); return Err(format!("Error while reading from file at {orig_path}"));
} }
0 => break, 0 => break,
n => data.set_len(data.len() + n as usize), n => data.set_len(data.len() + n as usize),