Skip to content

Commit 8c5dee6

Browse files
committed
Auto merge of #1155 - divergentdave:shim-posix_fadivse, r=RalfJung
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.
2 parents 5e4be45 + a30914b commit 8c5dee6

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/shims/foreign_items.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
780780
this.write_null(dest)?;
781781
}
782782

783+
"posix_fadvise" => {
784+
// fadvise is only informational, we can ignore it.
785+
this.write_null(dest)?;
786+
}
787+
783788
"mmap" => {
784789
// 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.
785790
let addr = this.read_scalar(args[0])?.not_undef()?;

tests/run-pass/libc.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ignore-windows: No libc on Windows
2+
// compile-flags: -Zmiri-disable-isolation
3+
4+
#![feature(rustc_private)]
5+
6+
#[allow(unused)] // necessary on macos due to conditional compilation
7+
extern crate libc;
8+
9+
#[cfg(not(target_os = "macos"))]
10+
fn test_posix_fadvise() {
11+
use std::convert::TryInto;
12+
use std::env::temp_dir;
13+
use std::fs::{File, remove_file};
14+
use std::io::Write;
15+
use std::os::unix::io::AsRawFd;
16+
17+
let path = temp_dir().join("miri_test_libc.txt");
18+
// Cleanup before test
19+
remove_file(&path).ok();
20+
21+
// Set up an open file
22+
let mut file = File::create(&path).unwrap();
23+
let bytes = b"Hello, World!\n";
24+
file.write(bytes).unwrap();
25+
26+
// Test calling posix_fadvise on a file.
27+
let result = unsafe {
28+
libc::posix_fadvise(
29+
file.as_raw_fd(),
30+
0,
31+
bytes.len().try_into().unwrap(),
32+
libc::POSIX_FADV_DONTNEED,
33+
)
34+
};
35+
drop(file);
36+
remove_file(&path).unwrap();
37+
assert_eq!(result, 0);
38+
}
39+
40+
fn main() {
41+
#[cfg(not(target_os = "macos"))]
42+
test_posix_fadvise();
43+
}

0 commit comments

Comments
 (0)