Skip to content

Commit 6730f22

Browse files
committed
Auto merge of #3177 - devnexen:fix_pthread_misc_fbsd, r=RalfJung
libc-misc test freebsd fixes attempt
2 parents 9ad93b3 + 991e53a commit 6730f22

File tree

8 files changed

+59
-69
lines changed

8 files changed

+59
-69
lines changed

src/tools/miri/ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ case $HOST_TARGET in
108108
MIRI_TEST_TARGET=aarch64-unknown-linux-gnu run_tests
109109
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
110110
MIRI_TEST_TARGET=i686-pc-windows-gnu run_tests
111-
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthread-threadname libc-getentropy libc-getrandom libc-misc atomic env align
111+
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthread-threadname libc-getentropy libc-getrandom libc-misc libc-fs atomic env align
112112
MIRI_TEST_TARGET=aarch64-linux-android run_tests_minimal hello integer vec panic/panic
113113
MIRI_TEST_TARGET=wasm32-wasi run_tests_minimal no_std integer strings wasm
114114
MIRI_TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std integer strings wasm

src/tools/miri/src/shims/time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
3131
let mut relative_clocks;
3232

3333
match this.tcx.sess.target.os.as_ref() {
34-
"linux" => {
35-
// Linux has two main kinds of clocks. REALTIME clocks return the actual time since the
34+
"linux" | "freebsd" => {
35+
// Linux and FreeBSD have two main kinds of clocks. REALTIME clocks return the actual time since the
3636
// Unix epoch, including effects which may cause time to move backwards such as NTP.
3737
// Linux further distinguishes regular and "coarse" clocks, but the "coarse" version
3838
// is just specified to be "faster and less precise", so we implement both the same way.

src/tools/miri/src/shims/unix/foreign_items.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
163163
"ftruncate64" => {
164164
let [fd, length] =
165165
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
166+
let fd = this.read_scalar(fd)?.to_i32()?;
167+
let length = this.read_scalar(length)?.to_i64()?;
168+
let result = this.ftruncate64(fd, length)?;
169+
this.write_scalar(result, dest)?;
170+
}
171+
"ftruncate" => {
172+
let [fd, length] =
173+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
174+
let fd = this.read_scalar(fd)?.to_i32()?;
175+
let length = this.read_target_isize(length)?;
166176
let result = this.ftruncate64(fd, length)?;
167177
this.write_scalar(result, dest)?;
168178
}

src/tools/miri/src/shims/unix/fs.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,16 +1504,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
15041504
}
15051505
}
15061506

1507-
fn ftruncate64(
1508-
&mut self,
1509-
fd_op: &OpTy<'tcx, Provenance>,
1510-
length_op: &OpTy<'tcx, Provenance>,
1511-
) -> InterpResult<'tcx, Scalar<Provenance>> {
1507+
fn ftruncate64(&mut self, fd: i32, length: i64) -> InterpResult<'tcx, Scalar<Provenance>> {
15121508
let this = self.eval_context_mut();
15131509

1514-
let fd = this.read_scalar(fd_op)?.to_i32()?;
1515-
let length = this.read_scalar(length_op)?.to_i64()?;
1516-
15171510
// Reject if isolation is enabled.
15181511
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
15191512
this.reject_in_isolation("`ftruncate64`", reject_with)?;

src/tools/miri/src/shims/unix/macos/foreign_items.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
7272
let result = this.lseek64(fd, offset, whence)?;
7373
this.write_scalar(result, dest)?;
7474
}
75-
"ftruncate" => {
76-
let [fd, length] =
77-
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
78-
// macOS is 64bit-only, so this is ftruncate64
79-
let result = this.ftruncate64(fd, length)?;
80-
this.write_scalar(result, dest)?;
81-
}
8275
"realpath$DARWIN_EXTSN" => {
8376
let [path, resolved_path] =
8477
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;

src/tools/miri/tests/pass-dep/shims/libc-fs-with-isolation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ignore-target-windows: no libc on Windows
2+
//@ignore-target-freebsd: FIXME needs foreign function `stat@FBSD_1.0`
23
//@compile-flags: -Zmiri-isolation-error=warn-nobacktrace
34
//@normalize-stderr-test: "(stat(x)?)" -> "$$STAT"
45

src/tools/miri/tests/pass-dep/shims/libc-fs.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn main() {
2323
test_file_open_unix_extra_third_arg();
2424
#[cfg(target_os = "linux")]
2525
test_o_tmpfile_flag();
26+
test_posix_mkstemp();
2627
}
2728

2829
/// Prepare: compute filename and make sure the file does not exist.
@@ -151,3 +152,45 @@ fn test_o_tmpfile_flag() {
151152
.raw_os_error(),
152153
);
153154
}
155+
156+
fn test_posix_mkstemp() {
157+
use std::ffi::OsStr;
158+
use std::os::unix::io::FromRawFd;
159+
use std::path::Path;
160+
161+
let valid_template = "fooXXXXXX";
162+
// C needs to own this as `mkstemp(3)` says:
163+
// "Since it will be modified, `template` must not be a string constant, but
164+
// should be declared as a character array."
165+
// There seems to be no `as_mut_ptr` on `CString` so we need to use `into_raw`.
166+
let ptr = CString::new(valid_template).unwrap().into_raw();
167+
let fd = unsafe { libc::mkstemp(ptr) };
168+
// Take ownership back in Rust to not leak memory.
169+
let slice = unsafe { CString::from_raw(ptr) };
170+
assert!(fd > 0);
171+
let osstr = OsStr::from_bytes(slice.to_bytes());
172+
let path: &Path = osstr.as_ref();
173+
let name = path.file_name().unwrap().to_string_lossy();
174+
assert!(name.ne("fooXXXXXX"));
175+
assert!(name.starts_with("foo"));
176+
assert_eq!(name.len(), 9);
177+
assert_eq!(
178+
name.chars().skip(3).filter(char::is_ascii_alphanumeric).collect::<Vec<char>>().len(),
179+
6
180+
);
181+
let file = unsafe { File::from_raw_fd(fd) };
182+
assert!(file.set_len(0).is_ok());
183+
184+
let invalid_templates = vec!["foo", "barXX", "XXXXXXbaz", "whatXXXXXXever", "X"];
185+
for t in invalid_templates {
186+
let ptr = CString::new(t).unwrap().into_raw();
187+
let fd = unsafe { libc::mkstemp(ptr) };
188+
let _ = unsafe { CString::from_raw(ptr) };
189+
// "On error, -1 is returned, and errno is set to
190+
// indicate the error"
191+
assert_eq!(fd, -1);
192+
let e = std::io::Error::last_os_error();
193+
assert_eq!(e.raw_os_error(), Some(libc::EINVAL));
194+
assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput);
195+
}
196+
}

src/tools/miri/tests/pass-dep/shims/libc-misc.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,13 @@ fn test_thread_local_errno() {
172172
}
173173

174174
/// Tests whether clock support exists at all
175-
#[cfg(not(target_os = "freebsd"))]
176175
fn test_clocks() {
177176
let mut tp = std::mem::MaybeUninit::<libc::timespec>::uninit();
178177
let is_error = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, tp.as_mut_ptr()) };
179178
assert_eq!(is_error, 0);
180179
let is_error = unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, tp.as_mut_ptr()) };
181180
assert_eq!(is_error, 0);
182-
#[cfg(target_os = "linux")]
181+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
183182
{
184183
let is_error = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME_COARSE, tp.as_mut_ptr()) };
185184
assert_eq!(is_error, 0);
@@ -238,51 +237,6 @@ fn test_isatty() {
238237
}
239238
}
240239

241-
#[cfg(not(target_os = "freebsd"))]
242-
fn test_posix_mkstemp() {
243-
use std::ffi::CString;
244-
use std::ffi::OsStr;
245-
use std::os::unix::ffi::OsStrExt;
246-
use std::os::unix::io::FromRawFd;
247-
use std::path::Path;
248-
249-
let valid_template = "fooXXXXXX";
250-
// C needs to own this as `mkstemp(3)` says:
251-
// "Since it will be modified, `template` must not be a string constant, but
252-
// should be declared as a character array."
253-
// There seems to be no `as_mut_ptr` on `CString` so we need to use `into_raw`.
254-
let ptr = CString::new(valid_template).unwrap().into_raw();
255-
let fd = unsafe { libc::mkstemp(ptr) };
256-
// Take ownership back in Rust to not leak memory.
257-
let slice = unsafe { CString::from_raw(ptr) };
258-
assert!(fd > 0);
259-
let osstr = OsStr::from_bytes(slice.to_bytes());
260-
let path: &Path = osstr.as_ref();
261-
let name = path.file_name().unwrap().to_string_lossy();
262-
assert!(name.ne("fooXXXXXX"));
263-
assert!(name.starts_with("foo"));
264-
assert_eq!(name.len(), 9);
265-
assert_eq!(
266-
name.chars().skip(3).filter(char::is_ascii_alphanumeric).collect::<Vec<char>>().len(),
267-
6
268-
);
269-
let file = unsafe { File::from_raw_fd(fd) };
270-
assert!(file.set_len(0).is_ok());
271-
272-
let invalid_templates = vec!["foo", "barXX", "XXXXXXbaz", "whatXXXXXXever", "X"];
273-
for t in invalid_templates {
274-
let ptr = CString::new(t).unwrap().into_raw();
275-
let fd = unsafe { libc::mkstemp(ptr) };
276-
let _ = unsafe { CString::from_raw(ptr) };
277-
// "On error, -1 is returned, and errno is set to
278-
// indicate the error"
279-
assert_eq!(fd, -1);
280-
let e = std::io::Error::last_os_error();
281-
assert_eq!(e.raw_os_error(), Some(libc::EINVAL));
282-
assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput);
283-
}
284-
}
285-
286240
fn test_memcpy() {
287241
unsafe {
288242
let src = [1i8, 2, 3];
@@ -406,9 +360,6 @@ fn test_reallocarray() {
406360
fn main() {
407361
test_posix_gettimeofday();
408362

409-
#[cfg(not(target_os = "freebsd"))] // FIXME we should support this on FreeBSD as well
410-
test_posix_mkstemp();
411-
412363
test_posix_realpath_alloc();
413364
test_posix_realpath_noalloc();
414365
test_posix_realpath_errors();
@@ -417,7 +368,6 @@ fn main() {
417368

418369
test_isatty();
419370

420-
#[cfg(not(target_os = "freebsd"))] // FIXME we should support this on FreeBSD as well
421371
test_clocks();
422372

423373
test_dlsym();

0 commit comments

Comments
 (0)