Skip to content

Commit a3808f9

Browse files
committed
Add support for panicking in the emulated application when unsupported syscalls are encountered
1 parent 25a43c7 commit a3808f9

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

src/bin/miri.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ fn main() {
233233
"-Zmiri-ignore-leaks" => {
234234
miri_config.ignore_leaks = true;
235235
}
236+
"-Zmiri-panic-on-unsupported-syscalls" => {
237+
miri_config.panic_on_unsupported_syscalls = true;
238+
}
236239
"-Zmiri-track-raw-pointers" => {
237240
miri_config.track_raw = true;
238241
}

src/eval.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub struct MiriConfig {
5454
/// Rate of spurious failures for compare_exchange_weak atomic operations,
5555
/// between 0.0 and 1.0, defaulting to 0.8 (80% chance of failure).
5656
pub cmpxchg_weak_failure_rate: f64,
57+
/// Panic when unsupported syscalls are encountered
58+
pub panic_on_unsupported_syscalls: bool,
5759
}
5860

5961
impl Default for MiriConfig {
@@ -73,6 +75,7 @@ impl Default for MiriConfig {
7375
track_raw: false,
7476
data_race_detector: true,
7577
cmpxchg_weak_failure_rate: 0.8,
78+
panic_on_unsupported_syscalls: false,
7679
}
7780
}
7881
}
@@ -92,8 +95,8 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
9295
tcx,
9396
rustc_span::source_map::DUMMY_SP,
9497
param_env,
95-
Evaluator::new(config.communicate, config.validate, layout_cx),
96-
MemoryExtra::new(&config),
98+
Evaluator::new(config.communicate, config.validate, config.panic_on_unsupported_syscalls, layout_cx),
99+
MemoryExtra::new(&config)
97100
);
98101
// Complete initialization.
99102
EnvVars::init(&mut ecx, config.excluded_env_vars)?;

src/machine.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,18 @@ pub struct Evaluator<'mir, 'tcx> {
274274

275275
/// Allocations that are considered roots of static memory (that may leak).
276276
pub(crate) static_roots: Vec<AllocId>,
277+
278+
/// Whether to raise a panic in the context of the evaluated process when unsupported
279+
/// syscalls are encountered. If `false`, an error is propagated in the Miri application context
280+
/// instead (default behavior)
281+
pub(crate) panic_on_unsupported_syscalls: bool,
277282
}
278283

279284
impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
280285
pub(crate) fn new(
281286
communicate: bool,
282287
validate: bool,
288+
panic_on_unsupported_syscalls: bool,
283289
layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
284290
) -> Self {
285291
let layouts =
@@ -300,6 +306,7 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
300306
layouts,
301307
threads: ThreadManager::default(),
302308
static_roots: Vec::new(),
309+
panic_on_unsupported_syscalls,
303310
}
304311
}
305312
}

src/shims/posix/linux/foreign_items.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
182182
id if id == sys_futex => {
183183
futex(this, args, dest)?;
184184
}
185-
id => throw_unsup_format!("Miri does not support syscall ID {}", id),
185+
id => {
186+
if this.eval_context_ref().machine.panic_on_unsupported_syscalls {
187+
// message is slightly different here to make automated analysis easier
188+
this.start_panic(format!("unsupported Miri functionality: syscall ID {} is not emulated", id).as_ref(), None)?;
189+
return Ok(false);
190+
} else {
191+
throw_unsup_format!("Miri does not support syscall ID {}", id);
192+
}
193+
}
186194
}
187195
}
188196

@@ -214,7 +222,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
214222
this.write_null(dest)?;
215223
}
216224

217-
_ => throw_unsup_format!("can't call foreign function: {}", link_name),
225+
_ => {
226+
if this.eval_context_ref().machine.panic_on_unsupported_syscalls {
227+
// message is slightly different here to make automated analysis easier
228+
this.start_panic(format!("unsupported Miri functionality: can't call foreign function {:?}", link_name).as_ref(), None)?;
229+
return Ok(false);
230+
} else {
231+
throw_unsup_format!("can't call foreign function: {}", link_name);
232+
}
233+
}
218234
};
219235

220236
Ok(true)

src/shims/posix/macos/foreign_items.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
156156
this.write_scalar(addr, dest)?;
157157
}
158158

159-
_ => throw_unsup_format!("can't call foreign function: {}", link_name),
159+
_ => {
160+
if this.eval_context_ref().machine.panic_on_unsupported_syscalls {
161+
// message is slightly different here to make automated analysis easier
162+
this.start_panic(format!("unsupported Miri functionality: can't call foreign function {:?}", link_name).as_ref(), None)?;
163+
return Ok(false);
164+
} else {
165+
throw_unsup_format!("can't call foreign function: {}", link_name);
166+
}
167+
}
160168
};
161169

162170
Ok(true)

src/shims/windows/foreign_items.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
342342
// Better error for attempts to create a thread
343343
"CreateThread" => {
344344
check_abi(abi, Abi::System { unwind: false })?;
345-
throw_unsup_format!("Miri does not support concurrency on Windows");
345+
if this.eval_context_ref().machine.panic_on_unsupported_syscalls {
346+
this.start_panic("unsupported Miri functionality: concurrency is not supported on Windows", None)?;
347+
return Ok(false);
348+
} else {
349+
throw_unsup_format!("Miri does not support concurrency on Windows");
350+
}
346351
}
347352

348353
// Incomplete shims that we "stub out" just to get pre-main initialization code to work.
@@ -415,7 +420,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
415420
this.write_scalar(Scalar::from_i32(1), dest)?;
416421
}
417422

418-
_ => throw_unsup_format!("can't call foreign function: {}", link_name),
423+
_ => {
424+
if this.eval_context_ref().machine.panic_on_unsupported_syscalls {
425+
// message is slightly different here to make automated analysis easier
426+
this.start_panic(format!("unsupported Miri functionality: can't call foreign function {:?}", link_name).as_ref(), None)?;
427+
return Ok(false);
428+
} else {
429+
throw_unsup_format!("can't call foreign function: {}", link_name);
430+
}
431+
}
419432
}
420433

421434
Ok(true)

0 commit comments

Comments
 (0)