diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index deeb37da44..7bd85a4e94 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -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()?; diff --git a/tests/run-pass/libc.rs b/tests/run-pass/libc.rs new file mode 100644 index 0000000000..5b7b37db32 --- /dev/null +++ b/tests/run-pass/libc.rs @@ -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(); +}