1 #include <cstddef>
2 #include <cstdint>
3 #include <cstring>
4 #include <string>
5
6 extern "C" {
7 #include "libjsonnet.h"
8 }
9
ImportCallback(void * ctx,const char * base,const char * rel,char ** found_here,int * success)10 char* ImportCallback(void* ctx, const char* base, const char* rel,
11 char** found_here, int* success) {
12 // Don't load file and mark it as failure.
13 *success = 0;
14 char* res = jsonnet_realloc(static_cast<struct JsonnetVm*>(ctx), nullptr, 1);
15 res[0] = 0;
16 return res;
17 }
18
ConvertJsonnetToJson(const std::string & jsonnet)19 std::string ConvertJsonnetToJson(const std::string& jsonnet) {
20 JsonnetVm* jvm = jsonnet_make();
21 jsonnet_import_callback(jvm, ImportCallback, jvm);
22 int error = 0;
23 char* res =
24 jsonnet_evaluate_snippet(jvm, /*filename=*/"", jsonnet.c_str(), &error);
25
26 std::string json;
27 if (error == 0 && res != nullptr) {
28 json = res;
29 }
30
31 if (res) {
32 jsonnet_realloc(jvm, res, 0);
33 }
34 jsonnet_destroy(jvm);
35 return json;
36 }
37
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
39 std::string fuzz_jsonnet(reinterpret_cast<const char*>(data), size);
40 ConvertJsonnetToJson(fuzz_jsonnet);
41 return 0;
42 }
43