Skip to content

Commit 03ed412

Browse files
committed
Add FileHandle struct
1 parent b540e5d commit 03ed412

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/shims/io.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ use std::io::Read;
55
use crate::stacked_borrows::Tag;
66
use crate::*;
77

8+
struct FileHandle {
9+
file: File,
10+
flag: i32,
11+
}
12+
813
pub struct FileHandler {
9-
files: HashMap<i32, File>,
10-
flags: HashMap<i32, i32>,
14+
handles: HashMap<i32, FileHandle>,
1115
low: i32,
1216
}
1317

1418
impl Default for FileHandler {
1519
fn default() -> Self {
1620
FileHandler {
17-
files: Default::default(),
18-
flags: Default::default(),
21+
handles: Default::default(),
1922
// 0, 1 and 2 are reserved for stdin, stdout and stderr
2023
low: 3,
2124
}
@@ -51,8 +54,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5154
Ok(file) => {
5255
let mut fh = &mut this.machine.file_handler;
5356
fh.low += 1;
54-
fh.files.insert(fh.low, file);
55-
fh.flags.insert(fh.low, flag);
57+
fh.handles.insert(fh.low, FileHandle{ file, flag});
5658
Ok(fh.low)
5759
}
5860

@@ -84,7 +86,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8486
let flag = this.read_scalar(arg_op.unwrap())?.to_i32()?;
8587
// The only usage of this in stdlib at the moment is to enable the `FD_CLOEXEC` flag.
8688
let fd_cloexec = this.eval_libc_i32("FD_CLOEXEC")?;
87-
if let Some(old_flag) = this.machine.file_handler.flags.get_mut(&fd) {
89+
if let Some(FileHandle{ flag: old_flag, .. }) = this.machine.file_handler.handles.get_mut(&fd) {
8890
if flag ^ *old_flag == fd_cloexec {
8991
*old_flag = flag;
9092
} else {
@@ -93,8 +95,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9395
}
9496
Ok(0)
9597
} else if cmd == this.eval_libc_i32("F_GETFD")? {
96-
if let Some(flag) = this.machine.file_handler.flags.get(&fd) {
97-
Ok(*flag)
98+
if let Some(handle) = this.machine.file_handler.handles.get(&fd) {
99+
Ok(handle.flag)
98100
} else {
99101
this.machine.last_error = this.eval_libc_i32("EBADF")? as u32;
100102
Ok(-1)
@@ -113,8 +115,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
113115

114116
let fd = this.read_scalar(fd_op)?.to_i32()?;
115117

116-
if let Some(file) = this.machine.file_handler.files.remove(&fd) {
117-
match file.sync_all() {
118+
if let Some(handle) = this.machine.file_handler.handles.remove(&fd) {
119+
match handle.file.sync_all() {
118120
Ok(()) => Ok(0),
119121
Err(e) => {
120122
this.machine.last_error = e.raw_os_error().unwrap() as u32;
@@ -145,7 +147,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
145147
let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
146148
let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?;
147149

148-
if let Some(file) = this.machine.file_handler.files.get_mut(&fd) {
150+
if let Some(FileHandle { file, ..}) = this.machine.file_handler.handles.get_mut(&fd) {
149151
let mut bytes = vec![0; count as usize];
150152
match file.read(&mut bytes) {
151153
Ok(read_bytes) => {

0 commit comments

Comments
 (0)