/src/starlark-rust/starlark/fuzz/fuzz_targets/starlark.rs
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019 The Starlark in Rust Authors. |
3 | | * Copyright (c) Facebook, Inc. and its affiliates. |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | * |
9 | | * https://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #![no_main] |
19 | | |
20 | | use libfuzzer_sys::fuzz_target; |
21 | | use starlark::environment::Globals; |
22 | | use starlark::environment::Module; |
23 | | use starlark::eval::Evaluator; |
24 | | use starlark::syntax::AstModule; |
25 | | use starlark::syntax::Dialect; |
26 | | |
27 | 24 | fn run_arbitrary_starlark_err(content: &str) -> starlark::Result<String> { |
28 | 10 | let ast: AstModule = |
29 | 24 | AstModule::parse("hello_world.star", content.to_owned(), &Dialect::Standard)?; |
30 | 10 | let globals: Globals = Globals::standard(); |
31 | 10 | let module: Module = Module::new(); |
32 | 10 | let mut eval: Evaluator = Evaluator::new(&module); |
33 | 10 | let value = eval.eval_module(ast, &globals)?; |
34 | 0 | Ok(format!("{value:?}")) |
35 | 24 | } |
36 | | |
37 | 24 | fn run_arbitrary_starlark(content: &str) -> String { |
38 | 24 | match run_arbitrary_starlark_err(content) { |
39 | 0 | Ok(v) => v, |
40 | 24 | Err(e) => { |
41 | 24 | const INTERNAL_ERROR: &str = "(internal error)"; |
42 | 24 | let s = format!("{e:?}"); |
43 | 24 | // We want to spot internal errors, but not encourage the fuzzer to write internal error in the input. |
44 | 24 | // A sufficiently smart fuzzer might outwit us, but hopefully not too quickly. |
45 | 24 | if s.contains(INTERNAL_ERROR) && !content.contains(INTERNAL_ERROR) { |
46 | 0 | panic!("Internal error as anyhow::Error: {s}") |
47 | 24 | } |
48 | 24 | s |
49 | | } |
50 | | } |
51 | 24 | } |
52 | | |
53 | | fuzz_target!(|content: &str| { |
54 | | let _ignore = run_arbitrary_starlark(content); |
55 | | }); |