Skip to content

Commit 49fc9c8

Browse files
committed
Move last error functions to helpers
1 parent f76f8ce commit 49fc9c8

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
@@ -304,4 +304,39 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
304304
fn eval_libc_i32(&mut self, name: &str) -> InterpResult<'tcx, i32> {
305305
self.eval_libc(name)?.to_i32()
306306
}
307+
308+
/// Sets the last error variable
309+
fn set_last_error(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx> {
310+
let this = self.eval_context_mut();
311+
let tcx = &{ this.tcx.tcx };
312+
let errno_ptr = this.machine.last_error.unwrap();
313+
this.memory_mut().get_mut(errno_ptr.alloc_id)?.write_scalar(
314+
tcx,
315+
errno_ptr,
316+
scalar.into(),
317+
Size::from_bits(32),
318+
)
319+
}
320+
321+
/// Gets the last error variable
322+
fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar<Tag>> {
323+
let this = self.eval_context_mut();
324+
let tcx = &{ this.tcx.tcx };
325+
let errno_ptr = this.machine.last_error.unwrap();
326+
this.memory()
327+
.get(errno_ptr.alloc_id)?
328+
.read_scalar(tcx, errno_ptr, Size::from_bits(32))?
329+
.not_undef()
330+
}
331+
332+
/// Sets the last error variable using a `std::io::Error`. It fails if the error cannot be
333+
/// transformed to a raw os error succesfully
334+
fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
335+
self.eval_context_mut().set_last_error(Scalar::from_int(
336+
e.raw_os_error().ok_or_else(|| {
337+
err_unsup_format!("The {} error cannot be transformed into a raw os error", e)
338+
})?,
339+
Size::from_bits(32),
340+
))
341+
}
307342
}

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
@@ -980,35 +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 tcx = &{ this.tcx.tcx };
987-
let errno_ptr = this.machine.last_error.unwrap();
988-
this.memory_mut().get_mut(errno_ptr.alloc_id)?.write_scalar(
989-
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 tcx = &{ this.tcx.tcx };
999-
let errno_ptr = this.machine.last_error.unwrap();
1000-
this.memory()
1001-
.get(errno_ptr.alloc_id)?
1002-
.read_scalar(tcx, errno_ptr, Size::from_bits(32))?
1003-
.not_undef()
1004-
}
1005-
1006-
fn consume_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> {
1007-
self.eval_context_mut().set_last_error(Scalar::from_int(
1008-
e.raw_os_error().unwrap(),
1009-
Size::from_bits(32),
1010-
))
1011-
}
1012983
}
1013984

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

src/shims/fs.rs

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

0 commit comments

Comments
 (0)