Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the Arbitrary fuzzing trait for contract value-types #715

Merged
merged 4 commits into from Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion soroban-env-common/Cargo.toml
Expand Up @@ -20,12 +20,13 @@ wasmi = { workspace = true, optional = true }
serde = { version = "1.0.0", features = ["derive"], optional = true }
static_assertions = "1.1.0"
ethnum = "1.3.2"
arbitrary = { version = "1.1.3", features = ["derive"], optional = true }

[features]
std = ["stellar-xdr/std", "stellar-xdr/base64"]
serde = ["dep:serde", "stellar-xdr/serde"]
vm = ["wasmi"]
testutils = []
testutils = ["dep:arbitrary", "stellar-xdr/arbitrary"]

[package.metadata.docs.rs]
all-features = true
18 changes: 18 additions & 0 deletions soroban-env-common/src/arbitrary.rs
@@ -0,0 +1,18 @@
//! Implementations of [`Arbitrary`] for contract types.

#![cfg(feature = "testutils")]

extern crate alloc;

use crate::xdr::ScStatus;
use crate::Status;
use arbitrary::{Arbitrary, Unstructured};

impl<'a> Arbitrary<'a> for Status {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let scstatus = ScStatus::arbitrary(u)?;
let status = Status::from(scstatus);

Ok(status)
}
}
1 change: 1 addition & 0 deletions soroban-env-common/src/lib.rs
Expand Up @@ -36,6 +36,7 @@ pub const VERSION: Version = Version {

mod wrapper_macros;

mod arbitrary;
mod bytes;
mod compare;
mod convert;
Expand Down
15 changes: 5 additions & 10 deletions soroban-env-host/src/host.rs
Expand Up @@ -3040,16 +3040,11 @@ impl VmCallerEnv for Host {
}

#[cfg(any(test, feature = "testutils"))]
mod testutils {
use crate::RawVal;
use std::any::Any;
pub(crate) mod testutils {
use std::cell::Cell;
use std::panic::UnwindSafe;
use std::panic::{catch_unwind, set_hook, take_hook};
use std::panic::{catch_unwind, set_hook, take_hook, UnwindSafe};
use std::sync::Once;

type PanicVal = Box<dyn Any + Send>;

/// Catch panics while suppressing the default panic hook that prints to the
/// console.
///
Expand All @@ -3062,9 +3057,9 @@ mod testutils {
/// hook. It then uses a thread local variable to track contract call depth.
/// If a panick occurs during a contract call the original hook is not
/// called, otherwise it is called.
pub fn call_with_suppressed_panic_hook<C>(closure: C) -> Result<Option<RawVal>, PanicVal>
pub fn call_with_suppressed_panic_hook<C, R>(closure: C) -> std::thread::Result<R>
where
C: FnOnce() -> Option<RawVal> + UnwindSafe,
C: FnOnce() -> R + UnwindSafe,
{
thread_local! {
static TEST_CONTRACT_CALL_COUNT: Cell<u64> = Cell::new(0);
Expand All @@ -3088,7 +3083,7 @@ mod testutils {
c.set(new_count);
});

let res: Result<Option<RawVal>, PanicVal> = catch_unwind(closure);
let res = catch_unwind(closure);

TEST_CONTRACT_CALL_COUNT.with(|c| {
let old_count = c.get();
Expand Down
3 changes: 3 additions & 0 deletions soroban-env-host/src/lib.rs
Expand Up @@ -46,6 +46,9 @@ pub mod storage;
#[cfg(test)]
mod test;

#[cfg(any(test, feature = "testutils"))]
#[doc(hidden)]
pub use host::testutils::call_with_suppressed_panic_hook;
#[cfg(any(test, feature = "testutils"))]
pub use host::ContractFunctionSet;
pub use host::{
Expand Down