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, LOCAL_CRATE};
16use rustc_hir_analysis::check::check_function_signature;
17use rustc_middle::middle::exported_symbols::ExportedSymbol;
18use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
19use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutCx};
20use rustc_middle::ty::{self, Ty, TyCtxt};
21use rustc_session::config::EntryFnType;
22use rustc_target::spec::Os;
23
24use crate::concurrency::GenmcCtx;
25use crate::concurrency::thread::TlsAllocAction;
26use crate::diagnostics::report_leaks;
27use crate::helpers::is_no_core;
28use crate::shims::{global_ctor, tls};
29use crate::*;
30
31#[derive(Copy, Clone, Debug)]
32pub enum MiriEntryFnType {
33 MiriStart,
34 Rustc(EntryFnType),
35}
36
37pub fn entry_fn(tcx: TyCtxt<'_>) -> (DefId, MiriEntryFnType) {
41 if let Some((def_id, entry_type)) = tcx.entry_fn(()) {
42 return (def_id, MiriEntryFnType::Rustc(entry_type));
43 }
44 let sym = tcx.exported_non_generic_symbols(LOCAL_CRATE).iter().find_map(|(sym, _)| {
46 if sym.symbol_name_for_local_instance(tcx).name == "miri_start" { Some(sym) } else { None }
47 });
48 if let Some(ExportedSymbol::NonGeneric(id)) = sym {
49 let start_def_id = id.expect_local();
50 let start_span = tcx.def_span(start_def_id);
51
52 let expected_sig = ty::Binder::dummy(tcx.mk_fn_sig_safe_rust_abi(
53 [tcx.types.isize, Ty::new_imm_ptr(tcx, Ty::new_imm_ptr(tcx, tcx.types.u8))],
54 tcx.types.isize,
55 ));
56
57 let correct_func_sig = check_function_signature(
58 tcx,
59 ObligationCause::new(start_span, start_def_id, ObligationCauseCode::Misc),
60 *id,
61 expected_sig,
62 )
63 .is_ok();
64
65 if correct_func_sig {
66 (*id, MiriEntryFnType::MiriStart)
67 } else {
68 tcx.dcx().fatal(
69 "`miri_start` must have the following signature:\n\
70 fn miri_start(argc: isize, argv: *const *const u8) -> isize",
71 );
72 }
73 } else {
74 tcx.dcx().fatal(
75 "Miri can only run programs that have a main function.\n\
76 Alternatively, you can export a `miri_start` function:\n\
77 \n\
78 #[cfg(miri)]\n\
79 #[unsafe(no_mangle)]\n\
80 fn miri_start(argc: isize, argv: *const *const u8) -> isize {\
81 \n // Call the actual start function that your project implements, based on your target's conventions.\n\
82 }"
83 );
84 }
85}
86
87const MAIN_THREAD_YIELDS_AT_SHUTDOWN: u32 = 256;
91
92#[derive(Clone)]
94pub struct MiriConfig {
95 pub env: Vec<(OsString, OsString)>,
98 pub validation: ValidationMode,
100 pub borrow_tracker: Option<BorrowTrackerMethod>,
102 pub check_alignment: AlignmentCheck,
104 pub isolated_op: IsolatedOp,
106 pub ignore_leaks: bool,
108 pub forwarded_env_vars: Vec<String>,
110 pub set_env_vars: FxHashMap<String, String>,
112 pub args: Vec<String>,
114 pub seed: Option<u64>,
116 pub tracked_pointer_tags: FxHashSet<BorTag>,
118 pub tracked_alloc_ids: FxHashSet<AllocId>,
120 pub track_alloc_accesses: bool,
122 pub data_race_detector: bool,
124 pub weak_memory_emulation: bool,
126 pub genmc_config: Option<GenmcConfig>,
128 pub track_outdated_loads: bool,
130 pub cmpxchg_weak_failure_rate: f64,
133 pub measureme_out: Option<String>,
136 pub backtrace_style: BacktraceStyle,
138 pub provenance_mode: ProvenanceMode,
140 pub mute_stdout_stderr: bool,
143 pub preemption_rate: f64,
145 pub report_progress: Option<u32>,
147 pub native_lib: Vec<PathBuf>,
149 pub native_lib_enable_tracing: bool,
151 pub gc_interval: u32,
153 pub num_cpus: u32,
155 pub page_size: Option<u64>,
157 pub collect_leak_backtraces: bool,
159 pub address_reuse_rate: f64,
161 pub address_reuse_cross_thread_rate: f64,
163 pub fixed_scheduling: bool,
165 pub force_intrinsic_fallback: bool,
167 pub float_nondet: bool,
169 pub float_rounding_error: FloatRoundingErrorMode,
171 pub short_fd_operations: bool,
173 pub user_relevant_crates: Vec<String>,
175}
176
177impl Default for MiriConfig {
178 fn default() -> MiriConfig {
179 MiriConfig {
180 env: vec![],
181 validation: ValidationMode::Shallow,
182 borrow_tracker: Some(BorrowTrackerMethod::StackedBorrows),
183 check_alignment: AlignmentCheck::Int,
184 isolated_op: IsolatedOp::Reject(RejectOpWith::Abort),
185 ignore_leaks: false,
186 forwarded_env_vars: vec![],
187 set_env_vars: FxHashMap::default(),
188 args: vec![],
189 seed: None,
190 tracked_pointer_tags: FxHashSet::default(),
191 tracked_alloc_ids: FxHashSet::default(),
192 track_alloc_accesses: false,
193 data_race_detector: true,
194 weak_memory_emulation: true,
195 genmc_config: None,
196 track_outdated_loads: false,
197 cmpxchg_weak_failure_rate: 0.8, measureme_out: None,
199 backtrace_style: BacktraceStyle::Short,
200 provenance_mode: ProvenanceMode::Default,
201 mute_stdout_stderr: false,
202 preemption_rate: 0.01, report_progress: None,
204 native_lib: vec![],
205 native_lib_enable_tracing: false,
206 gc_interval: 10_000,
207 num_cpus: 1,
208 page_size: None,
209 collect_leak_backtraces: true,
210 address_reuse_rate: 0.5,
211 address_reuse_cross_thread_rate: 0.1,
212 fixed_scheduling: false,
213 force_intrinsic_fallback: false,
214 float_nondet: true,
215 float_rounding_error: FloatRoundingErrorMode::Random,
216 short_fd_operations: true,
217 user_relevant_crates: vec![],
218 }
219 }
220}
221
222#[derive(Debug)]
224enum MainThreadState<'tcx> {
225 GlobalCtors {
226 ctor_state: global_ctor::GlobalCtorState<'tcx>,
227 entry_id: DefId,
229 entry_type: MiriEntryFnType,
230 argc: ImmTy<'tcx>,
232 argv: ImmTy<'tcx>,
233 },
234 Running,
235 TlsDtors(tls::TlsDtorsState<'tcx>),
236 Yield {
237 remaining: u32,
238 },
239 Done,
240}
241
242impl<'tcx> MainThreadState<'tcx> {
243 fn on_main_stack_empty(
244 &mut self,
245 this: &mut MiriInterpCx<'tcx>,
246 ) -> InterpResult<'tcx, Poll<()>> {
247 use MainThreadState::*;
248 match self {
249 GlobalCtors { ctor_state, entry_id, entry_type, argc, argv } => {
250 match ctor_state.on_stack_empty(this)? {
251 Poll::Pending => {} Poll::Ready(()) => {
253 call_main(this, *entry_id, *entry_type, argc.clone(), argv.clone())?;
254 *self = Running;
255 }
256 }
257 }
258 Running => {
259 *self = TlsDtors(Default::default());
260 }
261 TlsDtors(state) =>
262 match state.on_stack_empty(this)? {
263 Poll::Pending => {} Poll::Ready(()) => {
265 if this.machine.data_race.as_genmc_ref().is_some() {
266 *self = Done;
269 } else {
270 if this.machine.preemption_rate > 0.0 {
273 *self = Yield { remaining: MAIN_THREAD_YIELDS_AT_SHUTDOWN };
276 } else {
277 *self = Done;
280 }
281 }
282 }
283 },
284 Yield { remaining } =>
285 match remaining.checked_sub(1) {
286 None => *self = Done,
287 Some(new_remaining) => {
288 *remaining = new_remaining;
289 this.yield_active_thread();
290 }
291 },
292 Done => {
293 let ret_place = this.machine.main_fn_ret_place.clone().unwrap();
295 let exit_code = this.read_target_isize(&ret_place)?;
296 let exit_code = i32::try_from(exit_code).unwrap_or(if exit_code >= 0 {
299 i32::MAX
300 } else {
301 i32::MIN
302 });
303 this.terminate_active_thread(TlsAllocAction::Leak)?;
306
307 if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
309 genmc_ctx.handle_exit(
311 ThreadId::MAIN_THREAD,
312 exit_code,
313 crate::concurrency::ExitType::MainThreadFinish,
314 )?;
315 } else {
316 throw_machine_stop!(TerminationInfo::Exit {
318 code: exit_code,
319 leak_check: true
320 });
321 }
322 }
323 }
324 interp_ok(Poll::Pending)
325 }
326}
327
328pub fn create_ecx<'tcx>(
331 tcx: TyCtxt<'tcx>,
332 entry_id: DefId,
333 entry_type: MiriEntryFnType,
334 config: &MiriConfig,
335 genmc_ctx: Option<Rc<GenmcCtx>>,
336) -> InterpResult<'tcx, InterpCx<'tcx, MiriMachine<'tcx>>> {
337 let typing_env = ty::TypingEnv::fully_monomorphized();
338 let layout_cx = LayoutCx::new(tcx, typing_env);
339 let mut ecx = InterpCx::new(
340 tcx,
341 rustc_span::DUMMY_SP,
342 typing_env,
343 MiriMachine::new(config, layout_cx, genmc_ctx),
344 );
345
346 if !is_no_core(tcx) {
349 let sentinel = helpers::try_resolve_path(
350 tcx,
351 &["core", "ascii", "escape_default"],
352 Namespace::ValueNS,
353 );
354 if !matches!(sentinel, Some(s) if tcx.is_mir_available(s.def.def_id())) {
355 tcx.dcx().fatal(
356 "the current sysroot was built without `-Zalways-encode-mir`, or libcore seems missing.\n\
357 Note that directly invoking the `miri` binary is not supported; please use `cargo miri` instead."
358 );
359 }
360 }
361
362 let argc =
364 ImmTy::from_int(i64::try_from(config.args.len()).unwrap(), ecx.machine.layouts.isize);
365 let argv = {
366 let mut argvs = Vec::<Immediate<Provenance>>::with_capacity(config.args.len());
368 for arg in config.args.iter() {
369 let size = u64::try_from(arg.len()).unwrap().strict_add(1);
371 let arg_type = Ty::new_array(tcx, tcx.types.u8, size);
372 let arg_place =
373 ecx.allocate(ecx.layout_of(arg_type)?, MiriMemoryKind::Machine.into())?;
374 ecx.write_os_str_to_c_str(OsStr::new(arg), arg_place.ptr(), size)?;
375 ecx.mark_immutable(&arg_place);
376 argvs.push(arg_place.to_ref(&ecx));
377 }
378 let u8_ptr_type = Ty::new_imm_ptr(tcx, tcx.types.u8);
380 let u8_ptr_ptr_type = Ty::new_imm_ptr(tcx, u8_ptr_type);
381 let argvs_layout =
382 ecx.layout_of(Ty::new_array(tcx, u8_ptr_type, u64::try_from(argvs.len()).unwrap()))?;
383 let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Machine.into())?;
384 for (arg, idx) in argvs.into_iter().zip(0..) {
385 let place = ecx.project_index(&argvs_place, idx)?;
386 ecx.write_immediate(arg, &place)?;
387 }
388 ecx.mark_immutable(&argvs_place);
389 {
391 let argc_place =
392 ecx.allocate(ecx.machine.layouts.isize, MiriMemoryKind::Machine.into())?;
393 ecx.write_immediate(*argc, &argc_place)?;
394 ecx.mark_immutable(&argc_place);
395 ecx.machine.argc = Some(argc_place.ptr());
396
397 let argv_place =
398 ecx.allocate(ecx.layout_of(u8_ptr_ptr_type)?, MiriMemoryKind::Machine.into())?;
399 ecx.write_pointer(argvs_place.ptr(), &argv_place)?;
400 ecx.mark_immutable(&argv_place);
401 ecx.machine.argv = Some(argv_place.ptr());
402 }
403 if tcx.sess.target.os == Os::Windows {
405 let cmd_utf16: Vec<u16> = args_to_utf16_command_string(config.args.iter());
407
408 let cmd_type =
409 Ty::new_array(tcx, tcx.types.u16, u64::try_from(cmd_utf16.len()).unwrap());
410 let cmd_place =
411 ecx.allocate(ecx.layout_of(cmd_type)?, MiriMemoryKind::Machine.into())?;
412 ecx.machine.cmd_line = Some(cmd_place.ptr());
413 for (&c, idx) in cmd_utf16.iter().zip(0..) {
415 let place = ecx.project_index(&cmd_place, idx)?;
416 ecx.write_scalar(Scalar::from_u16(c), &place)?;
417 }
418 ecx.mark_immutable(&cmd_place);
419 }
420 let imm = argvs_place.to_ref(&ecx);
421 let layout = ecx.layout_of(u8_ptr_ptr_type)?;
422 ImmTy::from_immediate(imm, layout)
423 };
424
425 MiriMachine::late_init(&mut ecx, config, {
427 let mut main_thread_state = MainThreadState::GlobalCtors {
428 entry_id,
429 entry_type,
430 argc,
431 argv,
432 ctor_state: global_ctor::GlobalCtorState::default(),
433 };
434
435 Box::new(move |m| main_thread_state.on_main_stack_empty(m))
439 })?;
440
441 interp_ok(ecx)
442}
443
444fn call_main<'tcx>(
446 ecx: &mut MiriInterpCx<'tcx>,
447 entry_id: DefId,
448 entry_type: MiriEntryFnType,
449 argc: ImmTy<'tcx>,
450 argv: ImmTy<'tcx>,
451) -> InterpResult<'tcx, ()> {
452 let tcx = ecx.tcx();
453
454 let entry_instance = ty::Instance::mono(tcx, entry_id);
456
457 let ret_place = ecx.allocate(ecx.machine.layouts.isize, MiriMemoryKind::Machine.into())?;
459 ecx.machine.main_fn_ret_place = Some(ret_place.clone());
460
461 match entry_type {
463 MiriEntryFnType::Rustc(EntryFnType::Main { .. }) => {
464 let start_id = tcx.lang_items().start_fn().unwrap_or_else(|| {
465 tcx.dcx().fatal("could not find start lang item");
466 });
467 let main_ret_ty = tcx.fn_sig(entry_id).no_bound_vars().unwrap().output();
468 let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
469 let start_instance = ty::Instance::try_resolve(
470 tcx,
471 ecx.typing_env(),
472 start_id,
473 tcx.mk_args(&[ty::GenericArg::from(main_ret_ty)]),
474 )
475 .unwrap()
476 .unwrap();
477
478 let main_ptr = ecx.fn_ptr(FnVal::Instance(entry_instance));
479
480 let sigpipe = rustc_session::config::sigpipe::DEFAULT;
483
484 ecx.call_function(
485 start_instance,
486 ExternAbi::Rust,
487 &[
488 ImmTy::from_scalar(
489 Scalar::from_pointer(main_ptr, ecx),
490 ecx.machine.layouts.const_raw_ptr,
492 ),
493 argc,
494 argv,
495 ImmTy::from_uint(sigpipe, ecx.machine.layouts.u8),
496 ],
497 Some(&ret_place),
498 ReturnContinuation::Stop { cleanup: true },
499 )?;
500 }
501 MiriEntryFnType::MiriStart => {
502 ecx.call_function(
503 entry_instance,
504 ExternAbi::Rust,
505 &[argc, argv],
506 Some(&ret_place),
507 ReturnContinuation::Stop { cleanup: true },
508 )?;
509 }
510 }
511
512 interp_ok(())
513}
514
515pub fn eval_entry<'tcx>(
519 tcx: TyCtxt<'tcx>,
520 entry_id: DefId,
521 entry_type: MiriEntryFnType,
522 config: &MiriConfig,
523 genmc_ctx: Option<Rc<GenmcCtx>>,
524) -> Result<(), NonZeroI32> {
525 let ignore_leaks = config.ignore_leaks;
527
528 let mut ecx = match create_ecx(tcx, entry_id, entry_type, config, genmc_ctx).report_err() {
529 Ok(v) => v,
530 Err(err) => {
531 let (kind, backtrace) = err.into_parts();
532 backtrace.print_backtrace();
533 panic!("Miri initialization error: {kind:?}")
534 }
535 };
536
537 let res: thread::Result<InterpResult<'_, !>> =
539 panic::catch_unwind(AssertUnwindSafe(|| ecx.run_threads()));
540 let res = res.unwrap_or_else(|panic_payload| {
541 if !panic_payload.is::<FatalErrorMarker>() {
544 ecx.handle_ice();
545 }
546 panic::resume_unwind(panic_payload)
547 });
548 let Err(res) = res.report_err();
551
552 'miri_error: {
554 let Some((return_code, leak_check)) = report_result(&ecx, res) else {
556 break 'miri_error;
557 };
558
559 if leak_check && !ignore_leaks {
562 if !ecx.have_all_terminated() {
564 tcx.dcx()
565 .err("the main thread terminated without waiting for all remaining threads");
566 tcx.dcx().note("set `MIRIFLAGS=-Zmiri-ignore-leaks` to disable this check");
567 break 'miri_error;
568 }
569 info!("Additional static roots: {:?}", ecx.machine.static_roots);
571 let leaks = ecx.take_leaked_allocations(|ecx| &ecx.machine.static_roots);
572 if !leaks.is_empty() {
573 report_leaks(&ecx, leaks);
574 tcx.dcx().note("set `MIRIFLAGS=-Zmiri-ignore-leaks` to disable this check");
575 break 'miri_error;
578 }
579 }
580
581 return match NonZeroI32::new(return_code) {
584 None => Ok(()),
585 Some(return_code) => Err(return_code),
586 };
587 }
588
589 assert!(tcx.dcx().has_errors().is_some());
591 Err(NonZeroI32::new(rustc_driver::EXIT_FAILURE).unwrap())
592}
593
594fn args_to_utf16_command_string<I, T>(mut args: I) -> Vec<u16>
605where
606 I: Iterator<Item = T>,
607 T: AsRef<str>,
608{
609 let mut cmd = {
611 let Some(arg0) = args.next() else {
612 return vec![0];
613 };
614 let arg0 = arg0.as_ref();
615 if arg0.contains('"') {
616 panic!("argv[0] cannot contain a doublequote (\") character");
617 } else {
618 let mut s = String::new();
620 s.push('"');
621 s.push_str(arg0);
622 s.push('"');
623 s
624 }
625 };
626
627 for arg in args {
629 let arg = arg.as_ref();
630 cmd.push(' ');
631 if arg.is_empty() {
632 cmd.push_str("\"\"");
633 } else if !arg.bytes().any(|c| matches!(c, b'"' | b'\t' | b' ')) {
634 cmd.push_str(arg);
636 } else {
637 cmd.push('"');
644 let mut chars = arg.chars().peekable();
645 loop {
646 let mut nslashes = 0;
647 while let Some(&'\\') = chars.peek() {
648 chars.next();
649 nslashes += 1;
650 }
651
652 match chars.next() {
653 Some('"') => {
654 cmd.extend(iter::repeat_n('\\', nslashes * 2 + 1));
655 cmd.push('"');
656 }
657 Some(c) => {
658 cmd.extend(iter::repeat_n('\\', nslashes));
659 cmd.push(c);
660 }
661 None => {
662 cmd.extend(iter::repeat_n('\\', nslashes * 2));
663 break;
664 }
665 }
666 }
667 cmd.push('"');
668 }
669 }
670
671 if cmd.contains('\0') {
672 panic!("interior null in command line arguments");
673 }
674 cmd.encode_utf16().chain(iter::once(0)).collect()
675}
676
677#[cfg(test)]
678mod tests {
679 use super::*;
680 #[test]
681 #[should_panic(expected = "argv[0] cannot contain a doublequote (\") character")]
682 fn windows_argv0_panic_on_quote() {
683 args_to_utf16_command_string(["\""].iter());
684 }
685 #[test]
686 fn windows_argv0_no_escape() {
687 let cmd = String::from_utf16_lossy(&args_to_utf16_command_string(
689 [r"C:\Program Files\", "arg1", "arg 2", "arg \" 3"].iter(),
690 ));
691 assert_eq!(cmd.trim_end_matches('\0'), r#""C:\Program Files\" arg1 "arg 2" "arg \" 3""#);
692 }
693}