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;
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
34/// When the main thread would exit, we will yield to any other thread that is ready to execute.
35/// But we must only do that a finite number of times, or a background thread running `loop {}`
36/// will hang the program.
37const MAIN_THREAD_YIELDS_AT_SHUTDOWN: u32 = 256;
38
39/// Configuration needed to spawn a Miri instance.
40#[derive(Clone)]
41pub struct MiriConfig {
42    /// The host environment snapshot to use as basis for what is provided to the interpreted program.
43    /// (This is still subject to isolation as well as `forwarded_env_vars`.)
44    pub env: Vec<(OsString, OsString)>,
45    /// Determine if validity checking is enabled.
46    pub validation: ValidationMode,
47    /// Determines if Stacked Borrows or Tree Borrows is enabled.
48    pub borrow_tracker: Option<BorrowTrackerMethod>,
49    /// Controls alignment checking.
50    pub check_alignment: AlignmentCheck,
51    /// Action for an op requiring communication with the host.
52    pub isolated_op: IsolatedOp,
53    /// Determines if memory leaks should be ignored.
54    pub ignore_leaks: bool,
55    /// Environment variables that should always be forwarded from the host.
56    pub forwarded_env_vars: Vec<String>,
57    /// Additional environment variables that should be set in the interpreted program.
58    pub set_env_vars: FxHashMap<String, String>,
59    /// Command-line arguments passed to the interpreted program.
60    pub args: Vec<String>,
61    /// The seed to use when non-determinism or randomness are required (e.g. ptr-to-int cast, `getrandom()`).
62    pub seed: Option<u64>,
63    /// The stacked borrows pointer ids to report about.
64    pub tracked_pointer_tags: FxHashSet<BorTag>,
65    /// The allocation ids to report about.
66    pub tracked_alloc_ids: FxHashSet<AllocId>,
67    /// For the tracked alloc ids, also report read/write accesses.
68    pub track_alloc_accesses: bool,
69    /// Determine if data race detection should be enabled.
70    pub data_race_detector: bool,
71    /// Determine if weak memory emulation should be enabled. Requires data race detection to be enabled.
72    pub weak_memory_emulation: bool,
73    /// 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.
74    pub genmc_config: Option<GenmcConfig>,
75    /// Track when an outdated (weak memory) load happens.
76    pub track_outdated_loads: bool,
77    /// Rate of spurious failures for compare_exchange_weak atomic operations,
78    /// between 0.0 and 1.0, defaulting to 0.8 (80% chance of failure).
79    pub cmpxchg_weak_failure_rate: f64,
80    /// If `Some`, enable the `measureme` profiler, writing results to a file
81    /// with the specified prefix.
82    pub measureme_out: Option<String>,
83    /// Which style to use for printing backtraces.
84    pub backtrace_style: BacktraceStyle,
85    /// Which provenance to use for int2ptr casts.
86    pub provenance_mode: ProvenanceMode,
87    /// Whether to ignore any output by the program. This is helpful when debugging miri
88    /// as its messages don't get intermingled with the program messages.
89    pub mute_stdout_stderr: bool,
90    /// The probability of the active thread being preempted at the end of each basic block.
91    pub preemption_rate: f64,
92    /// Report the current instruction being executed every N basic blocks.
93    pub report_progress: Option<u32>,
94    /// The location of the shared object files to load when calling external functions
95    pub native_lib: Vec<PathBuf>,
96    /// Whether to enable the new native lib tracing system.
97    pub native_lib_enable_tracing: bool,
98    /// Run a garbage collector for BorTags every N basic blocks.
99    pub gc_interval: u32,
100    /// The number of CPUs to be reported by miri.
101    pub num_cpus: u32,
102    /// Requires Miri to emulate pages of a certain size.
103    pub page_size: Option<u64>,
104    /// Whether to collect a backtrace when each allocation is created, just in case it leaks.
105    pub collect_leak_backtraces: bool,
106    /// Probability for address reuse.
107    pub address_reuse_rate: f64,
108    /// Probability for address reuse across threads.
109    pub address_reuse_cross_thread_rate: f64,
110    /// Round Robin scheduling with no preemption.
111    pub fixed_scheduling: bool,
112    /// Always prefer the intrinsic fallback body over the native Miri implementation.
113    pub force_intrinsic_fallback: bool,
114    /// Whether floating-point operations can behave non-deterministically.
115    pub float_nondet: bool,
116    /// Whether floating-point operations can have a non-deterministic rounding error.
117    pub float_rounding_error: FloatRoundingErrorMode,
118    /// Whether Miri artificially introduces short reads/writes on file descriptors.
119    pub short_fd_operations: bool,
120    /// A list of crates that are considered user-relevant.
121    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, // 80%
145            measureme_out: None,
146            backtrace_style: BacktraceStyle::Short,
147            provenance_mode: ProvenanceMode::Default,
148            mute_stdout_stderr: false,
149            preemption_rate: 0.01, // 1%
150            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/// The state of the main thread. Implementation detail of `on_main_stack_empty`.
170#[derive(Debug)]
171enum MainThreadState<'tcx> {
172    GlobalCtors {
173        ctor_state: global_ctor::GlobalCtorState<'tcx>,
174        /// The main function to call.
175        entry_id: DefId,
176        entry_type: MiriEntryFnType,
177        /// Arguments passed to `main`.
178        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 => {} // just keep going
199                    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 => {} // just keep going
211                    Poll::Ready(()) => {
212                        if this.machine.data_race.as_genmc_ref().is_some() {
213                            // In GenMC mode, we don't yield at the end of the main thread.
214                            // Instead, the `GenmcCtx` will ensure that unfinished threads get a chance to run at this point.
215                            *self = Done;
216                        } else {
217                            // Give background threads a chance to finish by yielding the main thread a
218                            // couple of times -- but only if we would also preempt threads randomly.
219                            if this.machine.preemption_rate > 0.0 {
220                                // There is a non-zero chance they will yield back to us often enough to
221                                // make Miri terminate eventually.
222                                *self = Yield { remaining: MAIN_THREAD_YIELDS_AT_SHUTDOWN };
223                            } else {
224                                // The other threads did not get preempted, so no need to yield back to
225                                // them.
226                                *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                // Figure out exit code.
241                let ret_place = this.machine.main_fn_ret_place.clone().unwrap();
242                let exit_code = this.read_target_isize(&ret_place)?;
243                // Rust uses `isize` but the underlying type of an exit code is `i32`.
244                // Do a saturating cast.
245                let exit_code = i32::try_from(exit_code).unwrap_or(if exit_code >= 0 {
246                    i32::MAX
247                } else {
248                    i32::MIN
249                });
250                // Deal with our thread-local memory. We do *not* want to actually free it, instead we consider TLS
251                // to be like a global `static`, so that all memory reached by it is considered to "not leak".
252                this.terminate_active_thread(TlsAllocAction::Leak)?;
253
254                // In GenMC mode, we do not immediately stop execution on main thread exit.
255                if let Some(genmc_ctx) = this.machine.data_race.as_genmc_ref() {
256                    // If there's no error, execution will continue (on another thread).
257                    genmc_ctx.handle_exit(
258                        ThreadId::MAIN_THREAD,
259                        exit_code,
260                        crate::concurrency::ExitType::MainThreadFinish,
261                    )?;
262                } else {
263                    // Stop interpreter loop.
264                    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
275/// Returns a freshly created `InterpCx`.
276/// Public because this is also used by `priroda`.
277pub 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    // Make sure we have MIR. We check MIR for some stable monomorphic function in libcore. However,
294    // if the current crate is #![no_core] it's fine to be missing the usual items from libcore.
295    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    // Compute argc and argv from `config.args`.
310    let argc =
311        ImmTy::from_int(i64::try_from(config.args.len()).unwrap(), ecx.machine.layouts.isize);
312    let argv = {
313        // Put each argument in memory, collect pointers.
314        let mut argvs = Vec::<Immediate<Provenance>>::with_capacity(config.args.len());
315        for arg in config.args.iter() {
316            // Make space for `0` terminator.
317            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        // Make an array with all these pointers, in the Miri memory.
326        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        // Store `argc` and `argv` for macOS `_NSGetArg{c,v}`, and for the GC to see them.
337        {
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        // Store command line as UTF-16 for Windows `GetCommandLineW`.
351        if tcx.sess.target.os == Os::Windows {
352            // Construct a command string with all the arguments.
353            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            // Store the UTF-16 string. We just allocated so we know the bounds are fine.
361            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    // Some parts of initialization require a full `InterpCx`.
373    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        // Cannot capture anything GC-relevant here.
383        // `argc` and `argv` *are* GC_relevant, but they also get stored in `machine.argc` and
384        // `machine.argv` so we are good.
385        Box::new(move |m| main_thread_state.on_main_stack_empty(m))
386    })?;
387
388    interp_ok(ecx)
389}
390
391// Call the entry function.
392fn 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    // Setup first stack frame.
402    let entry_instance = ty::Instance::mono(tcx, entry_id);
403
404    // Return place (in static memory so that it does not count as leak).
405    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    // Call start function.
409    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            // Always using DEFAULT is okay since we don't support signals in Miri anyway.
428            // (This means we are effectively ignoring `-Zon-broken-pipe`.)
429            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                        // FIXME use a proper fn ptr type
438                        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
462/// Evaluates the entry function specified by `entry_id`.
463/// Returns `Some(return_code)` if program execution completed.
464/// Returns `None` if an evaluation error occurred.
465pub 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    // Copy setting before we move `config`.
473    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    // Perform the main execution.
485    let res: thread::Result<InterpResult<'_, !>> =
486        panic::catch_unwind(AssertUnwindSafe(|| ecx.run_threads()));
487    let res = res.unwrap_or_else(|panic_payload| {
488        // rustc "handles" some errors by unwinding with FatalErrorMarker
489        // (after emitting suitable diagnostics), so do not treat those as ICEs.
490        if !panic_payload.is::<FatalErrorMarker>() {
491            ecx.handle_ice();
492        }
493        panic::resume_unwind(panic_payload)
494    });
495    // Obtain the result of the execution. This is always an `Err`, but that doesn't necessarily
496    // indicate an error.
497    let Err(res) = res.report_err();
498
499    // Error reporting: if we survive all checks, we return the exit code the program gave us.
500    'miri_error: {
501        // Show diagnostic, if any.
502        let Some((return_code, leak_check)) = report_result(&ecx, res) else {
503            break 'miri_error;
504        };
505
506        // If we get here there was no fatal error -- yet.
507        // Possibly check for memory leaks.
508        if leak_check && !ignore_leaks {
509            // Check for thread leaks.
510            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            // Check for memory leaks.
517            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                // Ignore the provided return code - let the reported error
523                // determine the return code.
524                break 'miri_error;
525            }
526        }
527
528        // The interpreter has not reported an error.
529        // (There could still be errors in the session if there are other interpreters.)
530        return match NonZeroI32::new(return_code) {
531            None => Ok(()),
532            Some(return_code) => Err(return_code),
533        };
534    }
535
536    // The interpreter reported an error.
537    assert!(tcx.dcx().has_errors().is_some());
538    Err(NonZeroI32::new(rustc_driver::EXIT_FAILURE).unwrap())
539}
540
541/// Turns an array of arguments into a Windows command line string.
542///
543/// The string will be UTF-16 encoded and NUL terminated.
544///
545/// Panics if the zeroth argument contains the `"` character because doublequotes
546/// in `argv[0]` cannot be encoded using the standard command line parsing rules.
547///
548/// Further reading:
549/// * [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)
550/// * [The C/C++ Parameter Parsing Rules](https://daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULES)
551fn args_to_utf16_command_string<I, T>(mut args: I) -> Vec<u16>
552where
553    I: Iterator<Item = T>,
554    T: AsRef<str>,
555{
556    // Parse argv[0]. Slashes aren't escaped. Literal double quotes are not allowed.
557    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            // Always surround argv[0] with quotes.
566            let mut s = String::new();
567            s.push('"');
568            s.push_str(arg0);
569            s.push('"');
570            s
571        }
572    };
573
574    // Build the other arguments.
575    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            // No quote, tab, or space -- no escaping required.
582            cmd.push_str(arg);
583        } else {
584            // Spaces and tabs are escaped by surrounding them in quotes.
585            // Quotes are themselves escaped by using backslashes when in a
586            // quoted block.
587            // Backslashes only need to be escaped when one or more are directly
588            // followed by a quote. Otherwise they are taken literally.
589
590            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        // Ensure that a trailing backslash in argv[0] is not escaped.
635        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}