@@ -595,7 +595,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
595
595
let communicate = this. machine . communicate ( ) ;
596
596
597
597
let Some ( fd) = this. machine . fds . get ( fd_num) else {
598
- return interp_ok ( Scalar :: from_i64 ( this. fd_not_found ( ) ? ) ) ;
598
+ return this. set_last_error_and_return_i64 ( LibcError ( "EBADF" ) ) ;
599
599
} ;
600
600
let result = fd. seek ( communicate, seek_from) ?. map ( |offset| i64:: try_from ( offset) . unwrap ( ) ) ;
601
601
drop ( fd) ;
@@ -671,8 +671,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
671
671
672
672
// `stat` always follows symlinks.
673
673
let metadata = match FileMetadata :: from_path ( this, & path, true ) ? {
674
- Some ( metadata) => metadata,
675
- None => return interp_ok ( Scalar :: from_i32 ( - 1 ) ) , // `FileMetadata` has set errno
674
+ Ok ( metadata) => metadata,
675
+ Err ( err ) => return this . set_last_error_and_return_i32 ( err ) ,
676
676
} ;
677
677
678
678
interp_ok ( Scalar :: from_i32 ( this. macos_stat_write_buf ( metadata, buf_op) ?) )
@@ -700,8 +700,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
700
700
}
701
701
702
702
let metadata = match FileMetadata :: from_path ( this, & path, false ) ? {
703
- Some ( metadata) => metadata,
704
- None => return interp_ok ( Scalar :: from_i32 ( - 1 ) ) , // `FileMetadata` has set errno
703
+ Ok ( metadata) => metadata,
704
+ Err ( err ) => return this . set_last_error_and_return_i32 ( err ) ,
705
705
} ;
706
706
707
707
interp_ok ( Scalar :: from_i32 ( this. macos_stat_write_buf ( metadata, buf_op) ?) )
@@ -724,12 +724,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
724
724
if let IsolatedOp :: Reject ( reject_with) = this. machine . isolated_op {
725
725
this. reject_in_isolation ( "`fstat`" , reject_with) ?;
726
726
// Set error code as "EBADF" (bad fd)
727
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
727
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
728
728
}
729
729
730
730
let metadata = match FileMetadata :: from_fd_num ( this, fd) ? {
731
- Some ( metadata) => metadata,
732
- None => return interp_ok ( Scalar :: from_i32 ( - 1 ) ) ,
731
+ Ok ( metadata) => metadata,
732
+ Err ( err ) => return this . set_last_error_and_return_i32 ( err ) ,
733
733
} ;
734
734
interp_ok ( Scalar :: from_i32 ( this. macos_stat_write_buf ( metadata, buf_op) ?) )
735
735
}
@@ -816,8 +816,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
816
816
FileMetadata :: from_path ( this, & path, follow_symlink) ?
817
817
} ;
818
818
let metadata = match metadata {
819
- Some ( metadata) => metadata,
820
- None => return interp_ok ( Scalar :: from_i32 ( - 1 ) ) ,
819
+ Ok ( metadata) => metadata,
820
+ Err ( err ) => return this . set_last_error_and_return_i32 ( err ) ,
821
821
} ;
822
822
823
823
// The `mode` field specifies the type of the file and the permissions over the file for
@@ -1131,7 +1131,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1131
1131
if let IsolatedOp :: Reject ( reject_with) = this. machine . isolated_op {
1132
1132
this. reject_in_isolation ( "`readdir_r`" , reject_with) ?;
1133
1133
// Set error code as "EBADF" (bad fd)
1134
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1134
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1135
1135
}
1136
1136
1137
1137
let open_dir = this. machine . dirs . streams . get_mut ( & dirp) . ok_or_else ( || {
@@ -1242,20 +1242,20 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1242
1242
let dirp = this. read_target_usize ( dirp_op) ?;
1243
1243
1244
1244
// Reject if isolation is enabled.
1245
- interp_ok ( Scalar :: from_i32 (
1246
- if let IsolatedOp :: Reject ( reject_with ) = this. machine . isolated_op {
1247
- this. reject_in_isolation ( "`closedir`" , reject_with ) ? ;
1248
- this . fd_not_found ( ) ?
1249
- } else if let Some ( open_dir ) = this . machine . dirs . streams . remove ( & dirp ) {
1250
- if let Some ( entry ) = open_dir . entry {
1251
- this. deallocate_ptr ( entry , None , MiriMemoryKind :: Runtime . into ( ) ) ? ;
1252
- }
1253
- drop ( open_dir) ;
1254
- 0
1255
- } else {
1256
- this . fd_not_found ( ) ?
1257
- } ,
1258
- ) )
1245
+ if let IsolatedOp :: Reject ( reject_with ) = this . machine . isolated_op {
1246
+ this. reject_in_isolation ( "`closedir`" , reject_with ) ? ;
1247
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1248
+ }
1249
+
1250
+ let Some ( open_dir ) = this . machine . dirs . streams . remove ( & dirp ) else {
1251
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1252
+ } ;
1253
+ if let Some ( entry ) = open_dir. entry {
1254
+ this . deallocate_ptr ( entry , None , MiriMemoryKind :: Runtime . into ( ) ) ? ;
1255
+ }
1256
+ drop ( open_dir ) ;
1257
+
1258
+ interp_ok ( Scalar :: from_i32 ( 0 ) )
1259
1259
}
1260
1260
1261
1261
fn ftruncate64 ( & mut self , fd_num : i32 , length : i128 ) -> InterpResult < ' tcx , Scalar > {
@@ -1265,11 +1265,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1265
1265
if let IsolatedOp :: Reject ( reject_with) = this. machine . isolated_op {
1266
1266
this. reject_in_isolation ( "`ftruncate64`" , reject_with) ?;
1267
1267
// Set error code as "EBADF" (bad fd)
1268
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1268
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1269
1269
}
1270
1270
1271
1271
let Some ( fd) = this. machine . fds . get ( fd_num) else {
1272
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1272
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1273
1273
} ;
1274
1274
1275
1275
// FIXME: Support ftruncate64 for all FDs
@@ -1308,7 +1308,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1308
1308
if let IsolatedOp :: Reject ( reject_with) = this. machine . isolated_op {
1309
1309
this. reject_in_isolation ( "`fsync`" , reject_with) ?;
1310
1310
// Set error code as "EBADF" (bad fd)
1311
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1311
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1312
1312
}
1313
1313
1314
1314
self . ffullsync_fd ( fd)
@@ -1317,7 +1317,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1317
1317
fn ffullsync_fd ( & mut self , fd_num : i32 ) -> InterpResult < ' tcx , Scalar > {
1318
1318
let this = self . eval_context_mut ( ) ;
1319
1319
let Some ( fd) = this. machine . fds . get ( fd_num) else {
1320
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1320
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1321
1321
} ;
1322
1322
// Only regular files support synchronization.
1323
1323
let FileHandle { file, writable } = fd. downcast :: < FileHandle > ( ) . ok_or_else ( || {
@@ -1337,11 +1337,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1337
1337
if let IsolatedOp :: Reject ( reject_with) = this. machine . isolated_op {
1338
1338
this. reject_in_isolation ( "`fdatasync`" , reject_with) ?;
1339
1339
// Set error code as "EBADF" (bad fd)
1340
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1340
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1341
1341
}
1342
1342
1343
1343
let Some ( fd) = this. machine . fds . get ( fd) else {
1344
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1344
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1345
1345
} ;
1346
1346
// Only regular files support synchronization.
1347
1347
let FileHandle { file, writable } = fd. downcast :: < FileHandle > ( ) . ok_or_else ( || {
@@ -1380,11 +1380,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1380
1380
if let IsolatedOp :: Reject ( reject_with) = this. machine . isolated_op {
1381
1381
this. reject_in_isolation ( "`sync_file_range`" , reject_with) ?;
1382
1382
// Set error code as "EBADF" (bad fd)
1383
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1383
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1384
1384
}
1385
1385
1386
1386
let Some ( fd) = this. machine . fds . get ( fd) else {
1387
- return interp_ok ( Scalar :: from_i32 ( this. fd_not_found ( ) ? ) ) ;
1387
+ return this. set_last_error_and_return_i32 ( LibcError ( "EBADF" ) ) ;
1388
1388
} ;
1389
1389
// Only regular files support synchronization.
1390
1390
let FileHandle { file, writable } = fd. downcast :: < FileHandle > ( ) . ok_or_else ( || {
@@ -1667,7 +1667,7 @@ impl FileMetadata {
1667
1667
ecx : & mut MiriInterpCx < ' tcx > ,
1668
1668
path : & Path ,
1669
1669
follow_symlink : bool ,
1670
- ) -> InterpResult < ' tcx , Option < FileMetadata > > {
1670
+ ) -> InterpResult < ' tcx , Result < FileMetadata , IoError > > {
1671
1671
let metadata =
1672
1672
if follow_symlink { std:: fs:: metadata ( path) } else { std:: fs:: symlink_metadata ( path) } ;
1673
1673
@@ -1677,9 +1677,9 @@ impl FileMetadata {
1677
1677
fn from_fd_num < ' tcx > (
1678
1678
ecx : & mut MiriInterpCx < ' tcx > ,
1679
1679
fd_num : i32 ,
1680
- ) -> InterpResult < ' tcx , Option < FileMetadata > > {
1680
+ ) -> InterpResult < ' tcx , Result < FileMetadata , IoError > > {
1681
1681
let Some ( fd) = ecx. machine . fds . get ( fd_num) else {
1682
- return ecx . fd_not_found ( ) . map ( |_ : i32 | None ) ;
1682
+ return interp_ok ( Err ( LibcError ( "EBADF" ) ) ) ;
1683
1683
} ;
1684
1684
1685
1685
let file = & fd
@@ -1699,12 +1699,11 @@ impl FileMetadata {
1699
1699
fn from_meta < ' tcx > (
1700
1700
ecx : & mut MiriInterpCx < ' tcx > ,
1701
1701
metadata : Result < std:: fs:: Metadata , std:: io:: Error > ,
1702
- ) -> InterpResult < ' tcx , Option < FileMetadata > > {
1702
+ ) -> InterpResult < ' tcx , Result < FileMetadata , IoError > > {
1703
1703
let metadata = match metadata {
1704
1704
Ok ( metadata) => metadata,
1705
1705
Err ( e) => {
1706
- ecx. set_last_error ( e) ?;
1707
- return interp_ok ( None ) ;
1706
+ return interp_ok ( Err ( e. into ( ) ) ) ;
1708
1707
}
1709
1708
} ;
1710
1709
@@ -1727,6 +1726,6 @@ impl FileMetadata {
1727
1726
let modified = extract_sec_and_nsec ( metadata. modified ( ) ) ?;
1728
1727
1729
1728
// FIXME: Provide more fields using platform specific methods.
1730
- interp_ok ( Some ( FileMetadata { mode, size, created, accessed, modified } ) )
1729
+ interp_ok ( Ok ( FileMetadata { mode, size, created, accessed, modified } ) )
1731
1730
}
1732
1731
}
0 commit comments