From 62c8aaf843a58839b84375c96ac4404ef5a27bda Mon Sep 17 00:00:00 2001 From: Noah Citron Date: Tue, 16 Apr 2024 13:55:13 +0100 Subject: [PATCH 1/5] fix bad sys imports --- library/std/src/sys/pal/zkvm/args.rs | 2 +- library/std/src/sys/pal/zkvm/mod.rs | 2 -- library/std/src/sys/pal/zkvm/os.rs | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/library/std/src/sys/pal/zkvm/args.rs b/library/std/src/sys/pal/zkvm/args.rs index 7753cf63840e..20df7616f4ae 100644 --- a/library/std/src/sys/pal/zkvm/args.rs +++ b/library/std/src/sys/pal/zkvm/args.rs @@ -33,7 +33,7 @@ impl Args { // "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() }) + OsString::from_inner(crate::sys::os_str::Buf { inner: arg_bytes.to_vec() }) } } diff --git a/library/std/src/sys/pal/zkvm/mod.rs b/library/std/src/sys/pal/zkvm/mod.rs index 228a976dbabc..c5a1843d5ffd 100644 --- a/library/std/src/sys/pal/zkvm/mod.rs +++ b/library/std/src/sys/pal/zkvm/mod.rs @@ -12,8 +12,6 @@ const WORD_SIZE: usize = core::mem::size_of::(); 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; diff --git a/library/std/src/sys/pal/zkvm/os.rs b/library/std/src/sys/pal/zkvm/os.rs index d8739ee38246..4f3b43b3e86c 100644 --- a/library/std/src/sys/pal/zkvm/os.rs +++ b/library/std/src/sys/pal/zkvm/os.rs @@ -111,7 +111,7 @@ pub fn getenv(varname: &OsStr) -> Option { // 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() })) + Some(OsString::from_inner(crate::sys::os_str::Buf { inner: u8s.to_vec() })) } pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> { From 239a670808edbe2bc374bc43067780adfa971312 Mon Sep 17 00:00:00 2001 From: Noah Citron Date: Tue, 16 Apr 2024 15:47:51 +0100 Subject: [PATCH 2/5] add jolt toolchain --- compiler/rustc_target/src/spec/mod.rs | 1 + .../spec/targets/riscv32i_jolt_zkvm_elf.rs | 35 +++++++++++++++++++ src/tools/build-manifest/src/main.rs | 1 + 3 files changed, 37 insertions(+) create mode 100644 compiler/rustc_target/src/spec/targets/riscv32i_jolt_zkvm_elf.rs diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index e94c7f3cc582..396310492087 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1623,6 +1623,7 @@ supported_targets! { ("riscv32i-unknown-none-elf", riscv32i_unknown_none_elf), ("riscv32im-risc0-zkvm-elf", riscv32im_risc0_zkvm_elf), + ("riscv32i-jolt-zkvm-elf", riscv32i_jolt_zkvm_elf), ("riscv32im-unknown-none-elf", riscv32im_unknown_none_elf), ("riscv32ima-unknown-none-elf", riscv32ima_unknown_none_elf), ("riscv32imc-unknown-none-elf", riscv32imc_unknown_none_elf), diff --git a/compiler/rustc_target/src/spec/targets/riscv32i_jolt_zkvm_elf.rs b/compiler/rustc_target/src/spec/targets/riscv32i_jolt_zkvm_elf.rs new file mode 100644 index 000000000000..9ac044a5963f --- /dev/null +++ b/compiler/rustc_target/src/spec/targets/riscv32i_jolt_zkvm_elf.rs @@ -0,0 +1,35 @@ +use crate::spec::{Cc, LinkerFlavor, Lld, PanicStrategy, RelocModel}; +use crate::spec::{Target, TargetOptions}; + +pub fn target() -> Target { + Target { + data_layout: "e-m:e-p:32:32-i64:64-n32-S128".into(), + llvm_target: "riscv32".into(), + pointer_width: 32, + arch: "riscv32".into(), + + metadata: crate::spec::TargetMetadata { + description: None, + tier: None, + host_tools: None, + std: None, + }, + + options: TargetOptions { + os: "zkvm".into(), + vendor: "jolt".into(), + linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes), + linker: Some("rust-lld".into()), + cpu: "generic-rv32".into(), + max_atomic_width: Some(64), + atomic_cas: true, + executables: true, + panic_strategy: PanicStrategy::Abort, + relocation_model: RelocModel::Static, + emit_debug_gdb_scripts: false, + eh_frame_header: false, + singlethread: true, + ..Default::default() + }, + } +} diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs index 392a5a119676..aba066bd635e 100644 --- a/src/tools/build-manifest/src/main.rs +++ b/src/tools/build-manifest/src/main.rs @@ -125,6 +125,7 @@ static TARGETS: &[&str] = &[ "powerpc64le-unknown-linux-gnu", "riscv32i-unknown-none-elf", "riscv32im-risc0-zkvm-elf", + "riscv32i-jolt-zkvm-elf", "riscv32im-unknown-none-elf", "riscv32ima-unknown-none-elf", "riscv32imc-unknown-none-elf", From 647f6d356a5844898cec248d18235ab362273432 Mon Sep 17 00:00:00 2001 From: Noah Citron Date: Tue, 16 Apr 2024 17:11:38 +0100 Subject: [PATCH 3/5] remove sys functions --- library/panic_abort/src/zkvm.rs | 38 ++-- library/std/src/sys/pal/zkvm/abi.rs | 106 +++++----- library/std/src/sys/pal/zkvm/alloc.rs | 11 +- library/std/src/sys/pal/zkvm/args.rs | 160 +++++++-------- library/std/src/sys/pal/zkvm/mod.rs | 21 +- library/std/src/sys/pal/zkvm/os.rs | 278 +++++++++++++------------- library/std/src/sys/pal/zkvm/stdio.rs | 128 ++++++------ 7 files changed, 370 insertions(+), 372 deletions(-) diff --git a/library/panic_abort/src/zkvm.rs b/library/panic_abort/src/zkvm.rs index a6a02abf1097..3eca5c07f5e6 100644 --- a/library/panic_abort/src/zkvm.rs +++ b/library/panic_abort/src/zkvm.rs @@ -1,24 +1,24 @@ -use alloc::string::String; +// use alloc::string::String; use core::panic::PanicPayload; -// Forward the abort message to zkVM's sys_panic. This is implemented by RISC Zero's -// platform crate which exposes system calls specifically for the zkVM. -pub(crate) unsafe fn zkvm_set_abort_message(payload: &mut dyn PanicPayload) { - let payload = payload.get(); - let msg = match payload.downcast_ref::<&'static str>() { - Some(msg) => msg.as_bytes(), - None => match payload.downcast_ref::() { - Some(msg) => msg.as_bytes(), - None => &[], - }, - }; - if msg.is_empty() { - return; - } +pub(crate) unsafe fn zkvm_set_abort_message(_payload: &mut dyn PanicPayload) { + // TODO: fix - extern "C" { - fn sys_panic(msg_ptr: *const u8, len: usize) -> !; - } + // let payload = payload.get(); + // let msg = match payload.downcast_ref::<&'static str>() { + // Some(msg) => msg.as_bytes(), + // None => match payload.downcast_ref::() { + // Some(msg) => msg.as_bytes(), + // None => &[], + // }, + // }; + // if msg.is_empty() { + // return; + // } - sys_panic(msg.as_ptr(), msg.len()); + // extern "C" { + // fn sys_panic(msg_ptr: *const u8, len: usize) -> !; + // } + + // sys_panic(msg.as_ptr(), msg.len()); } diff --git a/library/std/src/sys/pal/zkvm/abi.rs b/library/std/src/sys/pal/zkvm/abi.rs index 53332d90e02c..4eb7904d615c 100644 --- a/library/std/src/sys/pal/zkvm/abi.rs +++ b/library/std/src/sys/pal/zkvm/abi.rs @@ -1,55 +1,55 @@ -//! ABI definitions for symbols exported by risc0-zkvm-platform. - -// Included here so we don't have to depend on risc0-zkvm-platform. +// //! ABI definitions for symbols exported by 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; +// // 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; +// } - // 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; -} +// 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; +// } diff --git a/library/std/src/sys/pal/zkvm/alloc.rs b/library/std/src/sys/pal/zkvm/alloc.rs index fd333f121515..737fd6b1fe19 100644 --- a/library/std/src/sys/pal/zkvm/alloc.rs +++ b/library/std/src/sys/pal/zkvm/alloc.rs @@ -1,15 +1,10 @@ -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()) + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + core::ptr::null_mut() } - #[inline] - unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { - // this allocator never deallocates memory - } + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} } diff --git a/library/std/src/sys/pal/zkvm/args.rs b/library/std/src/sys/pal/zkvm/args.rs index 20df7616f4ae..3d0fb6f2eb34 100644 --- a/library/std/src/sys/pal/zkvm/args.rs +++ b/library/std/src/sys/pal/zkvm/args.rs @@ -1,80 +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(crate::sys::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 { - 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) { - (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 { - 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) - } - } -} +// 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(crate::sys::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 { +// 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) { +// (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 { +// 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) +// } +// } +// } diff --git a/library/std/src/sys/pal/zkvm/mod.rs b/library/std/src/sys/pal/zkvm/mod.rs index c5a1843d5ffd..bbe92cdfc79d 100644 --- a/library/std/src/sys/pal/zkvm/mod.rs +++ b/library/std/src/sys/pal/zkvm/mod.rs @@ -1,4 +1,4 @@ -//! System bindings for the risc0 zkvm platform +//! System bindings for the jolt zkvm platform //! //! This module contains the facade (aka platform-specific) implementations of //! OS level functionality for zkvm. @@ -7,11 +7,10 @@ //! 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::(); - pub mod alloc; -#[path = "../zkvm/args.rs"] +#[path = "../unsupported/args.rs"] pub mod args; +#[path = "../unsupported/env.rs"] pub mod env; #[path = "../unsupported/fs.rs"] pub mod fs; @@ -19,11 +18,13 @@ pub mod fs; pub mod io; #[path = "../unsupported/net.rs"] pub mod net; +#[path = "../unsupported/os.rs"] pub mod os; #[path = "../unsupported/pipe.rs"] pub mod pipe; #[path = "../unsupported/process.rs"] pub mod process; +#[path = "../unsupported/stdio.rs"] pub mod stdio; pub mod thread_local_key; #[path = "../unsupported/time.rs"] @@ -71,9 +72,11 @@ pub fn abort_internal() -> ! { } pub fn hashmap_random_keys() -> (u64, u64) { - let mut buf = [0u32; 4]; - unsafe { - abi::sys_rand(buf.as_mut_ptr(), 4); - }; - ((buf[0] as u64) << 32 + buf[1] as u64, (buf[2] as u64) << 32 + buf[3] as u64) + // let mut buf = [0u32; 4]; + // unsafe { + // abi::sys_rand(buf.as_mut_ptr(), 4); + // }; + // (buf[0] as u64) << 32 + buf[1] as u64, (buf[2] as u64) << 32 + buf[3] as u64) + + (0, 0) } diff --git a/library/std/src/sys/pal/zkvm/os.rs b/library/std/src/sys/pal/zkvm/os.rs index 4f3b43b3e86c..4ef7e7e6cf01 100644 --- a/library/std/src/sys/pal/zkvm/os.rs +++ b/library/std/src/sys/pal/zkvm/os.rs @@ -1,139 +1,139 @@ -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 { - 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 { - self.0 - } -} - -#[derive(Debug)] -pub struct JoinPathsError; - -pub fn join_paths(_paths: I) -> Result -where - I: Iterator, - T: AsRef, -{ - 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 { - 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") -} - -impl Env { - pub fn str_debug(&self) -> impl fmt::Debug + '_ { - let Self(inner) = self; - match *inner {} - } -} - -impl fmt::Debug for Env { - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { - let Self(inner) = self; - match *inner {} - } -} - -pub fn getenv(varname: &OsStr) -> Option { - let varname = varname.as_encoded_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(crate::sys::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 { - None -} - -pub fn exit(_code: i32) -> ! { - crate::intrinsics::abort() -} - -pub fn getpid() -> u32 { - panic!("no pids on this platform") -} +// 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 { +// 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 { +// self.0 +// } +// } +// +// #[derive(Debug)] +// pub struct JoinPathsError; +// +// pub fn join_paths(_paths: I) -> Result +// where +// I: Iterator, +// T: AsRef, +// { +// 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 { +// 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") +// } +// +// impl Env { +// pub fn str_debug(&self) -> impl fmt::Debug + '_ { +// let Self(inner) = self; +// match *inner {} +// } +// } +// +// impl fmt::Debug for Env { +// fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { +// let Self(inner) = self; +// match *inner {} +// } +// } +// +// pub fn getenv(varname: &OsStr) -> Option { +// let varname = varname.as_encoded_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(crate::sys::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 { +// None +// } +// +// pub fn exit(_code: i32) -> ! { +// crate::intrinsics::abort() +// } +// +// pub fn getpid() -> u32 { +// panic!("no pids on this platform") +// } diff --git a/library/std/src/sys/pal/zkvm/stdio.rs b/library/std/src/sys/pal/zkvm/stdio.rs index e771ed0de28d..15aab6068afc 100644 --- a/library/std/src/sys/pal/zkvm/stdio.rs +++ b/library/std/src/sys/pal/zkvm/stdio.rs @@ -1,64 +1,64 @@ -use super::{abi, abi::fileno}; -use crate::io; - -pub struct Stdin; -pub struct Stdout; -pub struct Stderr; - -impl Stdin { - pub const fn new() -> Stdin { - Stdin - } -} - -impl io::Read for Stdin { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - Ok(unsafe { abi::sys_read(fileno::STDIN, buf.as_mut_ptr(), buf.len()) }) - } -} - -impl Stdout { - pub const fn new() -> Stdout { - Stdout - } -} - -impl io::Write for Stdout { - fn write(&mut self, buf: &[u8]) -> io::Result { - unsafe { abi::sys_write(fileno::STDOUT, buf.as_ptr(), buf.len()) } - - Ok(buf.len()) - } - - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } -} - -impl Stderr { - pub const fn new() -> Stderr { - Stderr - } -} - -impl io::Write for Stderr { - fn write(&mut self, buf: &[u8]) -> io::Result { - unsafe { abi::sys_write(fileno::STDERR, buf.as_ptr(), buf.len()) } - - Ok(buf.len()) - } - - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } -} - -pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; - -pub fn is_ebadf(_err: &io::Error) -> bool { - true -} - -pub fn panic_output() -> Option { - Some(Stderr::new()) -} +// use super::{abi, abi::fileno}; +// use crate::io; +// +// pub struct Stdin; +// pub struct Stdout; +// pub struct Stderr; +// +// impl Stdin { +// pub const fn new() -> Stdin { +// Stdin +// } +// } +// +// impl io::Read for Stdin { +// fn read(&mut self, buf: &mut [u8]) -> io::Result { +// Ok(unsafe { abi::sys_read(fileno::STDIN, buf.as_mut_ptr(), buf.len()) }) +// } +// } +// +// impl Stdout { +// pub const fn new() -> Stdout { +// Stdout +// } +// } +// +// impl io::Write for Stdout { +// fn write(&mut self, buf: &[u8]) -> io::Result { +// unsafe { abi::sys_write(fileno::STDOUT, buf.as_ptr(), buf.len()) } +// +// Ok(buf.len()) +// } +// +// fn flush(&mut self) -> io::Result<()> { +// Ok(()) +// } +// } +// +// impl Stderr { +// pub const fn new() -> Stderr { +// Stderr +// } +// } +// +// impl io::Write for Stderr { +// fn write(&mut self, buf: &[u8]) -> io::Result { +// unsafe { abi::sys_write(fileno::STDERR, buf.as_ptr(), buf.len()) } +// +// Ok(buf.len()) +// } +// +// fn flush(&mut self) -> io::Result<()> { +// Ok(()) +// } +// } +// +// pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; +// +// pub fn is_ebadf(_err: &io::Error) -> bool { +// true +// } +// +// pub fn panic_output() -> Option { +// Some(Stderr::new()) +// } From f55645c560454939a9e562a00a0ebbc87d7e6373 Mon Sep 17 00:00:00 2001 From: Noah Citron Date: Tue, 16 Apr 2024 19:05:18 +0100 Subject: [PATCH 4/5] fix allocator --- .../spec/targets/riscv32i_jolt_zkvm_elf.rs | 1 + library/std/src/lib.rs | 2 +- library/std/src/sys/pal/zkvm/alloc.rs | 50 +++++++++++++++++-- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_target/src/spec/targets/riscv32i_jolt_zkvm_elf.rs b/compiler/rustc_target/src/spec/targets/riscv32i_jolt_zkvm_elf.rs index 9ac044a5963f..783b9a744a10 100644 --- a/compiler/rustc_target/src/spec/targets/riscv32i_jolt_zkvm_elf.rs +++ b/compiler/rustc_target/src/spec/targets/riscv32i_jolt_zkvm_elf.rs @@ -29,6 +29,7 @@ pub fn target() -> Target { emit_debug_gdb_scripts: false, eh_frame_header: false, singlethread: true, + supports_stack_protector: false, ..Default::default() }, } diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index c713eefc72c9..de8ef847d3ec 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -242,7 +242,7 @@ #![allow(unused_lifetimes)] #![allow(internal_features)] #![deny(rustc::existing_doc_keyword)] -#![deny(fuzzy_provenance_casts)] +// #![deny(fuzzy_provenance_casts)] #![allow(rustdoc::redundant_explicit_links)] // Ensure that std can be linked against panic_abort despite compiled with `-C panic=unwind` #![deny(ffi_unwind_calls)] diff --git a/library/std/src/sys/pal/zkvm/alloc.rs b/library/std/src/sys/pal/zkvm/alloc.rs index 737fd6b1fe19..7ce4efb12502 100644 --- a/library/std/src/sys/pal/zkvm/alloc.rs +++ b/library/std/src/sys/pal/zkvm/alloc.rs @@ -1,10 +1,54 @@ -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::{alloc::{GlobalAlloc, Layout, System}, cell::UnsafeCell}; + +static mut BUMP_ALLOC: BumpAllocator = BumpAllocator::new(); #[stable(feature = "alloc_system_type", since = "1.28.0")] unsafe impl GlobalAlloc for System { - unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { - core::ptr::null_mut() + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + BUMP_ALLOC.alloc(layout) } unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} } + +extern "C" { + static _HEAP_PTR: u8; +} + +pub struct BumpAllocator { + offset: UnsafeCell, +} + +unsafe impl Sync for BumpAllocator {} + +fn heap_start() -> usize { + unsafe { _HEAP_PTR as *const u8 as usize } +} + +impl BumpAllocator { + pub const fn new() -> Self { + Self { + offset: UnsafeCell::new(0), + } + } + + pub fn free_memory(&self) -> usize { + heap_start() + (self.offset.get() as usize) + } +} + +unsafe impl GlobalAlloc for BumpAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + let alloc_start = align_up(self.free_memory(), layout.align()); + let alloc_end = alloc_start + layout.size(); + *self.offset.get() = alloc_end - self.free_memory(); + + alloc_start as *mut u8 + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} +} + +fn align_up(addr: usize, align: usize) -> usize { + (addr + align - 1) & !(align - 1) +} From b79954f679f7928174d6f726b3afcab3f3d3ac8d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:15:35 +0000 Subject: [PATCH 5/5] Bump rustix from 0.38.8 to 0.38.32 in /compiler/rustc_codegen_gcc Bumps [rustix](https://github.com/bytecodealliance/rustix) from 0.38.8 to 0.38.32. - [Release notes](https://github.com/bytecodealliance/rustix/releases) - [Commits](https://github.com/bytecodealliance/rustix/compare/v0.38.8...v0.38.32) --- updated-dependencies: - dependency-name: rustix dependency-type: indirect ... Signed-off-by: dependabot[bot] --- compiler/rustc_codegen_gcc/Cargo.lock | 128 ++++++++++++++++++-------- 1 file changed, 92 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_codegen_gcc/Cargo.lock b/compiler/rustc_codegen_gcc/Cargo.lock index 3ecb0ef6b4d2..8d28b71bcb4e 100644 --- a/compiler/rustc_codegen_gcc/Cargo.lock +++ b/compiler/rustc_codegen_gcc/Cargo.lock @@ -29,12 +29,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85fdb93f04c73bff54305fa437ffea5449c41edcaadfe882f35836206b166ac5" -[[package]] -name = "cc" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" - [[package]] name = "cfg-if" version = "1.0.0" @@ -43,23 +37,12 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "errno" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" -dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ - "cc", "libc", + "windows-sys 0.52.0", ] [[package]] @@ -128,15 +111,15 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.150" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "linux-raw-sys" -version = "0.4.5" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" [[package]] name = "memchr" @@ -203,15 +186,15 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.8" +version = "0.38.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19ed4fa021d81c8392ce04db050a3da9a60299050b7ae1cf482d862b54a7218f" +checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" dependencies = [ "bitflags 2.4.0", "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -239,7 +222,7 @@ dependencies = [ "fastrand", "redox_syscall", "rustix", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -322,7 +305,16 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.1", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.5", ] [[package]] @@ -331,13 +323,29 @@ version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.0", + "windows_aarch64_msvc 0.48.0", + "windows_i686_gnu 0.48.0", + "windows_i686_msvc 0.48.0", + "windows_x86_64_gnu 0.48.0", + "windows_x86_64_gnullvm 0.48.0", + "windows_x86_64_msvc 0.48.0", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", ] [[package]] @@ -346,38 +354,86 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + [[package]] name = "windows_aarch64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + [[package]] name = "windows_i686_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + [[package]] name = "windows_i686_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + [[package]] name = "windows_x86_64_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + [[package]] name = "windows_x86_64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"