Skip to content

Commit ffd2fde

Browse files
committed
OSS-Fuzz: Move fuzzers to upstream repository
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
1 parent 2ef32ab commit ffd2fde

6 files changed

Lines changed: 252 additions & 2 deletions

‎CMakeLists.txt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ option(BUILD_MAN_PAGES "Build manpages." ON)
5151
option(USE_SYSTEM_GTEST "Use system-provided gtest library" OFF)
5252
option(USE_SYSTEM_JSON "Use the system-provided json library" OFF)
5353
option(USE_SYSTEM_RAPIDYAML "Use the system-provided rapidyaml library" OFF)
54+
option(BUILD_FUZZERS "Build libFuzzer-based fuzz targets in fuzz/ (requires clang)." OFF)
5455

5556
#### Compute derived values from user-configurable state.
5657
#
@@ -209,7 +210,7 @@ endfunction()
209210
#### Static library target setup.
210211
#
211212

212-
if(BUILD_STATIC_LIBS OR (BUILD_SOME_BINARIES AND NOT BUILD_SHARED_BINARIES))
213+
if(BUILD_STATIC_LIBS OR (BUILD_SOME_BINARIES AND NOT BUILD_SHARED_BINARIES) OR (BUILD_FUZZERS AND NOT BUILD_SHARED_BINARIES))
213214
add_library(jsonnet_obj_static OBJECT)
214215
configure_jsonnet_obj_target(jsonnet_obj_static)
215216

@@ -227,7 +228,7 @@ endif()
227228
#### Shared library target setup.
228229
#
229230

230-
if(BUILD_SHARED_LIBS OR (BUILD_SOME_BINARIES AND BUILD_SHARED_BINARIES))
231+
if(BUILD_SHARED_LIBS OR (BUILD_SOME_BINARIES AND BUILD_SHARED_BINARIES) OR (BUILD_FUZZERS AND BUILD_SHARED_BINARIES))
231232
add_library(jsonnet_obj_shared OBJECT)
232233
set_property(TARGET jsonnet_obj_shared PROPERTY POSITION_INDEPENDENT_CODE ON)
233234
configure_jsonnet_obj_target(jsonnet_obj_shared)
@@ -295,6 +296,13 @@ if(BUILD_JSONNETFMT OR BUILD_TESTS)
295296
endif()
296297
endif()
297298

299+
#### libFuzzer-based fuzz targets.
300+
#
301+
302+
if(BUILD_FUZZERS)
303+
add_subdirectory(fuzz)
304+
endif()
305+
298306
#### GoogleTest based tests.
299307
#
300308

‎fuzz/CMakeLists.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# libFuzzer-based fuzz targets for jsonnet.
2+
# Only built with -DBUILD_FUZZERS=ON and requires clang. See README.md
3+
# in this directory.
4+
5+
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
6+
message(FATAL_ERROR "Building the fuzzers requires the Clang compiler (-fsanitize=fuzzer).")
7+
endif()
8+
9+
set(JSONNET_FUZZ_TARGETS
10+
convert_jsonnet_fuzzer_regular
11+
convert_jsonnet_fuzzer_stream
12+
convert_jsonnet_fuzzer_multi)
13+
14+
foreach(fuzz_target ${JSONNET_FUZZ_TARGETS})
15+
add_executable(${fuzz_target} ${fuzz_target}.cc)
16+
configure_target_cc_props(${fuzz_target})
17+
target_link_libraries(${fuzz_target} PRIVATE jsonnet_lib_for_binaries)
18+
target_compile_options(${fuzz_target} PRIVATE -fsanitize=fuzzer-no-link,address)
19+
target_link_options(${fuzz_target} PRIVATE -fsanitize=fuzzer,address)
20+
endforeach()

‎fuzz/README.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Fuzzing jsonnet
2+
3+
This directory contains libFuzzer-based fuzz targets for the C API of
4+
libjsonnet. The same targets are also built and run by
5+
[OSS-Fuzz](https://github.com/google/oss-fuzz).
6+
7+
## Targets
8+
9+
* `convert_jsonnet_fuzzer_regular` — evaluates the input as a Jsonnet
10+
snippet with `jsonnet_evaluate_snippet`.
11+
* `convert_jsonnet_fuzzer_stream` — evaluates the input as a Jsonnet
12+
snippet with `jsonnet_evaluate_snippet_stream`.
13+
* `convert_jsonnet_fuzzer_multi` — evaluates the input as a Jsonnet
14+
snippet with `jsonnet_evaluate_snippet_multi`.
15+
16+
All targets install an import callback that refuses to load any files,
17+
so only self-contained snippets are exercised.
18+
19+
## Build
20+
21+
Configure and build with clang and `-DBUILD_FUZZERS=ON`, e.g.:
22+
23+
cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
24+
-DBUILD_FUZZERS=ON ../
25+
make -j$(nproc)
26+
27+
## Run
28+
29+
./convert_jsonnet_fuzzer_regular <CORPUS DIR>
30+
31+
where `<CORPUS DIR>` is a directory containing Jsonnet files, for
32+
example files from the `test_suite/` directory.
33+
34+
If a fuzzer crashes, a `crash-*` file is written. Reproduce it with:
35+
36+
./<target> <crash-file>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
char** found_here, char **buf, size_t *buflen) {
27+
// Refuse to load any file. Per the JsonnetImportCallback contract, a
28+
// failure (return value 1) must deliver an error message in *buf,
29+
// allocated with jsonnet_realloc and without a trailing null byte.
30+
JsonnetVm* jvm = static_cast<JsonnetVm*>(ctx);
31+
const char* msg = "imports are disabled";
32+
size_t len = std::strlen(msg);
33+
*buf = jsonnet_realloc(jvm, nullptr, len);
34+
std::memcpy(*buf, msg, len);
35+
*buflen = len;
36+
return 1;
37+
}
38+
39+
std::string ConvertJsonnetToJson(const std::string& jsonnet) {
40+
JsonnetVm* jvm = jsonnet_make();
41+
jsonnet_import_callback(jvm, ImportCallback, jvm);
42+
int error = 0;
43+
char* res =
44+
jsonnet_evaluate_snippet_multi(jvm, /*filename=*/"", jsonnet.c_str(), &error);
45+
46+
std::string json;
47+
if (error == 0 && res != nullptr) {
48+
json = res;
49+
}
50+
51+
if (res) {
52+
jsonnet_realloc(jvm, res, 0);
53+
}
54+
jsonnet_destroy(jvm);
55+
return json;
56+
}
57+
58+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
59+
std::string fuzz_jsonnet(reinterpret_cast<const char*>(data), size);
60+
ConvertJsonnetToJson(fuzz_jsonnet);
61+
return 0;
62+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
char** found_here, char **buf, size_t *buflen) {
27+
// Refuse to load any file. Per the JsonnetImportCallback contract, a
28+
// failure (return value 1) must deliver an error message in *buf,
29+
// allocated with jsonnet_realloc and without a trailing null byte.
30+
JsonnetVm* jvm = static_cast<JsonnetVm*>(ctx);
31+
const char* msg = "imports are disabled";
32+
size_t len = std::strlen(msg);
33+
*buf = jsonnet_realloc(jvm, nullptr, len);
34+
std::memcpy(*buf, msg, len);
35+
*buflen = len;
36+
return 1;
37+
}
38+
39+
std::string ConvertJsonnetToJson(const std::string& jsonnet) {
40+
JsonnetVm* jvm = jsonnet_make();
41+
jsonnet_import_callback(jvm, ImportCallback, jvm);
42+
int error = 0;
43+
char* res =
44+
jsonnet_evaluate_snippet(jvm, /*filename=*/"", jsonnet.c_str(), &error);
45+
46+
std::string json;
47+
if (error == 0 && res != nullptr) {
48+
json = res;
49+
}
50+
51+
if (res) {
52+
jsonnet_realloc(jvm, res, 0);
53+
}
54+
jsonnet_destroy(jvm);
55+
return json;
56+
}
57+
58+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
59+
std::string fuzz_jsonnet(reinterpret_cast<const char*>(data), size);
60+
ConvertJsonnetToJson(fuzz_jsonnet);
61+
return 0;
62+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
char** found_here, char **buf, size_t *buflen) {
27+
// Refuse to load any file. Per the JsonnetImportCallback contract, a
28+
// failure (return value 1) must deliver an error message in *buf,
29+
// allocated with jsonnet_realloc and without a trailing null byte.
30+
JsonnetVm* jvm = static_cast<JsonnetVm*>(ctx);
31+
const char* msg = "imports are disabled";
32+
size_t len = std::strlen(msg);
33+
*buf = jsonnet_realloc(jvm, nullptr, len);
34+
std::memcpy(*buf, msg, len);
35+
*buflen = len;
36+
return 1;
37+
}
38+
39+
std::string ConvertJsonnetToJson(const std::string& jsonnet) {
40+
JsonnetVm* jvm = jsonnet_make();
41+
jsonnet_import_callback(jvm, ImportCallback, jvm);
42+
int error = 0;
43+
char* res =
44+
jsonnet_evaluate_snippet_stream(jvm, /*filename=*/"", jsonnet.c_str(), &error);
45+
46+
std::string json;
47+
if (error == 0 && res != nullptr) {
48+
json = res;
49+
}
50+
51+
if (res) {
52+
jsonnet_realloc(jvm, res, 0);
53+
}
54+
jsonnet_destroy(jvm);
55+
return json;
56+
}
57+
58+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
59+
std::string fuzz_jsonnet(reinterpret_cast<const char*>(data), size);
60+
ConvertJsonnetToJson(fuzz_jsonnet);
61+
return 0;
62+
}

0 commit comments

Comments
 (0)