Coverage Report

Created: 2024-10-11 07:39

/src/pybind_fuzzer.cc
Line
Count
Source
1
/* Copyright 2023 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
#include <fuzzer/FuzzedDataProvider.h>
14
#include <iostream>
15
#include <string>
16
#include "pybind11/embed.h"
17
#include "pybind11/pybind11.h"
18
19
namespace py = pybind11;
20
21
1.32k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
22
1.32k
  FuzzedDataProvider fdp(data, size);
23
24
1.32k
  static py::scoped_interpreter guard{};
25
1.32k
  try {
26
1.32k
    auto locals = py::dict();
27
1.32k
    py::exec(fdp.ConsumeRandomLengthString().c_str(), py::globals(), locals);
28
1.32k
  } catch (pybind11::error_already_set &e) {
29
1.18k
  }
30
31
1.32k
  try {
32
1.32k
    py::object os = py::module_::import("os");
33
1.32k
    py::object makedirs = os.attr(fdp.ConsumeRandomLengthString().c_str());
34
1.32k
  } catch (py::error_already_set &e) {
35
1.31k
  }
36
37
1.32k
  try {
38
1.32k
    py::tuple args =
39
1.32k
        py::make_tuple(fdp.ConsumeRandomLengthString().c_str(), py::none());
40
1.32k
    py::object Decimal = py::module_::import("decimal").attr("Decimal");
41
1.32k
    py::object pi = Decimal(fdp.ConsumeRandomLengthString().c_str());
42
1.32k
    py::object exp_pi = pi.attr(fdp.ConsumeRandomLengthString().c_str())();
43
1.32k
  } catch (py::error_already_set &e) {
44
1.31k
  }
45
46
1.32k
  try {
47
1.32k
    py::object obj = py::str(fdp.ConsumeRandomLengthString().c_str());
48
1.32k
  } catch (py::error_already_set &e) {
49
12
  }
50
1.32k
  return 0;
51
1.32k
}