make set_last_error directly callable on a bunch of ways to represent errors

This commit is contained in:
Ralf Jung 2024-10-01 09:02:33 +02:00
parent 88c74529c1
commit 9c21fd4b93
10 changed files with 88 additions and 80 deletions

View File

@ -150,7 +150,7 @@ pub use crate::range_map::RangeMap;
pub use crate::shims::EmulateItemResult;
pub use crate::shims::env::{EnvVars, EvalContextExt as _};
pub use crate::shims::foreign_items::{DynSym, EvalContextExt as _};
pub use crate::shims::io_error::EvalContextExt as _;
pub use crate::shims::io_error::{EvalContextExt as _, LibcError};
pub use crate::shims::os_str::EvalContextExt as _;
pub use crate::shims::panic::{CatchUnwindData, EvalContextExt as _};
pub use crate::shims::time::EvalContextExt as _;

View File

@ -2,6 +2,34 @@ use std::io;
use crate::*;
/// A representation of an IO error: either a libc error name,
/// or a host error.
#[derive(Debug)]
pub enum IoError {
LibcError(&'static str),
HostError(io::Error),
Raw(Scalar),
}
pub use self::IoError::*;
impl From<io::Error> for IoError {
fn from(value: io::Error) -> Self {
IoError::HostError(value)
}
}
impl From<io::ErrorKind> for IoError {
fn from(value: io::ErrorKind) -> Self {
IoError::HostError(value.into())
}
}
impl From<Scalar> for IoError {
fn from(value: Scalar) -> Self {
IoError::Raw(value)
}
}
// This mapping should match `decode_error_kind` in
// <https://github.com/rust-lang/rust/blob/master/library/std/src/sys/pal/unix/mod.rs>.
const UNIX_IO_ERROR_TABLE: &[(&str, std::io::ErrorKind)] = {
@ -80,10 +108,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}
/// Sets the last error variable.
fn set_last_error(&mut self, scalar: Scalar) -> InterpResult<'tcx> {
fn set_last_error(&mut self, err: impl Into<IoError>) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
let errno = match err.into() {
HostError(err) => this.io_error_to_errnum(err)?,
LibcError(name) => this.eval_libc(name),
Raw(val) => val,
};
let errno_place = this.last_error_place()?;
this.write_scalar(scalar, &errno_place)
this.write_scalar(errno, &errno_place)
}
/// Gets the last error variable.
@ -152,19 +185,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}
}
/// Sets the last OS error using a `std::io::ErrorKind`.
fn set_last_error_from_io_error(&mut self, err: std::io::Error) -> InterpResult<'tcx> {
self.set_last_error(self.io_error_to_errnum(err)?)
}
/// Sets the last OS error using a `std::io::ErrorKind` and writes -1 to dest place.
fn set_last_error_and_return(
&mut self,
err: impl Into<io::Error>,
err: impl Into<IoError>,
dest: &MPlaceTy<'tcx>,
) -> InterpResult<'tcx> {
let this = self.eval_context_mut();
this.set_last_error(this.io_error_to_errnum(err.into())?)?;
this.set_last_error(err)?;
this.write_int(-1, dest)?;
Ok(())
}
@ -182,7 +210,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
match result {
Ok(ok) => Ok(ok),
Err(e) => {
self.eval_context_mut().set_last_error_from_io_error(e)?;
self.eval_context_mut().set_last_error(e)?;
Ok((-1).into())
}
}

View File

@ -177,8 +177,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(Scalar::from_i32(0)) // return zero on success
} else {
// name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
Ok(Scalar::from_i32(-1))
}
}
@ -203,8 +202,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(Scalar::from_i32(0))
} else {
// name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
Ok(Scalar::from_i32(-1))
}
}
@ -218,7 +216,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`getcwd`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Pointer::null());
}
@ -228,10 +226,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
if this.write_path_to_c_str(&cwd, buf, size)?.0 {
return Ok(buf);
}
let erange = this.eval_libc("ERANGE");
this.set_last_error(erange)?;
this.set_last_error(LibcError("ERANGE"))?;
}
Err(e) => this.set_last_error_from_io_error(e)?,
Err(e) => this.set_last_error(e)?,
}
Ok(Pointer::null())
@ -245,7 +242,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`chdir`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_i32(-1));
}

View File

@ -524,7 +524,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Reject if isolation is enabled.
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`fcntl`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_i32(-1));
}
@ -606,8 +606,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
None => fd.read(&fd, communicate, buf, count, dest, this)?,
Some(offset) => {
let Ok(offset) = u64::try_from(offset) else {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
this.write_int(-1, dest)?;
return Ok(());
};
@ -651,8 +650,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
None => fd.write(&fd, communicate, buf, count, dest, this)?,
Some(offset) => {
let Ok(offset) = u64::try_from(offset) else {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
this.write_int(-1, dest)?;
return Ok(());
};

View File

@ -355,8 +355,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// FreeBSD: https://man.freebsd.org/cgi/man.cgi?query=reallocarray
match this.compute_size_in_bytes(Size::from_bytes(size), nmemb) {
None => {
let einval = this.eval_libc("ENOMEM");
this.set_last_error(einval)?;
let enmem = this.eval_libc("ENOMEM");
this.set_last_error(enmem)?;
this.write_null(dest)?;
}
Some(len) => {
@ -646,13 +646,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let chunk_size = CpuAffinityMask::chunk_size(this);
if this.ptr_is_null(mask)? {
let einval = this.eval_libc("EFAULT");
this.set_last_error(einval)?;
let efault = this.eval_libc("EFAULT");
this.set_last_error(efault)?;
this.write_int(-1, dest)?;
} else if cpusetsize == 0 || cpusetsize.checked_rem(chunk_size).unwrap() != 0 {
// we only copy whole chunks of size_of::<c_ulong>()
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
this.write_int(-1, dest)?;
} else if let Some(cpuset) = this.machine.thread_cpu_affinity.get(&thread_id) {
let cpuset = cpuset.clone();
@ -662,8 +661,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.write_null(dest)?;
} else {
// The thread whose ID is pid could not be found
let einval = this.eval_libc("ESRCH");
this.set_last_error(einval)?;
let esrch = this.eval_libc("ESRCH");
this.set_last_error(esrch)?;
this.write_int(-1, dest)?;
}
}
@ -689,8 +688,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
};
if this.ptr_is_null(mask)? {
let einval = this.eval_libc("EFAULT");
this.set_last_error(einval)?;
let efault = this.eval_libc("EFAULT");
this.set_last_error(efault)?;
this.write_int(-1, dest)?;
} else {
// NOTE: cpusetsize might be smaller than `CpuAffinityMask::CPU_MASK_BYTES`.
@ -707,8 +706,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
}
None => {
// The intersection between the mask and the available CPUs was empty.
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
this.write_int(-1, dest)?;
}
}

View File

@ -534,8 +534,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let o_tmpfile = this.eval_libc_i32("O_TMPFILE");
if flag & o_tmpfile == o_tmpfile {
// if the flag contains `O_TMPFILE` then we return a graceful error
let eopnotsupp = this.eval_libc("EOPNOTSUPP");
this.set_last_error(eopnotsupp)?;
this.set_last_error(LibcError("EOPNOTSUPP"))?;
return Ok(Scalar::from_i32(-1));
}
}
@ -572,7 +571,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Reject if isolation is enabled.
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`open`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_i32(-1));
}
@ -591,8 +590,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let seek_from = if whence == this.eval_libc_i32("SEEK_SET") {
if offset < 0 {
// Negative offsets return `EINVAL`.
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
return Ok(Scalar::from_i64(-1));
} else {
SeekFrom::Start(u64::try_from(offset).unwrap())
@ -602,8 +600,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
} else if whence == this.eval_libc_i32("SEEK_END") {
SeekFrom::End(i64::try_from(offset).unwrap())
} else {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
return Ok(Scalar::from_i64(-1));
};
@ -627,7 +624,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Reject if isolation is enabled.
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`unlink`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_i32(-1));
}
@ -658,7 +655,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Reject if isolation is enabled.
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`symlink`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_i32(-1));
}
@ -959,7 +956,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Reject if isolation is enabled.
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`rename`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_i32(-1));
}
@ -983,7 +980,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Reject if isolation is enabled.
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`mkdir`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_i32(-1));
}
@ -1011,7 +1008,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Reject if isolation is enabled.
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`rmdir`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_i32(-1));
}
@ -1045,7 +1042,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(Scalar::from_target_usize(id, this))
}
Err(e) => {
this.set_last_error_from_io_error(e)?;
this.set_last_error(e)?;
Ok(Scalar::null_ptr(this))
}
}
@ -1130,7 +1127,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
None
}
Some(Err(e)) => {
this.set_last_error_from_io_error(e)?;
this.set_last_error(e)?;
None
}
};
@ -1314,15 +1311,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(Scalar::from_i32(result))
} else {
drop(fd);
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
Ok(Scalar::from_i32(-1))
}
} else {
drop(fd);
// The file is not writable
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
Ok(Scalar::from_i32(-1))
}
}
@ -1400,16 +1395,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let flags = this.read_scalar(flags_op)?.to_i32()?;
if offset < 0 || nbytes < 0 {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
return Ok(Scalar::from_i32(-1));
}
let allowed_flags = this.eval_libc_i32("SYNC_FILE_RANGE_WAIT_BEFORE")
| this.eval_libc_i32("SYNC_FILE_RANGE_WRITE")
| this.eval_libc_i32("SYNC_FILE_RANGE_WAIT_AFTER");
if flags & allowed_flags != flags {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
return Ok(Scalar::from_i32(-1));
}
@ -1471,7 +1464,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(path_bytes.len().try_into().unwrap())
}
Err(e) => {
this.set_last_error_from_io_error(e)?;
this.set_last_error(e)?;
Ok(-1)
}
}
@ -1551,7 +1544,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(Scalar::from_maybe_pointer(dest, this))
}
Err(e) => {
this.set_last_error_from_io_error(e)?;
this.set_last_error(e)?;
Ok(Scalar::from_target_usize(0, this))
}
}
@ -1603,8 +1596,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// If we don't find the suffix, it is an error.
if last_six_char_bytes != suffix_bytes {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
return Ok(Scalar::from_i32(-1));
}
@ -1670,7 +1662,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
_ => {
// "On error, -1 is returned, and errno is set to
// indicate the error"
this.set_last_error_from_io_error(e)?;
this.set_last_error(e)?;
return Ok(Scalar::from_i32(-1));
}
},
@ -1749,7 +1741,7 @@ impl FileMetadata {
let metadata = match metadata {
Ok(metadata) => metadata,
Err(e) => {
ecx.set_last_error_from_io_error(e)?;
ecx.set_last_error(e)?;
return Ok(None);
}
};

View File

@ -261,8 +261,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Throw EINVAL if epfd and fd have the same value.
if epfd_value == fd {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
return Ok(Scalar::from_i32(-1));
}
@ -443,8 +442,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let timeout = this.read_scalar(timeout)?.to_i32()?;
if epfd_value <= 0 || maxevents <= 0 {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
this.write_int(-1, dest)?;
return Ok(());
}

View File

@ -75,8 +75,7 @@ pub fn futex<'tcx>(
};
if bitset == 0 {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
this.write_scalar(Scalar::from_target_isize(-1, this), dest)?;
return Ok(());
}
@ -88,8 +87,7 @@ pub fn futex<'tcx>(
let duration = match this.read_timespec(&timeout)? {
Some(duration) => duration,
None => {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
this.write_scalar(Scalar::from_target_isize(-1, this), dest)?;
return Ok(());
}
@ -198,8 +196,7 @@ pub fn futex<'tcx>(
u32::MAX
};
if bitset == 0 {
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
this.set_last_error(LibcError("EINVAL"))?;
this.write_scalar(Scalar::from_target_isize(-1, this), dest)?;
return Ok(());
}

View File

@ -150,7 +150,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`GetCurrentDirectoryW`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(Scalar::from_u32(0));
}
@ -163,7 +163,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.write_path_to_wide_str(&cwd, buf, size)?,
)));
}
Err(e) => this.set_last_error_from_io_error(e)?,
Err(e) => this.set_last_error(e)?,
}
Ok(Scalar::from_u32(0))
}
@ -182,7 +182,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
this.reject_in_isolation("`SetCurrentDirectoryW`", reject_with)?;
this.set_last_error_from_io_error(ErrorKind::PermissionDenied.into())?;
this.set_last_error(ErrorKind::PermissionDenied)?;
return Ok(this.eval_windows("c", "FALSE"));
}
@ -190,7 +190,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
match env::set_current_dir(path) {
Ok(()) => Ok(this.eval_windows("c", "TRUE")),
Err(e) => {
this.set_last_error_from_io_error(e)?;
this.set_last_error(e)?;
Ok(this.eval_windows("c", "FALSE"))
}
}

View File

@ -227,7 +227,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let filename = this.read_path_from_wide_str(filename)?;
let result = match win_absolute(&filename)? {
Err(err) => {
this.set_last_error_from_io_error(err)?;
this.set_last_error(err)?;
Scalar::from_u32(0) // return zero upon failure
}
Ok(abs_filename) => {