Skip to content

Commit

Permalink
Auto merge of #2466 - RalfJung:longsleep, r=RalfJung
Browse files Browse the repository at this point in the history
fix an ICE in nanosleep()
  • Loading branch information
bors committed Aug 6, 2022
2 parents 74c5f1b + d7875ea commit 4208764
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/shims/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
return Ok(-1);
}
};
let timeout_time = Time::Monotonic(Instant::now().checked_add(duration).unwrap());
// If adding the duration overflows, let's just sleep for an hour. Waking up early is always acceptable.
let timeout_time = Instant::now()
.checked_add(duration)
.unwrap_or_else(|| Instant::now().checked_add(Duration::from_secs(3600)).unwrap());
let timeout_time = Time::Monotonic(timeout_time);

let active_thread = this.get_active_thread();
this.block_thread(active_thread);
Expand Down
18 changes: 18 additions & 0 deletions tests/pass/sleep_long.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ignore-target-windows: no threads nor sleep on Windows
//@compile-flags: -Zmiri-ignore-leaks -Zmiri-disable-isolation
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

fn main() {
let finished = Arc::new(Mutex::new(false));
let t_finished = finished.clone();
thread::spawn(move || {
// Sleep very, very long.
thread::sleep(Duration::new(u64::MAX, 0));
*t_finished.lock().unwrap() = true;
});
thread::sleep(Duration::from_millis(100));
assert_eq!(*finished.lock().unwrap(), false);
// Stopping the main thread will also kill the sleeper.
}

0 comments on commit 4208764

Please sign in to comment.