Skip to content

Commit a3e14c6

Browse files
committed
Move last error functions to helpers
1 parent 0d01c30 commit a3e14c6

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

src/helpers.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,39 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
339339

340340
Ok(())
341341
}
342+
343+
/// Sets the last error variable
344+
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
345+
let this = self.eval_context_mut();
346+
let tcx = &{ this.tcx.tcx };
347+
let errno_ptr = this.machine.last_error.unwrap();
348+
this.memory_mut().get_mut(errno_ptr.alloc_id)?.write_scalar(
349+
tcx,
350+
errno_ptr,
351+
scalar.into(),
352+
Size::from_bits(32),
353+
)
354+
}
355+
356+
/// Gets the last error variable
357+
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
358+
let this = self.eval_context_mut();
359+
let tcx = &{ this.tcx.tcx };
360+
let errno_ptr = this.machine.last_error.unwrap();
361+
this.memory()
362+
.get(errno_ptr.alloc_id)?
363+
.read_scalar(tcx, errno_ptr, Size::from_bits(32))?
364+
.not_undef()
365+
}
366+
367+
/// Sets the last error variable using a `std::io::Error`. It fails if the error cannot be
368+
/// transformed to a raw os error succesfully
369+
fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
370+
self.eval_context_mut().set_last_error(Scalar::from_int(
371+
e.raw_os_error().ok_or_else(|| {
372+
err_unsup_format!("The {} error cannot be transformed into a raw os error", e)
373+
})?,
374+
Size::from_bits(32),
375+
))
376+
}
342377
}

src/shims/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
150150
let erange = this.eval_libc("ERANGE")?;
151151
this.set_last_error(erange)?;
152152
}
153-
Err(e) => this.consume_io_error(e)?,
153+
Err(e) => this.set_last_error_from_io_error(e)?,
154154
}
155155
Ok(Scalar::ptr_null(&*tcx))
156156
}
@@ -174,7 +174,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
174174
match env::set_current_dir(path) {
175175
Ok(()) => Ok(0),
176176
Err(e) => {
177-
this.consume_io_error(e)?;
177+
this.set_last_error_from_io_error(e)?;
178178
Ok(-1)
179179
}
180180
}

src/shims/foreign_items.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -990,35 +990,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
990990
}
991991
return Ok(None);
992992
}
993-
994-
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
995-
let this = self.eval_context_mut();
996-
let tcx = &{ this.tcx.tcx };
997-
let errno_ptr = this.machine.last_error.unwrap();
998-
this.memory_mut().get_mut(errno_ptr.alloc_id)?.write_scalar(
999-
tcx,
1000-
errno_ptr,
1001-
scalar.into(),
1002-
Size::from_bits(32),
1003-
)
1004-
}
1005-
1006-
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
1007-
let this = self.eval_context_mut();
1008-
let tcx = &{ this.tcx.tcx };
1009-
let errno_ptr = this.machine.last_error.unwrap();
1010-
this.memory()
1011-
.get(errno_ptr.alloc_id)?
1012-
.read_scalar(tcx, errno_ptr, Size::from_bits(32))?
1013-
.not_undef()
1014-
}
1015-
1016-
fn consume_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
1017-
self.eval_context_mut().set_last_error(Scalar::from_int(
1018-
e.raw_os_error().unwrap(),
1019-
Size::from_bits(32),
1020-
))
1021-
}
1022993
}
1023994

1024995
// Shims the linux 'getrandom()' syscall.

src/shims/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
284284
match result {
285285
Ok(ok) => Ok(ok),
286286
Err(e) => {
287-
self.eval_context_mut().consume_io_error(e)?;
287+
self.eval_context_mut().set_last_error_from_io_error(e)?;
288288
Ok((-1).into())
289289
}
290290
}

0 commit comments

Comments
 (0)