Implement read_buf for zkVM stdin
For the zkVM, even when a guest buffer is uninitialized, from the host's perspective it is just a normal piece of memory which was initialized before letting the guest write into it. This makes `sys_read` safe to use with an uninitialized buffer. See https://github.com/risc0/risc0/issues/2853.
This commit is contained in:
parent
f04bbc60f8
commit
98b0f050cf
1 changed files with 9 additions and 1 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use super::abi;
|
||||
use super::abi::fileno;
|
||||
use crate::io;
|
||||
use crate::io::{self, BorrowedCursor};
|
||||
|
||||
pub struct Stdin;
|
||||
pub struct Stdout;
|
||||
|
|
@ -16,6 +16,14 @@ impl io::Read for Stdin {
|
|||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
Ok(unsafe { abi::sys_read(fileno::STDIN, buf.as_mut_ptr(), buf.len()) })
|
||||
}
|
||||
|
||||
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
|
||||
unsafe {
|
||||
let n = abi::sys_read(fileno::STDIN, buf.as_mut().as_mut_ptr().cast(), buf.capacity());
|
||||
buf.advance_unchecked(n);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Stdout {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue