@@ -29,7 +29,10 @@ pub struct MiriConfig {
29
29
pub seed : Option < u64 > ,
30
30
}
31
31
32
- // Used by priroda.
32
+ /// Returns a freshly created `InterpCx`, along with an `MPlaceTy` representing
33
+ /// the location where the return value of the `start` lang item will be
34
+ /// written to.
35
+ /// Used by `priroda` and `miri
33
36
pub fn create_ecx < ' mir , ' tcx : ' mir > (
34
37
tcx : TyCtxt < ' tcx > ,
35
38
main_id : DefId ,
@@ -173,7 +176,10 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
173
176
Ok ( ( ecx, ret_ptr) )
174
177
}
175
178
176
- pub fn eval_main < ' tcx > ( tcx : TyCtxt < ' tcx > , main_id : DefId , config : MiriConfig ) {
179
+ /// Evaluates the main function specified by `main_id`.
180
+ /// Returns `Some(return_code)` if program executed completed.
181
+ /// Returns `None` if an evaluation error occured
182
+ pub fn eval_main < ' tcx > ( tcx : TyCtxt < ' tcx > , main_id : DefId , config : MiriConfig ) -> Option < i64 > {
177
183
let ( mut ecx, ret_ptr) = match create_ecx ( tcx, main_id, config) {
178
184
Ok ( v) => v,
179
185
Err ( mut err) => {
@@ -202,13 +208,16 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) {
202
208
let ignore_leaks = target_os == "windows" || target_os == "macos" ;
203
209
if !ignore_leaks && leaks != 0 {
204
210
tcx. sess . err ( "the evaluated program leaked memory" ) ;
211
+ // Ignore the provided return code - let the reported error
212
+ // determine the return code
213
+ return None ;
205
214
}
206
- std :: process :: exit ( return_code as i32 ) ;
215
+ return Some ( return_code)
207
216
}
208
217
Err ( mut e) => {
209
218
// Special treatment for some error kinds
210
219
let msg = match e. kind {
211
- InterpError :: Exit ( code) => std :: process :: exit ( code) ,
220
+ InterpError :: Exit ( code) => return Some ( code. into ( ) ) ,
212
221
err_unsup ! ( NoMirFor ( ..) ) =>
213
222
format ! ( "{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`." , e) ,
214
223
_ => e. to_string ( )
@@ -251,6 +260,8 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) {
251
260
trace ! ( " local {}: {:?}" , i, local. value) ;
252
261
}
253
262
}
263
+ // Let the reported error determine the return code
264
+ return None ;
254
265
}
255
266
}
256
267
}
0 commit comments