Skip to main content

miri/
eval.rs

1//! Main evaluator loop and setting up the initial stack frame.
2
3use 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
37/// Finds the entry point Miri should execute.
38///
39/// Public because this is used by Priroda.
40pub 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    // Look for a symbol in the local crate named `miri_start`, and treat that as the entry point.
45    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
87/// When the main thread would exit, we will yield to any other thread that is ready to execute.
88/// But we must only do that a finite number of times, or a background thread running `loop {}`
89/// will hang the program.
90const MAIN_THREAD_YIELDS_AT_SHUTDOWN: u32 = 256;
91
92/// Configuration needed to spawn a Miri instance.
93#[derive(Clone)]
94pub struct MiriConfig {
95    /// The host environment snapshot to use as basis for what is provided to the interpreted program.
96    /// (This is still subject to isolation as well as `forwarded_env_vars`.)
97    pub env: Vec<(OsString, OsString)>,
98    /// Determine if validity checking is enabled.
99    pub validation: ValidationMode,
100    /// Determines if Stacked Borrows or Tree Borrows is enabled.
101    pub borrow_tracker: Option<BorrowTrackerMethod>,
102    /// Controls alignment checking.
103    pub check_alignment: AlignmentCheck,
104    /// Action for an op requiring communication with the host.
105    pub isolated_op: IsolatedOp,
106    /// Determines if memory leaks should be ignored.
107    pub ignore_leaks: bool,
108    /// Environment variables that should always be forwarded from the host.
109    pub forwarded_env_vars: Vec<String>,
110    /// Additional environment variables that should be set in the interpreted program.
111    pub set_env_vars: FxHashMap<String, String>,
112    /// Command-line arguments passed to the interpreted program.
113    pub args: Vec<String>,
114    /// The binary we pretend to be.
115    pub current_exe: Option<PathBuf>,
116    /// The seed to use when non-determinism or randomness are required (e.g. ptr-to-int cast, `getrandom()`).
117    pub seed: Option<u64>,
118    /// The stacked borrows pointer ids to report about.
119    pub tracked_pointer_tags: FxHashSet<BorTag>,
120    /// The allocation ids to report about.
121    pub tracked_alloc_ids: FxHashSet<AllocId>,
122    /// For the tracked alloc ids, also report read/write accesses.
123    pub track_alloc_accesses: bool,
124    /// Determine if data race detection should be enabled.
125    pub data_race_detector: bool,
126    /// Determine if weak memory emulation should be enabled. Requires data race detection to be enabled.
127    pub weak_memory_emulation: bool,
128    /// Determine if we are running in GenMC mode and with which settings. In GenMC mode, Miri will explore multiple concurrent executions of the given program.
129    pub genmc_config: Option<GenmcConfig>,
130    /// Track when an outdated (weak memory) load happens.
131    pub track_outdated_loads: bool,
132    /// Rate of spurious failures for compare_exchange_weak atomic operations,
133    /// between 0.0 and 1.0, defaulting to 0.8 (80% chance of failure).
134    pub cmpxchg_weak_failure_rate: f64,
135    /// If `Some`, enable the `measureme` profiler, writing results to a file
136    /// with the specified prefix.
137    pub measureme_out: Option<String>,
138    /// Which style to use for printing backtraces.
139    pub backtrace_style: BacktraceStyle,
140    /// Which provenance to use for int2ptr casts.
141    pub provenance_mode: ProvenanceMode,
142    /// Whether to ignore any output by the program. This is helpful when debugging miri
143    /// as its messages don't get intermingled with the program messages.
144    pub mute_stdout_stderr: bool,
145    /// The probability of the active thread being preempted at the end of each basic block.
146    pub preemption_rate: f64,
147    /// Report the current instruction being executed every N basic blocks.
148    pub report_progress: Option<u32>,
149    /// The location of the shared object files to load when calling external functions
150    pub native_lib: Vec<PathBuf>,
151    /// Whether to enable the new native lib tracing system.
152    pub native_lib_enable_tracing: bool,
153    /// Run a garbage collector for BorTags every N basic blocks.
154    pub gc_interval: u32,
155    /// The number of CPUs to be reported by miri.
156    pub num_cpus: u32,
157    /// Requires Miri to emulate pages of a certain size.
158    pub page_size: Option<u64>,
159    /// Whether to collect a backtrace when each allocation is created, just in case it leaks.
160    pub collect_leak_backtraces: bool,
161    /// Probability for address reuse.
162    pub address_reuse_rate: f64,
163    /// Probability for address reuse across threads.
164    pub address_reuse_cross_thread_rate: f64,
165    /// Round Robin scheduling with no preemption.
166    pub fixed_scheduling: bool,
167    /// Whether floating-point operations can behave non-deterministically.
168    pub float_nondet: bool,
169    /// Whether floating-point operations can have a non-deterministic rounding error.
170    pub float_rounding_error: FloatRoundingErrorMode,
171    /// Whether Miri artificially introduces short reads/writes on file descriptors.
172    pub short_fd_operations: bool,
173    /// A list of crates that are considered user-relevant.
174    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            current_exe: None,
190            seed: None,
191            tracked_pointer_tags: FxHashSet::default(),
192            tracked_alloc_ids: FxHashSet::default(),
193            track_alloc_accesses: false,
194            data_race_detector: true,
195            weak_memory_emulation: true,
196            genmc_config: None,
197            track_outdated_loads: false,
198            cmpxchg_weak_failure_rate: 0.8, // 80%
199            measureme_out: None,
200            backtrace_style: BacktraceStyle::Short,
201            provenance_mode: ProvenanceMode::Default,
202            mute_stdout_stderr: false,
203            preemption_rate: 0.01, // 1%
204            report_progress: None,
205            native_lib: vec![],
206            native_lib_enable_tracing: false,
207            gc_interval: 10_000,
208            num_cpus: 1,
209            page_size: None,
210            collect_leak_backtraces: true,
211            address_reuse_rate: 0.5,
212            address_reuse_cross_thread_rate: 0.1,
213            fixed_scheduling: 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/// The state of the main thread. Implementation detail of `on_main_stack_empty`.
223#[derive(Debug)]
224enum MainThreadState<'tcx> {
225    GlobalCtors {
226        ctor_state: global_ctor::GlobalCtorState<'tcx>,
227        /// The main function to call.
228        entry_id: DefId,
229        entry_type: MiriEntryFnType,
230        /// Arguments passed to `main`.
231        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 => {} // just keep going
252                    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 => {} // just keep going
264                    Poll::Ready(()) => {
265                        if this.machine.data_race.as_genmc_ref().is_some() {
266                            // In GenMC mode, we don't yield at the end of the main thread.
267                            // Instead, the `GenmcCtx` will ensure that unfinished threads get a chance to run at this point.
268                            *self = Done;
269                        } else {
270                            // Give background threads a chance to finish by yielding the main thread a
271                            // couple of times -- but only if we would also preempt threads randomly.
272                            if this.machine.preemption_rate > 0.0 {
273                                // There is a non-zero chance they will yield back to us often enough to
274                                // make Miri terminate eventually.
275                                *self = Yield { remaining: MAIN_THREAD_YIELDS_AT_SHUTDOWN };
276                            } else {
277                                // The other threads did not get preempted, so no need to yield back to
278                                // them.
279                                *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                // Figure out exit code.
294                let ret_place = this.machine.main_fn_ret_place.clone().unwrap();
295                let exit_code = this.read_target_isize(&ret_place)?;
296                // Rust uses `isize` but the underlying type of an exit code is `i32`.
297                // Do a saturating cast.
298                let exit_code = i32::try_from(exit_code).unwrap_or(if exit_code >= 0 {
299                    i32::MAX
300                } else {
301                    i32::MIN
302                });
303                // Deal with our thread-local memory. We do *not* want to actually free it, instead we consider TLS
304                // to be like a global `static`, so that all memory reached by it is considered to "not leak".
305                this.terminate_active_thread(TlsAllocAction::Leak)?;
306
307                // In GenMC mode, we do not immediately stop execution on main thread exit.
308                if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
309                    // If there's no error, execution will continue (on another thread).
310                    genmc_ctx.handle_exit(
311                        ThreadId::MAIN_THREAD,
312                        exit_code,
313                        crate::concurrency::ExitType::MainThreadFinish,
314                    )?;
315                } else {
316                    // Stop interpreter loop.
317                    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
328/// Returns a freshly created `InterpCx`.
329/// Public because this is also used by `priroda`.
330pub 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    // Make sure we have MIR. We check MIR for some stable monomorphic function in libcore. However,
347    // if the current crate is #![no_core] it's fine to be missing the usual items from libcore.
348    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    // Compute argc and argv from `config.args`.
363    let argc =
364        ImmTy::from_int(i64::try_from(config.args.len()).unwrap(), ecx.machine.layouts.isize);
365    let argv = {
366        // Put each argument in memory, collect pointers.
367        let mut argvs = Vec::<Immediate<Provenance>>::with_capacity(config.args.len());
368        for arg in config.args.iter() {
369            // Make space for `0` terminator.
370            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        // Make an array with all these pointers, in the Miri memory.
379        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        // Store `argc` and `argv` for macOS `_NSGetArg{c,v}`, and for the GC to see them.
390        {
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        // Store command line as UTF-16 for Windows `GetCommandLineW`.
404        if tcx.sess.target.os == Os::Windows {
405            // Construct a command string with all the arguments.
406            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            // Store the UTF-16 string. We just allocated so we know the bounds are fine.
414            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    // Some parts of initialization require a full `InterpCx`.
426    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        // Cannot capture anything GC-relevant here.
436        // `argc` and `argv` *are* GC_relevant, but they also get stored in `machine.argc` and
437        // `machine.argv` so we are good.
438        Box::new(move |m| main_thread_state.on_main_stack_empty(m))
439    })?;
440
441    interp_ok(ecx)
442}
443
444// Call the entry function.
445fn 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    // Setup first stack frame.
455    let entry_instance = ty::Instance::mono(tcx, entry_id);
456
457    // Return place (in static memory so that it does not count as leak).
458    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    // Call start function.
462    match entry_type {
463        MiriEntryFnType::Rustc(EntryFnType::Main { .. }) => {
464            let entry_sig = tcx.fn_sig(entry_id).no_bound_vars().unwrap();
465            let main_ret_ty = entry_sig.output();
466            let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
467
468            let start_id = tcx.lang_items().start_fn().unwrap_or_else(|| {
469                tcx.dcx().fatal("could not find start lang item");
470            });
471            let start_instance = ty::Instance::try_resolve(
472                tcx,
473                ecx.typing_env(),
474                start_id,
475                tcx.mk_args(&[ty::GenericArg::from(main_ret_ty)]),
476            )
477            .unwrap()
478            .unwrap();
479
480            let main_ptr = ecx.fn_ptr(FnVal::Instance(entry_instance));
481
482            // Always using DEFAULT is okay since we don't support signals in Miri anyway.
483            // (This means we are effectively ignoring `-Zon-broken-pipe`.)
484            let sigpipe = rustc_session::config::sigpipe::DEFAULT;
485
486            ecx.call_function(
487                start_instance,
488                ExternAbi::Rust,
489                &[
490                    ImmTy::from_scalar(
491                        Scalar::from_pointer(main_ptr, ecx),
492                        ecx.layout_of(Ty::new_fn_ptr(tcx, entry_sig)).unwrap(),
493                    ),
494                    argc,
495                    argv,
496                    ImmTy::from_uint(sigpipe, ecx.machine.layouts.u8),
497                ],
498                Some(&ret_place),
499                ReturnContinuation::Stop { cleanup: true },
500            )?;
501        }
502        MiriEntryFnType::MiriStart => {
503            ecx.call_function(
504                entry_instance,
505                ExternAbi::Rust,
506                &[argc, argv],
507                Some(&ret_place),
508                ReturnContinuation::Stop { cleanup: true },
509            )?;
510        }
511    }
512
513    interp_ok(())
514}
515
516/// Evaluates the entry function specified by `entry_id`.
517/// Returns `Ok(())` if program execution completed with exit code 0.
518/// Returns `Err(code)` if an evaluation error occurred or the program returned a non-0 exit code.
519pub fn eval_entry<'tcx>(
520    tcx: TyCtxt<'tcx>,
521    entry_id: DefId,
522    entry_type: MiriEntryFnType,
523    config: &MiriConfig,
524    genmc_ctx: Option<Rc<GenmcCtx>>,
525) -> Result<(), NonZeroI32> {
526    // Copy setting before we move `config`.
527    let ignore_leaks = config.ignore_leaks;
528
529    let mut ecx = match create_ecx(tcx, entry_id, entry_type, config, genmc_ctx).report_err() {
530        Ok(v) => v,
531        Err(err) => {
532            let (kind, backtrace) = err.into_parts();
533            backtrace.print_backtrace();
534            panic!("Miri initialization error: {kind:?}")
535        }
536    };
537
538    // Perform the main execution.
539    let res: thread::Result<InterpResult<'_, !>> =
540        panic::catch_unwind(AssertUnwindSafe(|| ecx.run_threads()));
541    let res = res.unwrap_or_else(|panic_payload| {
542        // rustc "handles" some errors by unwinding with FatalErrorMarker
543        // (after emitting suitable diagnostics), so do not treat those as ICEs.
544        if !panic_payload.is::<FatalErrorMarker>() {
545            ecx.handle_ice();
546        }
547        panic::resume_unwind(panic_payload)
548    });
549    // Obtain the result of the execution. This is always an `Err`, but that doesn't necessarily
550    // indicate an error.
551    let Err(res) = res.report_err();
552
553    // Error reporting: if we survive all checks, we return the exit code the program gave us.
554    'miri_error: {
555        // Show diagnostic, if any.
556        let Some((return_code, leak_check)) = report_result(&ecx, res) else {
557            break 'miri_error;
558        };
559
560        // If we get here there was no fatal error -- yet.
561        // Possibly check for memory leaks.
562        if leak_check && !ignore_leaks {
563            // Check for thread leaks.
564            if !ecx.have_all_terminated() {
565                tcx.dcx()
566                    .err("the main thread terminated without waiting for all remaining threads");
567                tcx.dcx().note("set `MIRIFLAGS=-Zmiri-ignore-leaks` to disable this check");
568                break 'miri_error;
569            }
570            // Check for memory leaks.
571            info!("Additional static roots: {:?}", ecx.machine.static_roots);
572            let leaks = ecx.take_leaked_allocations(|ecx| &ecx.machine.static_roots);
573            if !leaks.is_empty() {
574                report_leaks(&ecx, leaks);
575                tcx.dcx().note("set `MIRIFLAGS=-Zmiri-ignore-leaks` to disable this check");
576                // Ignore the provided return code - let the reported error
577                // determine the return code.
578                break 'miri_error;
579            }
580        }
581
582        // The interpreter has not reported an error.
583        // (There could still be errors in the session if there are other interpreters.)
584        return match NonZeroI32::new(return_code) {
585            None => Ok(()),
586            Some(return_code) => Err(return_code),
587        };
588    }
589
590    // The interpreter reported an error.
591    assert!(tcx.dcx().has_errors().is_some());
592    Err(NonZeroI32::new(rustc_driver::EXIT_FAILURE).unwrap())
593}
594
595/// Turns an array of arguments into a Windows command line string.
596///
597/// The string will be UTF-16 encoded and NUL terminated.
598///
599/// Panics if the zeroth argument contains the `"` character because doublequotes
600/// in `argv[0]` cannot be encoded using the standard command line parsing rules.
601///
602/// Further reading:
603/// * [Parsing C++ command-line arguments](https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160#parsing-c-command-line-arguments)
604/// * [The C/C++ Parameter Parsing Rules](https://daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULES)
605fn args_to_utf16_command_string<I, T>(mut args: I) -> Vec<u16>
606where
607    I: Iterator<Item = T>,
608    T: AsRef<str>,
609{
610    // Parse argv[0]. Slashes aren't escaped. Literal double quotes are not allowed.
611    let mut cmd = {
612        let Some(arg0) = args.next() else {
613            return vec![0];
614        };
615        let arg0 = arg0.as_ref();
616        if arg0.contains('"') {
617            panic!("argv[0] cannot contain a doublequote (\") character");
618        } else {
619            // Always surround argv[0] with quotes.
620            let mut s = String::new();
621            s.push('"');
622            s.push_str(arg0);
623            s.push('"');
624            s
625        }
626    };
627
628    // Build the other arguments.
629    for arg in args {
630        let arg = arg.as_ref();
631        cmd.push(' ');
632        if arg.is_empty() {
633            cmd.push_str("\"\"");
634        } else if !arg.bytes().any(|c| matches!(c, b'"' | b'\t' | b' ')) {
635            // No quote, tab, or space -- no escaping required.
636            cmd.push_str(arg);
637        } else {
638            // Spaces and tabs are escaped by surrounding them in quotes.
639            // Quotes are themselves escaped by using backslashes when in a
640            // quoted block.
641            // Backslashes only need to be escaped when one or more are directly
642            // followed by a quote. Otherwise they are taken literally.
643
644            cmd.push('"');
645            let mut chars = arg.chars().peekable();
646            loop {
647                let mut nslashes = 0;
648                while let Some(&'\\') = chars.peek() {
649                    chars.next();
650                    nslashes += 1;
651                }
652
653                match chars.next() {
654                    Some('"') => {
655                        cmd.extend(iter::repeat_n('\\', nslashes * 2 + 1));
656                        cmd.push('"');
657                    }
658                    Some(c) => {
659                        cmd.extend(iter::repeat_n('\\', nslashes));
660                        cmd.push(c);
661                    }
662                    None => {
663                        cmd.extend(iter::repeat_n('\\', nslashes * 2));
664                        break;
665                    }
666                }
667            }
668            cmd.push('"');
669        }
670    }
671
672    if cmd.contains('\0') {
673        panic!("interior null in command line arguments");
674    }
675    cmd.encode_utf16().chain(iter::once(0)).collect()
676}
677
678#[cfg(test)]
679mod tests {
680    use super::*;
681    #[test]
682    #[should_panic(expected = "argv[0] cannot contain a doublequote (\") character")]
683    fn windows_argv0_panic_on_quote() {
684        args_to_utf16_command_string(["\""].iter());
685    }
686    #[test]
687    fn windows_argv0_no_escape() {
688        // Ensure that a trailing backslash in argv[0] is not escaped.
689        let cmd = String::from_utf16_lossy(&args_to_utf16_command_string(
690            [r"C:\Program Files\", "arg1", "arg 2", "arg \" 3"].iter(),
691        ));
692        assert_eq!(cmd.trim_end_matches('\0'), r#""C:\Program Files\" arg1 "arg 2" "arg \" 3""#);
693    }
694}