Skip to content

Commit 4e231ba

Browse files
committed
format much of Miri
1 parent 7b35660 commit 4e231ba

35 files changed

+752
-514
lines changed

benches/helpers/miri_helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ extern crate rustc_driver;
22
extern crate rustc_hir;
33
extern crate rustc_interface;
44

5-
use rustc_hir::def_id::LOCAL_CRATE;
65
use rustc_driver::Compilation;
6+
use rustc_hir::def_id::LOCAL_CRATE;
77
use rustc_interface::{interface, Queries};
88

99
use crate::test::Bencher;

src/bin/miri.rs

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![feature(rustc_private)]
22

3-
extern crate rustc_middle;
43
extern crate rustc_driver;
4+
extern crate rustc_errors;
55
extern crate rustc_hir;
66
extern crate rustc_interface;
7+
extern crate rustc_middle;
78
extern crate rustc_session;
8-
extern crate rustc_errors;
99

1010
use std::convert::TryFrom;
1111
use std::env;
@@ -14,11 +14,11 @@ use std::str::FromStr;
1414
use hex::FromHexError;
1515
use log::debug;
1616

17-
use rustc_session::{CtfeBacktrace, config::ErrorOutputType};
18-
use rustc_errors::emitter::{HumanReadableErrorType, ColorConfig};
1917
use rustc_driver::Compilation;
18+
use rustc_errors::emitter::{ColorConfig, HumanReadableErrorType};
2019
use rustc_hir::def_id::LOCAL_CRATE;
2120
use rustc_middle::ty::TyCtxt;
21+
use rustc_session::{config::ErrorOutputType, CtfeBacktrace};
2222

2323
struct MiriCompilerCalls {
2424
miri_config: miri::MiriConfig,
@@ -37,8 +37,13 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
3737
let (entry_def_id, _) = if let Some((entry_def, x)) = tcx.entry_fn(LOCAL_CRATE) {
3838
(entry_def, x)
3939
} else {
40-
let output_ty = ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(ColorConfig::Auto));
41-
rustc_session::early_error(output_ty, "miri can only run programs that have a main function");
40+
let output_ty = ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(
41+
ColorConfig::Auto,
42+
));
43+
rustc_session::early_error(
44+
output_ty,
45+
"miri can only run programs that have a main function",
46+
);
4247
};
4348
let mut config = self.miri_config.clone();
4449

@@ -249,28 +254,27 @@ fn main() {
249254
err => panic!("unknown error decoding -Zmiri-seed as hex: {:?}", err),
250255
});
251256
if seed_raw.len() > 8 {
252-
panic!(
253-
"-Zmiri-seed must be at most 8 bytes, was {}",
254-
seed_raw.len()
255-
);
257+
panic!("-Zmiri-seed must be at most 8 bytes, was {}", seed_raw.len());
256258
}
257259

258260
let mut bytes = [0; 8];
259261
bytes[..seed_raw.len()].copy_from_slice(&seed_raw);
260262
miri_config.seed = Some(u64::from_be_bytes(bytes));
261263
}
262264
arg if arg.starts_with("-Zmiri-env-exclude=") => {
263-
miri_config.excluded_env_vars
265+
miri_config
266+
.excluded_env_vars
264267
.push(arg.strip_prefix("-Zmiri-env-exclude=").unwrap().to_owned());
265268
}
266269
arg if arg.starts_with("-Zmiri-track-pointer-tag=") => {
267-
let id: u64 = match arg.strip_prefix("-Zmiri-track-pointer-tag=").unwrap().parse() {
268-
Ok(id) => id,
269-
Err(err) => panic!(
270-
"-Zmiri-track-pointer-tag requires a valid `u64` argument: {}",
271-
err
272-
),
273-
};
270+
let id: u64 =
271+
match arg.strip_prefix("-Zmiri-track-pointer-tag=").unwrap().parse() {
272+
Ok(id) => id,
273+
Err(err) => panic!(
274+
"-Zmiri-track-pointer-tag requires a valid `u64` argument: {}",
275+
err
276+
),
277+
};
274278
if let Some(id) = miri::PtrId::new(id) {
275279
miri_config.tracked_pointer_tag = Some(id);
276280
} else {
@@ -280,10 +284,8 @@ fn main() {
280284
arg if arg.starts_with("-Zmiri-track-call-id=") => {
281285
let id: u64 = match arg.strip_prefix("-Zmiri-track-call-id=").unwrap().parse() {
282286
Ok(id) => id,
283-
Err(err) => panic!(
284-
"-Zmiri-track-call-id requires a valid `u64` argument: {}",
285-
err
286-
),
287+
Err(err) =>
288+
panic!("-Zmiri-track-call-id requires a valid `u64` argument: {}", err),
287289
};
288290
if let Some(id) = miri::CallId::new(id) {
289291
miri_config.tracked_call_id = Some(id);
@@ -292,20 +294,28 @@ fn main() {
292294
}
293295
}
294296
arg if arg.starts_with("-Zmiri-track-alloc-id=") => {
295-
let id: u64 = match arg.strip_prefix("-Zmiri-track-alloc-id=").unwrap().parse() {
297+
let id: u64 = match arg.strip_prefix("-Zmiri-track-alloc-id=").unwrap().parse()
298+
{
296299
Ok(id) => id,
297-
Err(err) => panic!(
298-
"-Zmiri-track-alloc-id requires a valid `u64` argument: {}",
299-
err
300-
),
300+
Err(err) =>
301+
panic!("-Zmiri-track-alloc-id requires a valid `u64` argument: {}", err),
301302
};
302303
miri_config.tracked_alloc_id = Some(miri::AllocId(id));
303304
}
304305
arg if arg.starts_with("-Zmiri-compare-exchange-weak-failure-rate=") => {
305-
let rate = match arg.strip_prefix("-Zmiri-compare-exchange-weak-failure-rate=").unwrap().parse::<f64>() {
306+
let rate = match arg
307+
.strip_prefix("-Zmiri-compare-exchange-weak-failure-rate=")
308+
.unwrap()
309+
.parse::<f64>()
310+
{
306311
Ok(rate) if rate >= 0.0 && rate <= 1.0 => rate,
307-
Ok(_) => panic!("-Zmiri-compare-exchange-weak-failure-rate must be between `0.0` and `1.0`"),
308-
Err(err) => panic!("-Zmiri-compare-exchange-weak-failure-rate requires a `f64` between `0.0` and `1.0`: {}", err),
312+
Ok(_) => panic!(
313+
"-Zmiri-compare-exchange-weak-failure-rate must be between `0.0` and `1.0`"
314+
),
315+
Err(err) => panic!(
316+
"-Zmiri-compare-exchange-weak-failure-rate requires a `f64` between `0.0` and `1.0`: {}",
317+
err
318+
),
309319
};
310320
miri_config.cmpxchg_weak_failure_rate = rate;
311321
}
@@ -319,5 +329,9 @@ fn main() {
319329

320330
debug!("rustc arguments: {:?}", rustc_args);
321331
debug!("crate arguments: {:?}", miri_config.args);
322-
run_compiler(rustc_args, &mut MiriCompilerCalls { miri_config }, /* insert_default_args: */ true)
332+
run_compiler(
333+
rustc_args,
334+
&mut MiriCompilerCalls { miri_config },
335+
/* insert_default_args: */ true,
336+
)
323337
}

src/eval.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
135135
argvs.push(arg_place.ptr);
136136
}
137137
// Make an array with all these pointers, in the Miri memory.
138-
let argvs_layout =
139-
ecx.layout_of(tcx.mk_array(tcx.mk_imm_ptr(tcx.types.u8), u64::try_from(argvs.len()).unwrap()))?;
138+
let argvs_layout = ecx.layout_of(
139+
tcx.mk_array(tcx.mk_imm_ptr(tcx.types.u8), u64::try_from(argvs.len()).unwrap()),
140+
)?;
140141
let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Machine.into());
141142
for (idx, arg) in argvs.into_iter().enumerate() {
142143
let place = ecx.mplace_field(&argvs_place, idx)?;
@@ -224,9 +225,11 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
224225
assert!(ecx.step()?, "a terminated thread was scheduled for execution");
225226
}
226227
SchedulingAction::ExecuteTimeoutCallback => {
227-
assert!(ecx.machine.communicate,
228+
assert!(
229+
ecx.machine.communicate,
228230
"scheduler callbacks require disabled isolation, but the code \
229-
that created the callback did not check it");
231+
that created the callback did not check it"
232+
);
230233
ecx.run_timeout_callback()?;
231234
}
232235
SchedulingAction::ExecuteDtors => {
@@ -241,7 +244,8 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
241244
}
242245
ecx.process_diagnostics(info);
243246
}
244-
let return_code = ecx.read_scalar(&ret_place.into())?.check_init()?.to_machine_isize(&ecx)?;
247+
let return_code =
248+
ecx.read_scalar(&ret_place.into())?.check_init()?.to_machine_isize(&ecx)?;
245249
Ok(return_code)
246250
})();
247251

src/helpers.rs

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::time::Duration;
55

66
use log::trace;
77

8-
use rustc_middle::mir;
9-
use rustc_middle::ty::{self, List, TyCtxt, layout::TyAndLayout};
108
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
11-
use rustc_target::abi::{LayoutOf, Size, FieldsShape, Variants};
9+
use rustc_middle::mir;
10+
use rustc_middle::ty::{self, layout::TyAndLayout, List, TyCtxt};
11+
use rustc_target::abi::{FieldsShape, LayoutOf, Size, Variants};
1212
use rustc_target::spec::abi::Abi;
1313

1414
use rand::RngCore;
@@ -19,10 +19,8 @@ impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mi
1919

2020
/// Gets an instance for a path.
2121
fn try_resolve_did<'mir, 'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> Option<DefId> {
22-
tcx.crates()
23-
.iter()
24-
.find(|&&krate| tcx.original_crate_name(krate).as_str() == path[0])
25-
.and_then(|krate| {
22+
tcx.crates().iter().find(|&&krate| tcx.original_crate_name(krate).as_str() == path[0]).and_then(
23+
|krate| {
2624
let krate = DefId { krate: *krate, index: CRATE_DEF_INDEX };
2725
let mut items = tcx.item_children(krate);
2826
let mut path_it = path.iter().skip(1).peekable();
@@ -40,7 +38,8 @@ fn try_resolve_did<'mir, 'tcx>(tcx: TyCtxt<'tcx>, path: &[&str]) -> Option<DefId
4038
}
4139
}
4240
None
43-
})
41+
},
42+
)
4443
}
4544

4645
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
@@ -53,10 +52,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5352

5453
/// Evaluates the scalar at the specified path. Returns Some(val)
5554
/// if the path could be resolved, and None otherwise
56-
fn eval_path_scalar(
57-
&mut self,
58-
path: &[&str],
59-
) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
55+
fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
6056
let this = self.eval_context_mut();
6157
let instance = this.resolve_path(path);
6258
let cid = GlobalId { instance, promoted: None };
@@ -67,9 +63,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6763

6864
/// Helper function to get a `libc` constant as a `Scalar`.
6965
fn eval_libc(&mut self, name: &str) -> InterpResult<'tcx, Scalar<Tag>> {
70-
self.eval_context_mut()
71-
.eval_path_scalar(&["libc", name])?
72-
.check_init()
66+
self.eval_context_mut().eval_path_scalar(&["libc", name])?.check_init()
7367
}
7468

7569
/// Helper function to get a `libc` constant as an `i32`.
@@ -101,7 +95,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
10195
/// Helper function to get the `TyAndLayout` of a `windows` type
10296
fn windows_ty_layout(&mut self, name: &str) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
10397
let this = self.eval_context_mut();
104-
let ty = this.resolve_path(&["std", "sys", "windows", "c", name]).ty(*this.tcx, ty::ParamEnv::reveal_all());
98+
let ty = this
99+
.resolve_path(&["std", "sys", "windows", "c", name])
100+
.ty(*this.tcx, ty::ParamEnv::reveal_all());
105101
this.layout_of(ty)
106102
}
107103

@@ -170,7 +166,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
170166
let param_env = ty::ParamEnv::reveal_all(); // in Miri this is always the param_env we use... and this.param_env is private.
171167
let callee_abi = f.ty(*this.tcx, param_env).fn_sig(*this.tcx).abi();
172168
if callee_abi != caller_abi {
173-
throw_ub_format!("calling a function with ABI {} using caller ABI {}", callee_abi.name(), caller_abi.name())
169+
throw_ub_format!(
170+
"calling a function with ABI {} using caller ABI {}",
171+
callee_abi.name(),
172+
caller_abi.name()
173+
)
174174
}
175175

176176
// Push frame.
@@ -181,9 +181,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
181181
let mut callee_args = this.frame().body.args_iter();
182182
for arg in args {
183183
let callee_arg = this.local_place(
184-
callee_args.next().ok_or_else(||
185-
err_ub_format!("callee has fewer arguments than expected")
186-
)?
184+
callee_args
185+
.next()
186+
.ok_or_else(|| err_ub_format!("callee has fewer arguments than expected"))?,
187187
)?;
188188
this.write_immediate(*arg, &callee_arg)?;
189189
}
@@ -356,7 +356,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
356356
}
357357
}
358358

359-
fn visit_union(&mut self, _v: &MPlaceTy<'tcx, Tag>, _fields: NonZeroUsize) -> InterpResult<'tcx> {
359+
fn visit_union(
360+
&mut self,
361+
_v: &MPlaceTy<'tcx, Tag>,
362+
_fields: NonZeroUsize,
363+
) -> InterpResult<'tcx> {
360364
bug!("we should have already handled unions in `visit_value`")
361365
}
362366
}
@@ -465,12 +469,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
465469
})?
466470
} else if target.families.contains(&"windows".to_owned()) {
467471
// FIXME: we have to finish implementing the Windows equivalent of this.
468-
this.eval_windows("c", match e.kind() {
469-
NotFound => "ERROR_FILE_NOT_FOUND",
470-
_ => throw_unsup_format!("io error {} cannot be transformed into a raw os error", e)
471-
})?
472+
this.eval_windows(
473+
"c",
474+
match e.kind() {
475+
NotFound => "ERROR_FILE_NOT_FOUND",
476+
_ => throw_unsup_format!(
477+
"io error {} cannot be transformed into a raw os error",
478+
e
479+
),
480+
},
481+
)?
472482
} else {
473-
throw_unsup_format!("setting the last OS error from an io::Error is unsupported for {}.", target_os)
483+
throw_unsup_format!(
484+
"setting the last OS error from an io::Error is unsupported for {}.",
485+
target_os
486+
)
474487
};
475488
this.set_last_error(last_error)
476489
}
@@ -556,8 +569,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
556569
}
557570

558571
/// Check that the number of args is what we expect.
559-
pub fn check_arg_count<'a, 'tcx, const N: usize>(args: &'a [OpTy<'tcx, Tag>]) -> InterpResult<'tcx, &'a [OpTy<'tcx, Tag>; N]>
560-
where &'a [OpTy<'tcx, Tag>; N]: TryFrom<&'a [OpTy<'tcx, Tag>]> {
572+
pub fn check_arg_count<'a, 'tcx, const N: usize>(
573+
args: &'a [OpTy<'tcx, Tag>],
574+
) -> InterpResult<'tcx, &'a [OpTy<'tcx, Tag>; N]>
575+
where
576+
&'a [OpTy<'tcx, Tag>; N]: TryFrom<&'a [OpTy<'tcx, Tag>]>,
577+
{
561578
if let Ok(ops) = args.try_into() {
562579
return Ok(ops);
563580
}
@@ -569,7 +586,11 @@ pub fn check_abi<'a>(abi: Abi, exp_abi: Abi) -> InterpResult<'a, ()> {
569586
if abi == exp_abi {
570587
Ok(())
571588
} else {
572-
throw_ub_format!("calling a function with ABI {} using caller ABI {}", exp_abi.name(), abi.name())
589+
throw_ub_format!(
590+
"calling a function with ABI {} using caller ABI {}",
591+
exp_abi.name(),
592+
abi.name()
593+
)
573594
}
574595
}
575596

src/intptrcast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use log::trace;
66
use rand::Rng;
77

88
use rustc_data_structures::fx::FxHashMap;
9-
use rustc_target::abi::{Size, HasDataLayout};
9+
use rustc_target::abi::{HasDataLayout, Size};
1010

1111
use crate::*;
1212

0 commit comments

Comments
 (0)