forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from risc0/nils/from-upstream-nightly-2022-06-13
Add rust standard library support for zkvm to upstream's nightly-2022-06-13
- Loading branch information
Showing
17 changed files
with
247 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
36 changes: 36 additions & 0 deletions
36
compiler/rustc_target/src/spec/riscv32im_risc0_zkvm_elf.rs
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,36 @@ | ||
use crate::spec::{LinkerFlavor, LldFlavor, 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(), | ||
|
||
options: TargetOptions { | ||
os: "zkvm".into(), | ||
vendor: "risc0".into(), | ||
linker_flavor: LinkerFlavor::Lld(LldFlavor::Ld), | ||
linker: Some("rust-lld".into()), | ||
cpu: "generic-rv32".into(), | ||
|
||
// Some crates (*cough* crossbeam) assume you have 64 bit | ||
// atomics if the target name is not in a hardcoded list. | ||
// Since zkvm is singlethreaded and all operations are | ||
// atomic, I guess we can just say we support 64-bit | ||
// atomics. | ||
max_atomic_width: Some(64), | ||
atomic_cas: true, | ||
|
||
features: "+m".into(), | ||
executables: true, | ||
panic_strategy: PanicStrategy::Abort, | ||
relocation_model: RelocModel::Static, | ||
emit_debug_gdb_scripts: false, | ||
eh_frame_header: false, | ||
singlethread: true, | ||
..Default::default() | ||
}, | ||
} | ||
} |
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
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,36 @@ | ||
use crate::alloc::{GlobalAlloc, Layout, System}; | ||
use crate::cell::UnsafeCell; | ||
use risc0_zkvm_platform::{memory, WORD_SIZE}; | ||
|
||
struct BumpPointerAlloc { | ||
head: UnsafeCell<usize>, | ||
end: usize, | ||
} | ||
// SAFETY: single threaded environment | ||
unsafe impl Sync for BumpPointerAlloc {} | ||
|
||
static mut HEAP: BumpPointerAlloc = | ||
BumpPointerAlloc { head: UnsafeCell::new(memory::HEAP.start()), end: memory::HEAP.end() }; | ||
|
||
#[stable(feature = "alloc_system_type", since = "1.28.0")] | ||
unsafe impl GlobalAlloc for System { | ||
#[inline] | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
let head = HEAP.head.get(); | ||
|
||
// move start up to the next alignment boundary | ||
let alloc_start = (*head + WORD_SIZE) & !(WORD_SIZE - 1); | ||
let alloc_end = alloc_start.checked_add(layout.size()).unwrap(); | ||
if alloc_end > HEAP.end { | ||
panic!("out of heap"); | ||
} else { | ||
*head = alloc_end; | ||
alloc_start as *mut u8 | ||
} | ||
} | ||
|
||
#[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,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,45 @@ | ||
//! 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. | ||
|
||
pub mod alloc; | ||
#[path = "../unsupported/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/os.rs"] | ||
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::*; |
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,63 @@ | ||
use crate::io; | ||
|
||
use risc0_zkvm_platform::io::{host_sendrecv, SENDRECV_CHANNEL_STDERR, SENDRECV_CHANNEL_STDOUT}; | ||
|
||
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<usize> { | ||
Ok(0) | ||
} | ||
} | ||
|
||
impl Stdout { | ||
pub const fn new() -> Stdout { | ||
Stdout | ||
} | ||
} | ||
|
||
impl io::Write for Stdout { | ||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
host_sendrecv(SENDRECV_CHANNEL_STDOUT, buf); | ||
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<usize> { | ||
host_sendrecv(SENDRECV_CHANNEL_STDERR, buf); | ||
Ok(buf.len()) | ||
} | ||
|
||
fn flush(&mut self) -> io::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub const STDIN_BUF_SIZE: usize = 0; | ||
|
||
pub fn is_ebadf(_err: &io::Error) -> bool { | ||
true | ||
} | ||
|
||
pub fn panic_output() -> Option<impl io::Write> { | ||
Some(Stderr::new()) | ||
} |
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,28 @@ | ||
use crate::alloc::{alloc, Layout}; | ||
|
||
pub type Key = usize; | ||
|
||
#[inline] | ||
pub unsafe fn create(_dtor: Option<unsafe extern "C" fn(*mut u8)>) -> Key { | ||
alloc(Layout::new::<*mut u8>()) as _ | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn set(key: Key, value: *mut u8) { | ||
let key = key as *mut *mut u8; | ||
*key = value; | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn get(key: Key) -> *mut u8 { | ||
let key = key as *mut *mut u8; | ||
*key | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn destroy(_key: Key) {} | ||
|
||
#[inline] | ||
pub fn requires_synchronized_create() -> bool { | ||
false | ||
} |
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
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