Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tooling/ast_fuzzer/src/program/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@
// Generate a type or choose an existing one.
let max_depth = self.max_depth();
let comptime_friendly = self.is_comptime_friendly();
let typ = self.ctx.gen_type(u, max_depth, false, true, comptime_friendly)?;
let typ = self.ctx.gen_type(u, max_depth, false, false, true, comptime_friendly)?;
let expr = self.gen_expr(u, &typ, max_depth, Flags::TOP)?;
let mutable = bool::arbitrary(u)?;
Ok(self.let_var(mutable, typ, expr, true))
Expand Down Expand Up @@ -1163,9 +1163,9 @@
ctx.config.max_loop_size = 10;
ctx.config.vary_loop_size = false;
ctx.gen_main_decl(&mut u);
let mut fctx = FunctionContext::new(&mut ctx, FuncId(0));

Check warning on line 1166 in tooling/ast_fuzzer/src/program/func.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)
fctx.budget = 2;

Check warning on line 1167 in tooling/ast_fuzzer/src/program/func.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)
let loop_code = format!("{}", fctx.gen_loop(&mut u).unwrap()).replace(" ", "");

Check warning on line 1168 in tooling/ast_fuzzer/src/program/func.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)

println!("{loop_code}");
assert!(
Expand All @@ -1189,8 +1189,8 @@
ctx.config.max_loop_size = 10;
ctx.config.vary_loop_size = false;
ctx.gen_main_decl(&mut u);
let mut fctx = FunctionContext::new(&mut ctx, FuncId(0));

Check warning on line 1192 in tooling/ast_fuzzer/src/program/func.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)
fctx.budget = 2;

Check warning on line 1193 in tooling/ast_fuzzer/src/program/func.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)
let while_code = format!("{}", fctx.gen_while(&mut u).unwrap()).replace(" ", "");

println!("{while_code}");
Expand Down
32 changes: 27 additions & 5 deletions tooling/ast_fuzzer/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
ctx.set_function_decl(FuncId(1), decl_inner.clone());
ctx.gen_function(u, FuncId(1))?;

// Parameterless main declaration wrapping the inner "main"

Check warning on line 54 in tooling/ast_fuzzer/src/program/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Parameterless)
// function call
let decl_main = FunctionDeclaration {
name: "main".into(),
Expand All @@ -63,7 +63,7 @@
};

ctx.set_function_decl(FuncId(0), decl_main);
ctx.gen_function_with_body(u, FuncId(0), |u, fctx| fctx.gen_body_with_lit_call(u, FuncId(1)))?;

Check warning on line 66 in tooling/ast_fuzzer/src/program/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)

Check warning on line 66 in tooling/ast_fuzzer/src/program/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)
ctx.rewrite_functions(u)?;

let program = ctx.finalize();
Expand Down Expand Up @@ -133,8 +133,14 @@
u: &mut Unstructured,
i: usize,
) -> arbitrary::Result<(Name, Type, Expression)> {
let typ =
self.gen_type(u, self.config.max_depth, true, false, self.config.comptime_friendly)?;
let typ = self.gen_type(
u,
self.config.max_depth,
true,
false,
false,
self.config.comptime_friendly,
)?;
// By the time we get to the monomorphized AST the compiler will have already turned
// complex global expressions into literals.
let val = expr::gen_literal(u, &typ)?;
Expand Down Expand Up @@ -172,6 +178,7 @@
u,
self.config.max_depth,
false,
is_main,
false,
self.config.comptime_friendly,
)?;
Expand All @@ -189,8 +196,15 @@
params.push((id, is_mutable, name, typ, visibility));
}

let return_type =
self.gen_type(u, self.config.max_depth, false, false, self.config.comptime_friendly)?;
let return_type = self.gen_type(
u,
self.config.max_depth,
false,
is_main,
false,
self.config.comptime_friendly,
)?;

let return_visibility = if is_main {
if types::is_unit(&return_type) {
Visibility::Private
Expand Down Expand Up @@ -237,7 +251,7 @@

/// Generate random function body.
fn gen_function(&mut self, u: &mut Unstructured, id: FuncId) -> arbitrary::Result<()> {
self.gen_function_with_body(u, id, |u, fctx| fctx.gen_body(u))

Check warning on line 254 in tooling/ast_fuzzer/src/program/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)

Check warning on line 254 in tooling/ast_fuzzer/src/program/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fctx)
}

/// Generate function with a specified body generator.
Expand Down Expand Up @@ -321,6 +335,7 @@
u: &mut Unstructured,
max_depth: usize,
is_global: bool,
is_main: bool,
is_frontend_friendly: bool,
is_comptime_friendly: bool,
) -> arbitrary::Result<Type> {
Expand All @@ -330,6 +345,7 @@
.types
.iter()
.filter(|typ| !is_global || types::can_be_global(typ))
.filter(|typ| !is_main || !types::can_be_main(typ))
.filter(|typ| types::type_depth(typ) <= max_depth)
.filter(|typ| !is_frontend_friendly || !self.should_avoid_literals(typ))
.collect::<Vec<_>>();
Expand Down Expand Up @@ -378,6 +394,7 @@
u,
max_depth - 1,
is_global,
is_main,
is_frontend_friendly,
is_comptime_friendly,
)
Expand All @@ -392,14 +409,19 @@
u,
max_depth - 1,
is_global,
is_main,
is_frontend_friendly,
is_comptime_friendly,
)?;
Type::Array(size as u32, Box::new(typ))
}
_ => unreachable!("unexpected arbitrary type index"),
};
if !is_global || types::can_be_global(&typ) {
// Looping is kinda dangerous, we could get stuck if we run out of randomness,
// so we have to make sure the first type on the list is acceptable.
if is_global && !types::can_be_global(&typ) || is_main && !types::can_be_main(&typ) {
continue;
} else {
break;
}
}
Expand Down
12 changes: 12 additions & 0 deletions tooling/ast_fuzzer/src/program/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ pub(crate) fn can_be_global(typ: &Type) -> bool {
)
}

/// Check if a type can be used in the `main` function.
///
/// We decided we will avoid 0 length arrays in the main inputs and outputs, because we don't generate
/// witnesses for them anyway, and they are tricky to handle consistently when they can be regular inputs
/// as well as part of the databus. They are not expected in real programs as they don't do anything useful.
pub(crate) fn can_be_main(typ: &Type) -> bool {
match typ {
Type::Array(size, _) | Type::String(size) => *size > 0,
_ => true,
}
}

/// Collect all the sub-types produced by a type.
///
/// It's like a _power set_ of the type.
Expand Down
Loading