Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -209,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)

Expand All @@ -227,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)
Expand Down Expand Up @@ -295,6 +296,13 @@ if(BUILD_JSONNETFMT OR BUILD_TESTS)
endif()
endif()

#### libFuzzer-based fuzz targets.
#

if(BUILD_FUZZERS)
add_subdirectory(fuzz)
endif()
Comment thread
arthurscchan marked this conversation as resolved.
Resolved

#### GoogleTest based tests.
#

Expand Down
20 changes: 20 additions & 0 deletions fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# libFuzzer-based fuzz targets for jsonnet.
# Only built with -DBUILD_FUZZERS=ON and requires clang. See README.md
# in this directory.

Comment thread
arthurscchan marked this conversation as resolved.
Resolved
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
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()
36 changes: 36 additions & 0 deletions fuzz/README.md
Original file line number Diff line number Diff line change
@@ -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 <CORPUS DIR>

where `<CORPUS DIR>` 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:

./<target> <crash-file>
62 changes: 62 additions & 0 deletions fuzz/convert_jsonnet_fuzzer_multi.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 <cstddef>
#include <cstdint>
#include <cstring>
#include <string>

extern "C" {
#include "libjsonnet.h"
}

int ImportCallback(void* ctx, const char* base, const char* rel,
char** found_here, char **buf, size_t *buflen) {
// 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<JsonnetVm*>(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;
}
Comment thread
arthurscchan marked this conversation as resolved.
Resolved

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<const char*>(data), size);
ConvertJsonnetToJson(fuzz_jsonnet);
return 0;
}
62 changes: 62 additions & 0 deletions fuzz/convert_jsonnet_fuzzer_regular.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 <cstddef>
#include <cstdint>
#include <cstring>
#include <string>

extern "C" {
#include "libjsonnet.h"
}

int ImportCallback(void* ctx, const char* base, const char* rel,
char** found_here, char **buf, size_t *buflen) {
// 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<JsonnetVm*>(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;
}
Comment thread
arthurscchan marked this conversation as resolved.
Resolved

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<const char*>(data), size);
ConvertJsonnetToJson(fuzz_jsonnet);
return 0;
}
62 changes: 62 additions & 0 deletions fuzz/convert_jsonnet_fuzzer_stream.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 <cstddef>
#include <cstdint>
#include <cstring>
#include <string>

extern "C" {
#include "libjsonnet.h"
}

int ImportCallback(void* ctx, const char* base, const char* rel,
char** found_here, char **buf, size_t *buflen) {
// 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<JsonnetVm*>(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;
}
Comment thread
arthurscchan marked this conversation as resolved.
Resolved

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<const char*>(data), size);
ConvertJsonnetToJson(fuzz_jsonnet);
return 0;
}
Loading