Auto merge of #2306 - RalfJung:unix, r=RalfJung

make some things available for all Unixes
This commit is contained in:
bors 2022-07-02 13:45:27 +00:00
commit 428245072e
4 changed files with 46 additions and 41 deletions

View File

@ -62,6 +62,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.open(args)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"close" => {
let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.close(fd)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"fcntl" => {
// `fcntl` is variadic. The argument count is checked based on the first argument
// in `this.fcntl()`, so we do not use `check_shim` here.
@ -112,17 +117,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.rmdir(path)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"opendir" => {
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.opendir(name)?;
this.write_scalar(result, dest)?;
}
"closedir" => {
let [dirp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.closedir(dirp)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"lseek" | "lseek64" => {
"lseek64" => {
let [fd, offset, whence] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.lseek64(fd, offset, whence)?;
// "lseek" is only used on macOS which is 64bit-only, so `i64` always works.
this.write_scalar(Scalar::from_i64(result), dest)?;
}
"ftruncate64" => {
let [fd, length] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.ftruncate64(fd, length)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"fsync" => {
let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.fsync(fd)?;
@ -138,6 +153,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.readlink(pathname, buf, bufsize)?;
this.write_scalar(Scalar::from_machine_isize(result, this), dest)?;
}
"posix_fadvise" => {
let [fd, offset, len, advice] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
this.read_scalar(fd)?.to_i32()?;
this.read_scalar(offset)?.to_machine_isize(this)?;
this.read_scalar(len)?.to_machine_isize(this)?;
this.read_scalar(advice)?.to_i32()?;
// fadvise is only informational, we can ignore it.
this.write_null(dest)?;
}
// Time related shims
"gettimeofday" => {

View File

@ -30,40 +30,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
// File related shims (but also see "syscall" below for statx)
// These symbols have different names on Linux and macOS, which is the only reason they are not
// in the `posix` module.
"close" => {
let [fd] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.close(fd)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"opendir" => {
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.opendir(name)?;
this.write_scalar(result, dest)?;
}
"readdir64" => {
let [dirp] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.linux_readdir64(dirp)?;
this.write_scalar(result, dest)?;
}
"ftruncate64" => {
let [fd, length] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.ftruncate64(fd, length)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
// Linux-only
"posix_fadvise" => {
let [fd, offset, len, advice] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
this.read_scalar(fd)?.to_i32()?;
this.read_scalar(offset)?.to_machine_isize(this)?;
this.read_scalar(len)?.to_machine_isize(this)?;
this.read_scalar(advice)?.to_i32()?;
// fadvise is only informational, we can ignore it.
this.write_null(dest)?;
}
"sync_file_range" => {
let [fd, offset, nbytes, flags] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;

View File

@ -28,7 +28,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
// File related shims
"close" | "close$NOCANCEL" => {
"close$NOCANCEL" => {
let [result] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.close(result)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
@ -50,7 +50,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.macos_fstat(fd, buf)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"opendir" | "opendir$INODE64" => {
"opendir$INODE64" => {
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let result = this.opendir(name)?;
this.write_scalar(result, dest)?;
@ -61,9 +61,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let result = this.macos_readdir_r(dirp, entry, result)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"lseek" => {
let [fd, offset, whence] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
// macOS is 64bit-only, so this is lseek64
let result = this.lseek64(fd, offset, whence)?;
this.write_scalar(Scalar::from_i64(result), dest)?;
}
"ftruncate" => {
let [fd, length] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
// macOS is 64bit-only, so this is ftruncate64
let result = this.ftruncate64(fd, length)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}

View File

@ -42,7 +42,7 @@ fn test_posix_fadvise() {
assert_eq!(result, 0);
}
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
#[cfg(any(target_os = "linux"))]
fn test_sync_file_range() {
use std::fs::{remove_file, File};
use std::io::Write;
@ -208,7 +208,7 @@ fn test_rwlock_libc_static_initializer() {
/// Test whether the `prctl` shim correctly sets the thread name.
///
/// Note: `prctl` exists only on Linux.
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
#[cfg(any(target_os = "linux"))]
fn test_prctl_thread_name() {
use libc::c_long;
use std::ffi::CString;
@ -259,9 +259,9 @@ fn test_prctl_thread_name() {
/// Tests whether each thread has its own `__errno_location`.
fn test_thread_local_errno() {
#[cfg(not(target_os = "macos"))]
#[cfg(target_os = "linux")]
use libc::__errno_location;
#[cfg(target_os = "macos")]
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
use libc::__error as __errno_location;
unsafe {
@ -278,7 +278,7 @@ fn test_thread_local_errno() {
}
/// Tests whether clock support exists at all
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
#[cfg(any(target_os = "linux"))]
fn test_clocks() {
let mut tp = std::mem::MaybeUninit::<libc::timespec>::uninit();
let is_error = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, tp.as_mut_ptr()) };
@ -317,7 +317,7 @@ fn main() {
test_posix_gettimeofday();
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
#[cfg(any(target_os = "linux"))]
test_sync_file_range();
test_mutex_libc_init_recursive();
@ -325,14 +325,14 @@ fn main() {
test_mutex_libc_init_errorcheck();
test_rwlock_libc_static_initializer();
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
#[cfg(any(target_os = "linux"))]
test_mutex_libc_static_initializer_recursive();
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
#[cfg(any(target_os = "linux"))]
test_prctl_thread_name();
test_thread_local_errno();
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
#[cfg(any(target_os = "linux"))]
test_clocks();
}