/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 | 33 | char** found_here, char **buf, size_t *buflen) { |
27 | | // Don't load file and mark it as failure. |
28 | 33 | *buf = NULL; |
29 | 33 | *buflen = 0; |
30 | 33 | return 1; |
31 | 33 | } |
32 | | |
33 | 7.93k | std::string ConvertJsonnetToJson(const std::string& jsonnet) { |
34 | 7.93k | JsonnetVm* jvm = jsonnet_make(); |
35 | 7.93k | jsonnet_import_callback(jvm, ImportCallback, jvm); |
36 | 7.93k | int error = 0; |
37 | 7.93k | char* res = |
38 | 7.93k | jsonnet_evaluate_snippet(jvm, /*filename=*/"", jsonnet.c_str(), &error); |
39 | | |
40 | 7.93k | std::string json; |
41 | 7.93k | if (error == 0 && res != nullptr) { |
42 | 520 | json = res; |
43 | 520 | } |
44 | | |
45 | 7.93k | if (res) { |
46 | 7.93k | jsonnet_realloc(jvm, res, 0); |
47 | 7.93k | } |
48 | 7.93k | jsonnet_destroy(jvm); |
49 | 7.93k | return json; |
50 | 7.93k | } |
51 | | |
52 | 7.93k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
53 | 7.93k | std::string fuzz_jsonnet(reinterpret_cast<const char*>(data), size); |
54 | 7.93k | ConvertJsonnetToJson(fuzz_jsonnet); |
55 | 7.93k | return 0; |
56 | 7.93k | } |