Skip to content

Commit 81d90d8

Browse files
committed
Improve error messages of auxv loading
1 parent 5e52677 commit 81d90d8

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

library/std_detect/src/detect/os/linux/auxvec.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
119119
{
120120
// If calling getauxval fails, try to read the auxiliary vector from
121121
// its file:
122-
auxv_from_file("/proc/self/auxv")
122+
auxv_from_file("/proc/self/auxv").map_err(|_| ())
123123
}
124124
#[cfg(not(feature = "std_detect_file_io"))]
125125
{
@@ -157,17 +157,22 @@ fn getauxval(key: usize) -> Result<usize, ()> {
157157
/// Tries to read the auxiliary vector from the `file`. If this fails, this
158158
/// function returns `Err`.
159159
#[cfg(feature = "std_detect_file_io")]
160-
pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, ()> {
160+
pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, alloc::string::String> {
161161
let file = super::read_file(file)?;
162+
auxv_from_file_bytes(&file)
163+
}
162164

165+
/// Read auxiliary vector from a slice of bytes.
166+
#[cfg(feature = "std_detect_file_io")]
167+
pub(super) fn auxv_from_file_bytes(bytes: &[u8]) -> Result<AuxVec, alloc::string::String> {
163168
// See <https://github.com/torvalds/linux/blob/v5.15/include/uapi/linux/auxvec.h>.
164169
//
165170
// The auxiliary vector contains at most 34 (key,value) fields: from
166171
// `AT_MINSIGSTKSZ` to `AT_NULL`, but its number may increase.
167-
let len = file.len();
172+
let len = bytes.len();
168173
let mut buf = alloc::vec![0_usize; 1 + len / core::mem::size_of::<usize>()];
169174
unsafe {
170-
core::ptr::copy_nonoverlapping(file.as_ptr(), buf.as_mut_ptr() as *mut u8, len);
175+
core::ptr::copy_nonoverlapping(bytes.as_ptr(), buf.as_mut_ptr() as *mut u8, len);
171176
}
172177

173178
auxv_from_buf(&buf)
@@ -176,7 +181,7 @@ pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, ()> {
176181
/// Tries to interpret the `buffer` as an auxiliary vector. If that fails, this
177182
/// function returns `Err`.
178183
#[cfg(feature = "std_detect_file_io")]
179-
fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> {
184+
fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, alloc::string::String> {
180185
// Targets with only AT_HWCAP:
181186
#[cfg(any(
182187
target_arch = "riscv32",
@@ -222,7 +227,7 @@ fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> {
222227
}
223228
// Suppress unused variable
224229
let _ = buf;
225-
Err(())
230+
Err(alloc::string::String::from("hwcap not found"))
226231
}
227232

228233
#[cfg(test)]

library/std_detect/src/detect/os/linux/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ use alloc::vec::Vec;
66
mod auxvec;
77

88
#[cfg(feature = "std_detect_file_io")]
9-
fn read_file(path: &str) -> Result<Vec<u8>, ()> {
10-
let mut path = Vec::from(path.as_bytes());
9+
fn read_file(orig_path: &str) -> Result<Vec<u8>, alloc::string::String> {
10+
use alloc::format;
11+
12+
let mut path = Vec::from(orig_path.as_bytes());
1113
path.push(0);
1214

1315
unsafe {
1416
let file = libc::open(path.as_ptr() as *const libc::c_char, libc::O_RDONLY);
1517
if file == -1 {
16-
return Err(());
18+
return Err(format!("Cannot open file at {orig_path}"));
1719
}
1820

1921
let mut data = Vec::new();
@@ -23,7 +25,7 @@ fn read_file(path: &str) -> Result<Vec<u8>, ()> {
2325
match libc::read(file, spare.as_mut_ptr() as *mut _, spare.len()) {
2426
-1 => {
2527
libc::close(file);
26-
return Err(());
28+
return Err(format!("Error while reading from file at {orig_path}"));
2729
}
2830
0 => break,
2931
n => data.set_len(data.len() + n as usize),

0 commit comments

Comments
 (0)