Skip to content

Commit 8cc212b

Browse files
CDRIVER-5648 fix libfuzzer integration (#1686)
* Implement and simplify libfuzzer entrypoint * Structure libbson/CMakeLists.txt to isolate fuzzer definitions * Rework fuzzer enablement to use a switch
1 parent 84ac397 commit 8cc212b

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

build/cmake/Sanitizers.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ mongo_setting (
1111
endif()
1212
]])
1313

14+
mongo_bool_setting(
15+
MONGO_FUZZ "Enable LibFuzzer integration"
16+
DEFAULT VALUE OFF
17+
VALIDATE CODE [[
18+
if (MONGO_FUZZ AND NOT ENABLE_STATIC)
19+
message (FATAL_ERROR "MONGO_FUZZ requires ENABLE_STATIC=ON or ENABLE_STATIC=BUILD_ONLY")
20+
endif ()
21+
]]
22+
)
23+
24+
if (MONGO_FUZZ)
25+
set(mongo_fuzz_options "address,undefined,fuzzer-no-link")
26+
if (MONGO_SANITIZE AND NOT "${MONGO_SANITIZE}" STREQUAL "${mongo_fuzz_options}")
27+
message(WARNING "Overriding user-provided MONGO_SANITIZE options due to MONGO_FUZZ=ON")
28+
endif ()
29+
set_property (CACHE MONGO_SANITIZE PROPERTY VALUE "${mongo_fuzz_options}")
30+
endif ()
31+
1432
# Replace commas with semicolons for the genex
1533
string(REPLACE ";" "," _sanitize "${MONGO_SANITIZE}")
1634

src/libbson/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ install (EXPORT bson-targets
399399
include (LegacyPackage)
400400
include (CPack)
401401

402+
if (MONGO_FUZZ)
403+
add_subdirectory(fuzz)
404+
endif ()
405+
402406
# 8888888b.
403407
# 888 "Y88b
404408
# 888 888

src/libbson/fuzz/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_executable(fuzz_test_init_from_json EXCLUDE_FROM_ALL
2+
fuzz_test_init_from_json.c)
3+
target_link_libraries(fuzz_test_init_from_json PRIVATE bson_static)
4+
set_property(TARGET fuzz_test_init_from_json APPEND PROPERTY LINK_OPTIONS -fsanitize=fuzzer)

src/libbson/fuzz/fuzz_test_libbson.c renamed to src/libbson/fuzz/fuzz_test_init_from_json.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
int
77
LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
88
{
9-
char *nt = malloc (size + 1);
10-
memcpy (nt, data, size);
11-
nt[size] = '\0';
129
bson_error_t error;
1310

1411
bson_t b;
15-
if (bson_init_from_json (&b, nt, -1, &error)) {
12+
if (bson_init_from_json (&b, (const char *) data, size, &error)) {
1613
bson_destroy (&b);
1714
}
1815

19-
free (nt);
2016
return 0;
2117
}

0 commit comments

Comments
 (0)