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