Skip to content

Commit 73e9275

Browse files
committed
Add helper function for unsupported functionality
1 parent c3d235d commit 73e9275

File tree

5 files changed

+37
-52
lines changed

5 files changed

+37
-52
lines changed

src/shims/foreign_items.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
164164
"the program aborted execution".to_owned()
165165
))
166166
}
167-
_ => throw_unsup_format!("can't call (diverging) foreign function: {}", link_name),
167+
_ => {
168+
this.handle_unsupported(format!("can't call (diverging) foreign function: {}", link_name))?;
169+
return Ok(None);
170+
},
168171
},
169172
Some(p) => p,
170173
};

src/shims/panic.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
161161
}
162162
}
163163

164-
/// Starta a panic in the interpreter with the given message as payload.
164+
/// Handler that should be called when unsupported functionality is encountered.
165+
/// This function will either panic within the context of the emulated application
166+
/// or return an error in the Miri process context
167+
///
168+
/// Return value of `Ok(bool)` indicates whether execution should continue.
169+
fn handle_unsupported<S: AsRef<str>>(&mut self, error_msg: S) -> InterpResult<'tcx, ()> {
170+
let error = format!("unsupported Miri functionality: {}", error_msg.as_ref());
171+
172+
if self.eval_context_ref().machine.panic_on_unsupported {
173+
// message is slightly different here to make automated analysis easier
174+
self.start_panic(error.as_ref(), StackPopUnwind::Skip)?;
175+
return Ok(());
176+
} else {
177+
throw_unsup_format!("{}", error);
178+
}
179+
}
180+
181+
/// Start a panic in the interpreter with the given message as payload.
165182
fn start_panic(&mut self, msg: &str, unwind: StackPopUnwind) -> InterpResult<'tcx> {
166183
let this = self.eval_context_mut();
167184

src/shims/posix/linux/foreign_items.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
183183
futex(this, args, dest)?;
184184
}
185185
id => {
186-
let error_msg = format!(
187-
"unsupported Miri functionality: syscall ID {} is not emulated",
188-
id
189-
);
190-
if this.eval_context_ref().machine.panic_on_unsupported {
191-
// message is slightly different here to make automated analysis easier
192-
this.start_panic(error_msg.as_ref(), StackPopUnwind::Skip)?;
193-
return Ok(false);
194-
} else {
195-
throw_unsup_format!("{}", error_msg);
196-
}
186+
this.handle_unsupported(format!("syscall ID {} is not emulated", id))?;
187+
return Ok(false);
197188
}
198189
}
199190
}
@@ -227,17 +218,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
227218
}
228219

229220
_ => {
230-
let error_msg = format!(
231-
"unsupported Miri functionality: can't call foreign function {:?}",
221+
this.handle_unsupported(format!(
222+
"can't call foreign function {:?}",
232223
link_name
233-
);
234-
if this.eval_context_ref().machine.panic_on_unsupported {
235-
// message is slightly different here to make automated analysis easier
236-
this.start_panic(error_msg.as_ref(), StackPopUnwind::Skip)?;
237-
return Ok(false);
238-
} else {
239-
throw_unsup_format!("{}", error_msg);
240-
}
224+
))?;
225+
return Ok(false);
241226
}
242227
};
243228

src/shims/posix/macos/foreign_items.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
157157
}
158158

159159
_ => {
160-
let error_msg = format!(
161-
"unsupported Miri functionality: can't call foreign function {:?}",
160+
this.handle_unsupported(format!(
161+
"can't call foreign function {:?}",
162162
link_name
163-
);
164-
if this.eval_context_ref().machine.panic_on_unsupported {
165-
// message is slightly different here to make automated analysis easier
166-
this.start_panic(error_msg.as_ref(), StackPopUnwind::Skip)?;
167-
return Ok(false);
168-
} else {
169-
throw_unsup_format!("{}", error_msg);
170-
}
163+
))?;
164+
165+
return Ok(false);
171166
}
172167
};
173168

src/shims/windows/foreign_items.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
343343
"CreateThread" => {
344344
check_abi(abi, Abi::System { unwind: false })?;
345345

346-
let error_msg =
347-
"unsupported Miri functionality: concurrency is not supported on Windows";
348-
if this.eval_context_ref().machine.panic_on_unsupported {
349-
this.start_panic(error_msg, StackPopUnwind::Skip)?;
350-
return Ok(false);
351-
} else {
352-
throw_unsup_format!("{}", error_msg);
353-
}
346+
this.handle_unsupported("concurrency is not supported on Windows")?;
347+
return Ok(false);
354348
}
355349

356350
// Incomplete shims that we "stub out" just to get pre-main initialization code to work.
@@ -424,17 +418,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
424418
}
425419

426420
_ => {
427-
let error_msg = format!(
428-
"unsupported Miri functionality: can't call foreign function {:?}",
429-
link_name
430-
);
431-
if this.eval_context_ref().machine.panic_on_unsupported {
432-
// message is slightly different here to make automated analysis easier
433-
this.start_panic(error_msg.as_ref(), StackPopUnwind::Skip)?;
434-
return Ok(false);
435-
} else {
436-
throw_unsup_format!("{}", error_msg);
437-
}
421+
this.handle_unsupported(format!("can't call foreign function {:?}", link_name))?;
422+
return Ok(false);
438423
}
439424
}
440425

0 commit comments

Comments
 (0)