1use std::sync::atomic::Ordering::Relaxed;
4use std::task::Poll;
5use std::time::{Duration, SystemTime};
6use std::{io, mem};
7
8use rand::seq::IteratorRandom;
9use rustc_abi::ExternAbi;
10use rustc_const_eval::CTRL_C_RECEIVED;
11use rustc_data_structures::either::Either;
12use rustc_data_structures::fx::FxHashMap;
13use rustc_hir::def_id::DefId;
14use rustc_index::{Idx, IndexVec};
15use rustc_middle::mir::Mutability;
16use rustc_middle::ty::layout::TyAndLayout;
17use rustc_span::{DUMMY_SP, Span};
18use rustc_target::spec::Os;
19
20use crate::concurrency::GlobalDataRaceHandler;
21use crate::concurrency::blocking_io::InterestReceiver;
22use crate::shims::tls;
23use crate::*;
24
25#[derive(Clone, Copy, Debug, PartialEq)]
26enum SchedulingAction {
27 ExecuteStep,
29 SleepAndWaitForIo(Option<Duration>),
34}
35
36#[derive(Clone, Copy, Debug, PartialEq)]
38pub enum TlsAllocAction {
39 Deallocate,
41 Leak,
44}
45
46#[derive(Clone, Copy, Debug, PartialEq)]
48pub enum UnblockKind {
49 Ready,
51 TimedOut,
53}
54
55pub type DynUnblockCallback<'tcx> = DynMachineCallback<'tcx, UnblockKind>;
58
59#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
61pub struct ThreadId(u32);
62
63impl ThreadId {
64 pub fn to_u32(self) -> u32 {
65 self.0
66 }
67
68 pub fn new_unchecked(id: u32) -> Self {
70 Self(id)
71 }
72
73 pub const MAIN_THREAD: ThreadId = ThreadId(0);
74}
75
76impl Idx for ThreadId {
77 fn new(idx: usize) -> Self {
78 ThreadId(u32::try_from(idx).unwrap())
79 }
80
81 fn index(self) -> usize {
82 usize::try_from(self.0).unwrap()
83 }
84}
85
86impl From<ThreadId> for u64 {
87 fn from(t: ThreadId) -> Self {
88 t.0.into()
89 }
90}
91
92#[derive(Debug, Clone, PartialEq, Eq)]
94pub enum BlockReason {
95 Join(ThreadId),
98 Sleep,
100 Mutex,
102 Condvar,
104 RwLock,
106 Futex,
108 InitOnce,
110 Epoll,
112 Eventfd,
114 VirtualSocket,
116 IO,
118 Genmc,
121}
122
123enum ThreadState<'tcx> {
125 Enabled,
127 Blocked { reason: BlockReason, timeout: Option<Timeout>, callback: DynUnblockCallback<'tcx> },
129 Terminated,
132}
133
134impl<'tcx> std::fmt::Debug for ThreadState<'tcx> {
135 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136 match self {
137 Self::Enabled => write!(f, "Enabled"),
138 Self::Blocked { reason, timeout, .. } =>
139 f.debug_struct("Blocked").field("reason", reason).field("timeout", timeout).finish(),
140 Self::Terminated => write!(f, "Terminated"),
141 }
142 }
143}
144
145impl<'tcx> ThreadState<'tcx> {
146 fn is_enabled(&self) -> bool {
147 matches!(self, ThreadState::Enabled)
148 }
149
150 fn is_terminated(&self) -> bool {
151 matches!(self, ThreadState::Terminated)
152 }
153
154 fn is_blocked_on(&self, reason: &BlockReason) -> bool {
155 matches!(self, ThreadState::Blocked { reason: actual_reason, .. } if actual_reason == reason)
156 }
157}
158
159#[derive(Debug, Copy, Clone, PartialEq, Eq)]
161enum ThreadJoinStatus {
162 Joinable,
164 Detached,
167 Joined,
169}
170
171pub struct Thread<'tcx> {
173 state: ThreadState<'tcx>,
174
175 thread_name: Option<Vec<u8>>,
177
178 stack: Vec<Frame<'tcx, Provenance, FrameExtra<'tcx>>>,
180
181 pub(crate) origin_span: Span,
184
185 pub(crate) on_stack_empty: Option<StackEmptyCallback<'tcx>>,
190
191 top_user_relevant_frame: Option<usize>,
196
197 join_status: ThreadJoinStatus,
199
200 pub(crate) unwind_payloads: Vec<ImmTy<'tcx>>,
209
210 pub(crate) last_error: Option<MPlaceTy<'tcx>>,
212}
213
214pub type StackEmptyCallback<'tcx> =
215 Box<dyn FnMut(&mut MiriInterpCx<'tcx>) -> InterpResult<'tcx, Poll<()>> + 'tcx>;
216
217impl<'tcx> Thread<'tcx> {
218 fn thread_name(&self) -> Option<&[u8]> {
220 self.thread_name.as_deref()
221 }
222
223 pub fn is_enabled(&self) -> bool {
225 self.state.is_enabled()
226 }
227
228 fn thread_display_name(&self, id: ThreadId) -> String {
230 if let Some(ref thread_name) = self.thread_name {
231 String::from_utf8_lossy(thread_name).into_owned()
232 } else {
233 format!("unnamed-{}", id.index())
234 }
235 }
236
237 fn compute_top_user_relevant_frame(&self, skip: usize) -> Option<usize> {
243 let mut best = None;
245 for (idx, frame) in self.stack.iter().enumerate().rev().skip(skip) {
246 let relevance = frame.extra.user_relevance;
247 if relevance == u8::MAX {
248 return Some(idx);
250 }
251 if best.is_none_or(|(_best_idx, best_relevance)| best_relevance < relevance) {
252 best = Some((idx, relevance));
255 }
256 }
257 best.map(|(idx, _relevance)| idx)
258 }
259
260 pub fn recompute_top_user_relevant_frame(&mut self, skip: usize) {
263 self.top_user_relevant_frame = self.compute_top_user_relevant_frame(skip);
264 }
265
266 pub fn set_top_user_relevant_frame(&mut self, frame_idx: usize) {
269 debug_assert_eq!(Some(frame_idx), self.compute_top_user_relevant_frame(0));
270 self.top_user_relevant_frame = Some(frame_idx);
271 }
272
273 pub fn top_user_relevant_frame(&self) -> Option<usize> {
276 self.top_user_relevant_frame.or_else(|| self.stack.len().checked_sub(1))
280 }
281
282 pub fn current_user_relevance(&self) -> u8 {
283 self.top_user_relevant_frame()
284 .map(|frame_idx| self.stack[frame_idx].extra.user_relevance)
285 .unwrap_or(0)
286 }
287
288 pub fn current_user_relevant_span(&self) -> Span {
289 debug_assert_eq!(self.top_user_relevant_frame, self.compute_top_user_relevant_frame(0));
290 self.top_user_relevant_frame()
291 .map(|frame_idx| self.stack[frame_idx].current_span())
292 .unwrap_or(rustc_span::DUMMY_SP)
293 }
294}
295
296impl<'tcx> std::fmt::Debug for Thread<'tcx> {
297 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
298 write!(
299 f,
300 "{}({:?}, {:?})",
301 String::from_utf8_lossy(self.thread_name().unwrap_or(b"<unnamed>")),
302 self.state,
303 self.join_status
304 )
305 }
306}
307
308impl<'tcx> Thread<'tcx> {
309 fn new(name: Option<&str>, on_stack_empty: Option<StackEmptyCallback<'tcx>>) -> Self {
310 Self {
311 state: ThreadState::Enabled,
312 thread_name: name.map(|name| Vec::from(name.as_bytes())),
313 stack: Vec::new(),
314 origin_span: DUMMY_SP,
315 top_user_relevant_frame: None,
316 join_status: ThreadJoinStatus::Joinable,
317 unwind_payloads: Vec::new(),
318 last_error: None,
319 on_stack_empty,
320 }
321 }
322}
323
324impl VisitProvenance for Thread<'_> {
325 fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
326 let Thread {
327 unwind_payloads: panic_payload,
328 last_error,
329 stack,
330 origin_span: _,
331 top_user_relevant_frame: _,
332 state: _,
333 thread_name: _,
334 join_status: _,
335 on_stack_empty: _, } = self;
337
338 for payload in panic_payload {
339 payload.visit_provenance(visit);
340 }
341 last_error.visit_provenance(visit);
342 for frame in stack {
343 frame.visit_provenance(visit)
344 }
345 }
346}
347
348impl VisitProvenance for Frame<'_, Provenance, FrameExtra<'_>> {
349 fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
350 let return_place = self.return_place();
351 let Frame {
352 locals,
353 extra,
354 ..
356 } = self;
357
358 return_place.visit_provenance(visit);
360 for local in locals.iter() {
362 match local.as_mplace_or_imm() {
363 None => {}
364 Some(Either::Left((ptr, meta))) => {
365 ptr.visit_provenance(visit);
366 meta.visit_provenance(visit);
367 }
368 Some(Either::Right(imm)) => {
369 imm.visit_provenance(visit);
370 }
371 }
372 }
373
374 extra.visit_provenance(visit);
375 }
376}
377
378#[derive(Debug)]
380enum Timeout {
381 Monotonic(Instant),
382 RealTime(SystemTime),
383}
384
385impl Timeout {
386 fn get_wait_time(&self, clock: &MonotonicClock) -> Duration {
388 match self {
389 Timeout::Monotonic(instant) => instant.duration_since(clock.now()),
390 Timeout::RealTime(time) =>
391 time.duration_since(SystemTime::now()).unwrap_or(Duration::ZERO),
392 }
393 }
394
395 fn add_lossy(&self, duration: Duration) -> Self {
397 match self {
398 Timeout::Monotonic(i) => Timeout::Monotonic(i.add_lossy(duration)),
399 Timeout::RealTime(s) => {
400 Timeout::RealTime(
402 s.checked_add(duration)
403 .unwrap_or_else(|| s.checked_add(Duration::from_secs(3600)).unwrap()),
404 )
405 }
406 }
407 }
408}
409
410#[derive(Debug, Copy, Clone, PartialEq)]
412pub enum TimeoutClock {
413 Monotonic,
414 RealTime,
415}
416
417#[derive(Debug, Copy, Clone)]
419pub enum TimeoutAnchor {
420 Relative,
421 Absolute,
422}
423
424#[derive(Debug, Copy, Clone)]
426pub enum ThreadLookupError {
427 InvalidId,
429 Terminated(ThreadId),
431}
432
433#[derive(Debug)]
435pub struct ThreadManager<'tcx> {
436 active_thread: ThreadId,
438 threads: IndexVec<ThreadId, Thread<'tcx>>,
442 thread_local_allocs: FxHashMap<(DefId, ThreadId), StrictPointer>,
444 yield_active_thread: bool,
447 fixed_scheduling: bool,
449}
450
451impl VisitProvenance for ThreadManager<'_> {
452 fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
453 let ThreadManager {
454 threads,
455 thread_local_allocs,
456 active_thread: _,
457 yield_active_thread: _,
458 fixed_scheduling: _,
459 } = self;
460
461 for thread in threads {
462 thread.visit_provenance(visit);
463 }
464 for ptr in thread_local_allocs.values() {
465 ptr.visit_provenance(visit);
466 }
467 }
468}
469
470impl<'tcx> ThreadManager<'tcx> {
471 pub(crate) fn new(config: &MiriConfig) -> Self {
472 let mut threads = IndexVec::new();
473 threads.push(Thread::new(Some("main"), None));
475 Self {
476 active_thread: ThreadId::MAIN_THREAD,
477 threads,
478 thread_local_allocs: Default::default(),
479 yield_active_thread: false,
480 fixed_scheduling: config.fixed_scheduling,
481 }
482 }
483
484 pub(crate) fn init(
485 ecx: &mut MiriInterpCx<'tcx>,
486 on_main_stack_empty: StackEmptyCallback<'tcx>,
487 ) {
488 ecx.machine.threads.threads[ThreadId::MAIN_THREAD].on_stack_empty =
489 Some(on_main_stack_empty);
490 if ecx.tcx.sess.target.os != Os::Windows {
491 ecx.machine.threads.threads[ThreadId::MAIN_THREAD].join_status =
493 ThreadJoinStatus::Detached;
494 }
495 }
496
497 pub fn thread_id_try_from(&self, id: impl TryInto<u32>) -> Result<ThreadId, ThreadLookupError> {
501 if let Ok(id) = id.try_into()
502 && usize::try_from(id).is_ok_and(|id| id < self.threads.len())
503 {
504 let thread_id = ThreadId(id);
505 if self.threads[thread_id].state.is_terminated() {
506 Err(ThreadLookupError::Terminated(thread_id))
507 } else {
508 Ok(thread_id)
509 }
510 } else {
511 Err(ThreadLookupError::InvalidId)
512 }
513 }
514
515 fn get_thread_local_alloc_id(&self, def_id: DefId) -> Option<StrictPointer> {
518 self.thread_local_allocs.get(&(def_id, self.active_thread)).cloned()
519 }
520
521 fn set_thread_local_alloc(&mut self, def_id: DefId, ptr: StrictPointer) {
526 self.thread_local_allocs.try_insert((def_id, self.active_thread), ptr).unwrap();
527 }
528
529 pub fn active_thread_stack(&self) -> &[Frame<'tcx, Provenance, FrameExtra<'tcx>>] {
531 &self.threads[self.active_thread].stack
532 }
533
534 pub fn active_thread_stack_mut(
536 &mut self,
537 ) -> &mut Vec<Frame<'tcx, Provenance, FrameExtra<'tcx>>> {
538 &mut self.threads[self.active_thread].stack
539 }
540
541 pub fn all_blocked_stacks(
542 &self,
543 ) -> impl Iterator<Item = (ThreadId, &[Frame<'tcx, Provenance, FrameExtra<'tcx>>])> {
544 self.threads
545 .iter_enumerated()
546 .filter(|(_id, t)| matches!(t.state, ThreadState::Blocked { .. }))
547 .map(|(id, t)| (id, &t.stack[..]))
548 }
549
550 fn create_thread(&mut self, on_stack_empty: StackEmptyCallback<'tcx>) -> ThreadId {
552 let new_thread_id = ThreadId::new(self.threads.len());
553 self.threads.push(Thread::new(None, Some(on_stack_empty)));
554 new_thread_id
555 }
556
557 fn set_active_thread_id(&mut self, id: ThreadId) -> ThreadId {
559 assert!(id.index() < self.threads.len());
560 info!(
561 "---------- Now executing on thread `{}` (previous: `{}`) ----------------------------------------",
562 self.get_thread_display_name(id),
563 self.get_thread_display_name(self.active_thread)
564 );
565 std::mem::replace(&mut self.active_thread, id)
566 }
567
568 pub fn active_thread(&self) -> ThreadId {
570 self.active_thread
571 }
572
573 pub fn get_total_thread_count(&self) -> usize {
575 self.threads.len()
576 }
577
578 pub fn get_live_thread_count(&self) -> usize {
581 self.threads.iter().filter(|t| !t.state.is_terminated()).count()
582 }
583
584 fn has_terminated(&self, thread_id: ThreadId) -> bool {
586 self.threads[thread_id].state.is_terminated()
587 }
588
589 fn have_all_terminated(&self) -> bool {
591 self.threads.iter().all(|thread| thread.state.is_terminated())
592 }
593
594 fn enable_thread(&mut self, thread_id: ThreadId) {
596 assert!(self.has_terminated(thread_id));
597 self.threads[thread_id].state = ThreadState::Enabled;
598 }
599
600 pub fn active_thread_mut(&mut self) -> &mut Thread<'tcx> {
602 &mut self.threads[self.active_thread]
603 }
604
605 pub fn active_thread_ref(&self) -> &Thread<'tcx> {
607 &self.threads[self.active_thread]
608 }
609
610 pub fn thread_ref(&self, thread_id: ThreadId) -> &Thread<'tcx> {
611 &self.threads[thread_id]
612 }
613
614 fn detach_thread(&mut self, id: ThreadId, allow_terminated_joined: bool) -> InterpResult<'tcx> {
623 trace!("detaching {:?}", id);
625
626 let is_ub = if allow_terminated_joined && self.threads[id].state.is_terminated() {
627 self.threads[id].join_status == ThreadJoinStatus::Detached
629 } else {
630 self.threads[id].join_status != ThreadJoinStatus::Joinable
631 };
632 if is_ub {
633 throw_ub_format!("trying to detach thread that was already detached or joined");
634 }
635
636 self.threads[id].join_status = ThreadJoinStatus::Detached;
637 interp_ok(())
638 }
639
640 pub fn set_thread_name(&mut self, thread: ThreadId, new_thread_name: Vec<u8>) {
642 self.threads[thread].thread_name = Some(new_thread_name);
643 }
644
645 pub fn get_thread_name(&self, thread: ThreadId) -> Option<&[u8]> {
647 self.threads[thread].thread_name()
648 }
649
650 pub fn get_thread_display_name(&self, thread: ThreadId) -> String {
651 self.threads[thread].thread_display_name(thread)
652 }
653
654 fn block_thread(
656 &mut self,
657 reason: BlockReason,
658 timeout: Option<Timeout>,
659 callback: DynUnblockCallback<'tcx>,
660 ) {
661 let state = &mut self.threads[self.active_thread].state;
662 assert!(state.is_enabled());
663 *state = ThreadState::Blocked { reason, timeout, callback }
664 }
665
666 fn yield_active_thread(&mut self) {
668 self.yield_active_thread = true;
672 }
673}
674
675impl<'tcx> EvalContextPrivExt<'tcx> for MiriInterpCx<'tcx> {}
676trait EvalContextPrivExt<'tcx>: MiriInterpCxExt<'tcx> {
677 #[inline]
678 fn run_on_stack_empty(&mut self) -> InterpResult<'tcx, Poll<()>> {
679 let this = self.eval_context_mut();
680 let active_thread = this.active_thread_mut();
681 active_thread.origin_span = DUMMY_SP; let mut callback = active_thread
683 .on_stack_empty
684 .take()
685 .expect("`on_stack_empty` not set up, or already running");
686 let res = callback(this)?;
687 this.active_thread_mut().on_stack_empty = Some(callback);
688 interp_ok(res)
689 }
690
691 fn schedule(&mut self) -> InterpResult<'tcx, SchedulingAction> {
700 let this = self.eval_context_mut();
701
702 if this.machine.data_race.as_genmc_ref().is_some() {
704 loop {
705 let genmc_ctx = this.machine.data_race.as_genmc_ref().unwrap();
706 let Some(next_thread_id) = genmc_ctx.schedule_thread(this)? else {
707 return interp_ok(SchedulingAction::ExecuteStep);
708 };
709 if this.machine.threads.threads[next_thread_id]
711 .state
712 .is_blocked_on(&BlockReason::Genmc)
713 {
714 info!(
715 "GenMC: scheduling blocked thread {next_thread_id:?}, so we unblock it now."
716 );
717 this.unblock_thread(next_thread_id, BlockReason::Genmc)?;
718 }
719 let thread_manager = &mut this.machine.threads;
722 if thread_manager.threads[next_thread_id].state.is_enabled() {
723 thread_manager.active_thread = next_thread_id;
725 return interp_ok(SchedulingAction::ExecuteStep);
726 }
727 }
728 }
729
730 let thread_manager = &this.machine.threads;
732 if thread_manager.threads[thread_manager.active_thread].state.is_enabled()
734 && !thread_manager.yield_active_thread
735 {
736 return interp_ok(SchedulingAction::ExecuteStep);
738 }
739
740 if this.machine.communicate() {
744 this.poll_and_unblock(Some(Duration::ZERO))?;
751 }
752
753 let potential_sleep_time = this.unblock_expired_timeouts()?;
759
760 let thread_manager = &mut this.machine.threads;
761 let rng = this.machine.rng.get_mut();
762
763 let mut threads_iter = thread_manager
770 .threads
771 .iter_enumerated()
772 .skip(thread_manager.active_thread.index() + 1)
773 .chain(
774 thread_manager
775 .threads
776 .iter_enumerated()
777 .take(thread_manager.active_thread.index() + 1),
778 )
779 .filter(|(_id, thread)| thread.state.is_enabled());
780 let new_thread = if thread_manager.fixed_scheduling {
782 threads_iter.next()
783 } else {
784 threads_iter.choose(rng)
785 };
786
787 if let Some((id, _thread)) = new_thread {
788 if thread_manager.active_thread != id {
789 info!(
790 "---------- Now executing on thread `{}` (previous: `{}`) ----------------------------------------",
791 thread_manager.get_thread_display_name(id),
792 thread_manager.get_thread_display_name(thread_manager.active_thread)
793 );
794 thread_manager.active_thread = id;
795 }
796 }
797 thread_manager.yield_active_thread = false;
799
800 if thread_manager.threads[thread_manager.active_thread].state.is_enabled() {
801 return interp_ok(SchedulingAction::ExecuteStep);
802 }
803 if thread_manager.threads.iter().all(|thread| thread.state.is_terminated()) {
805 unreachable!("all threads terminated without the main thread terminating?!");
806 } else if let Some(sleep_time) = potential_sleep_time {
807 interp_ok(SchedulingAction::SleepAndWaitForIo(Some(sleep_time)))
811 } else if thread_manager
812 .threads
813 .iter()
814 .any(|thread| thread.state.is_blocked_on(&BlockReason::IO))
815 {
816 interp_ok(SchedulingAction::SleepAndWaitForIo(None))
820 } else {
821 throw_machine_stop!(TerminationInfo::GlobalDeadlock);
822 }
823 }
824
825 fn poll_and_unblock(&mut self, timeout: Option<Duration>) -> InterpResult<'tcx> {
828 let this = self.eval_context_mut();
829
830 let ready = match this.machine.blocking_io.poll(timeout) {
831 Ok(ready) => ready,
832 Err(e) if e.kind() == io::ErrorKind::Interrupted => return interp_ok(()),
834 Err(e) => panic!("unexpected error while polling: {e}"),
837 };
838
839 ready.into_iter().try_for_each(|(receiver, _source)| {
840 match receiver {
841 InterestReceiver::UnblockThread(thread_id) =>
842 this.unblock_thread(thread_id, BlockReason::IO),
843 }
844 })
845 }
846
847 fn unblock_expired_timeouts(&mut self) -> InterpResult<'tcx, Option<Duration>> {
852 let this = self.eval_context_mut();
853 let clock = &this.machine.monotonic_clock;
854
855 let mut min_wait_time = Option::<Duration>::None;
856 let mut callbacks = Vec::new();
857
858 for (id, thread) in this.machine.threads.threads.iter_enumerated_mut() {
859 match &thread.state {
860 ThreadState::Blocked { timeout: Some(timeout), .. } => {
861 let wait_time = timeout.get_wait_time(clock);
862 if wait_time.is_zero() {
863 let old_state = mem::replace(&mut thread.state, ThreadState::Enabled);
865 let ThreadState::Blocked { callback, .. } = old_state else {
866 unreachable!()
867 };
868 callbacks.push((id, callback));
870 } else {
871 min_wait_time = Some(wait_time.min(min_wait_time.unwrap_or(Duration::MAX)));
874 }
875 }
876 _ => {}
877 }
878 }
879
880 for (thread, callback) in callbacks {
881 let old_thread = this.machine.threads.set_active_thread_id(thread);
888 callback.call(this, UnblockKind::TimedOut)?;
889 this.machine.threads.set_active_thread_id(old_thread);
890 }
891
892 interp_ok(min_wait_time)
893 }
894}
895
896impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
898pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
899 #[inline]
900 fn thread_id_try_from(&self, id: impl TryInto<u32>) -> Result<ThreadId, ThreadLookupError> {
901 self.eval_context_ref().machine.threads.thread_id_try_from(id)
902 }
903
904 fn get_or_create_thread_local_alloc(
907 &mut self,
908 def_id: DefId,
909 ) -> InterpResult<'tcx, StrictPointer> {
910 let this = self.eval_context_mut();
911 let tcx = this.tcx;
912 if let Some(old_alloc) = this.machine.threads.get_thread_local_alloc_id(def_id) {
913 interp_ok(old_alloc)
916 } else {
917 if tcx.is_foreign_item(def_id) {
921 throw_unsup_format!("foreign thread-local statics are not supported");
922 }
923 let params = this.machine.get_default_alloc_params();
924 let alloc = this.ctfe_query(|tcx| tcx.eval_static_initializer(def_id))?;
925 let mut alloc = alloc.inner().adjust_from_tcx(
927 &this.tcx,
928 |bytes, align| {
929 interp_ok(MiriAllocBytes::from_bytes(
930 std::borrow::Cow::Borrowed(bytes),
931 align,
932 params,
933 ))
934 },
935 |ptr| this.global_root_pointer(ptr),
936 )?;
937 alloc.mutability = Mutability::Mut;
939 let ptr = this.insert_allocation(alloc, MiriMemoryKind::Tls.into())?;
941 this.machine.threads.set_thread_local_alloc(def_id, ptr);
942 interp_ok(ptr)
943 }
944 }
945
946 #[inline]
948 fn start_regular_thread(
949 &mut self,
950 thread: Option<MPlaceTy<'tcx>>,
951 start_routine: Pointer,
952 start_abi: ExternAbi,
953 func_arg: ImmTy<'tcx>,
954 ret_layout: TyAndLayout<'tcx>,
955 ) -> InterpResult<'tcx, ThreadId> {
956 let this = self.eval_context_mut();
957
958 let current_span = this.machine.current_user_relevant_span();
960 let new_thread_id = this.machine.threads.create_thread({
961 let mut state = tls::TlsDtorsState::default();
962 Box::new(move |m| state.on_stack_empty(m))
963 });
964 match &mut this.machine.data_race {
965 GlobalDataRaceHandler::None => {}
966 GlobalDataRaceHandler::Vclocks(data_race) =>
967 data_race.thread_created(&this.machine.threads, new_thread_id, current_span),
968 GlobalDataRaceHandler::Genmc(genmc_ctx) =>
969 genmc_ctx.handle_thread_create(
970 &this.machine.threads,
971 start_routine,
972 &func_arg,
973 new_thread_id,
974 )?,
975 }
976 if let Some(thread_info_place) = thread {
979 this.write_scalar(
980 Scalar::from_uint(new_thread_id.to_u32(), thread_info_place.layout.size),
981 &thread_info_place,
982 )?;
983 }
984
985 let old_thread_id = this.machine.threads.set_active_thread_id(new_thread_id);
988
989 if let Some(cpuset) = this.machine.thread_cpu_affinity.get(&old_thread_id).cloned() {
991 this.machine.thread_cpu_affinity.insert(new_thread_id, cpuset);
992 }
993
994 let instance = this.get_ptr_fn(start_routine)?.as_instance()?;
996
997 let ret_place = this.allocate(ret_layout, MiriMemoryKind::Machine.into())?;
1001
1002 this.call_thread_root_function(
1003 instance,
1004 start_abi,
1005 &[func_arg],
1006 Some(&ret_place),
1007 current_span,
1008 )?;
1009
1010 this.machine.threads.set_active_thread_id(old_thread_id);
1012
1013 interp_ok(new_thread_id)
1014 }
1015
1016 fn terminate_active_thread(&mut self, tls_alloc_action: TlsAllocAction) -> InterpResult<'tcx> {
1021 let this = self.eval_context_mut();
1022
1023 let thread = this.active_thread_mut();
1025 assert!(thread.stack.is_empty(), "only threads with an empty stack can be terminated");
1026 thread.state = ThreadState::Terminated;
1027
1028 let gone_thread = this.active_thread();
1030 {
1031 let mut free_tls_statics = Vec::new();
1032 this.machine.threads.thread_local_allocs.retain(|&(_def_id, thread), &mut alloc_id| {
1033 if thread != gone_thread {
1034 return true;
1036 }
1037 free_tls_statics.push(alloc_id);
1040 false
1041 });
1042 for ptr in free_tls_statics {
1044 match tls_alloc_action {
1045 TlsAllocAction::Deallocate =>
1046 this.deallocate_ptr(ptr.into(), None, MiriMemoryKind::Tls.into())?,
1047 TlsAllocAction::Leak =>
1048 if let Some(alloc) = ptr.provenance.get_alloc_id() {
1049 trace!(
1050 "Thread-local static leaked and stored as static root: {:?}",
1051 alloc
1052 );
1053 this.machine.static_roots.push(alloc);
1054 },
1055 }
1056 }
1057 }
1058
1059 match &mut this.machine.data_race {
1060 GlobalDataRaceHandler::None => {}
1061 GlobalDataRaceHandler::Vclocks(data_race) =>
1062 data_race.thread_terminated(&this.machine.threads),
1063 GlobalDataRaceHandler::Genmc(genmc_ctx) => {
1064 genmc_ctx.handle_thread_finish(&this.machine.threads)
1067 }
1068 }
1069
1070 let unblock_reason = BlockReason::Join(gone_thread);
1072 let threads = &this.machine.threads.threads;
1073 let joining_threads = threads
1074 .iter_enumerated()
1075 .filter(|(_, thread)| thread.state.is_blocked_on(&unblock_reason))
1076 .map(|(id, _)| id)
1077 .collect::<Vec<_>>();
1078 for thread in joining_threads {
1079 this.unblock_thread(thread, unblock_reason.clone())?;
1080 }
1081
1082 interp_ok(())
1083 }
1084
1085 #[inline]
1088 fn block_thread(
1089 &mut self,
1090 reason: BlockReason,
1091 timeout: Option<(TimeoutClock, TimeoutAnchor, Duration)>,
1092 callback: DynUnblockCallback<'tcx>,
1093 ) {
1094 let this = self.eval_context_mut();
1095 if timeout.is_some() && this.machine.data_race.as_genmc_ref().is_some() {
1096 panic!("Unimplemented: Timeouts not yet supported in GenMC mode.");
1097 }
1098 let timeout = timeout.map(|(clock, anchor, duration)| {
1099 let anchor = match clock {
1100 TimeoutClock::RealTime => {
1101 assert!(
1102 this.machine.communicate(),
1103 "cannot have `RealTime` timeout with isolation enabled!"
1104 );
1105 Timeout::RealTime(match anchor {
1106 TimeoutAnchor::Absolute => SystemTime::UNIX_EPOCH,
1107 TimeoutAnchor::Relative => SystemTime::now(),
1108 })
1109 }
1110 TimeoutClock::Monotonic =>
1111 Timeout::Monotonic(match anchor {
1112 TimeoutAnchor::Absolute => this.machine.monotonic_clock.epoch(),
1113 TimeoutAnchor::Relative => this.machine.monotonic_clock.now(),
1114 }),
1115 };
1116 anchor.add_lossy(duration)
1117 });
1118 this.machine.threads.block_thread(reason, timeout, callback);
1119 }
1120
1121 fn unblock_thread(&mut self, thread: ThreadId, reason: BlockReason) -> InterpResult<'tcx> {
1124 let this = self.eval_context_mut();
1125 let old_state =
1126 mem::replace(&mut this.machine.threads.threads[thread].state, ThreadState::Enabled);
1127 let callback = match old_state {
1128 ThreadState::Blocked { reason: actual_reason, callback, .. } => {
1129 assert_eq!(
1130 reason, actual_reason,
1131 "unblock_thread: thread was blocked for the wrong reason"
1132 );
1133 callback
1134 }
1135 _ => panic!("unblock_thread: thread was not blocked"),
1136 };
1137 let old_thread = this.machine.threads.set_active_thread_id(thread);
1139 callback.call(this, UnblockKind::Ready)?;
1140 this.machine.threads.set_active_thread_id(old_thread);
1141 interp_ok(())
1142 }
1143
1144 #[inline]
1145 fn detach_thread(
1146 &mut self,
1147 thread_id: ThreadId,
1148 allow_terminated_joined: bool,
1149 ) -> InterpResult<'tcx> {
1150 let this = self.eval_context_mut();
1151 this.machine.threads.detach_thread(thread_id, allow_terminated_joined)
1152 }
1153
1154 fn join_thread(
1158 &mut self,
1159 joined_thread_id: ThreadId,
1160 success_retval: Scalar,
1161 return_dest: &MPlaceTy<'tcx>,
1162 ) -> InterpResult<'tcx> {
1163 let this = self.eval_context_mut();
1164 let thread_mgr = &mut this.machine.threads;
1165 if thread_mgr.threads[joined_thread_id].join_status == ThreadJoinStatus::Detached {
1166 throw_ub_format!("trying to join a detached thread");
1168 }
1169
1170 fn after_join<'tcx>(
1171 this: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
1172 joined_thread_id: ThreadId,
1173 success_retval: Scalar,
1174 return_dest: &MPlaceTy<'tcx>,
1175 ) -> InterpResult<'tcx> {
1176 let threads = &this.machine.threads;
1177 match &mut this.machine.data_race {
1178 GlobalDataRaceHandler::None => {}
1179 GlobalDataRaceHandler::Vclocks(data_race) =>
1180 data_race.thread_joined(threads, joined_thread_id),
1181 GlobalDataRaceHandler::Genmc(genmc_ctx) =>
1182 genmc_ctx.handle_thread_join(threads.active_thread, joined_thread_id)?,
1183 }
1184 this.write_scalar(success_retval, return_dest)?;
1185 interp_ok(())
1186 }
1187
1188 thread_mgr.threads[joined_thread_id].join_status = ThreadJoinStatus::Joined;
1191 if !thread_mgr.threads[joined_thread_id].state.is_terminated() {
1192 trace!(
1193 "{:?} blocked on {:?} when trying to join",
1194 thread_mgr.active_thread, joined_thread_id
1195 );
1196 if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
1197 genmc_ctx.handle_thread_join(thread_mgr.active_thread, joined_thread_id)?;
1198 }
1199
1200 let dest = return_dest.clone();
1203 thread_mgr.block_thread(
1204 BlockReason::Join(joined_thread_id),
1205 None,
1206 callback!(
1207 @capture<'tcx> {
1208 joined_thread_id: ThreadId,
1209 dest: MPlaceTy<'tcx>,
1210 success_retval: Scalar,
1211 }
1212 |this, unblock: UnblockKind| {
1213 assert_eq!(unblock, UnblockKind::Ready);
1214 after_join(this, joined_thread_id, success_retval, &dest)
1215 }
1216 ),
1217 );
1218 } else {
1219 after_join(this, joined_thread_id, success_retval, return_dest)?;
1221 }
1222 interp_ok(())
1223 }
1224
1225 fn join_thread_exclusive(
1230 &mut self,
1231 joined_thread_id: ThreadId,
1232 success_retval: Scalar,
1233 return_dest: &MPlaceTy<'tcx>,
1234 ) -> InterpResult<'tcx> {
1235 let this = self.eval_context_mut();
1236 let threads = &this.machine.threads.threads;
1237 if threads[joined_thread_id].join_status == ThreadJoinStatus::Joined {
1238 throw_ub_format!("trying to join an already joined thread");
1239 }
1240
1241 if joined_thread_id == this.machine.threads.active_thread {
1242 throw_ub_format!("trying to join itself");
1243 }
1244
1245 assert!(
1247 threads.iter().all(|thread| {
1248 !thread.state.is_blocked_on(&BlockReason::Join(joined_thread_id))
1249 }),
1250 "this thread already has threads waiting for its termination"
1251 );
1252
1253 this.join_thread(joined_thread_id, success_retval, return_dest)
1254 }
1255
1256 #[inline]
1257 fn active_thread(&self) -> ThreadId {
1258 let this = self.eval_context_ref();
1259 this.machine.threads.active_thread()
1260 }
1261
1262 #[inline]
1263 fn active_thread_mut(&mut self) -> &mut Thread<'tcx> {
1264 let this = self.eval_context_mut();
1265 this.machine.threads.active_thread_mut()
1266 }
1267
1268 #[inline]
1269 fn active_thread_ref(&self) -> &Thread<'tcx> {
1270 let this = self.eval_context_ref();
1271 this.machine.threads.active_thread_ref()
1272 }
1273
1274 #[inline]
1275 fn get_total_thread_count(&self) -> usize {
1276 let this = self.eval_context_ref();
1277 this.machine.threads.get_total_thread_count()
1278 }
1279
1280 #[inline]
1281 fn have_all_terminated(&self) -> bool {
1282 let this = self.eval_context_ref();
1283 this.machine.threads.have_all_terminated()
1284 }
1285
1286 #[inline]
1287 fn enable_thread(&mut self, thread_id: ThreadId) {
1288 let this = self.eval_context_mut();
1289 this.machine.threads.enable_thread(thread_id);
1290 }
1291
1292 #[inline]
1293 fn active_thread_stack<'a>(&'a self) -> &'a [Frame<'tcx, Provenance, FrameExtra<'tcx>>] {
1294 let this = self.eval_context_ref();
1295 this.machine.threads.active_thread_stack()
1296 }
1297
1298 #[inline]
1299 fn active_thread_stack_mut<'a>(
1300 &'a mut self,
1301 ) -> &'a mut Vec<Frame<'tcx, Provenance, FrameExtra<'tcx>>> {
1302 let this = self.eval_context_mut();
1303 this.machine.threads.active_thread_stack_mut()
1304 }
1305
1306 #[inline]
1308 fn set_thread_name(&mut self, thread: ThreadId, new_thread_name: Vec<u8>) {
1309 self.eval_context_mut().machine.threads.set_thread_name(thread, new_thread_name);
1310 }
1311
1312 #[inline]
1313 fn get_thread_name<'c>(&'c self, thread: ThreadId) -> Option<&'c [u8]>
1314 where
1315 'tcx: 'c,
1316 {
1317 self.eval_context_ref().machine.threads.get_thread_name(thread)
1318 }
1319
1320 #[inline]
1321 fn yield_active_thread(&mut self) {
1322 self.eval_context_mut().machine.threads.yield_active_thread();
1323 }
1324
1325 #[inline]
1326 fn maybe_preempt_active_thread(&mut self) {
1327 use rand::Rng as _;
1328
1329 let this = self.eval_context_mut();
1330 if !this.machine.threads.fixed_scheduling
1331 && this.machine.rng.get_mut().random_bool(this.machine.preemption_rate)
1332 {
1333 this.yield_active_thread();
1334 }
1335 }
1336
1337 fn run_threads(&mut self) -> InterpResult<'tcx, !> {
1340 let this = self.eval_context_mut();
1341 loop {
1342 if CTRL_C_RECEIVED.load(Relaxed) {
1343 this.machine.handle_abnormal_termination();
1344 throw_machine_stop!(TerminationInfo::Interrupted);
1345 }
1346 match this.schedule()? {
1347 SchedulingAction::ExecuteStep => {
1348 if !this.step()? {
1349 match this.run_on_stack_empty()? {
1351 Poll::Pending => {} Poll::Ready(()) =>
1353 this.terminate_active_thread(TlsAllocAction::Deallocate)?,
1354 }
1355 }
1356 }
1357 SchedulingAction::SleepAndWaitForIo(duration) => {
1358 if this.machine.communicate() {
1359 this.poll_and_unblock(duration)?;
1364 } else {
1365 let duration = duration.expect(
1366 "Infinite sleep should not be triggered when isolation is enabled",
1367 );
1368 this.machine.monotonic_clock.sleep(duration);
1369 }
1370 }
1371 }
1372 }
1373 }
1374}