-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #774 from silvergasp/main
fuzz: Add a fuzz-testing harness
- Loading branch information
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target | ||
corpus | ||
artifacts | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "rhai-fuzz" | ||
version = "0.0.0" | ||
publish = false | ||
edition = "2018" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
arbitrary = { version = "1.3.2", features = ["derive"] } | ||
libfuzzer-sys = "0.4" | ||
|
||
[dependencies.rhai] | ||
path = ".." | ||
features = ["arbitrary"] | ||
|
||
# Prevent this from interfering with workspaces | ||
[workspace] | ||
members = ["."] | ||
|
||
[profile.release] | ||
debug = 1 | ||
|
||
[[bin]] | ||
name = "scripting" | ||
path = "fuzz_targets/scripting.rs" | ||
test = false | ||
doc = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#![no_main] | ||
use rhai::{Dynamic, Engine, OptimizationLevel}; | ||
|
||
use arbitrary::Arbitrary; | ||
use libfuzzer_sys::fuzz_target; | ||
use std::time::{Duration, Instant}; | ||
|
||
#[derive(Debug, Clone, Arbitrary)] | ||
struct Ctx<'a> { | ||
script: &'a str, | ||
optimization_level: OptimizationLevel, | ||
} | ||
|
||
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); | ||
engine.set_max_variables(1000); | ||
engine.set_max_modules(1000); | ||
engine.set_max_call_levels(10); | ||
engine.set_max_expr_depths(50, 5); | ||
engine.set_optimization_level(ctx.optimization_level); | ||
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 | ||
} | ||
}); | ||
let engine = engine; | ||
|
||
_ = engine.run(ctx.script); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters