Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flock shim #3759

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ default-run = "miri"
edition = "2021"

[lib]
test = true # we have unit tests
test = true # we have unit tests
doctest = false # but no doc tests

[[bin]]
name = "miri"
test = false # we have no unit tests
test = false # we have no unit tests
doctest = false # and no doc tests

[dependencies]
Expand Down Expand Up @@ -42,6 +42,13 @@ libc = "0.2"
libffi = "3.2.0"
libloading = "0.8"

[target.'cfg(target_family = "windows")'.dependencies]
windows-sys = { version = "0.52", features = [
"Win32_Foundation",
"Win32_System_IO",
"Win32_Storage_FileSystem",
] }

[dev-dependencies]
colored = "2"
ui_test = "0.21.1"
Expand Down
49 changes: 49 additions & 0 deletions src/shims/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ pub trait FileDescription: std::fmt::Debug + Any {
throw_unsup_format!("cannot close {}", self.name());
}

fn flock<'tcx>(
&self,
_communicate_allowed: bool,
_op: FlockOp,
) -> InterpResult<'tcx, io::Result<()>> {
throw_unsup_format!("cannot flock {}", self.name());
}

fn is_tty(&self, _communicate_allowed: bool) -> bool {
// Most FDs are not tty's and the consequence of a wrong `false` are minor,
// so we use a default impl here.
Expand Down Expand Up @@ -324,6 +332,40 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
Ok(new_fd)
}

fn flock(&mut self, fd: i32, op: i32) -> InterpResult<'tcx, Scalar> {
let this = self.eval_context_mut();
let Some(file_descriptor) = this.machine.fds.get(fd) else {
return Ok(Scalar::from_i32(this.fd_not_found()?));
};

// We need to check that there aren't unsupported options in `op`.
let lock_sh = this.eval_libc_i32("LOCK_SH");
let lock_ex = this.eval_libc_i32("LOCK_EX");
let lock_nb = this.eval_libc_i32("LOCK_NB");
let lock_un = this.eval_libc_i32("LOCK_UN");

use FlockOp::*;
let parsed_op = if op == lock_sh {
SharedLock { nonblocking: false }
} else if op == lock_sh | lock_nb {
SharedLock { nonblocking: true }
} else if op == lock_ex {
ExclusiveLock { nonblocking: false }
} else if op == lock_ex | lock_nb {
ExclusiveLock { nonblocking: true }
} else if op == lock_un {
Unlock
} else {
throw_unsup_format!("unsupported flags {:#x}", op);
};

let result = file_descriptor.flock(this.machine.communicate(), parsed_op)?;
drop(file_descriptor);
// return `0` if flock is successful
let result = result.map(|()| 0i32);
Ok(Scalar::from_i32(this.try_unwrap_io_result(result)?))
}

fn fcntl(&mut self, args: &[OpTy<'tcx>]) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

Expand Down Expand Up @@ -520,3 +562,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.try_unwrap_io_result(result)
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub(crate) enum FlockOp {
SharedLock { nonblocking: bool },
ExclusiveLock { nonblocking: bool },
Unlock,
}
7 changes: 7 additions & 0 deletions src/shims/unix/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let result = this.dup2(old_fd, new_fd)?;
this.write_scalar(Scalar::from_i32(result), dest)?;
}
"flock" => {
let [fd, op] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let fd = this.read_scalar(fd)?.to_i32()?;
let op = this.read_scalar(op)?.to_i32()?;
let result = this.flock(fd, op)?;
this.write_scalar(result, dest)?;
}

// File and file system access
"open" | "open64" => {
Expand Down
93 changes: 93 additions & 0 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use crate::shims::unix::*;
use crate::*;
use shims::time::system_time_to_duration;

use self::fd::FlockOp;

#[derive(Debug)]
struct FileHandle {
file: File,
Expand Down Expand Up @@ -127,6 +129,97 @@ impl FileDescription for FileHandle {
}
}

fn flock<'tcx>(
&self,
communicate_allowed: bool,
op: FlockOp,
) -> InterpResult<'tcx, io::Result<()>> {
assert!(communicate_allowed, "isolation should have prevented even opening a file");
#[cfg(target_family = "unix")]
{
use std::os::fd::AsRawFd;

use FlockOp::*;
// We always use non-blocking call to prevent interpreter from being blocked
let (host_op, lock_nb) = match op {
SharedLock { nonblocking } => (libc::LOCK_SH | libc::LOCK_NB, nonblocking),
ExclusiveLock { nonblocking } => (libc::LOCK_EX | libc::LOCK_NB, nonblocking),
Unlock => (libc::LOCK_UN, false),
};

let fd = self.file.as_raw_fd();
let ret = unsafe { libc::flock(fd, host_op) };
newpavlov marked this conversation as resolved.
Show resolved Hide resolved
let res = match ret {
0 => Ok(()),
-1 => {
let err = io::Error::last_os_error();
if !lock_nb && err.kind() == io::ErrorKind::WouldBlock {
throw_unsup_format!("blocking `flock` is not currently supported");
}
Err(err)
}
ret => panic!("Unexpected return value from flock: {ret}"),
};
Ok(res)
}

#[cfg(target_family = "windows")]
{
use std::os::windows::io::AsRawHandle;
use windows_sys::Win32::{
Foundation::{ERROR_IO_PENDING, ERROR_LOCK_VIOLATION, FALSE, HANDLE, TRUE},
Storage::FileSystem::{
LockFileEx, UnlockFile, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY,
},
};
let fh = self.file.as_raw_handle() as HANDLE;

use FlockOp::*;
let (ret, lock_nb) = match op {
SharedLock { nonblocking } | ExclusiveLock { nonblocking } => {
// We always use non-blocking call to prevent interpreter from being blocked
let mut flags = LOCKFILE_FAIL_IMMEDIATELY;
if matches!(op, ExclusiveLock { .. }) {
flags |= LOCKFILE_EXCLUSIVE_LOCK;
}
let ret = unsafe { LockFileEx(fh, flags, 0, !0, !0, &mut std::mem::zeroed()) };
(ret, nonblocking)
}
Unlock => {
let ret = unsafe { UnlockFile(fh, 0, 0, !0, !0) };
(ret, false)
}
};

let res = match ret {
TRUE => Ok(()),
FALSE => {
let mut err = io::Error::last_os_error();
let code: u32 = err.raw_os_error().unwrap().try_into().unwrap();
if matches!(code, ERROR_IO_PENDING | ERROR_LOCK_VIOLATION) {
if lock_nb {
// Replace error with a custom WouldBlock error, which later will be
// mapped in the `helpers` module
let desc = format!("LockFileEx wouldblock error: {err}");
err = io::Error::new(io::ErrorKind::WouldBlock, desc);
} else {
throw_unsup_format!("blocking `flock` is not currently supported");
}
}
Err(err)
}
_ => panic!("Unexpected return value: {ret}"),
};
Ok(res)
}

#[cfg(not(any(target_family = "unix", target_family = "windows")))]
{
let _ = op;
compile_error!("flock is supported only on UNIX and Windows hosts");
}
}

fn is_tty(&self, communicate_allowed: bool) -> bool {
communicate_allowed && self.file.is_terminal()
}
Expand Down
71 changes: 71 additions & 0 deletions tests/pass-dep/libc/libc-fs-flock.rs
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Flock tests are separate since they don't in general work on a Windows host.
Copy link
Member

@RalfJung RalfJung Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is confusing... CI seems to indicate that it works just fine on a windows host? It doesn't work with a windows target but that's normal for these libc tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it was copied from another test and I forgot to update it after the emulation was implemented.

//@ignore-target-windows: File handling is not implemented yet
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
//@compile-flags: -Zmiri-disable-isolation

use std::{fs::File, io::Error, os::fd::AsRawFd};

#[path = "../../utils/mod.rs"]
mod utils;

fn main() {
let bytes = b"Hello, World!\n";
let path = utils::prepare_with_content("miri_test_fs_shared_lock.txt", bytes);

let files: Vec<File> = (0..3).map(|_| File::open(&path).unwrap()).collect();

// Test that we can apply many shared locks
for file in files.iter() {
let fd = file.as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_SH) };
if ret != 0 {
panic!("flock error: {}", Error::last_os_error());
}
}

// Test that shared lock prevents exclusive lock
{
let fd = files[0].as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
assert_eq!(ret, -1);
let err = Error::last_os_error().raw_os_error().unwrap();
assert_eq!(err, libc::EWOULDBLOCK);
}

// Unlock shared lock
for file in files.iter() {
let fd = file.as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_UN) };
if ret != 0 {
panic!("flock error: {}", Error::last_os_error());
}
}

// Take exclusive lock
{
let fd = files[0].as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_EX) };
assert_eq!(ret, 0);
}

// Test that shared lock prevents exclusive and shared locks
{
let fd = files[1].as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
assert_eq!(ret, -1);
let err = Error::last_os_error().raw_os_error().unwrap();
assert_eq!(err, libc::EWOULDBLOCK);

let fd = files[2].as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_SH | libc::LOCK_NB) };
assert_eq!(ret, -1);
let err = Error::last_os_error().raw_os_error().unwrap();
assert_eq!(err, libc::EWOULDBLOCK);
}

// Unlock exclusive lock
{
let fd = files[0].as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_UN) };
assert_eq!(ret, 0);
}
}