Repository navigation
OSS-Fuzz: Move fuzzers to upstream repository #1343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arthurscchan
wants to merge
2
commits into
google:master
Choose a base branch
from
arthurscchan:fuzz/upstream-harness
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
|
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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
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; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.