19750e409Sopenharmony_cioption(ENABLE_FUZZING "Create executables and targets for fuzzing cJSON with afl." Off)
29750e409Sopenharmony_ciif (ENABLE_FUZZING)
39750e409Sopenharmony_ci    find_program(AFL_FUZZ afl-fuzz)
49750e409Sopenharmony_ci    if ("${AFL_FUZZ}" MATCHES "AFL_FUZZ-NOTFOUND")
59750e409Sopenharmony_ci        message(FATAL_ERROR "Couldn't find afl-fuzz.")
69750e409Sopenharmony_ci    endif()
79750e409Sopenharmony_ci
89750e409Sopenharmony_ci    add_executable(afl-main afl.c)
99750e409Sopenharmony_ci    target_link_libraries(afl-main "${CJSON_LIB}")
109750e409Sopenharmony_ci
119750e409Sopenharmony_ci    if (NOT ENABLE_SANITIZERS)
129750e409Sopenharmony_ci        message(FATAL_ERROR "Enable sanitizers with -DENABLE_SANITIZERS=On to do fuzzing.")
139750e409Sopenharmony_ci    endif()
149750e409Sopenharmony_ci
159750e409Sopenharmony_ci    option(ENABLE_FUZZING_PRINT "Fuzz printing functions together with parser." On)
169750e409Sopenharmony_ci    set(fuzz_print_parameter "no")
179750e409Sopenharmony_ci    if (ENABLE_FUZZING_PRINT)
189750e409Sopenharmony_ci        set(fuzz_print_parameter "yes")
199750e409Sopenharmony_ci    endif()
209750e409Sopenharmony_ci
219750e409Sopenharmony_ci    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error")
229750e409Sopenharmony_ci
239750e409Sopenharmony_ci    add_custom_target(afl
249750e409Sopenharmony_ci        COMMAND "${AFL_FUZZ}" -i "${CMAKE_CURRENT_SOURCE_DIR}/inputs" -o "${CMAKE_CURRENT_BINARY_DIR}/findings" -x "${CMAKE_CURRENT_SOURCE_DIR}/json.dict" -- "${CMAKE_CURRENT_BINARY_DIR}/afl-main" "@@" "${fuzz_print_parameter}"
259750e409Sopenharmony_ci        DEPENDS afl-main)
269750e409Sopenharmony_ci
279750e409Sopenharmony_ci
289750e409Sopenharmony_ciendif()
299750e409Sopenharmony_ci
309750e409Sopenharmony_ciif(ENABLE_CJSON_TEST)
319750e409Sopenharmony_ci    ADD_EXECUTABLE(fuzz_main fuzz_main.c cjson_read_fuzzer.c)
329750e409Sopenharmony_ci    TARGET_LINK_LIBRARIES(fuzz_main cjson)
339750e409Sopenharmony_ciendif()
349750e409Sopenharmony_ci
35