Skip to content

Commit

Permalink
Auto merge of #3689 - adwinwhite:lseek64, r=RalfJung
Browse files Browse the repository at this point in the history
Fix ICE caused by seeking past `i64::MAX`

Make Miri behave the same as standard library on file seeking offset.

Fixes #3680.
  • Loading branch information
bors committed Jun 21, 2024
2 parents dca0151 + c9c887b commit a784b63
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Isolation check is done via `FileDescriptor` trait.

let seek_from = if whence == this.eval_libc_i32("SEEK_SET") {
SeekFrom::Start(u64::try_from(offset).unwrap())
if offset < 0 {
// Negative offsets return `EINVAL`.
let einval = this.eval_libc("EINVAL");
this.set_last_error(einval)?;
return Ok(Scalar::from_i64(-1));
} else {
SeekFrom::Start(u64::try_from(offset).unwrap())
}
} else if whence == this.eval_libc_i32("SEEK_CUR") {
SeekFrom::Current(i64::try_from(offset).unwrap())
} else if whence == this.eval_libc_i32("SEEK_END") {
Expand Down
21 changes: 21 additions & 0 deletions tests/pass/issues/issue-miri-3680.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ignore-target-windows: File handling is not implemented yet
//@compile-flags: -Zmiri-disable-isolation

use std::fs::remove_file;
use std::io::{ErrorKind, Seek};

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

fn main() {
let path = utils::prepare("miri_test_fs_seek_i64_max_plus_1.txt");

let mut f = std::fs::File::create(&path).unwrap();
let error = f.seek(std::io::SeekFrom::Start(i64::MAX as u64 + 1)).unwrap_err();

// It should be error due to negative offset.
assert_eq!(error.kind(), ErrorKind::InvalidInput);

// Cleanup
remove_file(&path).unwrap();
}

0 comments on commit a784b63

Please sign in to comment.