Skip to content

Commit 181e973

Browse files
committed
Move last error functions to helpers
1 parent fccb239 commit 181e973

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

src/helpers.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,39 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
345345
}
346346
Ok(())
347347
}
348+
349+
/// Sets the last error variable
350+
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
351+
let this = self.eval_context_mut();
352+
let tcx = &{ this.tcx.tcx };
353+
let errno_ptr = this.machine.last_error.unwrap();
354+
this.memory.get_mut(errno_ptr.alloc_id)?.write_scalar(
355+
tcx,
356+
errno_ptr,
357+
scalar.into(),
358+
Size::from_bits(32),
359+
)
360+
}
361+
362+
/// Gets the last error variable
363+
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
364+
let this = self.eval_context_mut();
365+
let tcx = &{ this.tcx.tcx };
366+
let errno_ptr = this.machine.last_error.unwrap();
367+
this.memory
368+
.get(errno_ptr.alloc_id)?
369+
.read_scalar(tcx, errno_ptr, Size::from_bits(32))?
370+
.not_undef()
371+
}
372+
373+
/// Sets the last error variable using a `std::io::Error`. It fails if the error cannot be
374+
/// transformed to a raw os error succesfully
375+
fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
376+
self.eval_context_mut().set_last_error(Scalar::from_int(
377+
e.raw_os_error().ok_or_else(|| {
378+
err_unsup_format!("The {} error cannot be transformed into a raw os error", e)
379+
})?,
380+
Size::from_bits(32),
381+
))
382+
}
348383
}

src/shims/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
146146
let erange = this.eval_libc("ERANGE")?;
147147
this.set_last_error(erange)?;
148148
}
149-
Err(e) => this.consume_io_error(e)?,
149+
Err(e) => this.set_last_error_from_io_error(e)?,
150150
}
151151
Ok(Scalar::ptr_null(&*this.tcx))
152152
}
@@ -168,7 +168,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
168168
match env::set_current_dir(path) {
169169
Ok(()) => Ok(0),
170170
Err(e) => {
171-
this.consume_io_error(e)?;
171+
this.set_last_error_from_io_error(e)?;
172172
Ok(-1)
173173
}
174174
}

src/shims/foreign_items.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -980,34 +980,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
980980
}
981981
return Ok(None);
982982
}
983-
984-
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
985-
let this = self.eval_context_mut();
986-
let errno_ptr = this.machine.last_error.unwrap();
987-
// We allocated this during machine initialziation so the bounds are fine.
988-
this.memory.get_mut(errno_ptr.alloc_id)?.write_scalar(
989-
&*this.tcx,
990-
errno_ptr,
991-
scalar.into(),
992-
Size::from_bits(32),
993-
)
994-
}
995-
996-
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
997-
let this = self.eval_context_mut();
998-
let errno_ptr = this.machine.last_error.unwrap();
999-
this.memory
1000-
.get(errno_ptr.alloc_id)?
1001-
.read_scalar(&*this.tcx, errno_ptr, Size::from_bits(32))?
1002-
.not_undef()
1003-
}
1004-
1005-
fn consume_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
1006-
self.eval_context_mut().set_last_error(Scalar::from_int(
1007-
e.raw_os_error().unwrap(),
1008-
Size::from_bits(32),
1009-
))
1010-
}
1011983
}
1012984

1013985
// 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)