Skip to content

Commit c17ea5d

Browse files
authored
OSS-Fuzz initial integration (#216)
* add fuzz target * update fuzzer * add fuzzer to build with basic entry point * add build script * cleanup * build fuzz target using cmake in oss-fuzz env * ossfuzz.sh add newline * update build
1 parent 610730f commit c17ea5d

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(SO_PATCH 1)
1515

1616
option(UTF8PROC_INSTALL "Enable installation of utf8proc" On)
1717
option(UTF8PROC_ENABLE_TESTING "Enable testing of utf8proc" Off)
18+
option(LIB_FUZZING_ENGINE "Fuzzing engine to link against" Off)
1819

1920
add_library (utf8proc
2021
utf8proc.c
@@ -98,4 +99,12 @@ if(UTF8PROC_ENABLE_TESTING)
9899
target_link_libraries(normtest utf8proc)
99100
add_test(utf8proc.testgraphemetest graphemetest data/GraphemeBreakTest.txt)
100101
add_test(utf8proc.testnormtest normtest data/NormalizationTest.txt)
102+
103+
if(LIB_FUZZING_ENGINE)
104+
add_executable(fuzzer utf8proc.h test/fuzzer.c)
105+
target_link_libraries(fuzzer ${LIB_FUZZING_ENGINE} utf8proc)
106+
else()
107+
add_executable(fuzzer utf8proc.h test/fuzz_main.c test/fuzzer.c)
108+
target_link_libraries(fuzzer utf8proc)
109+
endif()
101110
endif()

test/fuzz_main.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
5+
/* Fuzz target entry point, works without libFuzzer */
6+
7+
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
8+
9+
int main(int argc, char **argv)
10+
{
11+
FILE *f;
12+
char *buf = NULL;
13+
long siz_buf;
14+
15+
if(argc < 2)
16+
{
17+
fprintf(stderr, "no input file\n");
18+
goto err;
19+
}
20+
21+
f = fopen(argv[1], "rb");
22+
if(f == NULL)
23+
{
24+
fprintf(stderr, "error opening input file %s\n", argv[1]);
25+
goto err;
26+
}
27+
28+
fseek(f, 0, SEEK_END);
29+
30+
siz_buf = ftell(f);
31+
rewind(f);
32+
33+
if(siz_buf < 1) goto err;
34+
35+
buf = (char*)malloc(siz_buf);
36+
if(buf == NULL)
37+
{
38+
fprintf(stderr, "malloc() failed\n");
39+
goto err;
40+
}
41+
42+
if(fread(buf, siz_buf, 1, f) != 1)
43+
{
44+
fprintf(stderr, "fread() failed\n");
45+
goto err;
46+
}
47+
48+
(void)LLVMFuzzerTestOneInput((uint8_t*)buf, siz_buf);
49+
50+
err:
51+
free(buf);
52+
53+
return 0;
54+
}

test/fuzzer.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <utf8proc.h>
2+
3+
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
4+
{
5+
if(size < 1) return 0;
6+
7+
if(data[size-1] != '\0') return 0;
8+
9+
free(utf8proc_NFD(data));
10+
free(utf8proc_NFC(data));
11+
free(utf8proc_NFKD(data));
12+
free(utf8proc_NFKC(data));
13+
free(utf8proc_NFKC_Casefold(data));
14+
15+
return 0;
16+
}

test/ossfuzz.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash -eu
2+
# This script is meant to be run by
3+
# https://github.com/google/oss-fuzz/blob/master/projects/utf8proc/Dockerfile
4+
5+
mkdir build
6+
cd build
7+
cmake .. -DUTF8PROC_ENABLE_TESTING=ON -DLIB_FUZZING_ENGINE="$LIB_FUZZING_ENGINE"
8+
make -j$(nproc)
9+
10+
cp $SRC/utf8proc/build/fuzzer utf8proc_fuzzer
11+
12+
find $SRC/utf8proc/test -name "*.txt" | \
13+
xargs zip $OUT/utf8proc_fuzzer_seed_corpus.zip

0 commit comments

Comments
 (0)