Skip to content

Commit 5cce5b7

Browse files
tgross35gitbot
authored and
gitbot
committed
Rollup merge of rust-lang#137349 - thaliaarchi:io-optional-methods/zkvm, r=Noratrieb
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 risc0/risc0#2853. cc `@bobbobbio,` `@flaub` r? `@Noratrieb` Tracked in rust-lang#136756
2 parents da0d982 + 2bc4b64 commit 5cce5b7

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

std/src/sys/pal/zkvm/stdio.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::abi;
22
use super::abi::fileno;
3-
use crate::io;
3+
use crate::io::{self, BorrowedCursor};
44

55
pub struct Stdin;
66
pub struct Stdout;
@@ -16,6 +16,14 @@ impl io::Read for Stdin {
1616
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
1717
Ok(unsafe { abi::sys_read(fileno::STDIN, buf.as_mut_ptr(), buf.len()) })
1818
}
19+
20+
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
21+
unsafe {
22+
let n = abi::sys_read(fileno::STDIN, buf.as_mut().as_mut_ptr().cast(), buf.capacity());
23+
buf.advance_unchecked(n);
24+
}
25+
Ok(())
26+
}
1927
}
2028

2129
impl Stdout {

0 commit comments

Comments
 (0)