Skip to content

Commit

Permalink
Merge pull request #72 from 0xPolygonHermez/edu/fix_cli
Browse files Browse the repository at this point in the history
fix ziskemu stats and add cargo-zisk build commnad
  • Loading branch information
eduadiez authored Sep 4, 2024
2 parents 26f94e8 + 6c36f73 commit c001e69
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 58 additions & 6 deletions cli/src/bin/cargo-zisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const ZISK_TARGET: &str = "riscv64ima-polygon-ziskos-elf";
pub enum Cargo {
Sdk(ZiskSdk),
Run(ZiskRun),
Build(ZiskBuild),
}

// Structure representing the 'sdk' subcommand of cargo.
Expand All @@ -45,8 +46,8 @@ pub struct ZiskRun {
#[clap(long)]
no_default_features: bool,
#[clap(long, short)]
sim: bool,
#[clap(long)]
emu: bool,
#[clap(long, short)]
stats: bool,
#[clap(long)]
gdb: bool,
Expand All @@ -58,6 +59,20 @@ pub struct ZiskRun {
args: Vec<String>,
}

// Structure representing the 'build' subcommand of cargo.
#[derive(clap::Args)]
#[command(author, about, long_about = None, version = ZISK_VERSION_MESSAGE)]
pub struct ZiskBuild {
#[clap(long, short = 'F')]
features: Option<String>,
#[clap(long)]
all_features: bool,
#[clap(long)]
release: bool,
#[clap(long)]
no_default_features: bool,
}

// Enum defining the available subcommands for `ZiskSdk`.
#[derive(Subcommand)]
pub enum ZiskSdkCommands {
Expand All @@ -66,6 +81,40 @@ pub enum ZiskSdkCommands {
New(NewCmd),
}

impl ZiskBuild {
fn run(&self) -> Result<()> {
// Construct the cargo run command
let mut command = Command::new("cargo");
command.args(["+zisk", "build"]);
// Add the feature selection flags
if let Some(features) = &self.features {
command.arg("--features").arg(features);
}
if self.all_features {
command.arg("--all-features");
}
if self.no_default_features {
command.arg("--no-default-features");
}
if self.release {
command.arg("--release");
}

command.args(["--target", ZISK_TARGET]);

// Set up the command to inherit the parent's stdout and stderr
command.stdout(Stdio::inherit());
command.stderr(Stdio::inherit());

// Execute the command
let status = command.status().context("Failed to execute cargo run command")?;
if !status.success() {
return Err(anyhow!("Cargo run command failed with status {}", status));
}

Ok(())
}
}
// Implement the run functionality for ZiskRun
impl ZiskRun {
fn run(&self) -> Result<()> {
Expand All @@ -87,19 +136,19 @@ impl ZiskRun {
if self.release {
command.arg("--release");
}
if self.sim {
if self.emu {
let mut extra_command: String = "".to_string();
let mut input_command: String = "".to_string();
if self.stats {
extra_command += " -s ";
extra_command += " -x ";
}
if self.metrics {
extra_command += " -m ";
}
if self.input.is_some() {
let path = Path::new(self.input.as_ref().unwrap());
if !path.exists() {
return Err(anyhow!("Input file does not exist at path: {}", path.display()));
return Err(anyhow!("Input file does not exist at path: {}, please try to run a cargo-zisk build or cargo build before", path.display()));
}
input_command = format!("-i {}", self.input.as_ref().unwrap());
}
Expand All @@ -113,7 +162,7 @@ impl ZiskRun {
let input_path: &Path = Path::new(self.input.as_ref().unwrap());

if !input_path.exists() {
return Err(anyhow!("Input file does not exist at path: {}", input_path.display()));
return Err(anyhow!("Input file does not exist at path: {}, please try to run a cargo-zisk build or cargo build before", input_path.display()));
}

let build_path = match input_path.parent() {
Expand Down Expand Up @@ -198,6 +247,9 @@ fn main() -> Result<()> {
Cargo::Run(args) => {
args.run().context("Error executing Run command")?;
}
Cargo::Build(args) => {
args.run().context("Error executing Run command")?;
}
}

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions ziskos/entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lazy_static = "1.5.0"
rand = "0.8.5"
getrandom = { version = "0.2", features = ["custom"] }
34 changes: 34 additions & 0 deletions ziskos/entrypoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ mod ziskos {
_ARCH_ID_ZISK = const ARCH_ID_ZISK,
options(noreturn) // we must handle "returning" from assembly
);

pub fn zkvm_getrandom(s: &mut [u8]) -> Result<(), getrandom::Error> {
unsafe {
sys_rand(s.as_mut_ptr(), s.len());
}

Ok(())
}

getrandom::register_custom_getrandom!(zkvm_getrandom);
}

#[no_mangle]
Expand Down Expand Up @@ -159,6 +169,30 @@ mod ziskos {
}
}
}
use lazy_static::lazy_static;
use std::sync::Mutex;
const PRNG_SEED: u64 = 0x123456789abcdef0;
use rand::{rngs::StdRng, Rng, SeedableRng};

lazy_static! {
/// A lazy static to generate a global random number generator.
static ref RNG: Mutex<StdRng> = Mutex::new(StdRng::seed_from_u64(PRNG_SEED));
}

/// A lazy static to print a warning once for using the `sys_rand` system call.
static SYS_RAND_WARNING: std::sync::Once = std::sync::Once::new();

#[no_mangle]
unsafe extern "C" fn sys_rand(recv_buf: *mut u8, words: usize) {
SYS_RAND_WARNING.call_once(|| {
println!("WARNING: Using insecure random number generator.");
});
let mut rng = RNG.lock().unwrap();
for i in 0..words {
let element = recv_buf.add(i);
*element = rng.gen();
}
}

#[no_mangle]
extern "C" fn sys_getenv() {
Expand Down

0 comments on commit c001e69

Please sign in to comment.