Skip to content

Commit defbb66

Browse files
committed
Auto merge of #2997 - RalfJung:test-utils, r=RalfJung
refactor tests/utils a bit, and move some FS functions there
2 parents 66fdcd2 + 415b117 commit defbb66

16 files changed

+77
-112
lines changed

tests/fail/tree_borrows/reserved/cell-protected-write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// Check how a Reserved with interior mutability
44
// responds to a Foreign Write under a Protector
55
#[path = "../../../utils/mod.rs"]
6+
#[macro_use]
67
mod utils;
7-
use utils::macros::*;
88

99
use std::cell::UnsafeCell;
1010

tests/fail/tree_borrows/reserved/int-protected-write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22

33
#[path = "../../../utils/mod.rs"]
4+
#[macro_use]
45
mod utils;
5-
use utils::macros::*;
66

77
// Check how a Reserved without interior mutability responds to a Foreign
88
// Write when under a protector

tests/pass-dep/shims/libc-fs.rs

+6-25
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
#![feature(io_error_uncategorized)]
66

77
use std::convert::TryInto;
8-
use std::ffi::{c_char, CStr, CString};
8+
use std::ffi::CString;
99
use std::fs::{canonicalize, remove_dir_all, remove_file, File};
1010
use std::io::{Error, ErrorKind, Write};
1111
use std::os::unix::ffi::OsStrExt;
1212
use std::path::PathBuf;
1313

14+
#[path = "../../utils/mod.rs"]
15+
mod utils;
16+
1417
fn main() {
1518
test_dup_stdout_stderr();
1619
test_canonicalize_too_long();
@@ -22,31 +25,9 @@ fn main() {
2225
test_o_tmpfile_flag();
2326
}
2427

25-
fn tmp() -> PathBuf {
26-
let path = std::env::var("MIRI_TEMP")
27-
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
28-
// These are host paths. We need to convert them to the target.
29-
let path = CString::new(path).unwrap();
30-
let mut out = Vec::with_capacity(1024);
31-
32-
unsafe {
33-
extern "Rust" {
34-
fn miri_host_to_target_path(
35-
path: *const c_char,
36-
out: *mut c_char,
37-
out_size: usize,
38-
) -> usize;
39-
}
40-
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
41-
assert_eq!(ret, 0);
42-
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
43-
PathBuf::from(out)
44-
}
45-
}
46-
4728
/// Prepare: compute filename and make sure the file does not exist.
4829
fn prepare(filename: &str) -> PathBuf {
49-
let path = tmp().join(filename);
30+
let path = utils::tmp().join(filename);
5031
// Clean the paths for robustness.
5132
remove_file(&path).ok();
5233
path
@@ -55,7 +36,7 @@ fn prepare(filename: &str) -> PathBuf {
5536
/// Prepare directory: compute directory name and make sure it does not exist.
5637
#[allow(unused)]
5738
fn prepare_dir(dirname: &str) -> PathBuf {
58-
let path = tmp().join(&dirname);
39+
let path = utils::tmp().join(&dirname);
5940
// Clean the directory for robustness.
6041
remove_dir_all(&path).ok();
6142
path

tests/pass-dep/shims/libc-misc.rs

+7-28
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,8 @@ use std::fs::{remove_file, File};
66
use std::os::unix::io::AsRawFd;
77
use std::path::PathBuf;
88

9-
fn tmp() -> PathBuf {
10-
use std::ffi::{c_char, CStr, CString};
11-
12-
let path = std::env::var("MIRI_TEMP")
13-
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
14-
// These are host paths. We need to convert them to the target.
15-
let path = CString::new(path).unwrap();
16-
let mut out = Vec::with_capacity(1024);
17-
18-
unsafe {
19-
extern "Rust" {
20-
fn miri_host_to_target_path(
21-
path: *const c_char,
22-
out: *mut c_char,
23-
out_size: usize,
24-
) -> usize;
25-
}
26-
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
27-
assert_eq!(ret, 0);
28-
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
29-
PathBuf::from(out)
30-
}
31-
}
9+
#[path = "../../utils/mod.rs"]
10+
mod utils;
3211

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

4019
let buf;
41-
let path = tmp().join("miri_test_libc_posix_realpath_alloc");
20+
let path = utils::tmp().join("miri_test_libc_posix_realpath_alloc");
4221
let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed");
4322

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

66-
let path = tmp().join("miri_test_libc_posix_realpath_noalloc");
45+
let path = utils::tmp().join("miri_test_libc_posix_realpath_noalloc");
6746
let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed");
6847

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

106-
let path = tmp().join("miri_test_libc_posix_fadvise.txt");
85+
let path = utils::tmp().join("miri_test_libc_posix_fadvise.txt");
10786
// Cleanup before test
10887
remove_file(&path).ok();
10988

@@ -130,7 +109,7 @@ fn test_posix_fadvise() {
130109
fn test_sync_file_range() {
131110
use std::io::Write;
132111

133-
let path = tmp().join("miri_test_libc_sync_file_range.txt");
112+
let path = utils::tmp().join("miri_test_libc_sync_file_range.txt");
134113
// Cleanup before test.
135114
remove_file(&path).ok();
136115

@@ -243,7 +222,7 @@ fn test_isatty() {
243222
libc::isatty(libc::STDERR_FILENO);
244223

245224
// But when we open a file, it is definitely not a TTY.
246-
let path = tmp().join("notatty.txt");
225+
let path = utils::tmp().join("notatty.txt");
247226
// Cleanup before test.
248227
remove_file(&path).ok();
249228
let file = File::create(&path).unwrap();

tests/pass/shims/fs.rs

+7-32
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
#![feature(io_error_uncategorized)]
66

77
use std::collections::HashMap;
8-
use std::ffi::{c_char, OsString};
8+
use std::ffi::OsString;
99
use std::fs::{
1010
canonicalize, create_dir, read_dir, read_link, remove_dir, remove_dir_all, remove_file, rename,
1111
File, OpenOptions,
1212
};
1313
use std::io::{Error, ErrorKind, IsTerminal, Read, Result, Seek, SeekFrom, Write};
1414
use std::path::{Path, PathBuf};
1515

16+
#[path = "../../utils/mod.rs"]
17+
mod utils;
18+
1619
fn main() {
1720
test_path_conversion();
1821
test_file();
@@ -30,45 +33,17 @@ fn main() {
3033
test_from_raw_os_error();
3134
}
3235

33-
fn host_to_target_path(path: String) -> PathBuf {
34-
use std::ffi::{CStr, CString};
35-
36-
let path = CString::new(path).unwrap();
37-
let mut out = Vec::with_capacity(1024);
38-
39-
unsafe {
40-
extern "Rust" {
41-
fn miri_host_to_target_path(
42-
path: *const c_char,
43-
out: *mut c_char,
44-
out_size: usize,
45-
) -> usize;
46-
}
47-
let ret = miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
48-
assert_eq!(ret, 0);
49-
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
50-
PathBuf::from(out)
51-
}
52-
}
53-
54-
fn tmp() -> PathBuf {
55-
let path = std::env::var("MIRI_TEMP")
56-
.unwrap_or_else(|_| std::env::temp_dir().into_os_string().into_string().unwrap());
57-
// These are host paths. We need to convert them to the target.
58-
host_to_target_path(path)
59-
}
60-
6136
/// Prepare: compute filename and make sure the file does not exist.
6237
fn prepare(filename: &str) -> PathBuf {
63-
let path = tmp().join(filename);
38+
let path = utils::tmp().join(filename);
6439
// Clean the paths for robustness.
6540
remove_file(&path).ok();
6641
path
6742
}
6843

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

8560
fn test_path_conversion() {
86-
let tmp = tmp();
61+
let tmp = utils::tmp();
8762
assert!(tmp.is_absolute(), "{:?} is not absolute", tmp);
8863
assert!(tmp.is_dir(), "{:?} is not a directory", tmp);
8964
}

tests/pass/tree_borrows/cell-alternate-writes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22
#[path = "../../utils/mod.rs"]
3+
#[macro_use]
34
mod utils;
4-
use utils::macros::*;
55

66
use std::cell::UnsafeCell;
77

tests/pass/tree_borrows/end-of-protector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// Check that a protector goes back to normal behavior when the function
44
// returns.
55
#[path = "../../utils/mod.rs"]
6+
#[macro_use]
67
mod utils;
7-
use utils::macros::*;
88

99
fn main() {
1010
unsafe {

tests/pass/tree_borrows/formatting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22

33
#[path = "../../utils/mod.rs"]
4+
#[macro_use]
45
mod utils;
5-
use utils::macros::*;
66

77
// Check the formatting of the trees.
88
fn main() {

tests/pass/tree_borrows/reborrow-is-read.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22

33
#[path = "../../utils/mod.rs"]
4+
#[macro_use]
45
mod utils;
5-
use utils::macros::*;
66

77
// To check that a reborrow is counted as a Read access, we use a reborrow
88
// with no additional Read to Freeze an Active pointer.

tests/pass/tree_borrows/reserved.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//@compile-flags: -Zmiri-tree-borrows -Zmiri-tag-gc=0
22

33
#[path = "../../utils/mod.rs"]
4+
#[macro_use]
45
mod utils;
5-
use utils::macros::*;
6-
use utils::miri_extern::miri_write_to_stderr;
76

87
use std::cell::UnsafeCell;
98

@@ -28,8 +27,8 @@ fn main() {
2827
}
2928

3029
unsafe fn print(msg: &str) {
31-
miri_write_to_stderr(msg.as_bytes());
32-
miri_write_to_stderr("\n".as_bytes());
30+
utils::miri_write_to_stderr(msg.as_bytes());
31+
utils::miri_write_to_stderr("\n".as_bytes());
3332
}
3433

3534
unsafe fn read_second<T>(x: &mut T, y: *mut u8) {

tests/pass/tree_borrows/unique.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#![feature(ptr_internals)]
66

77
#[path = "../../utils/mod.rs"]
8+
#[macro_use]
89
mod utils;
9-
use utils::macros::*;
1010

1111
use core::ptr::Unique;
1212

tests/pass/tree_borrows/vec_unique.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#![feature(vec_into_raw_parts)]
66

77
#[path = "../../utils/mod.rs"]
8+
#[macro_use]
89
mod utils;
9-
use utils::macros::*;
1010

1111
// Check general handling of `Unique`:
1212
// there is no *explicit* `Unique` being used here, but there is one

tests/utils/fs.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::ffi::OsString;
2+
use std::path::PathBuf;
3+
4+
use super::miri_extern;
5+
6+
pub fn host_to_target_path(path: OsString) -> PathBuf {
7+
use std::ffi::{CStr, CString};
8+
9+
// Once into_os_str_bytes is stable we can use it here.
10+
// (Unstable features would need feature flags in each test...)
11+
let path = CString::new(path.into_string().unwrap()).unwrap();
12+
let mut out = Vec::with_capacity(1024);
13+
14+
unsafe {
15+
let ret =
16+
miri_extern::miri_host_to_target_path(path.as_ptr(), out.as_mut_ptr(), out.capacity());
17+
assert_eq!(ret, 0);
18+
// Here we panic if it's not UTF-8... but that is hard to avoid with OsStr APIs.
19+
let out = CStr::from_ptr(out.as_ptr()).to_str().unwrap();
20+
PathBuf::from(out)
21+
}
22+
}
23+
24+
pub fn tmp() -> PathBuf {
25+
let path =
26+
std::env::var_os("MIRI_TEMP").unwrap_or_else(|| std::env::temp_dir().into_os_string());
27+
// These are host paths. We need to convert them to the target.
28+
host_to_target_path(path)
29+
}

tests/utils/macros.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// The id obtained can be passed directly to `print_state!`.
1010
macro_rules! alloc_id {
1111
($ptr:expr) => {
12-
crate::utils::miri_extern::miri_get_alloc_id($ptr as *const u8 as *const ())
12+
$crate::utils::miri_get_alloc_id($ptr as *const u8 as *const ())
1313
};
1414
}
1515

@@ -22,10 +22,10 @@ macro_rules! alloc_id {
2222
/// tags that have not been given a name. Defaults to `false`.
2323
macro_rules! print_state {
2424
($alloc_id:expr) => {
25-
crate::utils::macros::print_state!($alloc_id, false);
25+
print_state!($alloc_id, false);
2626
};
2727
($alloc_id:expr, $show:expr) => {
28-
crate::utils::miri_extern::miri_print_borrow_state($alloc_id, $show);
28+
$crate::utils::miri_print_borrow_state($alloc_id, $show);
2929
};
3030
}
3131

@@ -42,20 +42,16 @@ macro_rules! print_state {
4242
/// `stringify!($ptr)` the name of `ptr` in the source code.
4343
macro_rules! name {
4444
($ptr:expr, $name:expr) => {
45-
crate::utils::macros::name!($ptr => 0, $name);
45+
name!($ptr => 0, $name);
4646
};
4747
($ptr:expr) => {
48-
crate::utils::macros::name!($ptr => 0, stringify!($ptr));
48+
name!($ptr => 0, stringify!($ptr));
4949
};
5050
($ptr:expr => $nth_parent:expr) => {
51-
crate::utils::macros::name!($ptr => $nth_parent, stringify!($ptr));
51+
name!($ptr => $nth_parent, stringify!($ptr));
5252
};
5353
($ptr:expr => $nth_parent:expr, $name:expr) => {
5454
let name = $name.as_bytes();
55-
crate::utils::miri_extern::miri_pointer_name($ptr as *const u8 as *const (), $nth_parent, name);
55+
$crate::utils::miri_pointer_name($ptr as *const u8 as *const (), $nth_parent, name);
5656
};
5757
}
58-
59-
pub(crate) use alloc_id;
60-
pub(crate) use name;
61-
pub(crate) use print_state;

tests/utils/miri_extern.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(dead_code)]
2-
31
#[repr(C)]
42
/// Layout of the return value of `miri_resolve_frame`,
53
/// with fields in the exact same order.

0 commit comments

Comments
 (0)