]> git.lizzy.rs Git - rust.git/blob - src/thread.rs
Change representation and conversion of ThreadId and BlockSetId.
[rust.git] / src / thread.rs
1 //! Implements threads.
2
3 use std::convert::TryInto;
4 use std::cell::RefCell;
5 use std::convert::TryFrom;
6 use std::num::NonZeroU32;
7
8 use log::trace;
9
10 use rustc_data_structures::fx::FxHashMap;
11 use rustc_hir::def_id::DefId;
12 use rustc_index::vec::{Idx, IndexVec};
13 use rustc_middle::{
14     middle::codegen_fn_attrs::CodegenFnAttrFlags,
15     mir,
16     ty::{self, Instance},
17 };
18
19 use crate::*;
20
21 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
22 pub enum SchedulingAction {
23     /// Execute step on the active thread.
24     ExecuteStep,
25     /// Execute destructors of the active thread.
26     ExecuteDtors,
27     /// Stop the program.
28     Stop,
29 }
30
31 /// A thread identifier.
32 #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
33 pub struct ThreadId(u32);
34
35 /// The main thread. When it terminates, the whole application terminates.
36 const MAIN_THREAD: ThreadId = ThreadId(0);
37
38 impl ThreadId {
39     pub fn to_u128(self) -> u128 {
40         self.0.try_into().unwrap()
41     }
42 }
43
44 impl Idx for ThreadId {
45     fn new(idx: usize) -> Self {
46         ThreadId(u32::try_from(idx).unwrap())
47     }
48     fn index(self) -> usize {
49         usize::try_from(self.0).unwrap()
50     }
51 }
52
53 impl From<u64> for ThreadId {
54     fn from(id: u64) -> Self {
55         Self(u32::try_from(id).unwrap())
56     }
57 }
58
59 impl From<u32> for ThreadId {
60     fn from(id: u32) -> Self {
61         Self(u32::try_from(id).unwrap())
62     }
63 }
64
65 impl ThreadId {
66     pub fn to_u32_scalar<'tcx>(&self) -> Scalar<Tag> {
67         Scalar::from_u32(u32::try_from(self.0).unwrap())
68     }
69 }
70
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);
75
76 impl BlockSetId {
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"))
80     }
81     pub fn to_u32_scalar<'tcx>(&self) -> Scalar<Tag> {
82         Scalar::from_u32(self.0.get())
83     }
84 }
85
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.
90     Enabled,
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.
95     Blocked(BlockSetId),
96     /// The thread has terminated its execution (we do not delete terminated
97     /// threads).
98     Terminated,
99 }
100
101 /// The join status of a thread.
102 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
103 enum ThreadJoinStatus {
104     /// The thread can be joined.
105     Joinable,
106     /// A thread is detached if its join handle was destroyed and no other
107     /// thread can join it.
108     Detached,
109     /// The thread was already joined by some thread and cannot be joined again.
110     Joined,
111 }
112
113 /// A thread.
114 pub struct Thread<'mir, 'tcx> {
115     state: ThreadState,
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>>>,
120     /// The join status.
121     join_status: ThreadJoinStatus,
122 }
123
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;
131                 return true;
132             }
133         }
134         false
135     }
136 }
137
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))?;
142         } else {
143             write!(f, "<unnamed>")?;
144         }
145         write!(f, "({:?}, {:?})", self.state, self.join_status)
146     }
147 }
148
149 impl<'mir, 'tcx> Default for Thread<'mir, 'tcx> {
150     fn default() -> Self {
151         Self {
152             state: ThreadState::Enabled,
153             thread_name: None,
154             stack: Vec::new(),
155             join_status: ThreadJoinStatus::Joinable,
156         }
157     }
158 }
159
160 /// A set of threads.
161 #[derive(Debug)]
162 pub struct ThreadManager<'mir, 'tcx> {
163     /// Identifier of the currently active thread.
164     active_thread: ThreadId,
165     /// Threads used in the program.
166     ///
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,
176 }
177
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);
186         Self {
187             active_thread: ThreadId::new(0),
188             threads: threads,
189             blockset_counter: 0,
190             thread_local_alloc_ids: Default::default(),
191             yield_active_thread: false,
192         }
193     }
194 }
195
196 impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
197     /// Check if we have an allocation for the given thread local static for the
198     /// active thread.
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()
201     }
202
203     /// Set the allocation id as the allocation id of the given thread local
204     /// static for the active thread.
205     ///
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
209             .borrow_mut()
210             .insert((def_id, self.active_thread), new_alloc_id)
211             .unwrap_none();
212     }
213
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
217     }
218
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
222     }
223
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());
228         new_thread_id
229     }
230
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());
236         active_thread_id
237     }
238
239     /// Get the id of the currently active thread.
240     fn get_active_thread_id(&self) -> ThreadId {
241         self.active_thread
242     }
243
244     /// Get the total number of threads that were ever spawn by this program.
245     fn get_total_thread_count(&self) -> usize {
246         self.threads.len()
247     }
248
249     /// Has the given thread terminated?
250     fn has_terminated(&self, thread_id: ThreadId) -> bool {
251         self.threads[thread_id].state == ThreadState::Terminated
252     }
253
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;
258     }
259
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]
263     }
264
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]
268     }
269
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");
275         }
276         self.threads[id].join_status = ThreadJoinStatus::Detached;
277         Ok(())
278     }
279
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");
284         }
285         if joined_thread_id == self.active_thread {
286             throw_ub_format!("trying to join itself");
287         }
288         assert!(
289             self.threads
290                 .iter()
291                 .all(|thread| thread.state != ThreadState::BlockedOnJoin(joined_thread_id)),
292             "a joinable thread already has threads waiting for its termination"
293         );
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);
300             trace!(
301                 "{:?} blocked on {:?} when trying to join",
302                 self.active_thread,
303                 joined_thread_id
304             );
305         }
306         Ok(())
307     }
308
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);
312     }
313
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 {
317             Ok(thread_name)
318         } else {
319             throw_ub_format!("thread {:?} has no name set", self.active_thread)
320         }
321     }
322
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)
327     }
328
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);
334     }
335
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;
343                 return Some(id);
344             }
345         }
346         None
347     }
348
349     /// Change the active thread to some enabled thread.
350     fn yield_active_thread(&mut self) {
351         self.yield_active_thread = true;
352     }
353
354     /// Decide which action to take next and on which thread.
355     ///
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;
367                 }
368             }
369             return Ok(SchedulingAction::ExecuteDtors);
370         }
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");
381             }
382             return Ok(SchedulingAction::Stop);
383         }
384         if self.threads[self.active_thread].state == ThreadState::Enabled
385             && !self.yield_active_thread
386         {
387             // The currently active thread is still enabled, just continue with it.
388             return Ok(SchedulingAction::ExecuteStep);
389         }
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;
395                     break;
396                 }
397             }
398         }
399         self.yield_active_thread = false;
400         if self.threads[self.active_thread].state == ThreadState::Enabled {
401             return Ok(SchedulingAction::ExecuteStep);
402         }
403         // We have not found a thread to execute.
404         if self.threads.iter().all(|thread| thread.state == ThreadState::Terminated) {
405             unreachable!();
406         } else {
407             throw_machine_stop!(TerminationInfo::Deadlock);
408         }
409     }
410 }
411
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(
420         &self,
421         val: &mut mir::interpret::ConstValue<'tcx>,
422     ) -> InterpResult<'tcx> {
423         let this = self.eval_context_ref();
424         match *val {
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);
428                 let tcx = this.tcx;
429                 let is_thread_local = |def_id| {
430                     tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
431                 };
432                 match alloc {
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)?;
435                     }
436                     _ => {}
437                 }
438             }
439             _ => {
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.
445             }
446         }
447         Ok(())
448     }
449
450     /// Get a thread-specific allocation id for the given thread-local static.
451     /// If needed, allocate a new one.
452     ///
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();
457         let tcx = this.tcx;
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.
461             Ok(new_alloc_id)
462         } else {
463             // We need to allocate a thread-specific allocation id for this
464             // thread-local static.
465             //
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.
469             //
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");
475             }
476             // Invoke the `const_eval_raw` query.
477             let instance = Instance::mono(tcx.tcx, def_id);
478             let gid = GlobalId { instance, promoted: None };
479             let raw_const =
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
482                     // for statics
483                     assert!(tcx.is_static(def_id));
484                     err
485                 })?;
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
493             // access.
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);
496             Ok(new_alloc_id)
497         }
498     }
499
500     #[inline]
501     fn create_thread(&mut self) -> InterpResult<'tcx, ThreadId> {
502         let this = self.eval_context_mut();
503         Ok(this.machine.threads.create_thread())
504     }
505
506     #[inline]
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)
510     }
511
512     #[inline]
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)
516     }
517
518     #[inline]
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))
522     }
523
524     #[inline]
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())
528     }
529
530     #[inline]
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())
534     }
535
536     #[inline]
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))
540     }
541
542     #[inline]
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);
546         Ok(())
547     }
548
549     #[inline]
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()
553     }
554
555     #[inline]
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()
559     }
560
561     #[inline]
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))
565     }
566
567     #[inline]
568     fn get_active_thread_name<'c>(&'c self) -> InterpResult<'tcx, &'c [u8]>
569     where
570         'mir: 'c,
571     {
572         let this = self.eval_context_ref();
573         this.machine.threads.get_thread_name()
574     }
575
576     #[inline]
577     fn create_blockset(&mut self) -> InterpResult<'tcx, BlockSetId> {
578         let this = self.eval_context_mut();
579         Ok(this.machine.threads.create_blockset())
580     }
581
582     #[inline]
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))
586     }
587
588     #[inline]
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))
592     }
593
594     #[inline]
595     fn yield_active_thread(&mut self) -> InterpResult<'tcx> {
596         let this = self.eval_context_mut();
597         this.machine.threads.yield_active_thread();
598         Ok(())
599     }
600
601     /// Decide which action to take next and on which thread.
602     #[inline]
603     fn schedule(&mut self) -> InterpResult<'tcx, SchedulingAction> {
604         let this = self.eval_context_mut();
605         this.machine.threads.schedule()
606     }
607 }