-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Frank Laub <flaub@risc0.com> Co-authored-by: nils <nils@risc0.com> Co-authored-by: Victor Graf <victor@risczero.com>
- Loading branch information
1 parent
6954a67
commit d0516ea
Showing
11 changed files
with
428 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,55 @@ | ||
//! ABI definitions for symbols exported by risc0-zkvm-platform. | ||
|
||
// Included here so we don't have to depend on risc0-zkvm-platform. | ||
// | ||
// FIXME: Should we move this to the "libc" crate? It seems like other | ||
// architectures put a lot of this kind of stuff there. But there's | ||
// currently no risc0 fork of the libc crate, so we'd either have to | ||
// fork it or upstream it. | ||
|
||
#![allow(dead_code)] | ||
pub const DIGEST_WORDS: usize = 8; | ||
|
||
/// Standard IO file descriptors for use with sys_read and sys_write. | ||
pub mod fileno { | ||
pub const STDIN: u32 = 0; | ||
pub const STDOUT: u32 = 1; | ||
pub const STDERR: u32 = 2; | ||
pub const JOURNAL: u32 = 3; | ||
} | ||
|
||
extern "C" { | ||
// Wrappers around syscalls provided by risc0-zkvm-platform: | ||
pub fn sys_halt(); | ||
pub fn sys_output(output_id: u32, output_value: u32); | ||
pub fn sys_sha_compress( | ||
out_state: *mut [u32; DIGEST_WORDS], | ||
in_state: *const [u32; DIGEST_WORDS], | ||
block1_ptr: *const [u32; DIGEST_WORDS], | ||
block2_ptr: *const [u32; DIGEST_WORDS], | ||
); | ||
pub fn sys_sha_buffer( | ||
out_state: *mut [u32; DIGEST_WORDS], | ||
in_state: *const [u32; DIGEST_WORDS], | ||
buf: *const u8, | ||
count: u32, | ||
); | ||
pub fn sys_rand(recv_buf: *mut u32, words: usize); | ||
pub fn sys_panic(msg_ptr: *const u8, len: usize) -> !; | ||
pub fn sys_log(msg_ptr: *const u8, len: usize); | ||
pub fn sys_cycle_count() -> usize; | ||
pub fn sys_read(fd: u32, recv_buf: *mut u8, nrequested: usize) -> usize; | ||
pub fn sys_write(fd: u32, write_buf: *const u8, nbytes: usize); | ||
pub fn sys_getenv( | ||
recv_buf: *mut u32, | ||
words: usize, | ||
varname: *const u8, | ||
varname_len: usize, | ||
) -> usize; | ||
pub fn sys_argc() -> usize; | ||
pub fn sys_argv(out_words: *mut u32, out_nwords: usize, arg_index: usize) -> usize; | ||
|
||
// Allocate memory from global HEAP. | ||
pub fn sys_alloc_words(nwords: usize) -> *mut u32; | ||
pub fn sys_alloc_aligned(nwords: usize, align: usize) -> *mut u8; | ||
} |
This file contains 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,15 @@ | ||
use super::abi; | ||
use crate::alloc::{GlobalAlloc, Layout, System}; | ||
|
||
#[stable(feature = "alloc_system_type", since = "1.28.0")] | ||
unsafe impl GlobalAlloc for System { | ||
#[inline] | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
abi::sys_alloc_aligned(layout.size(), layout.align()) | ||
} | ||
|
||
#[inline] | ||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { | ||
// this allocator never deallocates memory | ||
} | ||
} |
This file contains 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,80 @@ | ||
use super::{abi, WORD_SIZE}; | ||
use crate::ffi::OsString; | ||
use crate::fmt; | ||
use crate::sys_common::FromInner; | ||
|
||
pub struct Args { | ||
i_forward: usize, | ||
i_back: usize, | ||
count: usize, | ||
} | ||
|
||
pub fn args() -> Args { | ||
let count = unsafe { abi::sys_argc() }; | ||
Args { i_forward: 0, i_back: 0, count } | ||
} | ||
|
||
impl Args { | ||
/// Use sys_argv to get the arg at the requested index. Does not check that i is less than argc | ||
/// and will not return if the index is out of bounds. | ||
fn argv(i: usize) -> OsString { | ||
let arg_len = unsafe { abi::sys_argv(crate::ptr::null_mut(), 0, i) }; | ||
|
||
let arg_len_words = (arg_len + WORD_SIZE - 1) / WORD_SIZE; | ||
let words = unsafe { abi::sys_alloc_words(arg_len_words) }; | ||
|
||
let arg_len2 = unsafe { abi::sys_argv(words, arg_len_words, i) }; | ||
debug_assert_eq!(arg_len, arg_len2); | ||
|
||
// Convert to OsString. | ||
// | ||
// FIXME: We can probably get rid of the extra copy here if we | ||
// reimplement "os_str" instead of just using the generic unix | ||
// "os_str". | ||
let arg_bytes: &[u8] = | ||
unsafe { crate::slice::from_raw_parts(words.cast() as *const u8, arg_len) }; | ||
OsString::from_inner(super::os_str::Buf { inner: arg_bytes.to_vec() }) | ||
} | ||
} | ||
|
||
impl fmt::Debug for Args { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_list().finish() | ||
} | ||
} | ||
|
||
impl Iterator for Args { | ||
type Item = OsString; | ||
|
||
fn next(&mut self) -> Option<OsString> { | ||
if self.i_forward >= self.count - self.i_back { | ||
None | ||
} else { | ||
let arg = Self::argv(self.i_forward); | ||
self.i_forward += 1; | ||
Some(arg) | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.count, Some(self.count)) | ||
} | ||
} | ||
|
||
impl ExactSizeIterator for Args { | ||
fn len(&self) -> usize { | ||
self.count | ||
} | ||
} | ||
|
||
impl DoubleEndedIterator for Args { | ||
fn next_back(&mut self) -> Option<OsString> { | ||
if self.i_back >= self.count - self.i_forward { | ||
None | ||
} else { | ||
let arg = Self::argv(self.count - 1 - self.i_back); | ||
self.i_back += 1; | ||
Some(arg) | ||
} | ||
} | ||
} |
This file contains 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,9 @@ | ||
pub mod os { | ||
pub const FAMILY: &str = ""; | ||
pub const OS: &str = ""; | ||
pub const DLL_PREFIX: &str = ""; | ||
pub const DLL_SUFFIX: &str = ".elf"; | ||
pub const DLL_EXTENSION: &str = "elf"; | ||
pub const EXE_SUFFIX: &str = ".elf"; | ||
pub const EXE_EXTENSION: &str = "elf"; | ||
} |
This file contains 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,50 @@ | ||
//! System bindings for the risc0 zkvm platform | ||
//! | ||
//! This module contains the facade (aka platform-specific) implementations of | ||
//! OS level functionality for zkvm. | ||
//! | ||
//! This is all super highly experimental and not actually intended for | ||
//! wide/production use yet, it's still all in the experimental category. This | ||
//! will likely change over time. | ||
|
||
const WORD_SIZE: usize = core::mem::size_of::<u32>(); | ||
|
||
pub mod alloc; | ||
#[path = "../zkvm/args.rs"] | ||
pub mod args; | ||
#[path = "../unix/cmath.rs"] | ||
pub mod cmath; | ||
pub mod env; | ||
#[path = "../unsupported/fs.rs"] | ||
pub mod fs; | ||
#[path = "../unsupported/io.rs"] | ||
pub mod io; | ||
#[path = "../unsupported/net.rs"] | ||
pub mod net; | ||
#[path = "../unsupported/once.rs"] | ||
pub mod once; | ||
pub mod os; | ||
#[path = "../unix/os_str.rs"] | ||
pub mod os_str; | ||
#[path = "../unix/path.rs"] | ||
pub mod path; | ||
#[path = "../unsupported/pipe.rs"] | ||
pub mod pipe; | ||
#[path = "../unsupported/process.rs"] | ||
pub mod process; | ||
pub mod stdio; | ||
pub mod thread_local_key; | ||
#[path = "../unsupported/time.rs"] | ||
pub mod time; | ||
|
||
#[path = "../unsupported/locks/mod.rs"] | ||
pub mod locks; | ||
#[path = "../unsupported/thread.rs"] | ||
pub mod thread; | ||
|
||
#[path = "../unsupported/common.rs"] | ||
#[deny(unsafe_op_in_unsafe_fn)] | ||
mod common; | ||
pub use common::*; | ||
|
||
mod abi; |
This file contains 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,125 @@ | ||
use super::{abi, unsupported, WORD_SIZE}; | ||
use crate::error::Error as StdError; | ||
use crate::ffi::{OsStr, OsString}; | ||
use crate::fmt; | ||
use crate::io; | ||
use crate::marker::PhantomData; | ||
use crate::path::{self, PathBuf}; | ||
use crate::sys_common::FromInner; | ||
|
||
pub fn errno() -> i32 { | ||
0 | ||
} | ||
|
||
pub fn error_string(_errno: i32) -> String { | ||
"operation successful".to_string() | ||
} | ||
|
||
pub fn getcwd() -> io::Result<PathBuf> { | ||
unsupported() | ||
} | ||
|
||
pub fn chdir(_: &path::Path) -> io::Result<()> { | ||
unsupported() | ||
} | ||
|
||
pub struct SplitPaths<'a>(!, PhantomData<&'a ()>); | ||
|
||
pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> { | ||
panic!("unsupported") | ||
} | ||
|
||
impl<'a> Iterator for SplitPaths<'a> { | ||
type Item = PathBuf; | ||
fn next(&mut self) -> Option<PathBuf> { | ||
self.0 | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct JoinPathsError; | ||
|
||
pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError> | ||
where | ||
I: Iterator<Item = T>, | ||
T: AsRef<OsStr>, | ||
{ | ||
Err(JoinPathsError) | ||
} | ||
|
||
impl fmt::Display for JoinPathsError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
"not supported on this platform yet".fmt(f) | ||
} | ||
} | ||
|
||
impl StdError for JoinPathsError { | ||
#[allow(deprecated)] | ||
fn description(&self) -> &str { | ||
"not supported on this platform yet" | ||
} | ||
} | ||
|
||
pub fn current_exe() -> io::Result<PathBuf> { | ||
unsupported() | ||
} | ||
|
||
pub struct Env(!); | ||
|
||
impl Iterator for Env { | ||
type Item = (OsString, OsString); | ||
fn next(&mut self) -> Option<(OsString, OsString)> { | ||
self.0 | ||
} | ||
} | ||
|
||
pub fn env() -> Env { | ||
panic!("not supported on this platform") | ||
} | ||
|
||
pub fn getenv(varname: &OsStr) -> Option<OsString> { | ||
let varname = varname.bytes(); | ||
let nbytes = | ||
unsafe { abi::sys_getenv(crate::ptr::null_mut(), 0, varname.as_ptr(), varname.len()) }; | ||
if nbytes == usize::MAX { | ||
return None; | ||
} | ||
|
||
let nwords = (nbytes + WORD_SIZE - 1) / WORD_SIZE; | ||
let words = unsafe { abi::sys_alloc_words(nwords) }; | ||
|
||
let nbytes2 = unsafe { abi::sys_getenv(words, nwords, varname.as_ptr(), varname.len()) }; | ||
debug_assert_eq!(nbytes, nbytes2); | ||
|
||
// Convert to OsString. | ||
// | ||
// FIXME: We can probably get rid of the extra copy here if we | ||
// reimplement "os_str" instead of just using the generic unix | ||
// "os_str". | ||
let u8s: &[u8] = unsafe { crate::slice::from_raw_parts(words.cast() as *const u8, nbytes) }; | ||
Some(OsString::from_inner(super::os_str::Buf { inner: u8s.to_vec() })) | ||
} | ||
|
||
pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> { | ||
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot set env vars on this platform")) | ||
} | ||
|
||
pub fn unsetenv(_: &OsStr) -> io::Result<()> { | ||
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot unset env vars on this platform")) | ||
} | ||
|
||
pub fn temp_dir() -> PathBuf { | ||
panic!("no filesystem on this platform") | ||
} | ||
|
||
pub fn home_dir() -> Option<PathBuf> { | ||
None | ||
} | ||
|
||
pub fn exit(_code: i32) -> ! { | ||
crate::intrinsics::abort() | ||
} | ||
|
||
pub fn getpid() -> u32 { | ||
panic!("no pids on this platform") | ||
} |
Oops, something went wrong.