]> git.lizzy.rs Git - rust.git/blob - src/eval.rs
Auto merge of #2186 - matthiaskrgr:clippy, r=RalfJung
[rust.git] / src / eval.rs
1 //! Main evaluator loop and setting up the initial stack frame.
2
3 use std::ffi::OsStr;
4 use std::iter;
5
6 use log::info;
7
8 use rustc_hir::def_id::DefId;
9 use rustc_middle::ty::{
10     self,
11     layout::{LayoutCx, LayoutOf},
12     TyCtxt,
13 };
14 use rustc_target::spec::abi::Abi;
15
16 use rustc_session::config::EntryFnType;
17
18 use std::collections::HashSet;
19
20 use crate::*;
21
22 #[derive(Copy, Clone, Debug, PartialEq)]
23 pub enum AlignmentCheck {
24     /// Do not check alignment.
25     None,
26     /// Check alignment "symbolically", i.e., using only the requested alignment for an allocation and not its real base address.
27     Symbolic,
28     /// Check alignment on the actual physical integer address.
29     Int,
30 }
31
32 #[derive(Copy, Clone, Debug, PartialEq)]
33 pub enum RejectOpWith {
34     /// Isolated op is rejected with an abort of the machine.
35     Abort,
36
37     /// If not Abort, miri returns an error for an isolated op.
38     /// Following options determine if user should be warned about such error.
39     /// Do not print warning about rejected isolated op.
40     NoWarning,
41
42     /// Print a warning about rejected isolated op, with backtrace.
43     Warning,
44
45     /// Print a warning about rejected isolated op, without backtrace.
46     WarningWithoutBacktrace,
47 }
48
49 #[derive(Copy, Clone, Debug, PartialEq)]
50 pub enum IsolatedOp {
51     /// Reject an op requiring communication with the host. By
52     /// default, miri rejects the op with an abort. If not, it returns
53     /// an error code, and prints a warning about it. Warning levels
54     /// are controlled by `RejectOpWith` enum.
55     Reject(RejectOpWith),
56
57     /// Execute op requiring communication with the host, i.e. disable isolation.
58     Allow,
59 }
60
61 #[derive(Copy, Clone, PartialEq, Eq)]
62 pub enum BacktraceStyle {
63     /// Prints a terser backtrace which ideally only contains relevant information.
64     Short,
65     /// Prints a backtrace with all possible information.
66     Full,
67     /// Prints only the frame that the error occurs in.
68     Off,
69 }
70
71 /// Configuration needed to spawn a Miri instance.
72 #[derive(Clone)]
73 pub struct MiriConfig {
74     /// Determine if validity checking is enabled.
75     pub validate: bool,
76     /// Determines if Stacked Borrows is enabled.
77     pub stacked_borrows: bool,
78     /// Controls alignment checking.
79     pub check_alignment: AlignmentCheck,
80     /// Controls integer and float validity initialization checking.
81     pub allow_uninit_numbers: bool,
82     /// Controls how we treat ptr2int and int2ptr transmutes.
83     pub allow_ptr_int_transmute: bool,
84     /// Controls function [ABI](Abi) checking.
85     pub check_abi: bool,
86     /// Action for an op requiring communication with the host.
87     pub isolated_op: IsolatedOp,
88     /// Determines if memory leaks should be ignored.
89     pub ignore_leaks: bool,
90     /// Environment variables that should always be isolated from the host.
91     pub excluded_env_vars: Vec<String>,
92     /// Environment variables that should always be forwarded from the host.
93     pub forwarded_env_vars: Vec<String>,
94     /// Command-line arguments passed to the interpreted program.
95     pub args: Vec<String>,
96     /// The seed to use when non-determinism or randomness are required (e.g. ptr-to-int cast, `getrandom()`).
97     pub seed: Option<u64>,
98     /// The stacked borrows pointer ids to report about
99     pub tracked_pointer_tags: HashSet<PtrId>,
100     /// The stacked borrows call IDs to report about
101     pub tracked_call_ids: HashSet<CallId>,
102     /// The allocation ids to report about.
103     pub tracked_alloc_ids: HashSet<AllocId>,
104     /// Whether to track raw pointers in stacked borrows.
105     pub tag_raw: bool,
106     /// Determine if data race detection should be enabled
107     pub data_race_detector: bool,
108     /// Rate of spurious failures for compare_exchange_weak atomic operations,
109     /// between 0.0 and 1.0, defaulting to 0.8 (80% chance of failure).
110     pub cmpxchg_weak_failure_rate: f64,
111     /// If `Some`, enable the `measureme` profiler, writing results to a file
112     /// with the specified prefix.
113     pub measureme_out: Option<String>,
114     /// Panic when unsupported functionality is encountered.
115     pub panic_on_unsupported: bool,
116     /// Which style to use for printing backtraces.
117     pub backtrace_style: BacktraceStyle,
118     /// Which provenance to use for int2ptr casts
119     pub provenance_mode: ProvenanceMode,
120     /// Whether to ignore any output by the program. This is helpful when debugging miri
121     /// as its messages don't get intermingled with the program messages.
122     pub mute_stdout_stderr: bool,
123 }
124
125 impl Default for MiriConfig {
126     fn default() -> MiriConfig {
127         MiriConfig {
128             validate: true,
129             stacked_borrows: true,
130             check_alignment: AlignmentCheck::Int,
131             allow_uninit_numbers: false,
132             allow_ptr_int_transmute: false,
133             check_abi: true,
134             isolated_op: IsolatedOp::Reject(RejectOpWith::Abort),
135             ignore_leaks: false,
136             excluded_env_vars: vec![],
137             forwarded_env_vars: vec![],
138             args: vec![],
139             seed: None,
140             tracked_pointer_tags: HashSet::default(),
141             tracked_call_ids: HashSet::default(),
142             tracked_alloc_ids: HashSet::default(),
143             tag_raw: false,
144             data_race_detector: true,
145             cmpxchg_weak_failure_rate: 0.8,
146             measureme_out: None,
147             panic_on_unsupported: false,
148             backtrace_style: BacktraceStyle::Short,
149             provenance_mode: ProvenanceMode::Legacy,
150             mute_stdout_stderr: false,
151         }
152     }
153 }
154
155 /// Returns a freshly created `InterpCx`, along with an `MPlaceTy` representing
156 /// the location where the return value of the `start` function will be
157 /// written to.
158 /// Public because this is also used by `priroda`.
159 pub fn create_ecx<'mir, 'tcx: 'mir>(
160     tcx: TyCtxt<'tcx>,
161     entry_id: DefId,
162     entry_type: EntryFnType,
163     config: &MiriConfig,
164 ) -> InterpResult<'tcx, (InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>, MPlaceTy<'tcx, Tag>)> {
165     let param_env = ty::ParamEnv::reveal_all();
166     let layout_cx = LayoutCx { tcx, param_env };
167     let mut ecx = InterpCx::new(
168         tcx,
169         rustc_span::source_map::DUMMY_SP,
170         param_env,
171         Evaluator::new(config, layout_cx),
172     );
173
174     // Capture the current interpreter stack state (which should be empty) so that we can emit
175     // allocation-tracking and tag-tracking diagnostics for allocations which are part of the
176     // early runtime setup.
177     let info = ecx.preprocess_diagnostics();
178
179     // Some parts of initialization require a full `InterpCx`.
180     Evaluator::late_init(&mut ecx, config)?;
181
182     // Make sure we have MIR. We check MIR for some stable monomorphic function in libcore.
183     let sentinel = ecx.try_resolve_path(&["core", "ascii", "escape_default"]);
184     if !matches!(sentinel, Some(s) if tcx.is_mir_available(s.def.def_id())) {
185         tcx.sess.fatal(
186             "the current sysroot was built without `-Zalways-encode-mir`, or libcore seems missing. \
187             Use `cargo miri setup` to prepare a sysroot that is suitable for Miri."
188         );
189     }
190
191     // Setup first stack frame.
192     let entry_instance = ty::Instance::mono(tcx, entry_id);
193
194     // First argument is constructed later, because it's skipped if the entry function uses #[start].
195
196     // Second argument (argc): length of `config.args`.
197     let argc = Scalar::from_machine_usize(u64::try_from(config.args.len()).unwrap(), &ecx);
198     // Third argument (`argv`): created from `config.args`.
199     let argv = {
200         // Put each argument in memory, collect pointers.
201         let mut argvs = Vec::<Immediate<Tag>>::new();
202         for arg in config.args.iter() {
203             // Make space for `0` terminator.
204             let size = u64::try_from(arg.len()).unwrap().checked_add(1).unwrap();
205             let arg_type = tcx.mk_array(tcx.types.u8, size);
206             let arg_place =
207                 ecx.allocate(ecx.layout_of(arg_type)?, MiriMemoryKind::Machine.into())?;
208             ecx.write_os_str_to_c_str(OsStr::new(arg), arg_place.ptr, size)?;
209             ecx.mark_immutable(&*arg_place);
210             argvs.push(arg_place.to_ref(&ecx));
211         }
212         // Make an array with all these pointers, in the Miri memory.
213         let argvs_layout = ecx.layout_of(
214             tcx.mk_array(tcx.mk_imm_ptr(tcx.types.u8), u64::try_from(argvs.len()).unwrap()),
215         )?;
216         let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Machine.into())?;
217         for (idx, arg) in argvs.into_iter().enumerate() {
218             let place = ecx.mplace_field(&argvs_place, idx)?;
219             ecx.write_immediate(arg, &place.into())?;
220         }
221         ecx.mark_immutable(&*argvs_place);
222         // A pointer to that place is the 3rd argument for main.
223         let argv = argvs_place.to_ref(&ecx);
224         // Store `argc` and `argv` for macOS `_NSGetArg{c,v}`.
225         {
226             let argc_place =
227                 ecx.allocate(ecx.machine.layouts.isize, MiriMemoryKind::Machine.into())?;
228             ecx.write_scalar(argc, &argc_place.into())?;
229             ecx.mark_immutable(&*argc_place);
230             ecx.machine.argc = Some(*argc_place);
231
232             let argv_place = ecx.allocate(
233                 ecx.layout_of(tcx.mk_imm_ptr(tcx.types.unit))?,
234                 MiriMemoryKind::Machine.into(),
235             )?;
236             ecx.write_immediate(argv, &argv_place.into())?;
237             ecx.mark_immutable(&*argv_place);
238             ecx.machine.argv = Some(*argv_place);
239         }
240         // Store command line as UTF-16 for Windows `GetCommandLineW`.
241         {
242             // Construct a command string with all the aguments.
243             let cmd_utf16: Vec<u16> = args_to_utf16_command_string(config.args.iter());
244
245             let cmd_type = tcx.mk_array(tcx.types.u16, u64::try_from(cmd_utf16.len()).unwrap());
246             let cmd_place =
247                 ecx.allocate(ecx.layout_of(cmd_type)?, MiriMemoryKind::Machine.into())?;
248             ecx.machine.cmd_line = Some(*cmd_place);
249             // Store the UTF-16 string. We just allocated so we know the bounds are fine.
250             for (idx, &c) in cmd_utf16.iter().enumerate() {
251                 let place = ecx.mplace_field(&cmd_place, idx)?;
252                 ecx.write_scalar(Scalar::from_u16(c), &place.into())?;
253             }
254             ecx.mark_immutable(&*cmd_place);
255         }
256         argv
257     };
258
259     // Return place (in static memory so that it does not count as leak).
260     let ret_place = ecx.allocate(ecx.machine.layouts.isize, MiriMemoryKind::Machine.into())?;
261     // Call start function.
262
263     match entry_type {
264         EntryFnType::Main => {
265             let start_id = tcx.lang_items().start_fn().unwrap();
266             let main_ret_ty = tcx.fn_sig(entry_id).output();
267             let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
268             let start_instance = ty::Instance::resolve(
269                 tcx,
270                 ty::ParamEnv::reveal_all(),
271                 start_id,
272                 tcx.mk_substs(::std::iter::once(ty::subst::GenericArg::from(main_ret_ty))),
273             )
274             .unwrap()
275             .unwrap();
276
277             let main_ptr = ecx.create_fn_alloc_ptr(FnVal::Instance(entry_instance));
278
279             ecx.call_function(
280                 start_instance,
281                 Abi::Rust,
282                 &[Scalar::from_pointer(main_ptr, &ecx).into(), argc.into(), argv],
283                 &ret_place.into(),
284                 StackPopCleanup::Root { cleanup: true },
285             )?;
286         }
287         EntryFnType::Start => {
288             ecx.call_function(
289                 entry_instance,
290                 Abi::Rust,
291                 &[argc.into(), argv],
292                 &ret_place.into(),
293                 StackPopCleanup::Root { cleanup: true },
294             )?;
295         }
296     }
297
298     // Emit any diagnostics related to the setup process for the runtime, so that when the
299     // interpreter loop starts there are no unprocessed diagnostics.
300     ecx.process_diagnostics(info);
301
302     Ok((ecx, ret_place))
303 }
304
305 /// Evaluates the entry function specified by `entry_id`.
306 /// Returns `Some(return_code)` if program executed completed.
307 /// Returns `None` if an evaluation error occured.
308 pub fn eval_entry<'tcx>(
309     tcx: TyCtxt<'tcx>,
310     entry_id: DefId,
311     entry_type: EntryFnType,
312     config: MiriConfig,
313 ) -> Option<i64> {
314     // Copy setting before we move `config`.
315     let ignore_leaks = config.ignore_leaks;
316
317     let (mut ecx, ret_place) = match create_ecx(tcx, entry_id, entry_type, &config) {
318         Ok(v) => v,
319         Err(err) => {
320             err.print_backtrace();
321             panic!("Miri initialization error: {}", err.kind())
322         }
323     };
324
325     // Perform the main execution.
326     let res: InterpResult<'_, i64> = (|| {
327         // Main loop.
328         loop {
329             let info = ecx.preprocess_diagnostics();
330             match ecx.schedule()? {
331                 SchedulingAction::ExecuteStep => {
332                     assert!(ecx.step()?, "a terminated thread was scheduled for execution");
333                 }
334                 SchedulingAction::ExecuteTimeoutCallback => {
335                     assert!(
336                         ecx.machine.communicate(),
337                         "scheduler callbacks require disabled isolation, but the code \
338                         that created the callback did not check it"
339                     );
340                     ecx.run_timeout_callback()?;
341                 }
342                 SchedulingAction::ExecuteDtors => {
343                     // This will either enable the thread again (so we go back
344                     // to `ExecuteStep`), or determine that this thread is done
345                     // for good.
346                     ecx.schedule_next_tls_dtor_for_active_thread()?;
347                 }
348                 SchedulingAction::Stop => {
349                     break;
350                 }
351             }
352             ecx.process_diagnostics(info);
353         }
354         let return_code = ecx.read_scalar(&ret_place.into())?.to_machine_isize(&ecx)?;
355         Ok(return_code)
356     })();
357
358     // Machine cleanup.
359     // Execution of the program has halted so any memory access we do here
360     // cannot produce a real data race. If we do not do something to disable
361     // data race detection here, some uncommon combination of errors will
362     // cause a data race to be detected:
363     // https://github.com/rust-lang/miri/issues/2020
364     ecx.allow_data_races_mut(|ecx| EnvVars::cleanup(ecx).unwrap());
365
366     // Process the result.
367     match res {
368         Ok(return_code) => {
369             if !ignore_leaks {
370                 // Check for thread leaks.
371                 if !ecx.have_all_terminated() {
372                     tcx.sess.err(
373                         "the main thread terminated without waiting for all remaining threads",
374                     );
375                     tcx.sess.note_without_error("pass `-Zmiri-ignore-leaks` to disable this check");
376                     return None;
377                 }
378                 // Check for memory leaks.
379                 info!("Additonal static roots: {:?}", ecx.machine.static_roots);
380                 let leaks = ecx.leak_report(&ecx.machine.static_roots);
381                 if leaks != 0 {
382                     tcx.sess.err("the evaluated program leaked memory");
383                     tcx.sess.note_without_error("pass `-Zmiri-ignore-leaks` to disable this check");
384                     // Ignore the provided return code - let the reported error
385                     // determine the return code.
386                     return None;
387                 }
388             }
389             Some(return_code)
390         }
391         Err(e) => report_error(&ecx, e),
392     }
393 }
394
395 /// Turns an array of arguments into a Windows command line string.
396 ///
397 /// The string will be UTF-16 encoded and NUL terminated.
398 ///
399 /// Panics if the zeroth argument contains the `"` character because doublequotes
400 /// in argv[0] cannot be encoded using the standard command line parsing rules.
401 ///
402 /// Further reading:
403 /// * [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)
404 /// * [The C/C++ Parameter Parsing Rules](https://daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULES)
405 fn args_to_utf16_command_string<I, T>(mut args: I) -> Vec<u16>
406 where
407     I: Iterator<Item = T>,
408     T: AsRef<str>,
409 {
410     // Parse argv[0]. Slashes aren't escaped. Literal double quotes are not allowed.
411     let mut cmd = {
412         let arg0 = if let Some(arg0) = args.next() {
413             arg0
414         } else {
415             return vec![0];
416         };
417         let arg0 = arg0.as_ref();
418         if arg0.contains('"') {
419             panic!("argv[0] cannot contain a doublequote (\") character");
420         } else {
421             // Always surround argv[0] with quotes.
422             let mut s = String::new();
423             s.push('"');
424             s.push_str(arg0);
425             s.push('"');
426             s
427         }
428     };
429
430     // Build the other arguments.
431     for arg in args {
432         let arg = arg.as_ref();
433         cmd.push(' ');
434         if arg.is_empty() {
435             cmd.push_str("\"\"");
436         } else if !arg.bytes().any(|c| matches!(c, b'"' | b'\t' | b' ')) {
437             // No quote, tab, or space -- no escaping required.
438             cmd.push_str(arg);
439         } else {
440             // Spaces and tabs are escaped by surrounding them in quotes.
441             // Quotes are themselves escaped by using backslashes when in a
442             // quoted block.
443             // Backslashes only need to be escaped when one or more are directly
444             // followed by a quote. Otherwise they are taken literally.
445
446             cmd.push('"');
447             let mut chars = arg.chars().peekable();
448             loop {
449                 let mut nslashes = 0;
450                 while let Some(&'\\') = chars.peek() {
451                     chars.next();
452                     nslashes += 1;
453                 }
454
455                 match chars.next() {
456                     Some('"') => {
457                         cmd.extend(iter::repeat('\\').take(nslashes * 2 + 1));
458                         cmd.push('"');
459                     }
460                     Some(c) => {
461                         cmd.extend(iter::repeat('\\').take(nslashes));
462                         cmd.push(c);
463                     }
464                     None => {
465                         cmd.extend(iter::repeat('\\').take(nslashes * 2));
466                         break;
467                     }
468                 }
469             }
470             cmd.push('"');
471         }
472     }
473
474     if cmd.contains('\0') {
475         panic!("interior null in command line arguments");
476     }
477     cmd.encode_utf16().chain(iter::once(0)).collect()
478 }
479
480 #[cfg(test)]
481 mod tests {
482     use super::*;
483     #[test]
484     #[should_panic(expected = "argv[0] cannot contain a doublequote (\") character")]
485     fn windows_argv0_panic_on_quote() {
486         args_to_utf16_command_string(["\""].iter());
487     }
488     #[test]
489     fn windows_argv0_no_escape() {
490         // Ensure that a trailing backslash in argv[0] is not escaped.
491         let cmd = String::from_utf16_lossy(&args_to_utf16_command_string(
492             [r"C:\Program Files\", "arg1", "arg 2", "arg \" 3"].iter(),
493         ));
494         assert_eq!(cmd.trim_end_matches('\0'), r#""C:\Program Files\" arg1 "arg 2" "arg \" 3""#);
495     }
496 }