Skip to content

Commit 0bca4e1

Browse files
committed
Convert u128 to nanosecond
1 parent 844450a commit 0bca4e1

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/tools/miri/src/clock.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,11 @@ impl Instant {
3939
InstantKind::Virtual { nanoseconds },
4040
InstantKind::Virtual { nanoseconds: earlier },
4141
) => {
42-
// Trade some nanosecond precision to prevent as much overflow as possible.
43-
let duration = match u64::try_from(
44-
// Manually convert from nanosecond to millisecond.
45-
// If it exceeded u64::MAX millisecond, we will just use u64::MAX millisecond,
46-
// Duration can't take in more than u64::MAX millisecond.
47-
nanoseconds.saturating_sub(earlier).saturating_div(1_000_000),
48-
) {
49-
Ok(millisecond) => Duration::from_millis(millisecond),
50-
_ => Duration::from_millis(u64::MAX),
42+
// If it exceeded u64::MAX nanosecond, we will just keep u64::MAX nanosecond,
43+
// Duration can't take in more than u64::MAX.
44+
let duration = match u64::try_from(nanoseconds.saturating_sub(earlier)) {
45+
Ok(nanosecond) => Duration::from_nanos(nanosecond),
46+
Err(_err) => Duration::from_nanos(u64::MAX),
5147
};
5248
Duration::new(duration.as_secs(), duration.subsec_nanos())
5349
}
@@ -104,7 +100,7 @@ impl Clock {
104100
ClockKind::Host { .. } => std::thread::sleep(duration),
105101
ClockKind::Virtual { nanoseconds } => {
106102
// Just pretend that we have slept for some time.
107-
let nanos: u128 = duration.as_nanos().try_into().unwrap();
103+
let nanos: u128 = duration.as_nanos();
108104
nanoseconds.update(|x| x + nanos);
109105
}
110106
}

src/tools/miri/tests/pass-dep/concurrency/linux-futex.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn concurrent_wait_wake() {
280280
assert!(woken > 0 && woken < rounds);
281281
}
282282

283-
// Reproduce of https://github.com/rust-lang/miri/issues/3647.
283+
// Reproduce https://github.com/rust-lang/miri/issues/3647. This should not ICE.
284284
fn large_timeout() {
285285
let futex: i32 = 123;
286286

@@ -296,7 +296,6 @@ fn large_timeout() {
296296
}
297297

298298
fn main() {
299-
large_timeout();
300299
wake_nobody();
301300
wake_dangling();
302301
wait_wrong_val();
@@ -305,4 +304,5 @@ fn main() {
305304
wait_wake();
306305
wait_wake_bitset();
307306
concurrent_wait_wake();
307+
large_timeout();
308308
}

0 commit comments

Comments
 (0)