diff --git a/.vim/coc-settings.json b/.vim/coc-settings.json index 718401e7..a9d2f4fd 100644 --- a/.vim/coc-settings.json +++ b/.vim/coc-settings.json @@ -3,6 +3,7 @@ "Cargo.toml", "benchmarks/Cargo.toml", "examples/Cargo.toml", + "risc0/cargo-risczero/tests/test_crate", "risc0/zkvm/methods/guest/Cargo.toml", "risc0/zkvm/methods/std/Cargo.toml", "tools/crates-validator/Cargo.toml" diff --git a/.vscode/settings.json b/.vscode/settings.json index 21024c5a..ba36f198 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -62,6 +62,7 @@ "Cargo.toml", "benchmarks/Cargo.toml", "examples/Cargo.toml", + "risc0/cargo-risczero/tests/test_crate", "risc0/zkvm/methods/guest/Cargo.toml", "risc0/zkvm/methods/std/Cargo.toml", "tools/crates-validator/Cargo.toml" diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f769cb1..196ab5ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,17 @@ ## Next (upcoming release) -TBD +### Fixes + +### 🚨 Breaking Changes + +* Environment syscalls `sys_getenv`, `sys_argc` and `sys_argv` are disabled by + default, with the option to enable them via a feature flag on the + `risc0-zkvm-platform` crate. In host programs, environment variables and + arguments are generally considered trusted. In contrast, the guest does not + trust the host, which leads to risk that code designed for running on the + host may result in insecure behavior when run in the guest. Disabling + environment variables and args by default mitigates this risk. ## [v1.1.0 (2024-09-09)](https://github.com/risc0/risc0/releases/tag/v1.1.0) diff --git a/examples/xgboost/methods/guest/Cargo.lock b/examples/xgboost/methods/guest/Cargo.lock index 990143bd..e2bddb2b 100644 --- a/examples/xgboost/methods/guest/Cargo.lock +++ b/examples/xgboost/methods/guest/Cargo.lock @@ -1137,6 +1137,7 @@ version = "0.1.0" dependencies = [ "forust-ml", "risc0-zkvm", + "risc0-zkvm-platform", "rmp-serde", ] diff --git a/examples/xgboost/methods/guest/Cargo.toml b/examples/xgboost/methods/guest/Cargo.toml index 0251ab05..8ad03200 100644 --- a/examples/xgboost/methods/guest/Cargo.toml +++ b/examples/xgboost/methods/guest/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" [workspace] [dependencies] -risc0-zkvm = { path = "../../../../risc0/zkvm", default-features = false, features = ["std"] } forust-ml = "0.4.2" +risc0-zkvm = { path = "../../../../risc0/zkvm", default-features = false, features = ["std"] } +# forust-ml uses rayon which expects to read RAYON_NUM_THREADS from the environment, so sys-getenv is enabled. +risc0-zkvm-platform = { path = "../../../../risc0/zkvm/platform", default-features = false, features = ["sys-getenv"] } rmp-serde = "1.1.2" diff --git a/license-check.py b/license-check.py index 5b7eb38c..dcfdaf4a 100755 --- a/license-check.py +++ b/license-check.py @@ -32,6 +32,7 @@ SKIP_DIRS = [ str(Path.cwd()) + "/risc0/sys/cxx/vendor", str(Path.cwd()) + "/risc0/zkvm/src/host/protos", str(Path.cwd()) + "/risc0/zkvm/src/host/server/exec", + str(Path.cwd()) + "/risc0/cargo-risczero/tests/test_crate", ] diff --git a/risc0/build/src/docker.rs b/risc0/build/src/docker.rs index ee34147b..2bacea86 100644 --- a/risc0/build/src/docker.rs +++ b/risc0/build/src/docker.rs @@ -265,7 +265,7 @@ mod test { build("../../risc0/zkvm/methods/guest/Cargo.toml"); compare_image_id( "risc0_zkvm_methods_guest/hello_commit", - "f560fae4def57a6ac327b39817fd40a37fc5699cceb4a94ba75591933be53235", + "5e95ee67893f25399250181749cf26baab984b896c9ce04ac6e7cdf367e250f6", ); } } diff --git a/risc0/build/src/lib.rs b/risc0/build/src/lib.rs index 4284f7e7..7d20840a 100644 --- a/risc0/build/src/lib.rs +++ b/risc0/build/src/lib.rs @@ -196,7 +196,7 @@ impl GuestBuilder for GuestListEntry { let image_id = match r0vm_image_id(elf_path) { Ok(image_id) => image_id, Err(err) => { - tty_println(&format!("{err}")); + tty_println(&format!("failed to get image ID using r0vm: {err}")); compute_image_id(&elf)? } }; @@ -486,12 +486,19 @@ fn cpp_toolchain_override() -> bool { /// Builds a static library providing a rust runtime. /// -/// This can be used to build programs for the zkvm which don't depend on -/// risc0_zkvm. +/// This can be used to build programs for the zkvm which don't depend on risc0_zkvm. pub fn build_rust_runtime() -> String { + build_rust_runtime_with_features(&[]) +} + +/// Builds a static library providing a rust runtime, with additional features given as arguments. +/// +/// This can be used to build programs for the zkvm which don't depend on risc0_zkvm. Feature flags +/// given will be pass when building risc0-zkvm-platform. +pub fn build_rust_runtime_with_features(features: &[&str]) -> String { build_staticlib( "risc0-zkvm-platform", - &["rust-runtime", "panic-handler", "entrypoint", "getrandom"], + &[&["rust-runtime", "panic-handler", "entrypoint"], features].concat(), ) } diff --git a/risc0/cargo-risczero/build.rs b/risc0/cargo-risczero/build.rs index 5660f15e..c1a02a8b 100644 --- a/risc0/cargo-risczero/build.rs +++ b/risc0/cargo-risczero/build.rs @@ -16,16 +16,17 @@ mod runtime { use std::{env, fs, io, path::Path}; - use risc0_build::build_rust_runtime; + use risc0_build::build_rust_runtime_with_features; use zip::{write::SimpleFileOptions, CompressionMethod, ZipWriter}; - pub fn build_and_zip_runtime() { - // Build the risc0-zkvm-platform.a file and place it in a zip archive for - // inclusion in the cargo-risczero binary. + pub(crate) fn build_and_zip_test_runtime() { + // Build the risc0-zkvm-platform.a file and place it in a zip archive for inclusion in the + // cargo-risczero binary. This is used to build test binaries for cargo risczero test let out_dir_env = env::var_os("OUT_DIR").unwrap(); let out_dir = Path::new(&out_dir_env); // $ROOT/target/$profile/build/$crate/out - let rust_runtime = build_rust_runtime(); + let rust_runtime = + build_rust_runtime_with_features(&["getrandom", "sys-getenv", "sys-args"]); let f = fs::File::create(out_dir.join("cargo-risczero.zip")).unwrap(); let mut zip = ZipWriter::new(f); let options = SimpleFileOptions::default().compression_method(CompressionMethod::Stored); @@ -43,6 +44,6 @@ fn main() { tracing_subscriber::fmt() .with_env_filter(tracing_subscriber::filter::EnvFilter::from_default_env()) .init(); - runtime::build_and_zip_runtime(); + runtime::build_and_zip_test_runtime(); } } diff --git a/risc0/cargo-risczero/src/commands/build.rs b/risc0/cargo-risczero/src/commands/build.rs index 0d76841f..cd1ac016 100644 --- a/risc0/cargo-risczero/src/commands/build.rs +++ b/risc0/cargo-risczero/src/commands/build.rs @@ -198,12 +198,19 @@ impl BuildCommand { for test in tests { eprintln!("Running test in guest: {test} {test_args:?}"); - let env = ExecutorEnv::builder() + let mut builder = ExecutorEnv::builder(); + builder // Add the test elf path as arg 0, the POSIX program name .args(&[test.clone()]) .args(&test_args) - .env_var("RUST_TEST_NOCAPTURE", "1") - .build()?; + .env_var("RUST_TEST_NOCAPTURE", "1"); + + // Forward all environment variables set on this process. + for (key, val) in std::env::vars().into_iter() { + builder.env_var(&key, &val); + } + + let env = builder.build()?; let exec = default_executor(); let session = exec.execute(env, &fs::read(test)?)?; diff --git a/risc0/cargo-risczero/tests/cargo_risczero_test.rs b/risc0/cargo-risczero/tests/cargo_risczero_test.rs new file mode 100644 index 00000000..bbfda92b --- /dev/null +++ b/risc0/cargo-risczero/tests/cargo_risczero_test.rs @@ -0,0 +1,40 @@ +// Copyright 2024 RISC Zero, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![cfg(feature = "experimental")] + +use std::process::Command; + +#[test] +#[ignore = "failing as of oct 18 with with cargo 1.79"] +fn basic_usage() { + let exe_path = env!("CARGO_BIN_EXE_cargo-risczero"); + let args = ["risczero", "test"]; + println!("{} {:?}", exe_path, args); + let output = Command::new(exe_path) + .env_clear() + .env("PATH", std::env::var("PATH").unwrap()) + .env("RUST_TEST_THREADS", "1") + .current_dir("./tests/test_crate") + .args(args) + .output() + .unwrap(); + + println!("{:#?}", &output); + + assert!( + output.status.success(), + "cargo risczero test command failed" + ); +} diff --git a/risc0/cargo-risczero/tests/test_crate/.gitignore b/risc0/cargo-risczero/tests/test_crate/.gitignore new file mode 100644 index 00000000..491f183a --- /dev/null +++ b/risc0/cargo-risczero/tests/test_crate/.gitignore @@ -0,0 +1,2 @@ +./target/ +!Cargo.lock diff --git a/risc0/cargo-risczero/tests/test_crate/Cargo.lock b/risc0/cargo-risczero/tests/test_crate/Cargo.lock new file mode 100644 index 00000000..6e10003b --- /dev/null +++ b/risc0/cargo-risczero/tests/test_crate/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cargo-risczero-test-crate" +version = "0.1.0" diff --git a/risc0/cargo-risczero/tests/test_crate/Cargo.toml b/risc0/cargo-risczero/tests/test_crate/Cargo.toml new file mode 100644 index 00000000..b1dabe6e --- /dev/null +++ b/risc0/cargo-risczero/tests/test_crate/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "cargo-risczero-test-crate" +version = "0.1.0" +edition = "2021" + +[workspace] + +[dependencies] diff --git a/risc0/cargo-risczero/tests/test_crate/src/lib.rs b/risc0/cargo-risczero/tests/test_crate/src/lib.rs new file mode 100644 index 00000000..7d12d9af --- /dev/null +++ b/risc0/cargo-risczero/tests/test_crate/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/risc0/zkvm/methods/Cargo.toml b/risc0/zkvm/methods/Cargo.toml index 27b3b86b..9ee803e3 100644 --- a/risc0/zkvm/methods/Cargo.toml +++ b/risc0/zkvm/methods/Cargo.toml @@ -11,7 +11,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } release = false [package.metadata.risc0] -methods = ["cfg", "guest", "heap", "rand", "std", "cpp-crates"] +methods = ["cfg", "guest", "heap", "rand", "std", "cpp-crates", "env"] [dependencies] bincode = { version = "1.3", optional = true } diff --git a/risc0/zkvm/methods/env/.gitignore b/risc0/zkvm/methods/env/.gitignore new file mode 100644 index 00000000..9da4a887 --- /dev/null +++ b/risc0/zkvm/methods/env/.gitignore @@ -0,0 +1 @@ +!Cargo.lock diff --git a/risc0/zkvm/methods/env/Cargo.lock b/risc0/zkvm/methods/env/Cargo.lock new file mode 100644 index 00000000..94cd2b71 --- /dev/null +++ b/risc0/zkvm/methods/env/Cargo.lock @@ -0,0 +1,1045 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "ark-bn254" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a22f4561524cd949590d78d7d4c5df8f592430d221f7f3c9497bbafd8972120f" +dependencies = [ + "ark-ec", + "ark-ff", + "ark-std", +] + +[[package]] +name = "ark-crypto-primitives" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3a13b34da09176a8baba701233fdffbaa7c1b1192ce031a3da4e55ce1f1a56" +dependencies = [ + "ark-ec", + "ark-ff", + "ark-relations", + "ark-serialize", + "ark-snark", + "ark-std", + "blake2", + "derivative", + "digest", + "sha2", +] + +[[package]] +name = "ark-ec" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defd9a439d56ac24968cca0571f598a61bc8c55f71d50a89cda591cb750670ba" +dependencies = [ + "ark-ff", + "ark-poly", + "ark-serialize", + "ark-std", + "derivative", + "hashbrown 0.13.2", + "itertools", + "num-traits", + "zeroize", +] + +[[package]] +name = "ark-ff" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" +dependencies = [ + "ark-ff-asm", + "ark-ff-macros", + "ark-serialize", + "ark-std", + "derivative", + "digest", + "itertools", + "num-bigint", + "num-traits", + "paste", + "rustc_version", + "zeroize", +] + +[[package]] +name = "ark-ff-asm" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-ff-macros" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abe79b0e4288889c4574159ab790824d0033b9fdcb2a112a3182fac2e514565" +dependencies = [ + "num-bigint", + "num-traits", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-groth16" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20ceafa83848c3e390f1cbf124bc3193b3e639b3f02009e0e290809a501b95fc" +dependencies = [ + "ark-crypto-primitives", + "ark-ec", + "ark-ff", + "ark-poly", + "ark-relations", + "ark-serialize", + "ark-std", +] + +[[package]] +name = "ark-poly" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d320bfc44ee185d899ccbadfa8bc31aab923ce1558716e1997a1e74057fe86bf" +dependencies = [ + "ark-ff", + "ark-serialize", + "ark-std", + "derivative", + "hashbrown 0.13.2", +] + +[[package]] +name = "ark-relations" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00796b6efc05a3f48225e59cb6a2cda78881e7c390872d5786aaf112f31fb4f0" +dependencies = [ + "ark-ff", + "ark-std", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "ark-serialize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" +dependencies = [ + "ark-serialize-derive", + "ark-std", + "digest", + "num-bigint", +] + +[[package]] +name = "ark-serialize-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae3281bc6d0fd7e549af32b52511e1302185bd688fd3359fa36423346ff682ea" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-snark" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84d3cc6833a335bb8a600241889ead68ee89a3cf8448081fb7694c0fe503da63" +dependencies = [ + "ark-ff", + "ark-relations", + "ark-serialize", + "ark-std", +] + +[[package]] +name = "ark-std" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" +dependencies = [ + "num-traits", + "rand", +] + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest", +] + +[[package]] +name = "block" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "borsh" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6362ed55def622cddc70a4746a68554d7b687713770de539e59a739b249f8ed" +dependencies = [ + "borsh-derive", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3ef8005764f53cd4dca619f5bf64cafd4664dada50ece25e4d81de54c80cc0b" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.66", + "syn_derive", +] + +[[package]] +name = "bytemuck" +version = "1.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b236fc92302c97ed75b38da1f4917b5cdda4984745740f153a5d3059e48d725e" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ee891b04274a59bd38b412188e24b849617b2e45a0fd8d057deb63e7403761b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + +[[package]] +name = "core-graphics-types" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "libc", +] + +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "const-oid", + "crypto-common", + "subtle", +] + +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + +[[package]] +name = "either" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" + +[[package]] +name = "elf" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "foreign-types-shared" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + +[[package]] +name = "indexmap" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0" +dependencies = [ + "equivalent", + "hashbrown 0.14.5", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "malloc_buf" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" +dependencies = [ + "libc", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "metal" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ecfd3296f8c56b7c1f6fbac3c71cefa9d78ce009850c45000015f206dc7fa21" +dependencies = [ + "bitflags 2.6.0", + "block", + "core-graphics-types", + "foreign-types", + "log", + "objc", + "paste", +] + +[[package]] +name = "num-bigint" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "objc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" +dependencies = [ + "malloc_buf", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro-crate" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "risc0-binfmt" +version = "1.2.0-alpha.1" +dependencies = [ + "anyhow", + "borsh", + "elf", + "risc0-zkp", + "risc0-zkvm-platform", + "serde", + "tracing", +] + +[[package]] +name = "risc0-circuit-recursion" +version = "1.2.0-alpha.1" +dependencies = [ + "anyhow", + "bytemuck", + "hex", + "metal", + "risc0-core", + "risc0-zkp", + "tracing", +] + +[[package]] +name = "risc0-circuit-rv32im" +version = "1.2.0-alpha.1" +dependencies = [ + "anyhow", + "metal", + "risc0-binfmt", + "risc0-core", + "risc0-zkp", + "risc0-zkvm-platform", + "serde", + "tracing", +] + +[[package]] +name = "risc0-core" +version = "1.2.0-alpha.1" +dependencies = [ + "bytemuck", + "rand_core", +] + +[[package]] +name = "risc0-groth16" +version = "1.2.0-alpha.1" +dependencies = [ + "anyhow", + "ark-bn254", + "ark-ec", + "ark-groth16", + "ark-serialize", + "bytemuck", + "hex", + "num-bigint", + "num-traits", + "risc0-binfmt", + "risc0-zkp", + "serde", + "stability", +] + +[[package]] +name = "risc0-zkp" +version = "1.2.0-alpha.1" +dependencies = [ + "anyhow", + "blake2", + "borsh", + "bytemuck", + "cfg-if", + "digest", + "hex", + "hex-literal", + "metal", + "paste", + "rand_core", + "risc0-core", + "risc0-zkvm-platform", + "serde", + "sha2", + "tracing", +] + +[[package]] +name = "risc0-zkvm" +version = "1.2.0-alpha.1" +dependencies = [ + "anyhow", + "borsh", + "bytemuck", + "getrandom", + "hex", + "risc0-binfmt", + "risc0-circuit-recursion", + "risc0-circuit-rv32im", + "risc0-core", + "risc0-groth16", + "risc0-zkp", + "risc0-zkvm-platform", + "rrs-lib", + "semver", + "serde", + "sha2", + "stability", + "tracing", +] + +[[package]] +name = "risc0-zkvm-methods-sys-env" +version = "0.1.0" +dependencies = [ + "risc0-zkvm", +] + +[[package]] +name = "risc0-zkvm-platform" +version = "1.2.0-alpha.1" +dependencies = [ + "bytemuck", + "getrandom", + "libm", + "stability", +] + +[[package]] +name = "rrs-lib" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4382d3af3a4ebdae7f64ba6edd9114fff92c89808004c4943b393377a25d001" +dependencies = [ + "downcast-rs", + "paste", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "serde" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "stability" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d904e7009df136af5297832a3ace3370cd14ff1546a232f4f185036c2736fcac" +dependencies = [ + "quote", + "syn 2.0.66", +] + +[[package]] +name = "subtle" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d0208408ba0c3df17ed26eb06992cb1a1268d41b2c0e12e65203fbe3972cee5" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" + +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-subscriber" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" +dependencies = [ + "tracing-core", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] + +[[package]] +name = "zerocopy" +version = "0.7.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae87e3fcd617500e5d106f0380cf7b77f3c6092aae37191433159dda23cfb087" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] diff --git a/risc0/zkvm/methods/env/Cargo.toml b/risc0/zkvm/methods/env/Cargo.toml new file mode 100644 index 00000000..1c53db8a --- /dev/null +++ b/risc0/zkvm/methods/env/Cargo.toml @@ -0,0 +1,28 @@ +[workspace] + +# Without resolver = "2", it seems that sometimes features get enabled +# in the guest based on features required by build dependencies. If +# resolver = "2" causes other problems, this may need to be +# investigated further. +resolver = "2" + +[package] +name = "risc0-zkvm-methods-sys-env" +version = "0.1.0" +edition = "2021" + +[dependencies] +# We don't enable getrandom, sys-getenv, or sys-args here. +risc0-zkvm = { path = "../..", default-features = false, features = ["std"] } + +[profile.release] +lto = "thin" +opt-level = 3 +debug = 1 + +[profile.release.package.risc0-zkvm-methods-sys-env] +# Include debug symbols so we can test the profiler. +debug = 1 + +[package.metadata.release] +release = false diff --git a/risc0/zkvm/methods/env/src/bin/sys_args.rs b/risc0/zkvm/methods/env/src/bin/sys_args.rs new file mode 100644 index 00000000..f430f570 --- /dev/null +++ b/risc0/zkvm/methods/env/src/bin/sys_args.rs @@ -0,0 +1,22 @@ +// Copyright 2024 RISC Zero, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![no_main] + +risc0_zkvm::guest::entry!(main); + +fn main() { + // Should panic, as sys_argc and sys_argv are disabled at build-time. + std::env::args().len(); +} diff --git a/risc0/zkvm/methods/env/src/bin/sys_env.rs b/risc0/zkvm/methods/env/src/bin/sys_env.rs new file mode 100644 index 00000000..43d40a35 --- /dev/null +++ b/risc0/zkvm/methods/env/src/bin/sys_env.rs @@ -0,0 +1,22 @@ +// Copyright 2024 RISC Zero, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![no_main] + +risc0_zkvm::guest::entry!(main); + +fn main() { + // Should panic, as sys_getenv is disabled at build-time. + std::env::var("FOO").ok(); +} diff --git a/risc0/zkvm/methods/std/Cargo.lock b/risc0/zkvm/methods/std/Cargo.lock index e0bb85b4..0e00ff73 100644 --- a/risc0/zkvm/methods/std/Cargo.lock +++ b/risc0/zkvm/methods/std/Cargo.lock @@ -1143,6 +1143,7 @@ version = "0.1.0" dependencies = [ "risc0-zkvm", "risc0-zkvm-methods", + "risc0-zkvm-platform", ] [[package]] diff --git a/risc0/zkvm/methods/std/Cargo.toml b/risc0/zkvm/methods/std/Cargo.toml index b351d5cb..4a2bfbd3 100644 --- a/risc0/zkvm/methods/std/Cargo.toml +++ b/risc0/zkvm/methods/std/Cargo.toml @@ -17,6 +17,7 @@ risc0-zkvm = { path = "../..", default-features = false, features = [ "getrandom", ] } risc0-zkvm-methods = { path = "..", features = ["std"] } +risc0-zkvm-platform = { path = "../../platform", default-features = false, features = ["sys-getenv", "sys-args"] } [profile.release] lto = true diff --git a/risc0/zkvm/platform/Cargo.toml b/risc0/zkvm/platform/Cargo.toml index 9de1a340..7a50cf5e 100644 --- a/risc0/zkvm/platform/Cargo.toml +++ b/risc0/zkvm/platform/Cargo.toml @@ -51,4 +51,8 @@ heap-embedded-alloc = [ panic-handler = [] # Build a rust runtime rust-runtime = ["export-libm", "export-syscalls"] +# Enable the sys_getenv syscall. +sys-getenv = [] +# Enable the sys_argc and sys_argv syscalls. +sys-args = [] unstable = [] diff --git a/risc0/zkvm/platform/src/syscall.rs b/risc0/zkvm/platform/src/syscall.rs index c792de19..2f883e3a 100644 --- a/risc0/zkvm/platform/src/syscall.rs +++ b/risc0/zkvm/platform/src/syscall.rs @@ -14,7 +14,7 @@ #[cfg(target_os = "zkvm")] use core::arch::asm; -use core::{cmp::min, ffi::CStr, ptr::null_mut, str::Utf8Error}; +use core::{cmp::min, ffi::CStr, ptr::null_mut, slice, str::Utf8Error}; use crate::WORD_SIZE; @@ -615,6 +615,11 @@ pub unsafe extern "C" fn sys_write(fd: u32, write_ptr: *const u8, nbytes: usize) } } +// Some environment variable names are considered safe by default to use in the guest, provided by +// the host, and are included in this list. It may be useful to allow guest developers to register +// additional variable names as part of their guest program. +const ALLOWED_ENV_VARNAMES: &[&[u8]] = &[b"RUST_BACKTRACE"]; + /// Retrieves the value of an environment variable, and stores as much /// of it as it can it in the memory at [out_words, out_words + /// out_nwords). @@ -639,6 +644,23 @@ pub unsafe extern "C" fn sys_getenv( varname: *const u8, varname_len: usize, ) -> usize { + if cfg!(not(feature = "sys-getenv")) { + let mut allowed = false; + for allowed_varname in ALLOWED_ENV_VARNAMES { + let varname_buf = unsafe { slice::from_raw_parts(varname, varname_len) }; + if *allowed_varname == varname_buf { + allowed = true; + break; + } + } + if !allowed { + const MSG_1: &[u8] = "sys_getenv not enabaled for var".as_bytes(); + unsafe { sys_log(MSG_1.as_ptr(), MSG_1.len()) }; + unsafe { sys_log(varname, varname_len) }; + const MSG_2: &[u8] = "sys_getenv is disabled; can be enabled with the sys-getenv feature flag on risc0-zkvm-platform".as_bytes(); + unsafe { sys_panic(MSG_2.as_ptr(), MSG_2.len()) }; + } + } let Return(a0, _) = syscall_2( nr::SYS_GETENV, out_words, @@ -659,6 +681,10 @@ pub unsafe extern "C" fn sys_getenv( /// data being returned. Returned data is entirely in the control of the host. #[cfg_attr(feature = "export-syscalls", no_mangle)] pub extern "C" fn sys_argc() -> usize { + if cfg!(not(feature = "sys-args")) { + const MSG: &[u8] = "sys_argc is disabled; can be enabled with the sys-args feature flag on risc0-zkvm-platform".as_bytes(); + unsafe { sys_panic(MSG.as_ptr(), MSG.len()) }; + } let Return(a0, _) = unsafe { syscall_0(nr::SYS_ARGC, null_mut(), 0) }; a0 as usize } @@ -686,6 +712,10 @@ pub unsafe extern "C" fn sys_argv( out_nwords: usize, arg_index: usize, ) -> usize { + if cfg!(not(feature = "sys-args")) { + const MSG: &[u8] = "sys_argv is disabled; can be enabled with the sys-args feature flag on risc0-zkvm-platform".as_bytes(); + unsafe { sys_panic(MSG.as_ptr(), MSG.len()) }; + } let Return(a0, _) = syscall_1(nr::SYS_ARGV, out_words, out_nwords, arg_index as u32); a0 as usize } diff --git a/risc0/zkvm/src/host/server/exec/tests.rs b/risc0/zkvm/src/host/server/exec/tests.rs index 184f40ce..02387e55 100644 --- a/risc0/zkvm/src/host/server/exec/tests.rs +++ b/risc0/zkvm/src/host/server/exec/tests.rs @@ -25,7 +25,7 @@ use risc0_binfmt::{MemoryImage, Program}; use risc0_zkvm_methods::{ multi_test::{MultiTestSpec, SYS_MULTI_TEST, SYS_MULTI_TEST_WORDS}, BLST_ELF, HEAP_ELF, HELLO_COMMIT_ELF, MULTI_TEST_ELF, RAND_ELF, SLICE_IO_ELF, STANDARD_LIB_ELF, - ZKVM_527_ELF, + SYS_ARGS_ELF, SYS_ENV_ELF, ZKVM_527_ELF, }; use risc0_zkvm_platform::{fileno, syscall::nr::SYS_RANDOM, PAGE_SIZE, WORD_SIZE}; use sha2::{Digest as _, Sha256}; @@ -875,6 +875,26 @@ fn getrandom_panic() { .unwrap(); } +#[test] +#[should_panic(expected = "Guest panicked: sys_getenv is disabled")] +fn sys_getenv_panic() { + let env = ExecutorEnv::builder().build().unwrap(); + let _session = ExecutorImpl::from_elf(env, SYS_ENV_ELF) + .unwrap() + .run() + .unwrap(); +} + +#[test] +#[should_panic(expected = "Guest panicked: sys_argc is disabled")] +fn sys_args_panic() { + let env = ExecutorEnv::builder().build().unwrap(); + let _session = ExecutorImpl::from_elf(env, SYS_ARGS_ELF) + .unwrap() + .run() + .unwrap(); +} + #[test] fn slice_io() { let run = |slice: &[u8]| {