Skip to content

Commit

Permalink
Make DRT compatible to arbitrary 1.4 (#467)
Browse files Browse the repository at this point in the history
Signed-off-by: Shaobo He <[email protected]>
  • Loading branch information
shaobo-he-aws authored Nov 6, 2024
1 parent 4748ff0 commit a5e8b1e
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 78 deletions.
12 changes: 7 additions & 5 deletions cedar-drt/fuzz/fuzz_targets/abac-type-directed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use cedar_policy_generators::{
schema::Schema,
settings::ABACSettings,
};
use libfuzzer_sys::arbitrary::{self, Arbitrary, Unstructured};
use libfuzzer_sys::arbitrary::{self, Arbitrary, MaxRecursionReached, Unstructured};
use log::{debug, info};
use serde::Serialize;
use std::convert::TryFrom;
Expand Down Expand Up @@ -91,9 +91,11 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> std::result::Result<(usize, Option<usize>), MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
Schema::arbitrary_request_size_hint(depth),
Expand All @@ -104,7 +106,7 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
])
]))
}
}

Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/abac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
Schema::arbitrary_request_size_hint(depth),
Expand All @@ -103,7 +105,7 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
])
]))
}
}

Expand Down
4 changes: 3 additions & 1 deletion cedar-drt/fuzz/fuzz_targets/common-type-resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ impl<'a> Arbitrary<'a> for Input {
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Schema::arbitrary_size_hint(depth)
}
}
Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/entity-validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Ok(Self { schema, hierarchy })
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
])
]))
}
}

Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/eval-type-directed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
Schema::arbitrary_request_size_hint(depth),
Expand All @@ -101,7 +103,7 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
])
]))
}
}

Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Ok(Self { policy, seed })
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
<u64 as Arbitrary>::size_hint(depth),
])
]))
}
}

Expand Down
4 changes: 3 additions & 1 deletion cedar-drt/fuzz/fuzz_targets/json-schema-roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ impl<'a> Arbitrary<'a> for Input {
Ok(Self { schema })
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Schema::arbitrary_size_hint(depth)
}
}
Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/partial-eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
Schema::arbitrary_request_size_hint(depth),
Expand All @@ -111,7 +113,7 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
])
]))
}
}
// The main fuzz target. This is for type-directed fuzzing of ABAC
Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/request-validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Expand All @@ -90,7 +92,7 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
])
]))
}
}

Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Ok(Self { policy })
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
])
]))
}
}

Expand Down
4 changes: 3 additions & 1 deletion cedar-drt/fuzz/fuzz_targets/schema-roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ impl<'a> Arbitrary<'a> for Input {
Ok(Self { schema })
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Schema::arbitrary_size_hint(depth)
}
}
Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/validation-drt-type-directed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Ok(Self { schema, policy })
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
])
]))
}
}

Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/validation-drt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Ok(Self { schema, policy })
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
])
]))
}
}

Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/validation-pbt-type-directed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
Schema::arbitrary_request_size_hint(depth),
Expand All @@ -102,7 +104,7 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
])
]))
}
}

Expand Down
10 changes: 6 additions & 4 deletions cedar-drt/fuzz/fuzz_targets/validation-pbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,11 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth),
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and_all(&[
Schema::arbitrary_size_hint(depth)?,
HierarchyGenerator::size_hint(depth),
Schema::arbitrary_policy_size_hint(&SETTINGS, depth),
Schema::arbitrary_request_size_hint(depth),
Expand All @@ -301,7 +303,7 @@ impl<'a> Arbitrary<'a> for FuzzTargetInput {
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
Schema::arbitrary_request_size_hint(depth),
])
]))
}
}

Expand Down
4 changes: 1 addition & 3 deletions cedar-policy-generators/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ version = "4.0.0"
publish = false

[dependencies]
# We had to fix the version of `arbitrary` as well as `derive_arbitrary` because 1.4.x introduces a breaking change
arbitrary = "=1.3.2"
derive_arbitrary = "=1.3.2"
arbitrary = "1.4"
cedar-policy-core = { path = "../cedar/cedar-policy-core", version = "4.*", features = ["arbitrary"] }
cedar-policy-validator = { path = "../cedar/cedar-policy-validator", version = "4.*", features = ["arbitrary"] }
clap = { version = "4.3.16", features = ["derive"] }
Expand Down
28 changes: 16 additions & 12 deletions cedar-policy-generators/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::schema::{
use crate::settings::ABACSettings;
use crate::size_hint_utils::{size_hint_for_choose, size_hint_for_range, size_hint_for_ratio};
use crate::{accum, gen, gen_inner, uniform};
use arbitrary::{Arbitrary, Unstructured};
use arbitrary::{Arbitrary, MaxRecursionReached, Unstructured};
use cedar_policy_core::ast::{self, UnreservedId};
use cedar_policy_validator::json_schema;
use smol_str::SmolStr;
Expand Down Expand Up @@ -1916,9 +1916,11 @@ impl<'a> ExprGenerator<'a> {

/// size hint for arbitrary_attr_value_for_type()
#[allow(dead_code)]
pub fn generate_attr_value_for_type_size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::recursion_guard(depth, |depth| {
arbitrary::size_hint::and(
pub fn generate_attr_value_for_type_size_hint(
depth: usize,
) -> std::result::Result<(usize, Option<usize>), MaxRecursionReached> {
arbitrary::size_hint::try_recursion_guard(depth, |depth| {
Ok(arbitrary::size_hint::and(
size_hint_for_range(0, 7),
arbitrary::size_hint::or_all(&[
<bool as Arbitrary>::size_hint(depth),
Expand All @@ -1931,13 +1933,13 @@ impl<'a> ExprGenerator<'a> {
),
size_hint_for_ratio(9, 10),
size_hint_for_range(0, 4),
Self::generate_attr_value_for_type_size_hint(depth),
Self::generate_attr_value_for_type_size_hint(depth)?,
]),
(1, None), // not sure how to hint for arbitrary_loop()
(1, None), // not sure how to hint for arbitrary_loop()
(1, None), // not sure how to hint for arbitrary_loop()
]),
)
))
})
}

Expand Down Expand Up @@ -2088,14 +2090,16 @@ impl<'a> ExprGenerator<'a> {

/// size hint for generate_attr_value_for_schematype()
#[allow(dead_code)]
pub fn generate_attr_value_for_schematype_size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::recursion_guard(depth, |depth| {
arbitrary::size_hint::or_all(&[
Self::generate_attr_value_for_type_size_hint(depth),
pub fn generate_attr_value_for_schematype_size_hint(
depth: usize,
) -> std::result::Result<(usize, Option<usize>), MaxRecursionReached> {
arbitrary::size_hint::try_recursion_guard(depth, |depth| {
Ok(arbitrary::size_hint::or_all(&[
Self::generate_attr_value_for_type_size_hint(depth)?,
(1, None), // not sure how to hint for arbitrary_loop()
Self::arbitrary_uid_with_type_size_hint(depth),
Self::generate_attr_value_for_type_size_hint(depth),
])
Self::generate_attr_value_for_type_size_hint(depth)?,
]))
})
}

Expand Down
Loading

0 comments on commit a5e8b1e

Please sign in to comment.