Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(time): fix time interception on ARM64 macOS #95

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.12] - 2022-12-13

### Fixed

- madsim: Fix `Instant` interception on ARM64 macOS caused by change in Rust nightly.

## [0.2.11] - 2022-12-02

### Added
Expand Down
3 changes: 2 additions & 1 deletion madsim/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "madsim"
version = "0.2.11"
version = "0.2.12"
edition = "2021"
authors = ["Runji Wang <wangrunji0408@163.com>"]
description = "Deterministic Simulator for distributed systems."
Expand Down Expand Up @@ -38,6 +38,7 @@ async-channel = "1.6"
downcast-rs = "1.2"
libc = "0.2"
naive-timer = "0.2"
rustversion = "1"
tokio = { version = "1", features = ["rt", "sync"] }
toml = "0.5"

Expand Down
49 changes: 25 additions & 24 deletions madsim/src/sim/time/system_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,39 @@ unsafe extern "C" fn gettimeofday(tp: *mut libc::timeval, tz: *mut libc::c_void)
}
}

/// Override the libc `clock_gettime` function. For Linux.
/// Override the libc `clock_gettime` function. For Linux and ARM64 macOS.
#[no_mangle]
#[inline(never)]
#[cfg(target_os = "linux")]
unsafe extern "C" fn clock_gettime(
clockid: libc::clockid_t,
tp: *mut libc::timespec,
) -> libc::c_int {
if let Some(time) = super::TimeHandle::try_current() {
// inside a madsim context, use the simulated time.
match clockid {
let system_time_duration = || {
time.now_time()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
};
let instant_duration = || time.now_instant().duration_since(std::mem::zeroed());

let dur = match clockid {
// used by SystemTime
libc::CLOCK_REALTIME | libc::CLOCK_REALTIME_COARSE => {
let dur = time
.now_time()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
tp.write(libc::timespec {
tv_sec: dur.as_secs() as _,
tv_nsec: dur.subsec_nanos() as _,
});
}
libc::CLOCK_REALTIME => system_time_duration(),
#[cfg(target_os = "linux")]
libc::CLOCK_REALTIME_COARSE => system_time_duration(),
// used by Instant
libc::CLOCK_MONOTONIC | libc::CLOCK_MONOTONIC_RAW | libc::CLOCK_MONOTONIC_COARSE => {
let dur = std::mem::transmute::<_, SystemTime>(time.now_instant())
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
tp.write(libc::timespec {
tv_sec: dur.as_secs() as _,
tv_nsec: dur.subsec_nanos() as _,
});
}
libc::CLOCK_MONOTONIC | libc::CLOCK_MONOTONIC_RAW => instant_duration(),
#[cfg(target_os = "linux")]
libc::CLOCK_MONOTONIC_COARSE => instant_duration(),
#[cfg(target_os = "macos")]
libc::CLOCK_UPTIME_RAW => instant_duration(),
_ => panic!("unsupported clockid: {}", clockid),
}
};
tp.write(libc::timespec {
tv_sec: dur.as_secs() as _,
tv_nsec: dur.subsec_nanos() as _,
});
0
} else {
lazy_static::lazy_static! {
Expand All @@ -87,10 +86,12 @@ unsafe extern "C" fn clock_gettime(
}
}

/// Override the `mach_absolute_time` function. For `Instant` on macOS.
/// Override the `mach_absolute_time` function. For `Instant` on x86_64 macOS.
#[no_mangle]
#[inline(never)]
#[cfg(target_os = "macos")]
// not used on ARM64 macOS after https://github.com/rust-lang/rust/pull/103594
#[rustversion::attr(since(1.67), cfg(not(target_arch = "aarch64")))]
extern "C" fn mach_absolute_time() -> u64 {
if let Some(time) = super::TimeHandle::try_current() {
// inside a madsim context, use the simulated time.
Expand Down