Skip to content

Commit e8561d3

Browse files
committed
Revise chewing fuzzer
1. Specify unique name for temp userdb. So we can run multiple fuzzers at the same time. 2. Reorganized as three fuzzers for different variants. So we can prioritize easily (default first, dynamic config last)
1 parent c55adb5 commit e8561d3

8 files changed

+92
-46
lines changed

libchewing/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ MAINTAINER kcwu@csie.org
1919
RUN apt-get install -y make autoconf automake libtool texinfo
2020

2121
RUN git clone https://github.com/chewing/libchewing.git
22-
COPY build.sh chewing_fuzzer.c /src/
22+
COPY build.sh chewing_fuzzer_common.[ch] chewing_*_fuzzer.c /src/

libchewing/build.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ make clean all
2525
# build your fuzzer(s)
2626
make -C test CFLAGS="$CFLAGS -Dmain=stress_main -Drand=get_fuzz_input" stress.o
2727

28-
$CC $CFLAGS \
29-
-o /out/chewing_fuzzer \
30-
/src/chewing_fuzzer.c \
31-
test/stress.o test/.libs/libtesthelper.a src/.libs/libchewing.a \
32-
-lfuzzer $FUZZER_LDFLAGS
28+
for variant in default random_init dynamic_config; do
29+
$CC $CFLAGS \
30+
-o /out/chewing_${variant}_fuzzer \
31+
/src/chewing_${variant}_fuzzer.c /src/chewing_fuzzer_common.c \
32+
test/stress.o test/.libs/libtesthelper.a src/.libs/libchewing.a \
33+
-lfuzzer $FUZZER_LDFLAGS
34+
done
3335

3436
# install data files
3537
make -C data pkgdatadir=/out install
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
3+
#include "chewing_fuzzer_common.h"
4+
5+
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
6+
fuzz_input = fuzz_ptr = data;
7+
fuzz_size = size;
8+
9+
const char* stress_argv[] = {
10+
"./chewing_fuzzer", "-loop", "1", NULL,
11+
};
12+
stress_main(sizeof(stress_argv) / sizeof(stress_argv[0]) - 1,
13+
(char**)stress_argv);
14+
return 0;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
3+
#include "chewing_fuzzer_common.h"
4+
5+
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
6+
fuzz_input = fuzz_ptr = data;
7+
fuzz_size = size;
8+
9+
const char* stress_argv[] = {
10+
"./chewing_fuzzer", "-loop", "1", "-extra", NULL,
11+
};
12+
stress_main(sizeof(stress_argv) / sizeof(stress_argv[0]) - 1,
13+
(char**)stress_argv);
14+
return 0;
15+
}

libchewing/chewing_fuzzer.c

Lines changed: 0 additions & 40 deletions
This file was deleted.

libchewing/chewing_fuzzer_common.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "chewing_fuzzer_common.h"
2+
3+
#include <libgen.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
static char userphrase_path[] = "/tmp/chewing_userphrase.db.XXXXXX";
8+
9+
int LLVMFuzzerInitialize(int* argc, char*** argv) {
10+
char* exe_path = (*argv)[0];
11+
char* dir = dirname(exe_path);
12+
// Assume data files are at the same location as executable.
13+
setenv("CHEWING_PATH", dir, 0);
14+
15+
// Specify user db of this process. So we can run multiple fuzzers at the
16+
// same time.
17+
mktemp(userphrase_path);
18+
setenv("TEST_USERPHRASE_PATH", userphrase_path, 0);
19+
return 0;
20+
}
21+
22+
int get_fuzz_input() {
23+
if (fuzz_ptr - fuzz_input >= fuzz_size)
24+
return EOF;
25+
return *fuzz_ptr++;
26+
}

libchewing/chewing_fuzzer_common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CHEWING_FUZZER_COMMON_H
2+
#define CHEWING_FUZZER_COMMON_H
3+
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
7+
const uint8_t* fuzz_ptr;
8+
const uint8_t* fuzz_input;
9+
size_t fuzz_size;
10+
11+
int stress_main(int argc, char** argv);
12+
13+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
3+
#include "chewing_fuzzer_common.h"
4+
5+
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
6+
fuzz_input = fuzz_ptr = data;
7+
fuzz_size = size;
8+
9+
const char* stress_argv[] = {
10+
"./chewing_fuzzer", "-loop", "1", "-init", "-extra", NULL,
11+
};
12+
stress_main(sizeof(stress_argv) / sizeof(stress_argv[0]) - 1,
13+
(char**)stress_argv);
14+
return 0;
15+
}

0 commit comments

Comments
 (0)