Skip to content

Commit

Permalink
Auto merge of #1155 - divergentdave:shim-posix_fadivse, r=RalfJung
Browse files Browse the repository at this point in the history
Add no-op shim for posix_fadvise

This function is present in the libc crate, but not exposed through the standard library anywhere, so I haven't included a test for it.
  • Loading branch information
bors committed Jan 29, 2020
2 parents 5e4be45 + a30914b commit 8c5dee6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_null(dest)?;
}

"posix_fadvise" => {
// fadvise is only informational, we can ignore it.
this.write_null(dest)?;
}

"mmap" => {
// This is a horrible hack, but since the guard page mechanism calls mmap and expects a particular return value, we just give it that value.
let addr = this.read_scalar(args[0])?.not_undef()?;
Expand Down
43 changes: 43 additions & 0 deletions tests/run-pass/libc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ignore-windows: No libc on Windows
// compile-flags: -Zmiri-disable-isolation

#![feature(rustc_private)]

#[allow(unused)] // necessary on macos due to conditional compilation
extern crate libc;

#[cfg(not(target_os = "macos"))]
fn test_posix_fadvise() {
use std::convert::TryInto;
use std::env::temp_dir;
use std::fs::{File, remove_file};
use std::io::Write;
use std::os::unix::io::AsRawFd;

let path = temp_dir().join("miri_test_libc.txt");
// Cleanup before test
remove_file(&path).ok();

// Set up an open file
let mut file = File::create(&path).unwrap();
let bytes = b"Hello, World!\n";
file.write(bytes).unwrap();

// Test calling posix_fadvise on a file.
let result = unsafe {
libc::posix_fadvise(
file.as_raw_fd(),
0,
bytes.len().try_into().unwrap(),
libc::POSIX_FADV_DONTNEED,
)
};
drop(file);
remove_file(&path).unwrap();
assert_eq!(result, 0);
}

fn main() {
#[cfg(not(target_os = "macos"))]
test_posix_fadvise();
}

0 comments on commit 8c5dee6

Please sign in to comment.