From 321b8cc9ba23dd23bb2a6661597fa1ea2fadf30d Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Fri, 16 Oct 2026 11:11:49 +0100 Subject: [PATCH 1/2] OSS-Fuzz: Move fuzzers to upstream repository Signed-off-by: Arthur Chan --- CMakeLists.txt | 8 ++++ fuzz/CMakeLists.txt | 16 ++++++++ fuzz/README.md | 36 +++++++++++++++++ fuzz/convert_jsonnet_fuzzer_multi.cc | 56 ++++++++++++++++++++++++++ fuzz/convert_jsonnet_fuzzer_regular.cc | 56 ++++++++++++++++++++++++++ fuzz/convert_jsonnet_fuzzer_stream.cc | 56 ++++++++++++++++++++++++++ 6 files changed, 228 insertions(+) create mode 100644 fuzz/CMakeLists.txt create mode 100644 fuzz/README.md create mode 100644 fuzz/convert_jsonnet_fuzzer_multi.cc create mode 100644 fuzz/convert_jsonnet_fuzzer_regular.cc create mode 100644 fuzz/convert_jsonnet_fuzzer_stream.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 597c429d..df32cf9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ option(BUILD_MAN_PAGES "Build manpages." ON) option(USE_SYSTEM_GTEST "Use system-provided gtest library" OFF) option(USE_SYSTEM_JSON "Use the system-provided json library" OFF) option(USE_SYSTEM_RAPIDYAML "Use the system-provided rapidyaml library" OFF) +option(BUILD_FUZZERS "Build libFuzzer-based fuzz targets in fuzz/ (requires clang)." OFF) #### Compute derived values from user-configurable state. # @@ -295,6 +296,13 @@ if(BUILD_JSONNETFMT OR BUILD_TESTS) endif() endif() +#### libFuzzer-based fuzz targets. +# + +if(BUILD_FUZZERS) + add_subdirectory(fuzz) +endif() + #### GoogleTest based tests. # diff --git a/fuzz/CMakeLists.txt b/fuzz/CMakeLists.txt new file mode 100644 index 00000000..46e5d1b3 --- /dev/null +++ b/fuzz/CMakeLists.txt @@ -0,0 +1,16 @@ +# libFuzzer-based fuzz targets for jsonnet. +# Only built with -DBUILD_FUZZERS=ON and requires clang. See README.md +# in this directory. + +set(JSONNET_FUZZ_TARGETS + convert_jsonnet_fuzzer_regular + convert_jsonnet_fuzzer_stream + convert_jsonnet_fuzzer_multi) + +foreach(fuzz_target ${JSONNET_FUZZ_TARGETS}) + add_executable(${fuzz_target} ${fuzz_target}.cc) + configure_target_cc_props(${fuzz_target}) + target_link_libraries(${fuzz_target} PRIVATE jsonnet_lib_for_binaries) + target_compile_options(${fuzz_target} PRIVATE -fsanitize=fuzzer-no-link,address) + target_link_options(${fuzz_target} PRIVATE -fsanitize=fuzzer,address) +endforeach() diff --git a/fuzz/README.md b/fuzz/README.md new file mode 100644 index 00000000..f69ef815 --- /dev/null +++ b/fuzz/README.md @@ -0,0 +1,36 @@ +# Fuzzing jsonnet + +This directory contains libFuzzer-based fuzz targets for the C API of +libjsonnet. The same targets are also built and run by +[OSS-Fuzz](https://github.com/google/oss-fuzz). + +## Targets + +* `convert_jsonnet_fuzzer_regular` — evaluates the input as a Jsonnet + snippet with `jsonnet_evaluate_snippet`. +* `convert_jsonnet_fuzzer_stream` — evaluates the input as a Jsonnet + snippet with `jsonnet_evaluate_snippet_stream`. +* `convert_jsonnet_fuzzer_multi` — evaluates the input as a Jsonnet + snippet with `jsonnet_evaluate_snippet_multi`. + +All targets install an import callback that refuses to load any files, +so only self-contained snippets are exercised. + +## Build + +Configure and build with clang and `-DBUILD_FUZZERS=ON`, e.g.: + + cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \ + -DBUILD_FUZZERS=ON ../ + make -j$(nproc) + +## Run + + ./convert_jsonnet_fuzzer_regular + +where `` is a directory containing Jsonnet files, for +example files from the `test_suite/` directory. + +If a fuzzer crashes, a `crash-*` file is written. Reproduce it with: + + ./ diff --git a/fuzz/convert_jsonnet_fuzzer_multi.cc b/fuzz/convert_jsonnet_fuzzer_multi.cc new file mode 100644 index 00000000..46163988 --- /dev/null +++ b/fuzz/convert_jsonnet_fuzzer_multi.cc @@ -0,0 +1,56 @@ +// Copyright 2022 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +#include +#include +#include +#include + +extern "C" { +#include "libjsonnet.h" +} + +int ImportCallback(void* ctx, const char* base, const char* rel, + char** found_here, char **buf, size_t *buflen) { + // Don't load file and mark it as failure. + *buf = NULL; + *buflen = 0; + return 1; +} + +std::string ConvertJsonnetToJson(const std::string& jsonnet) { + JsonnetVm* jvm = jsonnet_make(); + jsonnet_import_callback(jvm, ImportCallback, jvm); + int error = 0; + char* res = + jsonnet_evaluate_snippet_multi(jvm, /*filename=*/"", jsonnet.c_str(), &error); + + std::string json; + if (error == 0 && res != nullptr) { + json = res; + } + + if (res) { + jsonnet_realloc(jvm, res, 0); + } + jsonnet_destroy(jvm); + return json; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + std::string fuzz_jsonnet(reinterpret_cast(data), size); + ConvertJsonnetToJson(fuzz_jsonnet); + return 0; +} diff --git a/fuzz/convert_jsonnet_fuzzer_regular.cc b/fuzz/convert_jsonnet_fuzzer_regular.cc new file mode 100644 index 00000000..1e6c96ab --- /dev/null +++ b/fuzz/convert_jsonnet_fuzzer_regular.cc @@ -0,0 +1,56 @@ +// Copyright 2022 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +#include +#include +#include +#include + +extern "C" { +#include "libjsonnet.h" +} + +int ImportCallback(void* ctx, const char* base, const char* rel, + char** found_here, char **buf, size_t *buflen) { + // Don't load file and mark it as failure. + *buf = NULL; + *buflen = 0; + return 1; +} + +std::string ConvertJsonnetToJson(const std::string& jsonnet) { + JsonnetVm* jvm = jsonnet_make(); + jsonnet_import_callback(jvm, ImportCallback, jvm); + int error = 0; + char* res = + jsonnet_evaluate_snippet(jvm, /*filename=*/"", jsonnet.c_str(), &error); + + std::string json; + if (error == 0 && res != nullptr) { + json = res; + } + + if (res) { + jsonnet_realloc(jvm, res, 0); + } + jsonnet_destroy(jvm); + return json; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + std::string fuzz_jsonnet(reinterpret_cast(data), size); + ConvertJsonnetToJson(fuzz_jsonnet); + return 0; +} diff --git a/fuzz/convert_jsonnet_fuzzer_stream.cc b/fuzz/convert_jsonnet_fuzzer_stream.cc new file mode 100644 index 00000000..66719da7 --- /dev/null +++ b/fuzz/convert_jsonnet_fuzzer_stream.cc @@ -0,0 +1,56 @@ +// Copyright 2022 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +#include +#include +#include +#include + +extern "C" { +#include "libjsonnet.h" +} + +int ImportCallback(void* ctx, const char* base, const char* rel, + char** found_here, char **buf, size_t *buflen) { + // Don't load file and mark it as failure. + *buf = NULL; + *buflen = 0; + return 1; +} + +std::string ConvertJsonnetToJson(const std::string& jsonnet) { + JsonnetVm* jvm = jsonnet_make(); + jsonnet_import_callback(jvm, ImportCallback, jvm); + int error = 0; + char* res = + jsonnet_evaluate_snippet_stream(jvm, /*filename=*/"", jsonnet.c_str(), &error); + + std::string json; + if (error == 0 && res != nullptr) { + json = res; + } + + if (res) { + jsonnet_realloc(jvm, res, 0); + } + jsonnet_destroy(jvm); + return json; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + std::string fuzz_jsonnet(reinterpret_cast(data), size); + ConvertJsonnetToJson(fuzz_jsonnet); + return 0; +} From d0014fefe894acd46371ed5aafc3c32b823ea11b Mon Sep 17 00:00:00 2001 From: Arthur SC Chan Date: Thu, 22 Oct 2026 17:44:37 +0800 Subject: [PATCH 2/2] fuzz: address code review feedback - ImportCallback: return an allocated error message (via jsonnet_realloc) on failure, as required by the JsonnetImportCallback contract, instead of a NULL buffer. - CMake: build the jsonnet library targets when only BUILD_FUZZERS is enabled, so jsonnet_lib_for_binaries resolves. - CMake: fail with a clear error when the fuzzers are built without Clang. Signed-off-by: Arthur Chan --- CMakeLists.txt | 4 ++-- fuzz/CMakeLists.txt | 4 ++++ fuzz/convert_jsonnet_fuzzer_multi.cc | 12 +++++++++--- fuzz/convert_jsonnet_fuzzer_regular.cc | 12 +++++++++--- fuzz/convert_jsonnet_fuzzer_stream.cc | 12 +++++++++--- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index df32cf9f..bc4002b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,7 +210,7 @@ endfunction() #### Static library target setup. # -if(BUILD_STATIC_LIBS OR (BUILD_SOME_BINARIES AND NOT BUILD_SHARED_BINARIES)) +if(BUILD_STATIC_LIBS OR (BUILD_SOME_BINARIES AND NOT BUILD_SHARED_BINARIES) OR (BUILD_FUZZERS AND NOT BUILD_SHARED_BINARIES)) add_library(jsonnet_obj_static OBJECT) configure_jsonnet_obj_target(jsonnet_obj_static) @@ -228,7 +228,7 @@ endif() #### Shared library target setup. # -if(BUILD_SHARED_LIBS OR (BUILD_SOME_BINARIES AND BUILD_SHARED_BINARIES)) +if(BUILD_SHARED_LIBS OR (BUILD_SOME_BINARIES AND BUILD_SHARED_BINARIES) OR (BUILD_FUZZERS AND BUILD_SHARED_BINARIES)) add_library(jsonnet_obj_shared OBJECT) set_property(TARGET jsonnet_obj_shared PROPERTY POSITION_INDEPENDENT_CODE ON) configure_jsonnet_obj_target(jsonnet_obj_shared) diff --git a/fuzz/CMakeLists.txt b/fuzz/CMakeLists.txt index 46e5d1b3..b3c42c5c 100644 --- a/fuzz/CMakeLists.txt +++ b/fuzz/CMakeLists.txt @@ -2,6 +2,10 @@ # Only built with -DBUILD_FUZZERS=ON and requires clang. See README.md # in this directory. +if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") + message(FATAL_ERROR "Building the fuzzers requires the Clang compiler (-fsanitize=fuzzer).") +endif() + set(JSONNET_FUZZ_TARGETS convert_jsonnet_fuzzer_regular convert_jsonnet_fuzzer_stream diff --git a/fuzz/convert_jsonnet_fuzzer_multi.cc b/fuzz/convert_jsonnet_fuzzer_multi.cc index 46163988..79bdb604 100644 --- a/fuzz/convert_jsonnet_fuzzer_multi.cc +++ b/fuzz/convert_jsonnet_fuzzer_multi.cc @@ -24,9 +24,15 @@ extern "C" { int ImportCallback(void* ctx, const char* base, const char* rel, char** found_here, char **buf, size_t *buflen) { - // Don't load file and mark it as failure. - *buf = NULL; - *buflen = 0; + // Refuse to load any file. Per the JsonnetImportCallback contract, a + // failure (return value 1) must deliver an error message in *buf, + // allocated with jsonnet_realloc and without a trailing null byte. + JsonnetVm* jvm = static_cast(ctx); + const char* msg = "imports are disabled"; + size_t len = std::strlen(msg); + *buf = jsonnet_realloc(jvm, nullptr, len); + std::memcpy(*buf, msg, len); + *buflen = len; return 1; } diff --git a/fuzz/convert_jsonnet_fuzzer_regular.cc b/fuzz/convert_jsonnet_fuzzer_regular.cc index 1e6c96ab..b3197f9c 100644 --- a/fuzz/convert_jsonnet_fuzzer_regular.cc +++ b/fuzz/convert_jsonnet_fuzzer_regular.cc @@ -24,9 +24,15 @@ extern "C" { int ImportCallback(void* ctx, const char* base, const char* rel, char** found_here, char **buf, size_t *buflen) { - // Don't load file and mark it as failure. - *buf = NULL; - *buflen = 0; + // Refuse to load any file. Per the JsonnetImportCallback contract, a + // failure (return value 1) must deliver an error message in *buf, + // allocated with jsonnet_realloc and without a trailing null byte. + JsonnetVm* jvm = static_cast(ctx); + const char* msg = "imports are disabled"; + size_t len = std::strlen(msg); + *buf = jsonnet_realloc(jvm, nullptr, len); + std::memcpy(*buf, msg, len); + *buflen = len; return 1; } diff --git a/fuzz/convert_jsonnet_fuzzer_stream.cc b/fuzz/convert_jsonnet_fuzzer_stream.cc index 66719da7..5c574d13 100644 --- a/fuzz/convert_jsonnet_fuzzer_stream.cc +++ b/fuzz/convert_jsonnet_fuzzer_stream.cc @@ -24,9 +24,15 @@ extern "C" { int ImportCallback(void* ctx, const char* base, const char* rel, char** found_here, char **buf, size_t *buflen) { - // Don't load file and mark it as failure. - *buf = NULL; - *buflen = 0; + // Refuse to load any file. Per the JsonnetImportCallback contract, a + // failure (return value 1) must deliver an error message in *buf, + // allocated with jsonnet_realloc and without a trailing null byte. + JsonnetVm* jvm = static_cast(ctx); + const char* msg = "imports are disabled"; + size_t len = std::strlen(msg); + *buf = jsonnet_realloc(jvm, nullptr, len); + std::memcpy(*buf, msg, len); + *buflen = len; return 1; }