1use std::ffi::{OsStr, OsString};
4use std::num::NonZeroI32;
5use std::panic::{self, AssertUnwindSafe};
6use std::path::PathBuf;
7use std::rc::Rc;
8use std::task::Poll;
9use std::{iter, thread};
10
11use rustc_abi::ExternAbi;
12use rustc_data_structures::fx::{FxHashMap, FxHashSet};
13use rustc_errors::FatalErrorMarker;
14use rustc_hir::def::Namespace;
15use rustc_hir::def_id::DefId;
16use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutCx};
17use rustc_middle::ty::{self, Ty, TyCtxt};
18use rustc_session::config::EntryFnType;
19use rustc_target::spec::Os;
20
21use crate::concurrency::GenmcCtx;
22use crate::concurrency::thread::TlsAllocAction;
23use crate::diagnostics::report_leaks;
24use crate::helpers::is_no_core;
25use crate::shims::{global_ctor, tls};
26use crate::*;
27
28#[derive(Copy, Clone, Debug)]
29pub enum MiriEntryFnType {
30 MiriStart,
31 Rustc(EntryFnType),
32}
33
34const MAIN_THREAD_YIELDS_AT_SHUTDOWN: u32 = 256;
38
39#[derive(Clone)]
41pub struct MiriConfig {
42 pub env: Vec<(OsString, OsString)>,
45 pub validation: ValidationMode,
47 pub borrow_tracker: Option<BorrowTrackerMethod>,
49 pub check_alignment: AlignmentCheck,
51 pub isolated_op: IsolatedOp,
53 pub ignore_leaks: bool,
55 pub forwarded_env_vars: Vec<String>,
57 pub set_env_vars: FxHashMap<String, String>,
59 pub args: Vec<String>,
61 pub seed: Option<u64>,
63 pub tracked_pointer_tags: FxHashSet<BorTag>,
65 pub tracked_alloc_ids: FxHashSet<AllocId>,
67 pub track_alloc_accesses: bool,
69 pub data_race_detector: bool,
71 pub weak_memory_emulation: bool,
73 pub genmc_config: Option<GenmcConfig>,
75 pub track_outdated_loads: bool,
77 pub cmpxchg_weak_failure_rate: f64,
80 pub measureme_out: Option<String>,
83 pub backtrace_style: BacktraceStyle,
85 pub provenance_mode: ProvenanceMode,
87 pub mute_stdout_stderr: bool,
90 pub preemption_rate: f64,
92 pub report_progress: Option<u32>,
94 pub native_lib: Vec<PathBuf>,
96 pub native_lib_enable_tracing: bool,
98 pub gc_interval: u32,
100 pub num_cpus: u32,
102 pub page_size: Option<u64>,
104 pub collect_leak_backtraces: bool,
106 pub address_reuse_rate: f64,
108 pub address_reuse_cross_thread_rate: f64,
110 pub fixed_scheduling: bool,
112 pub force_intrinsic_fallback: bool,
114 pub float_nondet: bool,
116 pub float_rounding_error: FloatRoundingErrorMode,
118 pub short_fd_operations: bool,
120 pub user_relevant_crates: Vec<String>,
122}
123
124impl Default for MiriConfig {
125 fn default() -> MiriConfig {
126 MiriConfig {
127 env: vec![],
128 validation: ValidationMode::Shallow,
129 borrow_tracker: Some(BorrowTrackerMethod::StackedBorrows),
130 check_alignment: AlignmentCheck::Int,
131 isolated_op: IsolatedOp::Reject(RejectOpWith::Abort),
132 ignore_leaks: false,
133 forwarded_env_vars: vec![],
134 set_env_vars: FxHashMap::default(),
135 args: vec![],
136 seed: None,
137 tracked_pointer_tags: FxHashSet::default(),
138 tracked_alloc_ids: FxHashSet::default(),
139 track_alloc_accesses: false,
140 data_race_detector: true,
141 weak_memory_emulation: true,
142 genmc_config: None,
143 track_outdated_loads: false,
144 cmpxchg_weak_failure_rate: 0.8, measureme_out: None,
146 backtrace_style: BacktraceStyle::Short,
147 provenance_mode: ProvenanceMode::Default,
148 mute_stdout_stderr: false,
149 preemption_rate: 0.01, report_progress: None,
151 native_lib: vec![],
152 native_lib_enable_tracing: false,
153 gc_interval: 10_000,
154 num_cpus: 1,
155 page_size: None,
156 collect_leak_backtraces: true,
157 address_reuse_rate: 0.5,
158 address_reuse_cross_thread_rate: 0.1,
159 fixed_scheduling: false,
160 force_intrinsic_fallback: false,
161 float_nondet: true,
162 float_rounding_error: FloatRoundingErrorMode::Random,
163 short_fd_operations: true,
164 user_relevant_crates: vec![],
165 }
166 }
167}
168
169#[derive(Debug)]
171enum MainThreadState<'tcx> {
172 GlobalCtors {
173 ctor_state: global_ctor::GlobalCtorState<'tcx>,
174 entry_id: DefId,
176 entry_type: MiriEntryFnType,
177 argc: ImmTy<'tcx>,
179 argv: ImmTy<'tcx>,
180 },
181 Running,
182 TlsDtors(tls::TlsDtorsState<'tcx>),
183 Yield {
184 remaining: u32,
185 },
186 Done,
187}
188
189impl<'tcx> MainThreadState<'tcx> {
190 fn on_main_stack_empty(
191 &mut self,
192 this: &mut MiriInterpCx<'tcx>,
193 ) -> InterpResult<'tcx, Poll<()>> {
194 use MainThreadState::*;
195 match self {
196 GlobalCtors { ctor_state, entry_id, entry_type, argc, argv } => {
197 match ctor_state.on_stack_empty(this)? {
198 Poll::Pending => {} Poll::Ready(()) => {
200 call_main(this, *entry_id, *entry_type, argc.clone(), argv.clone())?;
201 *self = Running;
202 }
203 }
204 }
205 Running => {
206 *self = TlsDtors(Default::default());
207 }
208 TlsDtors(state) =>
209 match state.on_stack_empty(this)? {
210 Poll::Pending => {} Poll::Ready(()) => {
212 if this.machine.data_race.as_genmc_ref().is_some() {
213 *self = Done;
216 } else {
217 if this.machine.preemption_rate > 0.0 {
220 *self = Yield { remaining: MAIN_THREAD_YIELDS_AT_SHUTDOWN };
223 } else {
224 *self = Done;
227 }
228 }
229 }
230 },
231 Yield { remaining } =>
232 match remaining.checked_sub(1) {
233 None => *self = Done,
234 Some(new_remaining) => {
235 *remaining = new_remaining;
236 this.yield_active_thread();
237 }
238 },
239 Done => {
240 let ret_place = this.machine.main_fn_ret_place.clone().unwrap();
242 let exit_code = this.read_target_isize(&ret_place)?;
243 let exit_code = i32::try_from(exit_code).unwrap_or(if exit_code >= 0 {
246 i32::MAX
247 } else {
248 i32::MIN
249 });
250 this.terminate_active_thread(TlsAllocAction::Leak)?;
253
254 if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
256 genmc_ctx.handle_exit(
258 ThreadId::MAIN_THREAD,
259 exit_code,
260 crate::concurrency::ExitType::MainThreadFinish,
261 )?;
262 } else {
263 throw_machine_stop!(TerminationInfo::Exit {
265 code: exit_code,
266 leak_check: true
267 });
268 }
269 }
270 }
271 interp_ok(Poll::Pending)
272 }
273}
274
275pub fn create_ecx<'tcx>(
278 tcx: TyCtxt<'tcx>,
279 entry_id: DefId,
280 entry_type: MiriEntryFnType,
281 config: &MiriConfig,
282 genmc_ctx: Option<Rc<GenmcCtx>>,
283) -> InterpResult<'tcx, InterpCx<'tcx, MiriMachine<'tcx>>> {
284 let typing_env = ty::TypingEnv::fully_monomorphized();
285 let layout_cx = LayoutCx::new(tcx, typing_env);
286 let mut ecx = InterpCx::new(
287 tcx,
288 rustc_span::DUMMY_SP,
289 typing_env,
290 MiriMachine::new(config, layout_cx, genmc_ctx),
291 );
292
293 if !is_no_core(tcx) {
296 let sentinel = helpers::try_resolve_path(
297 tcx,
298 &["core", "ascii", "escape_default"],
299 Namespace::ValueNS,
300 );
301 if !matches!(sentinel, Some(s) if tcx.is_mir_available(s.def.def_id())) {
302 tcx.dcx().fatal(
303 "the current sysroot was built without `-Zalways-encode-mir`, or libcore seems missing.\n\
304 Note that directly invoking the `miri` binary is not supported; please use `cargo miri` instead."
305 );
306 }
307 }
308
309 let argc =
311 ImmTy::from_int(i64::try_from(config.args.len()).unwrap(), ecx.machine.layouts.isize);
312 let argv = {
313 let mut argvs = Vec::<Immediate<Provenance>>::with_capacity(config.args.len());
315 for arg in config.args.iter() {
316 let size = u64::try_from(arg.len()).unwrap().strict_add(1);
318 let arg_type = Ty::new_array(tcx, tcx.types.u8, size);
319 let arg_place =
320 ecx.allocate(ecx.layout_of(arg_type)?, MiriMemoryKind::Machine.into())?;
321 ecx.write_os_str_to_c_str(OsStr::new(arg), arg_place.ptr(), size)?;
322 ecx.mark_immutable(&arg_place);
323 argvs.push(arg_place.to_ref(&ecx));
324 }
325 let u8_ptr_type = Ty::new_imm_ptr(tcx, tcx.types.u8);
327 let u8_ptr_ptr_type = Ty::new_imm_ptr(tcx, u8_ptr_type);
328 let argvs_layout =
329 ecx.layout_of(Ty::new_array(tcx, u8_ptr_type, u64::try_from(argvs.len()).unwrap()))?;
330 let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Machine.into())?;
331 for (arg, idx) in argvs.into_iter().zip(0..) {
332 let place = ecx.project_index(&argvs_place, idx)?;
333 ecx.write_immediate(arg, &place)?;
334 }
335 ecx.mark_immutable(&argvs_place);
336 {
338 let argc_place =
339 ecx.allocate(ecx.machine.layouts.isize, MiriMemoryKind::Machine.into())?;
340 ecx.write_immediate(*argc, &argc_place)?;
341 ecx.mark_immutable(&argc_place);
342 ecx.machine.argc = Some(argc_place.ptr());
343
344 let argv_place =
345 ecx.allocate(ecx.layout_of(u8_ptr_ptr_type)?, MiriMemoryKind::Machine.into())?;
346 ecx.write_pointer(argvs_place.ptr(), &argv_place)?;
347 ecx.mark_immutable(&argv_place);
348 ecx.machine.argv = Some(argv_place.ptr());
349 }
350 if tcx.sess.target.os == Os::Windows {
352 let cmd_utf16: Vec<u16> = args_to_utf16_command_string(config.args.iter());
354
355 let cmd_type =
356 Ty::new_array(tcx, tcx.types.u16, u64::try_from(cmd_utf16.len()).unwrap());
357 let cmd_place =
358 ecx.allocate(ecx.layout_of(cmd_type)?, MiriMemoryKind::Machine.into())?;
359 ecx.machine.cmd_line = Some(cmd_place.ptr());
360 for (&c, idx) in cmd_utf16.iter().zip(0..) {
362 let place = ecx.project_index(&cmd_place, idx)?;
363 ecx.write_scalar(Scalar::from_u16(c), &place)?;
364 }
365 ecx.mark_immutable(&cmd_place);
366 }
367 let imm = argvs_place.to_ref(&ecx);
368 let layout = ecx.layout_of(u8_ptr_ptr_type)?;
369 ImmTy::from_immediate(imm, layout)
370 };
371
372 MiriMachine::late_init(&mut ecx, config, {
374 let mut main_thread_state = MainThreadState::GlobalCtors {
375 entry_id,
376 entry_type,
377 argc,
378 argv,
379 ctor_state: global_ctor::GlobalCtorState::default(),
380 };
381
382 Box::new(move |m| main_thread_state.on_main_stack_empty(m))
386 })?;
387
388 interp_ok(ecx)
389}
390
391fn call_main<'tcx>(
393 ecx: &mut MiriInterpCx<'tcx>,
394 entry_id: DefId,
395 entry_type: MiriEntryFnType,
396 argc: ImmTy<'tcx>,
397 argv: ImmTy<'tcx>,
398) -> InterpResult<'tcx, ()> {
399 let tcx = ecx.tcx();
400
401 let entry_instance = ty::Instance::mono(tcx, entry_id);
403
404 let ret_place = ecx.allocate(ecx.machine.layouts.isize, MiriMemoryKind::Machine.into())?;
406 ecx.machine.main_fn_ret_place = Some(ret_place.clone());
407
408 match entry_type {
410 MiriEntryFnType::Rustc(EntryFnType::Main { .. }) => {
411 let start_id = tcx.lang_items().start_fn().unwrap_or_else(|| {
412 tcx.dcx().fatal("could not find start lang item");
413 });
414 let main_ret_ty = tcx.fn_sig(entry_id).no_bound_vars().unwrap().output();
415 let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
416 let start_instance = ty::Instance::try_resolve(
417 tcx,
418 ecx.typing_env(),
419 start_id,
420 tcx.mk_args(&[ty::GenericArg::from(main_ret_ty)]),
421 )
422 .unwrap()
423 .unwrap();
424
425 let main_ptr = ecx.fn_ptr(FnVal::Instance(entry_instance));
426
427 let sigpipe = rustc_session::config::sigpipe::DEFAULT;
430
431 ecx.call_function(
432 start_instance,
433 ExternAbi::Rust,
434 &[
435 ImmTy::from_scalar(
436 Scalar::from_pointer(main_ptr, ecx),
437 ecx.machine.layouts.const_raw_ptr,
439 ),
440 argc,
441 argv,
442 ImmTy::from_uint(sigpipe, ecx.machine.layouts.u8),
443 ],
444 Some(&ret_place),
445 ReturnContinuation::Stop { cleanup: true },
446 )?;
447 }
448 MiriEntryFnType::MiriStart => {
449 ecx.call_function(
450 entry_instance,
451 ExternAbi::Rust,
452 &[argc, argv],
453 Some(&ret_place),
454 ReturnContinuation::Stop { cleanup: true },
455 )?;
456 }
457 }
458
459 interp_ok(())
460}
461
462pub fn eval_entry<'tcx>(
466 tcx: TyCtxt<'tcx>,
467 entry_id: DefId,
468 entry_type: MiriEntryFnType,
469 config: &MiriConfig,
470 genmc_ctx: Option<Rc<GenmcCtx>>,
471) -> Result<(), NonZeroI32> {
472 let ignore_leaks = config.ignore_leaks;
474
475 let mut ecx = match create_ecx(tcx, entry_id, entry_type, config, genmc_ctx).report_err() {
476 Ok(v) => v,
477 Err(err) => {
478 let (kind, backtrace) = err.into_parts();
479 backtrace.print_backtrace();
480 panic!("Miri initialization error: {kind:?}")
481 }
482 };
483
484 let res: thread::Result<InterpResult<'_, !>> =
486 panic::catch_unwind(AssertUnwindSafe(|| ecx.run_threads()));
487 let res = res.unwrap_or_else(|panic_payload| {
488 if !panic_payload.is::<FatalErrorMarker>() {
491 ecx.handle_ice();
492 }
493 panic::resume_unwind(panic_payload)
494 });
495 let Err(res) = res.report_err();
498
499 'miri_error: {
501 let Some((return_code, leak_check)) = report_result(&ecx, res) else {
503 break 'miri_error;
504 };
505
506 if leak_check && !ignore_leaks {
509 if !ecx.have_all_terminated() {
511 tcx.dcx()
512 .err("the main thread terminated without waiting for all remaining threads");
513 tcx.dcx().note("set `MIRIFLAGS=-Zmiri-ignore-leaks` to disable this check");
514 break 'miri_error;
515 }
516 info!("Additional static roots: {:?}", ecx.machine.static_roots);
518 let leaks = ecx.take_leaked_allocations(|ecx| &ecx.machine.static_roots);
519 if !leaks.is_empty() {
520 report_leaks(&ecx, leaks);
521 tcx.dcx().note("set `MIRIFLAGS=-Zmiri-ignore-leaks` to disable this check");
522 break 'miri_error;
525 }
526 }
527
528 return match NonZeroI32::new(return_code) {
531 None => Ok(()),
532 Some(return_code) => Err(return_code),
533 };
534 }
535
536 assert!(tcx.dcx().has_errors().is_some());
538 Err(NonZeroI32::new(rustc_driver::EXIT_FAILURE).unwrap())
539}
540
541fn args_to_utf16_command_string<I, T>(mut args: I) -> Vec<u16>
552where
553 I: Iterator<Item = T>,
554 T: AsRef<str>,
555{
556 let mut cmd = {
558 let Some(arg0) = args.next() else {
559 return vec![0];
560 };
561 let arg0 = arg0.as_ref();
562 if arg0.contains('"') {
563 panic!("argv[0] cannot contain a doublequote (\") character");
564 } else {
565 let mut s = String::new();
567 s.push('"');
568 s.push_str(arg0);
569 s.push('"');
570 s
571 }
572 };
573
574 for arg in args {
576 let arg = arg.as_ref();
577 cmd.push(' ');
578 if arg.is_empty() {
579 cmd.push_str("\"\"");
580 } else if !arg.bytes().any(|c| matches!(c, b'"' | b'\t' | b' ')) {
581 cmd.push_str(arg);
583 } else {
584 cmd.push('"');
591 let mut chars = arg.chars().peekable();
592 loop {
593 let mut nslashes = 0;
594 while let Some(&'\\') = chars.peek() {
595 chars.next();
596 nslashes += 1;
597 }
598
599 match chars.next() {
600 Some('"') => {
601 cmd.extend(iter::repeat_n('\\', nslashes * 2 + 1));
602 cmd.push('"');
603 }
604 Some(c) => {
605 cmd.extend(iter::repeat_n('\\', nslashes));
606 cmd.push(c);
607 }
608 None => {
609 cmd.extend(iter::repeat_n('\\', nslashes * 2));
610 break;
611 }
612 }
613 }
614 cmd.push('"');
615 }
616 }
617
618 if cmd.contains('\0') {
619 panic!("interior null in command line arguments");
620 }
621 cmd.encode_utf16().chain(iter::once(0)).collect()
622}
623
624#[cfg(test)]
625mod tests {
626 use super::*;
627 #[test]
628 #[should_panic(expected = "argv[0] cannot contain a doublequote (\") character")]
629 fn windows_argv0_panic_on_quote() {
630 args_to_utf16_command_string(["\""].iter());
631 }
632 #[test]
633 fn windows_argv0_no_escape() {
634 let cmd = String::from_utf16_lossy(&args_to_utf16_command_string(
636 [r"C:\Program Files\", "arg1", "arg 2", "arg \" 3"].iter(),
637 ));
638 assert_eq!(cmd.trim_end_matches('\0'), r#""C:\Program Files\" arg1 "arg 2" "arg \" 3""#);
639 }
640}