Skip to content

Commit

Permalink
Auto merge of #2997 - RalfJung:test-utils, r=RalfJung
Browse files Browse the repository at this point in the history
refactor tests/utils a bit, and move some FS functions there
  • Loading branch information
bors committed Jul 30, 2023
2 parents 2183cda + e565624 commit a95a432
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Check how a Reserved with interior mutability
// responds to a Foreign Write under a Protector
#[path = "../../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;

use std::cell::UnsafeCell;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0

#[path = "../../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;

// Check how a Reserved without interior mutability responds to a Foreign
// Write when under a protector
Expand Down
31 changes: 6 additions & 25 deletions src/tools/miri/tests/pass-dep/shims/libc-fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
#![feature(io_error_uncategorized)]

use std::convert::TryInto;
use std::ffi::{c_char, CStr, CString};
use std::ffi::CString;
use std::fs::{canonicalize, remove_dir_all, remove_file, File};
use std::io::{Error, ErrorKind, Write};
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;

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

fn main() {
test_dup_stdout_stderr();
test_canonicalize_too_long();
Expand All @@ -22,31 +25,9 @@ fn main() {
test_o_tmpfile_flag();
}

fn tmp() -> PathBuf {
let path = std::env::var("MIRI_TEMP")
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
// These are host paths. We need to convert them to the target.
let path = CString::new(path).unwrap();
let mut out = Vec::with_capacity(1024);

unsafe {
extern "Rust" {
fn miri_host_to_target_path(
path: *const c_char,
out: *mut c_char,
out_size: usize,
) -> usize;
}
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
assert_eq!(ret, 0);
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
PathBuf::from(out)
}
}

/// Prepare: compute filename and make sure the file does not exist.
fn prepare(filename: &str) -> PathBuf {
let path = tmp().join(filename);
let path = utils::tmp().join(filename);
// Clean the paths for robustness.
remove_file(&path).ok();
path
Expand All @@ -55,7 +36,7 @@ fn prepare(filename: &str) -> PathBuf {
/// Prepare directory: compute directory name and make sure it does not exist.
#[allow(unused)]
fn prepare_dir(dirname: &str) -> PathBuf {
let path = tmp().join(&dirname);
let path = utils::tmp().join(&dirname);
// Clean the directory for robustness.
remove_dir_all(&path).ok();
path
Expand Down
35 changes: 7 additions & 28 deletions src/tools/miri/tests/pass-dep/shims/libc-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,8 @@ use std::fs::{remove_file, File};
use std::os::unix::io::AsRawFd;
use std::path::PathBuf;

fn tmp() -> PathBuf {
use std::ffi::{c_char, CStr, CString};

let path = std::env::var("MIRI_TEMP")
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
// These are host paths. We need to convert them to the target.
let path = CString::new(path).unwrap();
let mut out = Vec::with_capacity(1024);

unsafe {
extern "Rust" {
fn miri_host_to_target_path(
path: *const c_char,
out: *mut c_char,
out_size: usize,
) -> usize;
}
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
assert_eq!(ret, 0);
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
PathBuf::from(out)
}
}
#[path = "../../utils/mod.rs"]
mod utils;

/// Test allocating variant of `realpath`.
fn test_posix_realpath_alloc() {
Expand All @@ -38,7 +17,7 @@ fn test_posix_realpath_alloc() {
use std::os::unix::ffi::OsStringExt;

let buf;
let path = tmp().join("miri_test_libc_posix_realpath_alloc");
let path = utils::tmp().join("miri_test_libc_posix_realpath_alloc");
let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed");

// Cleanup before test.
Expand All @@ -63,7 +42,7 @@ fn test_posix_realpath_noalloc() {
use std::ffi::{CStr, CString};
use std::os::unix::ffi::OsStrExt;

let path = tmp().join("miri_test_libc_posix_realpath_noalloc");
let path = utils::tmp().join("miri_test_libc_posix_realpath_noalloc");
let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed");

let mut v = vec![0; libc::PATH_MAX as usize];
Expand Down Expand Up @@ -103,7 +82,7 @@ fn test_posix_realpath_errors() {
fn test_posix_fadvise() {
use std::io::Write;

let path = tmp().join("miri_test_libc_posix_fadvise.txt");
let path = utils::tmp().join("miri_test_libc_posix_fadvise.txt");
// Cleanup before test
remove_file(&path).ok();

Expand All @@ -130,7 +109,7 @@ fn test_posix_fadvise() {
fn test_sync_file_range() {
use std::io::Write;

let path = tmp().join("miri_test_libc_sync_file_range.txt");
let path = utils::tmp().join("miri_test_libc_sync_file_range.txt");
// Cleanup before test.
remove_file(&path).ok();

Expand Down Expand Up @@ -243,7 +222,7 @@ fn test_isatty() {
libc::isatty(libc::STDERR_FILENO);

// But when we open a file, it is definitely not a TTY.
let path = tmp().join("notatty.txt");
let path = utils::tmp().join("notatty.txt");
// Cleanup before test.
remove_file(&path).ok();
let file = File::create(&path).unwrap();
Expand Down
39 changes: 7 additions & 32 deletions src/tools/miri/tests/pass/shims/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
#![feature(io_error_uncategorized)]

use std::collections::HashMap;
use std::ffi::{c_char, OsString};
use std::ffi::OsString;
use std::fs::{
canonicalize, create_dir, read_dir, read_link, remove_dir, remove_dir_all, remove_file, rename,
File, OpenOptions,
};
use std::io::{Error, ErrorKind, IsTerminal, Read, Result, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

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

fn main() {
test_path_conversion();
test_file();
Expand All @@ -30,45 +33,17 @@ fn main() {
test_from_raw_os_error();
}

fn host_to_target_path(path: String) -> PathBuf {
use std::ffi::{CStr, CString};

let path = CString::new(path).unwrap();
let mut out = Vec::with_capacity(1024);

unsafe {
extern "Rust" {
fn miri_host_to_target_path(
path: *const c_char,
out: *mut c_char,
out_size: usize,
) -> usize;
}
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
assert_eq!(ret, 0);
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
PathBuf::from(out)
}
}

fn tmp() -> PathBuf {
let path = std::env::var("MIRI_TEMP")
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
// These are host paths. We need to convert them to the target.
host_to_target_path(path)
}

/// Prepare: compute filename and make sure the file does not exist.
fn prepare(filename: &str) -> PathBuf {
let path = tmp().join(filename);
let path = utils::tmp().join(filename);
// Clean the paths for robustness.
remove_file(&path).ok();
path
}

/// Prepare directory: compute directory name and make sure it does not exist.
fn prepare_dir(dirname: &str) -> PathBuf {
let path = tmp().join(&dirname);
let path = utils::tmp().join(&dirname);
// Clean the directory for robustness.
remove_dir_all(&path).ok();
path
Expand All @@ -83,7 +58,7 @@ fn prepare_with_content(filename: &str, content: &[u8]) -> PathBuf {
}

fn test_path_conversion() {
let tmp = tmp();
let tmp = utils::tmp();
assert!(tmp.is_absolute(), "{:?} is not absolute", tmp);
assert!(tmp.is_dir(), "{:?} is not a directory", tmp);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
#[path = "../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;

use std::cell::UnsafeCell;

Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/tree_borrows/end-of-protector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Check that a protector goes back to normal behavior when the function
// returns.
#[path = "../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;

fn main() {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/tree_borrows/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0

#[path = "../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;

// Check the formatting of the trees.
fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/tree_borrows/reborrow-is-read.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0

#[path = "../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;

// To check that a reborrow is counted as a Read access, we use a reborrow
// with no additional Read to Freeze an Active pointer.
Expand Down
7 changes: 3 additions & 4 deletions src/tools/miri/tests/pass/tree_borrows/reserved.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0

#[path = "../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;
use utils::miri_extern::miri_write_to_stderr;

use std::cell::UnsafeCell;

Expand All @@ -28,8 +27,8 @@ fn main() {
}

unsafe fn print(msg: &str) {
miri_write_to_stderr(msg.as_bytes());
miri_write_to_stderr("\n".as_bytes());
utils::miri_write_to_stderr(msg.as_bytes());
utils::miri_write_to_stderr("\n".as_bytes());
}

unsafe fn read_second<T>(x: &mut T, y: *mut u8) {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/tree_borrows/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#![feature(ptr_internals)]

#[path = "../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;

use core::ptr::Unique;

Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/tree_borrows/vec_unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#![feature(vec_into_raw_parts)]

#[path = "../../utils/mod.rs"]
#[macro_use]
mod utils;
use utils::macros::*;

// Check general handling of `Unique`:
// there is no *explicit* `Unique` being used here, but there is one
Expand Down
29 changes: 29 additions & 0 deletions src/tools/miri/tests/utils/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::ffi::OsString;
use std::path::PathBuf;

use super::miri_extern;

pub fn host_to_target_path(path: OsString) -> PathBuf {
use std::ffi::{CStr, CString};

// Once into_os_str_bytes is stable we can use it here.
// (Unstable features would need feature flags in each test...)
let path = CString::new(path.into_string().unwrap()).unwrap();
let mut out = Vec::with_capacity(1024);

unsafe {
let ret =
miri_extern::miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
assert_eq!(ret, 0);
// Here we panic if it's not UTF-8... but that is hard to avoid with OsStr APIs.
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
PathBuf::from(out)
}
}

pub fn tmp() -> PathBuf {
let path =
std::env::var_os("MIRI_TEMP").unwrap_or_else(|| std::env::temp_dir().into_os_string());
// These are host paths. We need to convert them to the target.
host_to_target_path(path)
}
18 changes: 7 additions & 11 deletions src/tools/miri/tests/utils/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// The id obtained can be passed directly to `print_state!`.
macro_rules! alloc_id {
($ptr:expr) => {
crate::utils::miri_extern::miri_get_alloc_id($ptr as *const u8 as *const ())
$crate::utils::miri_get_alloc_id($ptr as *const u8 as *const ())
};
}

Expand All @@ -22,10 +22,10 @@ macro_rules! alloc_id {
/// tags that have not been given a name. Defaults to `false`.
macro_rules! print_state {
($alloc_id:expr) => {
crate::utils::macros::print_state!($alloc_id, false);
print_state!($alloc_id, false);
};
($alloc_id:expr, $show:expr) => {
crate::utils::miri_extern::miri_print_borrow_state($alloc_id, $show);
$crate::utils::miri_print_borrow_state($alloc_id, $show);
};
}

Expand All @@ -42,20 +42,16 @@ macro_rules! print_state {
/// `stringify!($ptr)` the name of `ptr` in the source code.
macro_rules! name {
($ptr:expr, $name:expr) => {
crate::utils::macros::name!($ptr => 0, $name);
name!($ptr => 0, $name);
};
($ptr:expr) => {
crate::utils::macros::name!($ptr => 0, stringify!($ptr));
name!($ptr => 0, stringify!($ptr));
};
($ptr:expr => $nth_parent:expr) => {
crate::utils::macros::name!($ptr => $nth_parent, stringify!($ptr));
name!($ptr => $nth_parent, stringify!($ptr));
};
($ptr:expr => $nth_parent:expr, $name:expr) => {
let name = $name.as_bytes();
crate::utils::miri_extern::miri_pointer_name($ptr as *const u8 as *const (), $nth_parent, name);
$crate::utils::miri_pointer_name($ptr as *const u8 as *const (), $nth_parent, name);
};
}

pub(crate) use alloc_id;
pub(crate) use name;
pub(crate) use print_state;
2 changes: 0 additions & 2 deletions src/tools/miri/tests/utils/miri_extern.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code)]

#[repr(C)]
/// Layout of the return value of `miri_resolve_frame`,
/// with fields in the exact same order.
Expand Down
Loading

0 comments on commit a95a432

Please sign in to comment.