Skip to content

Commit

Permalink
feat(fuzz): Adds a fuzz harness for the starlark executor (#64)
Browse files Browse the repository at this point in the history
Summary:
This PR adds a basic fuzz test for the starlark parser and executor. The fuzzer has found a couple of non-critical bugs already :) so hopefully it'll be of some use to you :)

X-link: facebook/starlark-rust#64

Reviewed By: stepancheg

Differential Revision: D43697975

Pulled By: ndmitchell

fbshipit-source-id: f8274390e804bafc4003d7018930ede14a506dd4
  • Loading branch information
nathaniel-brough authored and facebook-github-bot committed Mar 1, 2023
1 parent 630a353 commit 0cf4c9f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions starlark-rust/starlark/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
26 changes: 26 additions & 0 deletions starlark-rust/starlark/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "starlark-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
anyhow = "1.0.69"
libfuzzer-sys = "0.4"
starlark.path = ".."

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[profile.release]
debug = 1

[[bin]]
name = "starlark"
path = "fuzz_targets/starlark.rs"
test = false
doc = false
44 changes: 44 additions & 0 deletions starlark-rust/starlark/fuzz/fuzz_targets/starlark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2019 The Starlark in Rust Authors.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#![no_main]

use std::hint::black_box;

use libfuzzer_sys::fuzz_target;
use starlark::environment::Globals;
use starlark::environment::Module;
use starlark::eval::Evaluator;
use starlark::syntax::AstModule;
use starlark::syntax::Dialect;

fn run_arbitrary_starlark(content: &str) -> anyhow::Result<()> {
let ast: AstModule =
AstModule::parse("hello_world.star", content.to_owned(), &Dialect::Standard)?;
let globals: Globals = Globals::standard();
let module: Module = Module::new();
let mut eval: Evaluator = Evaluator::new(&module);
let value = black_box(eval.eval_module(ast, &globals)?);
_ = black_box(format!("{value:?}"));
return Ok(());
}

fuzz_target!(|content: &str| {
if let Err(e) = black_box(run_arbitrary_starlark(content)) {
_ = black_box(format!("{e:?}"));
}
});

0 comments on commit 0cf4c9f

Please sign in to comment.