Skip to content

Commit 258a306

Browse files
committed
Move last error functions to helpers
1 parent fbc1d91 commit 258a306

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

src/helpers.rs

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

src/shims/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
148148
let erange = this.eval_libc("ERANGE")?;
149149
this.set_last_error(erange)?;
150150
}
151-
Err(e) => this.consume_io_error(e)?,
151+
Err(e) => this.set_last_error_from_io_error(e)?,
152152
}
153153
Ok(Scalar::ptr_null(&*tcx))
154154
}
@@ -170,7 +170,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
170170
match env::set_current_dir(path) {
171171
Ok(()) => Ok(0),
172172
Err(e) => {
173-
this.consume_io_error(e)?;
173+
this.set_last_error_from_io_error(e)?;
174174
Ok(-1)
175175
}
176176
}

src/shims/foreign_items.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -991,34 +991,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
991991
return Ok(None);
992992
}
993993

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.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-
1016994
fn consume_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
1017995
self.eval_context_mut().set_last_error(Scalar::from_int(
1018996
e.raw_os_error().unwrap(),
1019997
Size::from_bits(32),
1020998
))
1021999
}
1000+
10221001
}
10231002

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

src/shims/fs.rs

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

0 commit comments

Comments
 (0)