Skip to content

fix(time): fix panic cause by std internal structure change #88

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

Merged
merged 4 commits into from
Oct 28, 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: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
toolchain: [stable, nightly]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -76,7 +77,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: ${{ matrix.toolchain }}
- name: Test std
uses: actions-rs/cargo@v1
with:
Expand All @@ -87,6 +88,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
toolchain: [stable, nightly]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -97,7 +99,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: ${{ matrix.toolchain }}
- name: Test sim
uses: actions-rs/cargo@v1
env:
Expand Down
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.9] - 2022-10-28

### Fixed

- madsim: Fix internal structure change of nightly `std::time::{SystemTime, Interval}`.

## [0.2.8] - 2022-09-26

### Added
Expand Down
2 changes: 1 addition & 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.8"
version = "0.2.9"
edition = "2021"
authors = ["Runji Wang <[email protected]>"]
description = "Deterministic Simulator for distributed systems."
Expand Down
14 changes: 11 additions & 3 deletions madsim/src/sim/time/system_time.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::SystemTime;

/// Override the libc `gettimeofday` function. For `SystemTime` on macOS.
#[no_mangle]
#[inline(never)]
Expand All @@ -12,7 +14,7 @@ unsafe extern "C" fn gettimeofday(tp: *mut libc::timeval, tz: *mut libc::c_void)
// inside a madsim context, use the simulated time.
let dur = time
.now_time()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
tp.write(libc::timeval {
tv_sec: dur.as_secs() as _,
Expand Down Expand Up @@ -50,7 +52,7 @@ unsafe extern "C" fn clock_gettime(
libc::CLOCK_REALTIME | libc::CLOCK_REALTIME_COARSE => {
let dur = time
.now_time()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
tp.write(libc::timespec {
tv_sec: dur.as_secs() as _,
Expand All @@ -59,7 +61,13 @@ unsafe extern "C" fn clock_gettime(
}
// used by Instant
libc::CLOCK_MONOTONIC | libc::CLOCK_MONOTONIC_RAW | libc::CLOCK_MONOTONIC_COARSE => {
tp.write(std::mem::transmute(time.now_instant()));
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 _,
});
}
_ => panic!("unsupported clockid: {}", clockid),
}
Expand Down