Skip to content

Commit 12e3f75

Browse files
committed
don't make it qutie so easy to get Miri to panic
1 parent 654e15b commit 12e3f75

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/bin/miri.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,15 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
152152
}
153153
}
154154

155-
fn show_error(msg: String) -> ! {
156-
eprintln!("fatal error: {}", msg);
155+
fn show_error(msg: &str) -> ! {
156+
eprintln!("fatal error: {msg}");
157157
std::process::exit(1)
158158
}
159159

160+
macro_rules! show_error {
161+
($($tt:tt)*) => { show_error(&format!($($tt)*)) };
162+
}
163+
160164
fn init_early_loggers() {
161165
// Note that our `extern crate log` is *not* the same as rustc's; as a result, we have to
162166
// initialize them both, and we always initialize `miri`'s first.
@@ -234,19 +238,19 @@ fn host_sysroot() -> Option<String> {
234238
env::var_os("RUSTUP_TOOLCHAIN").or_else(|| env::var_os("MULTIRUST_TOOLCHAIN"))
235239
{
236240
if toolchain_runtime != toolchain {
237-
show_error(format!(
241+
show_error!(
238242
"This Miri got built with local toolchain `{toolchain}`, but now is being run under a different toolchain. \n\
239243
Make sure to run Miri in the toolchain it got built with, e.g. via `cargo +{toolchain} miri`."
240-
));
244+
)
241245
}
242246
}
243247
format!("{}/toolchains/{}", home, toolchain)
244248
}
245249
_ => option_env!("RUST_SYSROOT")
246250
.unwrap_or_else(|| {
247-
show_error(format!(
251+
show_error!(
248252
"To build Miri without rustup, set the `RUST_SYSROOT` env var at build time",
249-
))
253+
)
250254
})
251255
.to_owned(),
252256
})
@@ -272,9 +276,9 @@ fn run_compiler(
272276
// Using the built-in default here would be plain wrong, so we *require*
273277
// the env var to make sure things make sense.
274278
Some(env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
275-
show_error(format!(
279+
show_error!(
276280
"Miri was invoked in 'target' mode without `MIRI_SYSROOT` or `--sysroot` being set"
277-
))
281+
)
278282
}))
279283
} else {
280284
host_default_sysroot
@@ -379,7 +383,9 @@ fn main() {
379383
miri_config.check_abi = false;
380384
} else if arg == "-Zmiri-disable-isolation" {
381385
if matches!(isolation_enabled, Some(true)) {
382-
panic!("-Zmiri-disable-isolation cannot be used along with -Zmiri-isolation-error");
386+
show_error!(
387+
"-Zmiri-disable-isolation cannot be used along with -Zmiri-isolation-error"
388+
);
383389
} else {
384390
isolation_enabled = Some(false);
385391
}
@@ -390,7 +396,9 @@ fn main() {
390396
miri_config.track_outdated_loads = true;
391397
} else if let Some(param) = arg.strip_prefix("-Zmiri-isolation-error=") {
392398
if matches!(isolation_enabled, Some(false)) {
393-
panic!("-Zmiri-isolation-error cannot be used along with -Zmiri-disable-isolation");
399+
show_error!(
400+
"-Zmiri-isolation-error cannot be used along with -Zmiri-disable-isolation"
401+
);
394402
} else {
395403
isolation_enabled = Some(true);
396404
}
@@ -402,7 +410,7 @@ fn main() {
402410
"warn-nobacktrace" =>
403411
miri::IsolatedOp::Reject(miri::RejectOpWith::WarningWithoutBacktrace),
404412
_ =>
405-
panic!(
413+
show_error!(
406414
"-Zmiri-isolation-error must be `abort`, `hide`, `warn`, or `warn-nobacktrace`"
407415
),
408416
};
@@ -426,11 +434,11 @@ fn main() {
426434
);
427435
} else if let Some(param) = arg.strip_prefix("-Zmiri-seed=") {
428436
if miri_config.seed.is_some() {
429-
panic!("Cannot specify -Zmiri-seed multiple times!");
437+
show_error!("Cannot specify -Zmiri-seed multiple times!");
430438
}
431439
let seed = u64::from_str_radix(param, 16)
432-
.unwrap_or_else(|_| panic!(
433-
"-Zmiri-seed should only contain valid hex digits [0-9a-fA-F] and fit into a u64 (max 16 characters)"
440+
.unwrap_or_else(|_| show_error!(
441+
"-Zmiri-seed should only contain valid hex digits [0-9a-fA-F] and must fit into a u64 (max 16 characters)"
434442
));
435443
miri_config.seed = Some(seed);
436444
} else if let Some(param) = arg.strip_prefix("-Zmiri-env-exclude=") {
@@ -441,7 +449,7 @@ fn main() {
441449
let ids: Vec<u64> = match parse_comma_list(param) {
442450
Ok(ids) => ids,
443451
Err(err) =>
444-
panic!(
452+
show_error!(
445453
"-Zmiri-track-pointer-tag requires a comma separated list of valid `u64` arguments: {}",
446454
err
447455
),
@@ -450,14 +458,14 @@ fn main() {
450458
if let Some(id) = id {
451459
miri_config.tracked_pointer_tags.insert(id);
452460
} else {
453-
panic!("-Zmiri-track-pointer-tag requires nonzero arguments");
461+
show_error!("-Zmiri-track-pointer-tag requires nonzero arguments");
454462
}
455463
}
456464
} else if let Some(param) = arg.strip_prefix("-Zmiri-track-call-id=") {
457465
let ids: Vec<u64> = match parse_comma_list(param) {
458466
Ok(ids) => ids,
459467
Err(err) =>
460-
panic!(
468+
show_error!(
461469
"-Zmiri-track-call-id requires a comma separated list of valid `u64` arguments: {}",
462470
err
463471
),
@@ -466,14 +474,14 @@ fn main() {
466474
if let Some(id) = id {
467475
miri_config.tracked_call_ids.insert(id);
468476
} else {
469-
panic!("-Zmiri-track-call-id requires a nonzero argument");
477+
show_error!("-Zmiri-track-call-id requires a nonzero argument");
470478
}
471479
}
472480
} else if let Some(param) = arg.strip_prefix("-Zmiri-track-alloc-id=") {
473481
let ids: Vec<miri::AllocId> = match parse_comma_list::<NonZeroU64>(param) {
474482
Ok(ids) => ids.into_iter().map(miri::AllocId).collect(),
475483
Err(err) =>
476-
panic!(
484+
show_error!(
477485
"-Zmiri-track-alloc-id requires a comma separated list of valid non-zero `u64` arguments: {}",
478486
err
479487
),
@@ -483,11 +491,11 @@ fn main() {
483491
let rate = match param.parse::<f64>() {
484492
Ok(rate) if rate >= 0.0 && rate <= 1.0 => rate,
485493
Ok(_) =>
486-
panic!(
494+
show_error!(
487495
"-Zmiri-compare-exchange-weak-failure-rate must be between `0.0` and `1.0`"
488496
),
489497
Err(err) =>
490-
panic!(
498+
show_error!(
491499
"-Zmiri-compare-exchange-weak-failure-rate requires a `f64` between `0.0` and `1.0`: {}",
492500
err
493501
),
@@ -496,9 +504,9 @@ fn main() {
496504
} else if let Some(param) = arg.strip_prefix("-Zmiri-preemption-rate=") {
497505
let rate = match param.parse::<f64>() {
498506
Ok(rate) if rate >= 0.0 && rate <= 1.0 => rate,
499-
Ok(_) => panic!("-Zmiri-preemption-rate must be between `0.0` and `1.0`"),
507+
Ok(_) => show_error!("-Zmiri-preemption-rate must be between `0.0` and `1.0`"),
500508
Err(err) =>
501-
panic!(
509+
show_error!(
502510
"-Zmiri-preemption-rate requires a `f64` between `0.0` and `1.0`: {}",
503511
err
504512
),
@@ -510,7 +518,7 @@ fn main() {
510518
} else if let Some(param) = arg.strip_prefix("-Zmiri-report-progress=") {
511519
let interval = match param.parse::<u32>() {
512520
Ok(i) => i,
513-
Err(err) => panic!("-Zmiri-report-progress requires a `u32`: {}", err),
521+
Err(err) => show_error!("-Zmiri-report-progress requires a `u32`: {}", err),
514522
};
515523
miri_config.report_progress = Some(interval);
516524
} else if let Some(param) = arg.strip_prefix("-Zmiri-measureme=") {
@@ -520,7 +528,7 @@ fn main() {
520528
"0" => BacktraceStyle::Off,
521529
"1" => BacktraceStyle::Short,
522530
"full" => BacktraceStyle::Full,
523-
_ => panic!("-Zmiri-backtrace may only be 0, 1, or full"),
531+
_ => show_error!("-Zmiri-backtrace may only be 0, 1, or full"),
524532
};
525533
} else {
526534
// Forward to rustc.

0 commit comments

Comments
 (0)