Skip to content

Commit 1b3332a

Browse files
committed
Auto merge of rust-lang#2286 - LegNeato:patch-2, r=RalfJung
Support `gettimeofday` on more than macos This appears to be in linux and in openbsd as well: * https://github.com/torvalds/linux/blob/master/lib/vdso/gettimeofday.c * https://github.com/openbsd/src/blob/master/sys/sys/time.h#L439 Note that std currently says [different syscalls are used on mac vs linux](https://doc.rust-lang.org/std/time/struct.SystemTime.html#platform-specific-behavior) but this is not part of[ std's contract](https://doc.rust-lang.org/std/io/index.html#platform-specific-behavior) and third party code could call the syscall directly on different platforms.
2 parents ccfb869 + 73a1a27 commit 1b3332a

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

src/shims/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6464
) -> InterpResult<'tcx, i32> {
6565
let this = self.eval_context_mut();
6666

67-
this.assert_target_os("macos", "gettimeofday");
67+
this.assert_target_os_is_unix("gettimeofday");
6868
this.check_no_isolation("`gettimeofday`")?;
6969

7070
// Using tz is obsolete and should always be null

src/shims/unix/foreign_items.rs

+7
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
139139
this.write_scalar(Scalar::from_machine_isize(result, this), dest)?;
140140
}
141141

142+
// Time related shims
143+
"gettimeofday" => {
144+
let [tv, tz] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
145+
let result = this.gettimeofday(tv, tz)?;
146+
this.write_scalar(Scalar::from_i32(result), dest)?;
147+
}
148+
142149
// Allocation
143150
"posix_memalign" => {
144151
let [ret, align, size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;

src/shims/unix/macos/foreign_items.rs

-5
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7878
}
7979

8080
// Time related shims
81-
"gettimeofday" => {
82-
let [tv, tz] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
83-
let result = this.gettimeofday(tv, tz)?;
84-
this.write_scalar(Scalar::from_i32(result), dest)?;
85-
}
8681
"mach_absolute_time" => {
8782
let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
8883
let result = this.mach_absolute_time()?;

tests/pass/libc.rs

+22
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,32 @@ fn test_clocks() {
290290
assert_eq!(is_error, 0);
291291
}
292292

293+
fn test_posix_gettimeofday() {
294+
let mut tp = std::mem::MaybeUninit::<libc::timeval>::uninit();
295+
let tz = std::ptr::null_mut::<libc::timezone>();
296+
#[cfg(target_os = "macos")] // `tz` has a different type on macOS
297+
let tz = tz as *mut libc::c_void;
298+
let is_error = unsafe { libc::gettimeofday(tp.as_mut_ptr(), tz) };
299+
assert_eq!(is_error, 0);
300+
let tv = unsafe { tp.assume_init() };
301+
assert!(tv.tv_sec > 0);
302+
assert!(tv.tv_usec >= 0); // Theoretically this could be 0.
303+
304+
// Test that non-null tz returns an error.
305+
let mut tz = std::mem::MaybeUninit::<libc::timezone>::uninit();
306+
let tz_ptr = tz.as_mut_ptr();
307+
#[cfg(target_os = "macos")] // `tz` has a different type on macOS
308+
let tz_ptr = tz_ptr as *mut libc::c_void;
309+
let is_error = unsafe { libc::gettimeofday(tp.as_mut_ptr(), tz_ptr) };
310+
assert_eq!(is_error, -1);
311+
}
312+
293313
fn main() {
294314
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
295315
test_posix_fadvise();
296316

317+
test_posix_gettimeofday();
318+
297319
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
298320
test_sync_file_range();
299321

0 commit comments

Comments
 (0)