@@ -5,17 +5,20 @@ use std::io::Read;
5
5
use crate :: stacked_borrows:: Tag ;
6
6
use crate :: * ;
7
7
8
+ struct FileHandle {
9
+ file : File ,
10
+ flag : i32 ,
11
+ }
12
+
8
13
pub struct FileHandler {
9
- files : HashMap < i32 , File > ,
10
- flags : HashMap < i32 , i32 > ,
14
+ handles : HashMap < i32 , FileHandle > ,
11
15
low : i32 ,
12
16
}
13
17
14
18
impl Default for FileHandler {
15
19
fn default ( ) -> Self {
16
20
FileHandler {
17
- files : Default :: default ( ) ,
18
- flags : Default :: default ( ) ,
21
+ handles : Default :: default ( ) ,
19
22
// 0, 1 and 2 are reserved for stdin, stdout and stderr
20
23
low : 3 ,
21
24
}
@@ -51,8 +54,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
51
54
Ok ( file) => {
52
55
let mut fh = & mut this. machine . file_handler ;
53
56
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} ) ;
56
58
Ok ( fh. low )
57
59
}
58
60
@@ -84,7 +86,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
84
86
let flag = this. read_scalar ( arg_op. unwrap ( ) ) ?. to_i32 ( ) ?;
85
87
// The only usage of this in stdlib at the moment is to enable the `FD_CLOEXEC` flag.
86
88
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) {
88
90
if flag ^ * old_flag == fd_cloexec {
89
91
* old_flag = flag;
90
92
} else {
@@ -93,8 +95,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
93
95
}
94
96
Ok ( 0 )
95
97
} 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 )
98
100
} else {
99
101
this. machine . last_error = this. eval_libc_i32 ( "EBADF" ) ? as u32 ;
100
102
Ok ( -1 )
@@ -113,8 +115,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
113
115
114
116
let fd = this. read_scalar ( fd_op) ?. to_i32 ( ) ?;
115
117
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 ( ) {
118
120
Ok ( ( ) ) => Ok ( 0 ) ,
119
121
Err ( e) => {
120
122
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
145
147
let buf = this. force_ptr ( this. read_scalar ( buf_op) ?. not_undef ( ) ?) ?;
146
148
let count = this. read_scalar ( count_op) ?. to_usize ( & * this. tcx ) ?;
147
149
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) {
149
151
let mut bytes = vec ! [ 0 ; count as usize ] ;
150
152
match file. read ( & mut bytes) {
151
153
Ok ( read_bytes) => {
0 commit comments