Skip to content

Commit

Permalink
Merge pull request #809 from silvergasp/fuzz-serde
Browse files Browse the repository at this point in the history
fuzz: Fuzz more of the serde API
  • Loading branch information
schungx committed Jan 8, 2024
2 parents f61beb6 + 8cb6d92 commit 9a79116
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rhai::{Engine, OptimizationLevel};
use anyhow::Result;
use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
use std::{hint::black_box};
use std::hint::black_box;

#[derive(Debug, Clone, Arbitrary)]
struct Ctx<'a> {
Expand Down Expand Up @@ -39,6 +39,7 @@ fn fuzz(ctx: Ctx) -> Result<()> {
_ = black_box(ast.iter_functions().count());
_ = black_box(ast.iter_literal_variables(true, true).count());
_ = black_box(ast.walk(&mut |_| true));
_ = black_box(engine.gen_metadata_to_json(&ast, true));

let mut function_only_ast = ast.clone_functions_only();
assert!(function_only_ast.clear_functions().iter_functions().count() == 0);
Expand Down
27 changes: 26 additions & 1 deletion fuzz/fuzz_targets/fuzz_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,55 @@ use rhai::{
};
use serde::{Deserialize, Serialize};

enum Enum {
Normal,
Newtype(u8),
StructType { _x: u8, _y: u8 },
}

#[derive(Arbitrary, Debug, Clone, PartialEq, Serialize, Deserialize)]
struct SubStruct {
_x: f64,
}

#[derive(Arbitrary, Debug, Clone, PartialEq, Serialize, Deserialize)]
struct NewType(u8);

#[derive(Arbitrary, Debug, Clone, PartialEq, Serialize, Deserialize)]
struct Tuple(u8, u8);

#[derive(Arbitrary, Debug, Clone, PartialEq, Serialize, Deserialize)]
struct AllTypes {
_bool: bool,
_str: String,
_bool_vec: Vec<bool>,
_char: char,

_i8: i8,
_i16: i16,
_i32: i32,
_i64: i64,
_i128: i128,

_u8: u8,
_u16: u16,
_u32: u32,
_u64: u64,
_u128: u128,

_f32: f32,
_f64: f64,

_unit: (),
_tuple: (u8, u8),

_bytes: Vec<u8>,
_bool_vec: Vec<bool>,
_option: Option<u8>,

_struct: SubStruct,
_new_type: NewType,
_tuple: Tuple,
_enum: Enum,
}

#[derive(Debug, Clone, Arbitrary)]
Expand Down

0 comments on commit 9a79116

Please sign in to comment.