-
Notifications
You must be signed in to change notification settings - Fork 390
Add shims for file handling #962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
79b1f91
First version of file handling
pvdrz 3726081
Add helper function to fetch `libc` constants
pvdrz 01f6461
Check that the only flag change is done to enable `FD_CLOEXEC`
pvdrz bdaa90c
Add FIXME to file reading test
pvdrz ca3a917
Enable close call for macos
pvdrz b540e5d
Reserve fides for stdio and fix merge issues
pvdrz 03ed412
Add FileHandle struct
pvdrz 775246e
Add method to consume std::io::Result
pvdrz efbe798
Avoid buffer allocation to read files
pvdrz 644467c
Add methods to handle invalid fides
pvdrz 50be5a8
Remove return argument when fd is not found
pvdrz d0509d7
Add docs for helper functions
pvdrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
use std::collections::HashMap; | ||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
use rustc::ty::layout::Size; | ||
|
||
use crate::stacked_borrows::Tag; | ||
use crate::*; | ||
|
||
pub struct FileHandle { | ||
file: File, | ||
flag: i32, | ||
} | ||
|
||
pub struct FileHandler { | ||
handles: HashMap<i32, FileHandle>, | ||
low: i32, | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
impl Default for FileHandler { | ||
fn default() -> Self { | ||
FileHandler { | ||
handles: Default::default(), | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 0, 1 and 2 are reserved for stdin, stdout and stderr | ||
low: 3, | ||
} | ||
} | ||
} | ||
|
||
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {} | ||
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> { | ||
fn open( | ||
&mut self, | ||
path_op: OpTy<'tcx, Tag>, | ||
flag_op: OpTy<'tcx, Tag>, | ||
) -> InterpResult<'tcx, i32> { | ||
let this = self.eval_context_mut(); | ||
|
||
if !this.machine.communicate { | ||
throw_unsup_format!("`open` not available when isolation is enabled") | ||
} | ||
|
||
let flag = this.read_scalar(flag_op)?.to_i32()?; | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if flag != this.eval_libc_i32("O_RDONLY")? && flag != this.eval_libc_i32("O_CLOEXEC")? { | ||
throw_unsup_format!("Unsupported flag {:#x}", flag); | ||
} | ||
|
||
let path_bytes = this | ||
.memory() | ||
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?; | ||
let path = std::str::from_utf8(path_bytes) | ||
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?; | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let fd = File::open(path).map(|file| { | ||
let mut fh = &mut this.machine.file_handler; | ||
fh.low += 1; | ||
fh.handles.insert(fh.low, FileHandle { file, flag }); | ||
fh.low | ||
}); | ||
|
||
this.consume_result(fd) | ||
} | ||
|
||
fn fcntl( | ||
&mut self, | ||
fd_op: OpTy<'tcx, Tag>, | ||
cmd_op: OpTy<'tcx, Tag>, | ||
arg_op: Option<OpTy<'tcx, Tag>>, | ||
) -> InterpResult<'tcx, i32> { | ||
let this = self.eval_context_mut(); | ||
|
||
if !this.machine.communicate { | ||
throw_unsup_format!("`open` not available when isolation is enabled") | ||
} | ||
|
||
let fd = this.read_scalar(fd_op)?.to_i32()?; | ||
let cmd = this.read_scalar(cmd_op)?.to_i32()?; | ||
|
||
if cmd == this.eval_libc_i32("F_SETFD")? { | ||
// This does not affect the file itself. Certain flags might require changing the file | ||
// or the way it is accessed somehow. | ||
let flag = this.read_scalar(arg_op.unwrap())?.to_i32()?; | ||
// The only usage of this in stdlib at the moment is to enable the `FD_CLOEXEC` flag. | ||
let fd_cloexec = this.eval_libc_i32("FD_CLOEXEC")?; | ||
if let Some(FileHandle { flag: old_flag, .. }) = | ||
this.machine.file_handler.handles.get_mut(&fd) | ||
{ | ||
if flag ^ *old_flag == fd_cloexec { | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*old_flag = flag; | ||
} else { | ||
throw_unsup_format!("Unsupported arg {:#x} for `F_SETFD`", flag); | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
Ok(0) | ||
} else if cmd == this.eval_libc_i32("F_GETFD")? { | ||
this.get_handle_and(fd, |handle| Ok(handle.flag)) | ||
} else { | ||
throw_unsup_format!("Unsupported command {:#x}", cmd); | ||
} | ||
} | ||
|
||
fn close(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> { | ||
let this = self.eval_context_mut(); | ||
|
||
if !this.machine.communicate { | ||
throw_unsup_format!("`open` not available when isolation is enabled") | ||
} | ||
|
||
let fd = this.read_scalar(fd_op)?.to_i32()?; | ||
|
||
this.remove_handle_and( | ||
fd, | ||
|handle, this| this.consume_result(handle.file.sync_all().map(|_| 0i32)), | ||
) | ||
} | ||
|
||
fn read( | ||
&mut self, | ||
fd_op: OpTy<'tcx, Tag>, | ||
buf_op: OpTy<'tcx, Tag>, | ||
count_op: OpTy<'tcx, Tag>, | ||
) -> InterpResult<'tcx, i64> { | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let this = self.eval_context_mut(); | ||
|
||
if !this.machine.communicate { | ||
throw_unsup_format!("`open` not available when isolation is enabled") | ||
} | ||
|
||
let tcx = &{ this.tcx.tcx }; | ||
|
||
let fd = this.read_scalar(fd_op)?.to_i32()?; | ||
let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?; | ||
let count = this.read_scalar(count_op)?.to_usize(&*this.tcx)?; | ||
|
||
// Remove the file handle to avoid borrowing issues | ||
this.remove_handle_and( | ||
fd, | ||
|mut handle, this| { | ||
let bytes = handle | ||
.file | ||
.read(this.memory_mut().get_mut(buf.alloc_id)?.get_bytes_mut( | ||
tcx, | ||
buf, | ||
Size::from_bytes(count), | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)?) | ||
.map(|bytes| bytes as i64); | ||
// Reinsert the file handle | ||
this.machine.file_handler.handles.insert(fd, handle); | ||
this.consume_result(bytes) | ||
}, | ||
) | ||
} | ||
|
||
fn get_handle_and<F, T: From<i32>>(&mut self, fd: i32, f: F) -> InterpResult<'tcx, T> | ||
where | ||
F: Fn(&FileHandle) -> InterpResult<'tcx, T>, | ||
{ | ||
let this = self.eval_context_mut(); | ||
if let Some(handle) = this.machine.file_handler.handles.get(&fd) { | ||
f(handle) | ||
} else { | ||
this.machine.last_error = this.eval_libc_i32("EBADF")? as u32; | ||
Ok((-1).into()) | ||
} | ||
} | ||
|
||
fn remove_handle_and<F, T: From<i32>>(&mut self, fd: i32, mut f: F) -> InterpResult<'tcx, T> | ||
where | ||
F: FnMut(FileHandle, &mut MiriEvalContext<'mir, 'tcx>) -> InterpResult<'tcx, T>, | ||
{ | ||
let this = self.eval_context_mut(); | ||
if let Some(handle) = this.machine.file_handler.handles.remove(&fd) { | ||
f(handle, this) | ||
} else { | ||
this.machine.last_error = this.eval_libc_i32("EBADF")? as u32; | ||
Ok((-1).into()) | ||
} | ||
} | ||
|
||
fn consume_result<T: From<i32>>(&mut self, result: std::io::Result<T>) -> InterpResult<'tcx, T> { | ||
match result { | ||
Ok(ok) => Ok(ok), | ||
Err(e) => { | ||
self.eval_context_mut().machine.last_error = e.raw_os_error().unwrap() as u32; | ||
Ok((-1).into()) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello, World! | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// ignore-windows: File handling is not implemented yet | ||
// compile-flags: -Zmiri-disable-isolation | ||
|
||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
fn main() { | ||
// FIXME: create the file and delete it when `rm` is implemented. | ||
let mut file = File::open("./tests/hello.txt").unwrap(); | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents).unwrap(); | ||
assert_eq!("Hello, World!\n", contents); | ||
pvdrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.