Skip to content

Commit

Permalink
Build: added clang fuzzer support.
Browse files Browse the repository at this point in the history
Use "cmake . -DLEXBOR_BUILD_WITH_FUZZER=ON".
  • Loading branch information
lexborisov committed Jun 6, 2020
1 parent 7cfc4b4 commit 710d76e
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cmake_minimum_required(VERSION 2.8)
# Used with LEXBOR_BUILD_TESTS
# LEXBOR_BUILD_UTILS default: OFF; Build utils/helpers for project.
# LEXBOR_BUILD_WITH_ASAN default: OFF; Build with address sanitizer if possible
# LEXBOR_BUILD_WITH_FUZZER default: OFF; Build with fuzzer tests if possible
# LEXBOR_INSTALL_HEADERS default: ON; The header files will be installed
# if set to ON
# LEXBOR_MAKE_RPM_FILES default: OFF; Create "spec" files in "packaging" direcotry
Expand Down Expand Up @@ -49,6 +50,7 @@ option(LEXBOR_BUILD_TESTS "Build tests" OFF)
option(LEXBOR_BUILD_TESTS_CPP "Build C++ tests" ON)
option(LEXBOR_BUILD_UTILS "Build utils" OFF)
option(LEXBOR_BUILD_WITH_ASAN "Build with address sanitizer" OFF)
option(LEXBOR_BUILD_WITH_FUZZER "Build with fuzzer" OFF)
option(LEXBOR_INSTALL_HEADERS "Install header files" ON)
option(LEXBOR_MAKE_PACKAGES_FILES "Create files for build packages" OFF)
option(LEXBOR_PRINT_MODULE_DEPENDENCIES "Prints dependencies" OFF)
Expand Down Expand Up @@ -251,6 +253,11 @@ IF(${LEXBOR_HAVE_ASAN})
add_definitions(-DLEXBOR_HAVE_ADDRESS_SANITIZER)
ENDIF()

FEATURE_CHECK_FUZZER(LEXBOR_HAVE_FUZZER)
IF(${LEXBOR_HAVE_FUZZER})
add_definitions(-DLEXBOR_HAVE_FUZZER)
ENDIF()

################
## Tests
#########################
Expand Down
49 changes: 49 additions & 0 deletions feature.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,55 @@ int main(void) {
ENDIF()
ENDIF()

unset(lexbor_asan_flags)
unset(FEATUTE_CHECK_STRING)
unset(feature_filename)
ENDMACRO()

MACRO(FEATURE_CHECK_FUZZER out_result)
set(lexbor_fuzzer_flags "-O0 -g -fsanitize=fuzzer")

IF(LEXBOR_BUILD_WITH_FUZZER)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${lexbor_fuzzer_flags}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${lexbor_fuzzer_flags}")
ENDIF()

set(feature_filename "${CMAKE_BINARY_DIR}/feature_check.c")

set(FEATUTE_CHECK_STRING "
#include <sanitizer/asan_interface.h>
int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
return 0;
}")

file(WRITE ${feature_filename} "${FEATUTE_CHECK_STRING}")

try_compile(${out_result} "${CMAKE_BINARY_DIR}" "${feature_filename}"
CMAKE_FLAGS "-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}"
)

IF(${${out_result}})
message(STATUS "Feature Fuzzer: enabled")
ELSE()
message(STATUS "Feature Fuzzer: disable")
ENDIF()

file(REMOVE ${feature_filename})

IF(LEXBOR_BUILD_WITH_FUZZER)
IF(NOT ${${out_result}})
STRING(REGEX REPLACE " ${lexbor_fuzzer_flags}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
STRING(REGEX REPLACE " ${lexbor_fuzzer_flags}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE()
message(STATUS "Updated CFLAGS: ${CMAKE_C_FLAGS}")
message(STATUS "Updated CXXFLAGS: ${CMAKE_CXX_FLAGS}")
ENDIF()
ENDIF()

unset(lexbor_fuzzer_flags)
unset(FEATUTE_CHECK_STRING)
unset(feature_filename)
ENDMACRO()
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ include(unit/CMakeLists.txt)
## Subs
#########################
FIND_AND_APPEND_SUB_DIRS("lexbor" OFF)
FIND_AND_APPEND_SUB_DIRS("fuzzers/lexbor" OFF)
16 changes: 16 additions & 0 deletions test/fuzzers/lexbor/encoding/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 2.8)

################
## Search and Includes
#########################
include_directories(".")

################
## Sources
#########################
file(GLOB_RECURSE FUZZ_LEXBOR_ENCODING_SOURCES "*.c")

################
## Create
#########################
EXECUTABLE_LIST("fuzz_lexbor_encoding_" "${FUZZ_LEXBOR_ENCODING_SOURCES}" ${TEST_UNIT_LIB_NAME} ${LEXBOR_LIB_NAME})
49 changes: 49 additions & 0 deletions test/fuzzers/lexbor/encoding/decode.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
#include <lexbor/encoding/encoding.h>


#ifndef LEXBOR_HAVE_FUZZER
int
main(int argc, const char *argv[])
{
size_t size;
lxb_status_t status;
const lxb_encoding_data_t *encoding;

bool loop = true;

/* Decode */
char inbuf[4096];
const lxb_char_t *data, *end;

lxb_codepoint_t cp[4096];

lxb_encoding_decode_t decode = {0};
lxb_encoding_t encoding_type = (lxb_encoding_t) (argc + 1);

encoding = lxb_encoding_data(encoding_type);

status = lxb_encoding_decode_init(&decode, encoding, cp,
sizeof(cp) / sizeof(lxb_codepoint_t));
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}

do {
size = fread(inbuf, 1, sizeof(inbuf), stdin);
if (size != sizeof(inbuf)) {
if (feof(stdin)) {
loop = false;
}
else {
return EXIT_FAILURE;
}
}

data = (const lxb_char_t *) inbuf;
end = data + size;

encoding->decode(&decode, &data, end);
}
while (loop);

return EXIT_SUCCESS;
}
#endif

int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
Expand Down
16 changes: 16 additions & 0 deletions test/fuzzers/lexbor/html/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 2.8)

################
## Search and Includes
#########################
include_directories(".")

################
## Sources
#########################
file(GLOB_RECURSE FUZZ_LEXBOR_HTML_SOURCES "*.c")

################
## Create
#########################
EXECUTABLE_LIST("fuzz_lexbor_html_" "${FUZZ_LEXBOR_HTML_SOURCES}" ${TEST_UNIT_LIB_NAME} ${LEXBOR_LIB_NAME})
72 changes: 72 additions & 0 deletions test/fuzzers/lexbor/html/document_parse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <lexbor/html/html.h>


#ifndef LEXBOR_HAVE_FUZZER
int
main(int argc, const char *argv[])
{
size_t size;
lxb_status_t status;
lxb_html_document_t *document;
char inbuf[4096];

document = lxb_html_document_create();
if (document == NULL) {
return EXIT_FAILURE;
}

status = lxb_html_document_parse_chunk_begin(document);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}

do {
size = fread(inbuf, 1, sizeof(inbuf), stdin);
if (size != sizeof(inbuf)) {
if (feof(stdin)) {
loop = false;
}
else {
return EXIT_FAILURE;
}
}

status = lxb_html_document_parse_chunk(document,
(const lxb_char_t *) inbuf, size);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}
}
while (loop);

status = lxb_html_document_parse_chunk_end(document);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}

lxb_html_document_destroy(document);

return EXIT_SUCCESS;
}
#endif

int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t length)
{
lxb_status_t status;
lxb_html_document_t *document;

document = lxb_html_document_create();
if (document == NULL) {
return EXIT_FAILURE;
}

status = lxb_html_document_parse(document, data, length);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}

lxb_html_document_destroy(document);

return EXIT_SUCCESS;
}
4 changes: 4 additions & 0 deletions test/lexbor/encoding/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include_directories(".")
################
## Sources
#########################
file(GLOB TEST_LEXBOR_ENCODING_FUZZ_SOURCES "fuzz_*.c")
file(GLOB_RECURSE TEST_LEXBOR_ENCODING_BUFFER_SOURCES "buffer/*.c")
file(GLOB_RECURSE TEST_LEXBOR_ENCODING_SINGLE_SOURCES "single/*.c")

Expand All @@ -31,6 +32,9 @@ set(single_gb18030_arg "${CMAKE_SOURCE_DIR}/test/files/lexbor/encoding/gb18030_m
################
## Create tests
#########################
EXECUTABLE_LIST("lexbor_encoding_" "${TEST_LEXBOR_ENCODING_FUZZ_SOURCES}" ${TEST_UNIT_LIB_NAME} ${LEXBOR_LIB_NAME})
APPEND_TESTS("lexbor_encoding_" "${TEST_LEXBOR_ENCODING_FUZZ_SOURCES}")

EXECUTABLE_LIST("lexbor_encoding_" "${TEST_LEXBOR_ENCODING_BUFFER_SOURCES}" ${TEST_UNIT_LIB_NAME} ${LEXBOR_LIB_NAME})
APPEND_TESTS("lexbor_encoding_" "${TEST_LEXBOR_ENCODING_BUFFER_SOURCES}")

Expand Down

0 comments on commit 710d76e

Please sign in to comment.