Coverage Report

Created: 2025-06-22 07:16

/src/convert_jsonnet_fuzzer_regular.cc
Line
Count
Source
1
// Copyright 2022 Google Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
#include <cstddef>
17
#include <cstdint>
18
#include <cstring>
19
#include <string>
20
21
extern "C" {
22
#include "libjsonnet.h"
23
}
24
25
int ImportCallback(void* ctx, const char* base, const char* rel,
26
50
                     char** found_here, char **buf, size_t *buflen) {
27
  // Don't load file and mark it as failure.
28
50
  *buf = NULL;
29
50
  *buflen = 0;
30
50
  return 1;
31
50
}
32
33
14.2k
std::string ConvertJsonnetToJson(const std::string& jsonnet) {
34
14.2k
  JsonnetVm* jvm = jsonnet_make();
35
14.2k
  jsonnet_import_callback(jvm, ImportCallback, jvm);
36
14.2k
  int error = 0;
37
14.2k
  char* res =
38
14.2k
      jsonnet_evaluate_snippet(jvm, /*filename=*/"", jsonnet.c_str(), &error);
39
40
14.2k
  std::string json;
41
14.2k
  if (error == 0 && res != nullptr) {
42
1.72k
    json = res;
43
1.72k
  }
44
45
14.2k
  if (res) {
46
14.2k
    jsonnet_realloc(jvm, res, 0);
47
14.2k
  }
48
14.2k
  jsonnet_destroy(jvm);
49
14.2k
  return json;
50
14.2k
}
51
52
14.2k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
53
14.2k
  std::string fuzz_jsonnet(reinterpret_cast<const char*>(data), size);
54
14.2k
  ConvertJsonnetToJson(fuzz_jsonnet);
55
14.2k
  return 0;
56
14.2k
}