Skip to content

Commit

Permalink
Limit script length in fuzzing.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Nov 29, 2023
1 parent a03a539 commit 4b345d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
5 changes: 1 addition & 4 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ cargo-fuzz = true
[dependencies]
arbitrary = { version = "1.3.2", features = ["derive"] }
libfuzzer-sys = "0.4"

[dependencies.rhai]
path = ".."
features = ["arbitrary"]
rhai = { path = "..", features = ["arbitrary"] }

# Prevent this from interfering with workspaces
[workspace]
Expand Down
20 changes: 10 additions & 10 deletions fuzz/fuzz_targets/scripting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rhai::{Dynamic, Engine, OptimizationLevel};

use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
use std::time::{Duration, Instant};
use std::time::Instant;

#[derive(Debug, Clone, Arbitrary)]
struct Ctx<'a> {
Expand All @@ -13,6 +13,7 @@ struct Ctx<'a> {

fuzz_target!(|ctx: Ctx| {
let mut engine = Engine::new();

engine.set_max_string_size(1000);
engine.set_max_array_size(500);
engine.set_max_map_size(500);
Expand All @@ -21,16 +22,15 @@ fuzz_target!(|ctx: Ctx| {
engine.set_max_call_levels(10);
engine.set_max_expr_depths(50, 5);
engine.set_optimization_level(ctx.optimization_level);

// Limit the length of scripts.
let script = &ctx.script[..(ctx.script.len().min(32 * 1020))];

// We need fuzzing to be fast, so we'll stop executing after 1s.
let start = Instant::now();
engine.on_progress(move |_| {
// We need fuzzing to be fast, so we'll stop executing after 1s.
if start.elapsed() > Duration::from_secs(1) {
Some(Dynamic::UNIT)
} else {
None
}
});
engine.on_progress(move |_| (start.elapsed().as_millis() > 1000).then_some(Dynamic::UNIT));

let engine = engine;

_ = engine.run(ctx.script);
_ = engine.run(script);
});

0 comments on commit 4b345d3

Please sign in to comment.