1 //! Implements threads.
3 use std::convert::TryInto;
4 use std::cell::RefCell;
5 use std::convert::TryFrom;
6 use std::num::NonZeroU32;
10 use rustc_data_structures::fx::FxHashMap;
11 use rustc_hir::def_id::DefId;
12 use rustc_index::vec::{Idx, IndexVec};
14 middle::codegen_fn_attrs::CodegenFnAttrFlags,
21 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
22 pub enum SchedulingAction {
23 /// Execute step on the active thread.
25 /// Execute destructors of the active thread.
31 /// A thread identifier.
32 #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
33 pub struct ThreadId(u32);
35 /// The main thread. When it terminates, the whole application terminates.
36 const MAIN_THREAD: ThreadId = ThreadId(0);
39 pub fn to_u128(self) -> u128 {
40 self.0.try_into().unwrap()
44 impl Idx for ThreadId {
45 fn new(idx: usize) -> Self {
46 ThreadId(u32::try_from(idx).unwrap())
48 fn index(self) -> usize {
49 usize::try_from(self.0).unwrap()
53 impl From<u64> for ThreadId {
54 fn from(id: u64) -> Self {
55 Self(u32::try_from(id).unwrap())
59 impl From<u32> for ThreadId {
60 fn from(id: u32) -> Self {
61 Self(u32::try_from(id).unwrap())
66 pub fn to_u32_scalar<'tcx>(&self) -> Scalar<Tag> {
67 Scalar::from_u32(u32::try_from(self.0).unwrap())
71 /// An identifier of a set of blocked threads. 0 is used to indicate the absence
72 /// of a blockset identifier and, therefore, is not a valid identifier.
73 #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
74 pub struct BlockSetId(NonZeroU32);
77 /// Panics if `id` is 0.
78 pub fn new(id: u32) -> Self {
79 Self(NonZeroU32::new(id).expect("0 is not a valid blockset id"))
81 pub fn to_u32_scalar<'tcx>(&self) -> Scalar<Tag> {
82 Scalar::from_u32(self.0.get())
86 /// The state of a thread.
87 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
88 pub enum ThreadState {
89 /// The thread is enabled and can be executed.
91 /// The thread tried to join the specified thread and is blocked until that
92 /// thread terminates.
93 BlockedOnJoin(ThreadId),
94 /// The thread is blocked and belongs to the given blockset.
96 /// The thread has terminated its execution (we do not delete terminated
101 /// The join status of a thread.
102 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
103 enum ThreadJoinStatus {
104 /// The thread can be joined.
106 /// A thread is detached if its join handle was destroyed and no other
107 /// thread can join it.
109 /// The thread was already joined by some thread and cannot be joined again.
114 pub struct Thread<'mir, 'tcx> {
116 /// Name of the thread.
117 thread_name: Option<Vec<u8>>,
118 /// The virtual call stack.
119 stack: Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>>,
121 join_status: ThreadJoinStatus,
124 impl<'mir, 'tcx> Thread<'mir, 'tcx> {
125 /// Check if the thread is done executing (no more stack frames). If yes,
126 /// change the state to terminated and return `true`.
127 fn check_terminated(&mut self) -> bool {
128 if self.state == ThreadState::Enabled {
129 if self.stack.is_empty() {
130 self.state = ThreadState::Terminated;
138 impl<'mir, 'tcx> std::fmt::Debug for Thread<'mir, 'tcx> {
139 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140 if let Some(ref name) = self.thread_name {
141 write!(f, "{}", String::from_utf8_lossy(name))?;
143 write!(f, "<unnamed>")?;
145 write!(f, "({:?}, {:?})", self.state, self.join_status)
149 impl<'mir, 'tcx> Default for Thread<'mir, 'tcx> {
150 fn default() -> Self {
152 state: ThreadState::Enabled,
155 join_status: ThreadJoinStatus::Joinable,
160 /// A set of threads.
162 pub struct ThreadManager<'mir, 'tcx> {
163 /// Identifier of the currently active thread.
164 active_thread: ThreadId,
165 /// Threads used in the program.
167 /// Note that this vector also contains terminated threads.
168 threads: IndexVec<ThreadId, Thread<'mir, 'tcx>>,
169 /// A counter used to generate unique identifiers for blocksets.
170 blockset_counter: u32,
171 /// A mapping from a thread-local static to an allocation id of a thread
172 /// specific allocation.
173 thread_local_alloc_ids: RefCell<FxHashMap<(DefId, ThreadId), AllocId>>,
174 /// A flag that indicates that we should change the active thread.
175 yield_active_thread: bool,
178 impl<'mir, 'tcx> Default for ThreadManager<'mir, 'tcx> {
179 fn default() -> Self {
180 let mut threads = IndexVec::new();
181 // Create the main thread and add it to the list of threads.
182 let mut main_thread = Thread::default();
183 // The main thread can *not* be joined on.
184 main_thread.join_status = ThreadJoinStatus::Detached;
185 threads.push(main_thread);
187 active_thread: ThreadId::new(0),
190 thread_local_alloc_ids: Default::default(),
191 yield_active_thread: false,
196 impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
197 /// Check if we have an allocation for the given thread local static for the
199 fn get_thread_local_alloc_id(&self, def_id: DefId) -> Option<AllocId> {
200 self.thread_local_alloc_ids.borrow().get(&(def_id, self.active_thread)).cloned()
203 /// Set the allocation id as the allocation id of the given thread local
204 /// static for the active thread.
206 /// Panics if a thread local is initialized twice for the same thread.
207 fn set_thread_local_alloc_id(&self, def_id: DefId, new_alloc_id: AllocId) {
208 self.thread_local_alloc_ids
210 .insert((def_id, self.active_thread), new_alloc_id)
214 /// Borrow the stack of the active thread.
215 fn active_thread_stack(&self) -> &[Frame<'mir, 'tcx, Tag, FrameData<'tcx>>] {
216 &self.threads[self.active_thread].stack
219 /// Mutably borrow the stack of the active thread.
220 fn active_thread_stack_mut(&mut self) -> &mut Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
221 &mut self.threads[self.active_thread].stack
224 /// Create a new thread and returns its id.
225 fn create_thread(&mut self) -> ThreadId {
226 let new_thread_id = ThreadId::new(self.threads.len());
227 self.threads.push(Default::default());
231 /// Set an active thread and return the id of the thread that was active before.
232 fn set_active_thread_id(&mut self, id: ThreadId) -> ThreadId {
233 let active_thread_id = self.active_thread;
234 self.active_thread = id;
235 assert!(self.active_thread.index() < self.threads.len());
239 /// Get the id of the currently active thread.
240 fn get_active_thread_id(&self) -> ThreadId {
244 /// Get the total number of threads that were ever spawn by this program.
245 fn get_total_thread_count(&self) -> usize {
249 /// Has the given thread terminated?
250 fn has_terminated(&self, thread_id: ThreadId) -> bool {
251 self.threads[thread_id].state == ThreadState::Terminated
254 /// Enable the thread for execution. The thread must be terminated.
255 fn enable_thread(&mut self, thread_id: ThreadId) {
256 assert!(self.has_terminated(thread_id));
257 self.threads[thread_id].state = ThreadState::Enabled;
260 /// Get a mutable borrow of the currently active thread.
261 fn active_thread_mut(&mut self) -> &mut Thread<'mir, 'tcx> {
262 &mut self.threads[self.active_thread]
265 /// Get a shared borrow of the currently active thread.
266 fn active_thread_ref(&self) -> &Thread<'mir, 'tcx> {
267 &self.threads[self.active_thread]
270 /// Mark the thread as detached, which means that no other thread will try
271 /// to join it and the thread is responsible for cleaning up.
272 fn detach_thread(&mut self, id: ThreadId) -> InterpResult<'tcx> {
273 if self.threads[id].join_status != ThreadJoinStatus::Joinable {
274 throw_ub_format!("trying to detach thread that was already detached or joined");
276 self.threads[id].join_status = ThreadJoinStatus::Detached;
280 /// Mark that the active thread tries to join the thread with `joined_thread_id`.
281 fn join_thread(&mut self, joined_thread_id: ThreadId) -> InterpResult<'tcx> {
282 if self.threads[joined_thread_id].join_status != ThreadJoinStatus::Joinable {
283 throw_ub_format!("trying to join a detached or already joined thread");
285 if joined_thread_id == self.active_thread {
286 throw_ub_format!("trying to join itself");
291 .all(|thread| thread.state != ThreadState::BlockedOnJoin(joined_thread_id)),
292 "a joinable thread already has threads waiting for its termination"
294 // Mark the joined thread as being joined so that we detect if other
295 // threads try to join it.
296 self.threads[joined_thread_id].join_status = ThreadJoinStatus::Joined;
297 if self.threads[joined_thread_id].state != ThreadState::Terminated {
298 // The joined thread is still running, we need to wait for it.
299 self.active_thread_mut().state = ThreadState::BlockedOnJoin(joined_thread_id);
301 "{:?} blocked on {:?} when trying to join",
309 /// Set the name of the active thread.
310 fn set_thread_name(&mut self, new_thread_name: Vec<u8>) {
311 self.active_thread_mut().thread_name = Some(new_thread_name);
314 /// Get the name of the active thread.
315 fn get_thread_name(&self) -> InterpResult<'tcx, &[u8]> {
316 if let Some(ref thread_name) = self.active_thread_ref().thread_name {
319 throw_ub_format!("thread {:?} has no name set", self.active_thread)
323 /// Allocate a new blockset id.
324 fn create_blockset(&mut self) -> BlockSetId {
325 self.blockset_counter = self.blockset_counter.checked_add(1).unwrap();
326 BlockSetId::new(self.blockset_counter)
329 /// Block the currently active thread and put it into the given blockset.
330 fn block_active_thread(&mut self, set: BlockSetId) {
331 let state = &mut self.active_thread_mut().state;
332 assert_eq!(*state, ThreadState::Enabled);
333 *state = ThreadState::Blocked(set);
336 /// Unblock any one thread from the given blockset if it contains at least
337 /// one. Return the id of the unblocked thread.
338 fn unblock_some_thread(&mut self, set: BlockSetId) -> Option<ThreadId> {
339 for (id, thread) in self.threads.iter_enumerated_mut() {
340 if thread.state == ThreadState::Blocked(set) {
341 trace!("unblocking {:?} in blockset {:?}", id, set);
342 thread.state = ThreadState::Enabled;
349 /// Change the active thread to some enabled thread.
350 fn yield_active_thread(&mut self) {
351 self.yield_active_thread = true;
354 /// Decide which action to take next and on which thread.
356 /// The currently implemented scheduling policy is the one that is commonly
357 /// used in stateless model checkers such as Loom: run the active thread as
358 /// long as we can and switch only when we have to (the active thread was
359 /// blocked, terminated, or has explicitly asked to be preempted).
360 fn schedule(&mut self) -> InterpResult<'tcx, SchedulingAction> {
361 if self.threads[self.active_thread].check_terminated() {
362 // Check if we need to unblock any threads.
363 for (i, thread) in self.threads.iter_enumerated_mut() {
364 if thread.state == ThreadState::BlockedOnJoin(self.active_thread) {
365 trace!("unblocking {:?} because {:?} terminated", i, self.active_thread);
366 thread.state = ThreadState::Enabled;
369 return Ok(SchedulingAction::ExecuteDtors);
371 if self.threads[MAIN_THREAD].state == ThreadState::Terminated {
372 // The main thread terminated; stop the program.
373 if self.threads.iter().any(|thread| thread.state != ThreadState::Terminated) {
374 // FIXME: This check should be either configurable or just emit
375 // a warning. For example, it seems normal for a program to
376 // terminate without waiting for its detached threads to
377 // terminate. However, this case is not trivial to support
378 // because we also probably do not want to consider the memory
379 // owned by these threads as leaked.
380 throw_unsup_format!("the main thread terminated without waiting for other threads");
382 return Ok(SchedulingAction::Stop);
384 if self.threads[self.active_thread].state == ThreadState::Enabled
385 && !self.yield_active_thread
387 // The currently active thread is still enabled, just continue with it.
388 return Ok(SchedulingAction::ExecuteStep);
390 // We need to pick a new thread for execution.
391 for (id, thread) in self.threads.iter_enumerated() {
392 if thread.state == ThreadState::Enabled {
393 if !self.yield_active_thread || id != self.active_thread {
394 self.active_thread = id;
399 self.yield_active_thread = false;
400 if self.threads[self.active_thread].state == ThreadState::Enabled {
401 return Ok(SchedulingAction::ExecuteStep);
403 // We have not found a thread to execute.
404 if self.threads.iter().all(|thread| thread.state == ThreadState::Terminated) {
407 throw_machine_stop!(TerminationInfo::Deadlock);
412 // Public interface to thread management.
413 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
414 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
415 /// A workaround for thread-local statics until
416 /// https://github.com/rust-lang/rust/issues/70685 is fixed: change the
417 /// thread-local allocation id with a freshly generated allocation id for
418 /// the currently active thread.
419 fn remap_thread_local_alloc_ids(
421 val: &mut mir::interpret::ConstValue<'tcx>,
422 ) -> InterpResult<'tcx> {
423 let this = self.eval_context_ref();
425 mir::interpret::ConstValue::Scalar(Scalar::Ptr(ref mut ptr)) => {
426 let alloc_id = ptr.alloc_id;
427 let alloc = this.tcx.alloc_map.lock().get(alloc_id);
429 let is_thread_local = |def_id| {
430 tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
433 Some(GlobalAlloc::Static(def_id)) if is_thread_local(def_id) => {
434 ptr.alloc_id = this.get_or_create_thread_local_alloc_id(def_id)?;
440 // FIXME: Handling only `Scalar` seems to work for now, but at
441 // least in principle thread-locals could be in any constant, so
442 // we should also consider other cases. However, once
443 // https://github.com/rust-lang/rust/issues/70685 gets fixed,
444 // this code will have to be rewritten anyway.
450 /// Get a thread-specific allocation id for the given thread-local static.
451 /// If needed, allocate a new one.
453 /// FIXME: This method should be replaced as soon as
454 /// https://github.com/rust-lang/rust/issues/70685 gets fixed.
455 fn get_or_create_thread_local_alloc_id(&self, def_id: DefId) -> InterpResult<'tcx, AllocId> {
456 let this = self.eval_context_ref();
458 if let Some(new_alloc_id) = this.machine.threads.get_thread_local_alloc_id(def_id) {
459 // We already have a thread-specific allocation id for this
460 // thread-local static.
463 // We need to allocate a thread-specific allocation id for this
464 // thread-local static.
466 // At first, we invoke the `const_eval_raw` query and extract the
467 // allocation from it. Unfortunately, we have to duplicate the code
468 // from `Memory::get_global_alloc` that does this.
470 // Then we store the retrieved allocation back into the `alloc_map`
471 // to get a fresh allocation id, which we can use as a
472 // thread-specific allocation id for the thread-local static.
473 if tcx.is_foreign_item(def_id) {
474 throw_unsup_format!("foreign thread-local statics are not supported");
476 // Invoke the `const_eval_raw` query.
477 let instance = Instance::mono(tcx.tcx, def_id);
478 let gid = GlobalId { instance, promoted: None };
480 tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| {
481 // no need to report anything, the const_eval call takes care of that
483 assert!(tcx.is_static(def_id));
486 let id = raw_const.alloc_id;
487 // Extract the allocation from the query result.
488 let mut alloc_map = tcx.alloc_map.lock();
489 let allocation = alloc_map.unwrap_memory(id);
490 // Create a new allocation id for the same allocation in this hacky
491 // way. Internally, `alloc_map` deduplicates allocations, but this
492 // is fine because Miri will make a copy before a first mutable
494 let new_alloc_id = alloc_map.create_memory_alloc(allocation);
495 this.machine.threads.set_thread_local_alloc_id(def_id, new_alloc_id);
501 fn create_thread(&mut self) -> InterpResult<'tcx, ThreadId> {
502 let this = self.eval_context_mut();
503 Ok(this.machine.threads.create_thread())
507 fn detach_thread(&mut self, thread_id: ThreadId) -> InterpResult<'tcx> {
508 let this = self.eval_context_mut();
509 this.machine.threads.detach_thread(thread_id)
513 fn join_thread(&mut self, joined_thread_id: ThreadId) -> InterpResult<'tcx> {
514 let this = self.eval_context_mut();
515 this.machine.threads.join_thread(joined_thread_id)
519 fn set_active_thread(&mut self, thread_id: ThreadId) -> InterpResult<'tcx, ThreadId> {
520 let this = self.eval_context_mut();
521 Ok(this.machine.threads.set_active_thread_id(thread_id))
525 fn get_active_thread(&self) -> InterpResult<'tcx, ThreadId> {
526 let this = self.eval_context_ref();
527 Ok(this.machine.threads.get_active_thread_id())
531 fn get_total_thread_count(&self) -> InterpResult<'tcx, usize> {
532 let this = self.eval_context_ref();
533 Ok(this.machine.threads.get_total_thread_count())
537 fn has_terminated(&self, thread_id: ThreadId) -> InterpResult<'tcx, bool> {
538 let this = self.eval_context_ref();
539 Ok(this.machine.threads.has_terminated(thread_id))
543 fn enable_thread(&mut self, thread_id: ThreadId) -> InterpResult<'tcx> {
544 let this = self.eval_context_mut();
545 this.machine.threads.enable_thread(thread_id);
550 fn active_thread_stack(&self) -> &[Frame<'mir, 'tcx, Tag, FrameData<'tcx>>] {
551 let this = self.eval_context_ref();
552 this.machine.threads.active_thread_stack()
556 fn active_thread_stack_mut(&mut self) -> &mut Vec<Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
557 let this = self.eval_context_mut();
558 this.machine.threads.active_thread_stack_mut()
562 fn set_active_thread_name(&mut self, new_thread_name: Vec<u8>) -> InterpResult<'tcx, ()> {
563 let this = self.eval_context_mut();
564 Ok(this.machine.threads.set_thread_name(new_thread_name))
568 fn get_active_thread_name<'c>(&'c self) -> InterpResult<'tcx, &'c [u8]>
572 let this = self.eval_context_ref();
573 this.machine.threads.get_thread_name()
577 fn create_blockset(&mut self) -> InterpResult<'tcx, BlockSetId> {
578 let this = self.eval_context_mut();
579 Ok(this.machine.threads.create_blockset())
583 fn block_active_thread(&mut self, set: BlockSetId) -> InterpResult<'tcx> {
584 let this = self.eval_context_mut();
585 Ok(this.machine.threads.block_active_thread(set))
589 fn unblock_some_thread(&mut self, set: BlockSetId) -> InterpResult<'tcx, Option<ThreadId>> {
590 let this = self.eval_context_mut();
591 Ok(this.machine.threads.unblock_some_thread(set))
595 fn yield_active_thread(&mut self) -> InterpResult<'tcx> {
596 let this = self.eval_context_mut();
597 this.machine.threads.yield_active_thread();
601 /// Decide which action to take next and on which thread.
603 fn schedule(&mut self) -> InterpResult<'tcx, SchedulingAction> {
604 let this = self.eval_context_mut();
605 this.machine.threads.schedule()