Skip to content

Commit 03d66a0

Browse files
committed
Improve error messages of auxv loading
1 parent 27fcba1 commit 03d66a0

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-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
@@ -118,7 +118,7 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
118118
{
119119
// If calling getauxval fails, try to read the auxiliary vector from
120120
// its file:
121-
auxv_from_file("/proc/self/auxv")
121+
auxv_from_file("/proc/self/auxv").map_err(|_| ())
122122
}
123123
#[cfg(not(feature = "std_detect_file_io"))]
124124
{
@@ -156,17 +156,22 @@ fn getauxval(key: usize) -> Result<usize, ()> {
156156
/// Tries to read the auxiliary vector from the `file`. If this fails, this
157157
/// function returns `Err`.
158158
#[cfg(feature = "std_detect_file_io")]
159-
pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, ()> {
159+
pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, alloc::string::String> {
160160
let file = super::read_file(file)?;
161+
auxv_from_file_bytes(&file)
162+
}
161163

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

172177
auxv_from_buf(&buf)
@@ -175,7 +180,7 @@ pub(super) fn auxv_from_file(file: &str) -> Result<AuxVec, ()> {
175180
/// Tries to interpret the `buffer` as an auxiliary vector. If that fails, this
176181
/// function returns `Err`.
177182
#[cfg(feature = "std_detect_file_io")]
178-
fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> {
183+
fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, alloc::string::String> {
179184
// Targets with only AT_HWCAP:
180185
#[cfg(any(
181186
target_arch = "riscv32",
@@ -220,7 +225,7 @@ fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> {
220225
}
221226
// Suppress unused variable
222227
let _ = buf;
223-
Err(())
228+
Err(alloc::string::String::from("hwcap not found"))
224229
}
225230

226231
#[cfg(test)]

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

Lines changed: 9 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}: {}", *libc::__errno_location()));
1719
}
1820

1921
let mut data = Vec::new();
@@ -23,7 +25,10 @@ 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!(
29+
"Error while reading from file at {orig_path}: {}",
30+
*libc::__errno_location()
31+
));
2732
}
2833
0 => break,
2934
n => data.set_len(data.len() + n as usize),

0 commit comments

Comments
 (0)