about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThalia Archibald <thalia@archibald.dev>2025-02-07 17:40:15 -0800
committerThalia Archibald <thalia@archibald.dev>2025-02-20 16:07:12 -0700
commit98b0f050cf61f86cd39babe1fa4b33808fdc0770 (patch)
treea0fbf8dee7fce9a4684a22712a7db4942e94b282
parentf04bbc60f8c353ee5ba0677bc583ac4a88b2c180 (diff)
downloadrust-98b0f050cf61f86cd39babe1fa4b33808fdc0770.tar.gz
rust-98b0f050cf61f86cd39babe1fa4b33808fdc0770.zip
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.
-rw-r--r--library/std/src/sys/pal/zkvm/stdio.rs10
1 files changed, 9 insertions, 1 deletions
diff --git a/library/std/src/sys/pal/zkvm/stdio.rs b/library/std/src/sys/pal/zkvm/stdio.rs
index 5f1d06dd1d7..0bcb54744b0 100644
--- a/library/std/src/sys/pal/zkvm/stdio.rs
+++ b/library/std/src/sys/pal/zkvm/stdio.rs
@@ -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 {