Skip to content

Commit 9fcc8a2

Browse files
committed
Review comments
1 parent dd00e58 commit 9fcc8a2

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

src/shims/foreign_items/posix/linux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
3737
// The `macos` module has a parallel foreign item, `readdir_r`, which uses a different
3838
// struct layout.
3939
"readdir64_r" => {
40-
let result = this.readdir64_r(args[0], args[1], args[2])?;
40+
let result = this.linux_readdir64_r(args[0], args[1], args[2])?;
4141
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
4242
}
4343

src/shims/foreign_items/posix/macos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5252
// The `linux` module has a parallel foreign item, `readdir64_r`, which uses a
5353
// different struct layout.
5454
"readdir_r$INODE64" => {
55-
let result = this.readdir_r(args[0], args[1], args[2])?;
55+
let result = this.macos_readdir_r(args[0], args[1], args[2])?;
5656
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
5757
}
5858

src/shims/fs.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
809809

810810
this.check_no_isolation("mkdir")?;
811811

812-
let _mode = if this.tcx.sess.target.target.target_os.to_lowercase() == "macos" {
812+
let _mode = if this.tcx.sess.target.target.target_os.as_str() == "macos" {
813813
this.read_scalar(mode_op)?.not_undef()?.to_u16()? as u32
814814
} else {
815815
this.read_scalar(mode_op)?.to_u32()?
@@ -863,16 +863,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
863863
// The libc API for opendir says that this method returns a pointer to an opaque
864864
// structure, but we are returning an ID number. Thus, pass it as a scalar of
865865
// pointer width.
866-
Ok(Scalar::from_int(id, this.pointer_size()))
866+
Ok(Scalar::from_machine_usize(id, this))
867867
}
868868
Err(e) => {
869869
this.set_last_error_from_io_error(e)?;
870-
Ok(Scalar::from_int(0, this.memory.pointer_size()))
870+
Ok(Scalar::from_machine_usize(0, this))
871871
}
872872
}
873873
}
874874

875-
fn readdir64_r(
875+
fn linux_readdir64_r(
876876
&mut self,
877877
dirp_op: OpTy<'tcx, Tag>,
878878
entry_op: OpTy<'tcx, Tag>,
@@ -884,9 +884,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
884884

885885
let dirp = this.read_scalar(dirp_op)?.to_machine_usize(this)?;
886886

887-
let entry_ptr = this.force_ptr(this.read_scalar(entry_op)?.not_undef()?)?;
888-
let dirent64_layout = this.libc_ty_layout("dirent64")?;
889-
890887
let dir_iter = this.machine.dir_handler.streams.get_mut(&dirp).ok_or_else(|| {
891888
err_unsup_format!("The DIR pointer passed to readdir64_r did not come from opendir")
892889
})?;
@@ -896,13 +893,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
896893
// The name is written with write_os_str_to_c_str, while the rest of the
897894
// dirent64 struct is written using write_packed_immediates.
898895

896+
// For reference:
897+
// pub struct dirent64 {
898+
// pub d_ino: ino64_t,
899+
// pub d_off: off64_t,
900+
// pub d_reclen: c_ushort,
901+
// pub d_type: c_uchar,
902+
// pub d_name: [c_char; 256],
903+
// }
904+
905+
let entry_ptr = this.force_ptr(this.read_scalar(entry_op)?.not_undef()?)?;
906+
let dirent64_layout = this.libc_ty_layout("dirent64")?;
899907
let name_offset = dirent64_layout.details.fields.offset(4);
900908
let name_ptr = entry_ptr.offset(name_offset, this)?;
901909

902910
let file_name = dir_entry.file_name();
903911
let name_fits = this.write_os_str_to_c_str(&file_name, Scalar::Ptr(name_ptr), 256)?;
904912
if !name_fits {
905-
panic!("A directory entry had a name too large to fit in libc::dirent64");
913+
throw_unsup_format!("A directory entry had a name too large to fit in libc::dirent64");
906914
}
907915

908916
let entry_place = this.deref_operand(entry_op)?;
@@ -948,7 +956,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
948956
}
949957
}
950958

951-
fn readdir_r(
959+
fn macos_readdir_r(
952960
&mut self,
953961
dirp_op: OpTy<'tcx, Tag>,
954962
entry_op: OpTy<'tcx, Tag>,
@@ -960,8 +968,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
960968

961969
let dirp = this.read_scalar(dirp_op)?.to_machine_usize(this)?;
962970

963-
let entry_ptr = this.force_ptr(this.read_scalar(entry_op)?.not_undef()?)?;
964-
let dirent_layout = this.libc_ty_layout("dirent")?;
965971

966972
let dir_iter = this.machine.dir_handler.streams.get_mut(&dirp).ok_or_else(|| {
967973
err_unsup_format!("The DIR pointer passed to readdir_r did not come from opendir")
@@ -972,13 +978,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
972978
// The name is written with write_os_str_to_c_str, while the rest of the
973979
// dirent struct is written using write_packed_Immediates.
974980

981+
// For reference:
982+
// pub struct dirent {
983+
// pub d_ino: u64,
984+
// pub d_seekoff: u64,
985+
// pub d_reclen: u16,
986+
// pub d_namlen: u16,
987+
// pub d_type: u8,
988+
// pub d_name: [c_char; 1024],
989+
// }
990+
991+
let entry_ptr = this.force_ptr(this.read_scalar(entry_op)?.not_undef()?)?;
992+
let dirent_layout = this.libc_ty_layout("dirent")?;
975993
let name_offset = dirent_layout.details.fields.offset(5);
976994
let name_ptr = entry_ptr.offset(name_offset, this)?;
977995

978996
let file_name = dir_entry.file_name();
979997
let name_fits = this.write_os_str_to_c_str(&file_name, Scalar::Ptr(name_ptr), 1024)?;
980998
if !name_fits {
981-
panic!("A directory entry had a name too large to fit in libc::dirent");
999+
throw_unsup_format!("A directory entry had a name too large to fit in libc::dirent");
9821000
}
9831001

9841002
let entry_place = this.deref_operand(entry_op)?;

tests/run-pass/fs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ fn test_directory() {
199199
create_dir(&dir_path).unwrap();
200200
// Test that the metadata of a directory is correct.
201201
assert!(dir_path.metadata().unwrap().is_dir());
202+
// Creating a directory when it already exists should fail.
203+
assert_eq!(ErrorKind::AlreadyExists, create_dir(&dir_path).unwrap_err().kind());
202204

203205
// Create some files inside the directory
204206
let path_1 = dir_path.join("test_file_1");

0 commit comments

Comments
 (0)