Skip to content

Commit 00ba25c

Browse files
Merge pull request commonmark#304 from kevinbackhouse/quadratic-fuzz-2
Fuzz target for finding quadratic performance issues
2 parents 57d5e09 + 4604792 commit 00ba25c

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ option(CMARK_TESTS "Build cmark-gfm tests and enable testing" ON)
1818
option(CMARK_STATIC "Build static libcmark-gfm library" ON)
1919
option(CMARK_SHARED "Build shared libcmark-gfm library" ON)
2020
option(CMARK_LIB_FUZZER "Build libFuzzer fuzzing harness" OFF)
21+
option(CMARK_FUZZ_QUADRATIC "Build quadratic fuzzing harness" OFF)
22+
23+
if(CMARK_FUZZ_QUADRATIC)
24+
set(FUZZER_FLAGS "-fsanitize=fuzzer-no-link,address -g")
25+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FUZZER_FLAGS}")
26+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FUZZER_FLAGS}")
27+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FUZZER_FLAGS}")
28+
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FUZZER_FLAGS}")
29+
endif()
2130

2231
add_subdirectory(src)
2332
add_subdirectory(extensions)
@@ -29,6 +38,9 @@ if(CMARK_TESTS)
2938
enable_testing()
3039
add_subdirectory(test testdir)
3140
endif()
41+
if(CMARK_FUZZ_QUADRATIC)
42+
add_subdirectory(fuzz)
43+
endif()
3244

3345
if(NOT CMAKE_BUILD_TYPE)
3446
set(CMAKE_BUILD_TYPE "Release" CACHE STRING

fuzz/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
include_directories(
2+
${PROJECT_BINARY_DIR}/extensions
3+
${PROJECT_BINARY_DIR}/src
4+
../extensions
5+
../src
6+
)
7+
8+
macro(fuzzer name)
9+
add_executable(${name} ${name}.c)
10+
set_target_properties(${name}
11+
PROPERTIES
12+
COMPILE_FLAGS "-fsanitize=fuzzer"
13+
LINK_FLAGS "-fsanitize=fuzzer")
14+
if(CMARK_SHARED)
15+
target_link_libraries(${name} libcmark-gfm-extensions libcmark-gfm)
16+
elseif(CMARK_STATIC)
17+
target_link_libraries(${name} libcmark-gfm-extensions_static libcmark-gfm_static)
18+
endif()
19+
endmacro()
20+
21+
fuzzer(fuzz_quadratic)

fuzz/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The quadratic fuzzer generates long sequences of repeated characters, such as `<?x<?x<?x<?x...`,
2+
to detect quadratic complexity performance issues.
3+
4+
To build and run the quadratic fuzzer:
5+
6+
```bash
7+
mkdir build-fuzz
8+
cd build-fuzz
9+
cmake -DCMARK_FUZZ_QUADRATIC=ON -DCMAKE_C_COMPILER=$(which clang) -DCMAKE_CXX_COMPILER=$(which clang++) -DCMAKE_BUILD_TYPE=Release ..
10+
make
11+
../fuzz/fuzzloop.sh
12+
```

fuzz/fuzz_quadratic.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "cmark-gfm.h"
5+
#include "cmark-gfm-core-extensions.h"
6+
#include <sys/types.h>
7+
#include <sys/stat.h>
8+
#include <fcntl.h>
9+
#include <unistd.h>
10+
11+
const char *extension_names[] = {
12+
"autolink",
13+
"strikethrough",
14+
"table",
15+
"tagfilter",
16+
NULL,
17+
};
18+
19+
int LLVMFuzzerInitialize(int *argc, char ***argv) {
20+
cmark_init_standard_node_flags();
21+
cmark_gfm_core_extensions_ensure_registered();
22+
return 0;
23+
}
24+
25+
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
26+
struct __attribute__((packed)) {
27+
int options;
28+
int width;
29+
uint8_t splitpoint;
30+
uint8_t repeatlen;
31+
} fuzz_config;
32+
33+
if (size >= sizeof(fuzz_config)) {
34+
/* The beginning of `data` is treated as fuzzer configuration */
35+
memcpy(&fuzz_config, data, sizeof(fuzz_config));
36+
37+
/* Test options that are used by GitHub. */
38+
fuzz_config.options = CMARK_OPT_UNSAFE | CMARK_OPT_FOOTNOTES | CMARK_OPT_GITHUB_PRE_LANG | CMARK_OPT_HARDBREAKS;
39+
40+
/* Remainder of input is the markdown */
41+
const char *markdown0 = (const char *)(data + sizeof(fuzz_config));
42+
const size_t markdown_size0 = size - sizeof(fuzz_config);
43+
char markdown[0x80000];
44+
if (markdown_size0 <= sizeof(markdown)) {
45+
size_t markdown_size = 0;
46+
if (fuzz_config.splitpoint <= markdown_size0 && 0 < fuzz_config.repeatlen &&
47+
fuzz_config.repeatlen <= markdown_size0 - fuzz_config.splitpoint) {
48+
const size_t size_after_splitpoint = markdown_size0 - fuzz_config.splitpoint - fuzz_config.repeatlen;
49+
memcpy(&markdown[markdown_size], &markdown0[0], fuzz_config.splitpoint);
50+
markdown_size += fuzz_config.splitpoint;
51+
52+
while (markdown_size + fuzz_config.repeatlen + size_after_splitpoint <= sizeof(markdown)) {
53+
memcpy(&markdown[markdown_size], &markdown0[fuzz_config.splitpoint],
54+
fuzz_config.repeatlen);
55+
markdown_size += fuzz_config.repeatlen;
56+
}
57+
memcpy(&markdown[markdown_size], &markdown0[fuzz_config.splitpoint + fuzz_config.repeatlen],
58+
size_after_splitpoint);
59+
markdown_size += size_after_splitpoint;
60+
} else {
61+
markdown_size = markdown_size0;
62+
memcpy(markdown, markdown0, markdown_size);
63+
}
64+
65+
cmark_parser *parser = cmark_parser_new(fuzz_config.options);
66+
67+
for (const char **it = extension_names; *it; ++it) {
68+
const char *extension_name = *it;
69+
cmark_syntax_extension *syntax_extension = cmark_find_syntax_extension(extension_name);
70+
if (!syntax_extension) {
71+
fprintf(stderr, "%s is not a valid syntax extension\n", extension_name);
72+
abort();
73+
}
74+
cmark_parser_attach_syntax_extension(parser, syntax_extension);
75+
}
76+
77+
cmark_parser_feed(parser, markdown, markdown_size);
78+
cmark_node *doc = cmark_parser_finish(parser);
79+
80+
free(cmark_render_html(doc, fuzz_config.options, NULL));
81+
82+
cmark_node_free(doc);
83+
cmark_parser_free(parser);
84+
}
85+
}
86+
return 0;
87+
}

fuzz/fuzzloop.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
# Stop when an error is found
4+
set -e
5+
6+
# Create a corpus sub-directory if it doesn't already exist.
7+
mkdir -p corpus
8+
9+
# The memory and disk usage grows over time, so this loop restarts the
10+
# fuzzer every 4 hours. The `-merge=1` option is used to minimize the
11+
# corpus on each iteration.
12+
while :
13+
do
14+
date
15+
echo restarting loop
16+
17+
# Minimize the corpus
18+
mv corpus/ corpus2
19+
mkdir corpus
20+
echo minimizing corpus
21+
./fuzz/fuzz_quadratic -merge=1 corpus ../bench corpus2/ -max_len=1024
22+
rm -r corpus2
23+
24+
# Run the fuzzer for 4 hours
25+
date
26+
echo start fuzzer
27+
./fuzz/fuzz_quadratic corpus -dict=../test/fuzzing_dictionary -jobs=$(nproc) -workers=$(nproc) -max_len=1024 -max_total_time=14400
28+
done

0 commit comments

Comments
 (0)