Skip to content

Commit 4232939

Browse files
committed
Move last error functions to helpers
1 parent cfd9599 commit 4232939

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
@@ -977,34 +977,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
977977
}
978978
return Ok(None);
979979
}
980-
981-
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
982-
let this = self.eval_context_mut();
983-
let errno_ptr = this.machine.last_error.unwrap();
984-
// We allocated this during machine initialziation so the bounds are fine.
985-
this.memory.get_mut(errno_ptr.alloc_id)?.write_scalar(
986-
&*this.tcx,
987-
errno_ptr,
988-
scalar.into(),
989-
Size::from_bits(32),
990-
)
991-
}
992-
993-
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
994-
let this = self.eval_context_mut();
995-
let errno_ptr = this.machine.last_error.unwrap();
996-
this.memory
997-
.get(errno_ptr.alloc_id)?
998-
.read_scalar(&*this.tcx, errno_ptr, Size::from_bits(32))?
999-
.not_undef()
1000-
}
1001-
1002-
fn consume_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
1003-
self.eval_context_mut().set_last_error(Scalar::from_int(
1004-
e.raw_os_error().unwrap(),
1005-
Size::from_bits(32),
1006-
))
1007-
}
1008980
}
1009981

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

src/shims/fs.rs

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

0 commit comments

Comments
 (0)