Skip to content

CDRIVER-5648 fix libfuzzer integration #1686

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

Merged
merged 9 commits into from
Aug 13, 2024
Merged
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
18 changes: 18 additions & 0 deletions build/cmake/Sanitizers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ mongo_setting (
endif()
]])

mongo_bool_setting(
MONGO_FUZZ "Enable LibFuzzer integration"
DEFAULT VALUE OFF
VALIDATE CODE [[
if (MONGO_FUZZ AND NOT ENABLE_STATIC)
message (FATAL_ERROR "MONGO_FUZZ requires ENABLE_STATIC=ON or ENABLE_STATIC=BUILD_ONLY")
endif ()
]]
)

if (MONGO_FUZZ)
set(mongo_fuzz_options "address,undefined,fuzzer-no-link")
if (MONGO_SANITIZE AND NOT "${MONGO_SANITIZE}" STREQUAL "${mongo_fuzz_options}")
message(WARNING "Overriding user-provided MONGO_SANITIZE options due to MONGO_FUZZ=ON")
endif ()
set_property (CACHE MONGO_SANITIZE PROPERTY VALUE "${mongo_fuzz_options}")
endif ()

# Replace commas with semicolons for the genex
string(REPLACE ";" "," _sanitize "${MONGO_SANITIZE}")

Expand Down
4 changes: 4 additions & 0 deletions src/libbson/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ install (EXPORT bson-targets
include (LegacyPackage)
include (CPack)

if (MONGO_FUZZ)
add_subdirectory(fuzz)
endif ()

# 8888888b.
# 888 "Y88b
# 888 888
Expand Down
4 changes: 4 additions & 0 deletions src/libbson/fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_executable(fuzz_test_init_from_json EXCLUDE_FROM_ALL
fuzz_test_init_from_json.c)
target_link_libraries(fuzz_test_init_from_json PRIVATE bson_static)
set_property(TARGET fuzz_test_init_from_json APPEND PROPERTY LINK_OPTIONS -fsanitize=fuzzer)
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
int
LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
{
char *nt = malloc (size + 1);
memcpy (nt, data, size);
nt[size] = '\0';
bson_error_t error;

bson_t b;
if (bson_init_from_json (&b, nt, -1, &error)) {
if (bson_init_from_json (&b, (const char *) data, size, &error)) {
bson_destroy (&b);
}

free (nt);
return 0;
}