From f8c9c2f30700e53ea1ef5143a9f82f561a9b0a24 Mon Sep 17 00:00:00 2001 From: Nathaniel Brough Date: Mon, 1 Jan 2024 15:43:38 -0800 Subject: [PATCH] fuzz: Add basic fuzzing of Scope --- fuzz/fuzz_targets/scripting.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/fuzz/fuzz_targets/scripting.rs b/fuzz/fuzz_targets/scripting.rs index 624122fab..398bf18bf 100644 --- a/fuzz/fuzz_targets/scripting.rs +++ b/fuzz/fuzz_targets/scripting.rs @@ -3,12 +3,16 @@ use rhai::{Dynamic, Engine, OptimizationLevel}; use arbitrary::Arbitrary; use libfuzzer_sys::fuzz_target; +use rhai::Scope; +use std::collections::HashMap; use std::{hint::black_box, time::Instant}; #[derive(Debug, Clone, Arbitrary)] struct Ctx<'a> { script: &'a str, optimization_level: OptimizationLevel, + scoped_constants: HashMap<&'a str, i64>, + scoped_vars: HashMap<&'a str, i64>, } fuzz_target!(|ctx: Ctx| { @@ -39,7 +43,16 @@ fuzz_target!(|ctx: Ctx| { let engine = engine; - match engine.eval::(&script) { + let mut scope = Scope::new(); + for (var_name, value) in ctx.scoped_constants.into_iter() { + scope.push_constant(var_name, value); + } + + for (var_name, value) in ctx.scoped_vars.into_iter() { + scope.set_or_push(var_name, value); + } + + match engine.eval_with_scope::(&mut scope, &script) { Ok(val) => { if val.is_array() { _ = black_box(val.clone().into_array().unwrap()); @@ -82,4 +95,12 @@ fuzz_target!(|ctx: Ctx| { } Err(e) => _ = black_box(format!("{e}")), } + _ = black_box(format!("{scope}")); + _ = black_box(scope.clone().into_iter().count()); + _ = black_box(scope.iter().count()); + _ = black_box((&scope).into_iter().count()); + if !scope.is_empty() { + _ = black_box(scope.pop()); + } + _ = black_box(scope.clone_visible()); });