##// END OF EJS Templates
fuzz: add a fuzzer for xdiff...
Augie Fackler -
r36697:624cbd14 default
parent child Browse files
Show More
@@ -0,0 +1,67 b''
1 /*
2 * xdiff.cc - fuzzer harness for thirdparty/xdiff
3 *
4 * Copyright 2018, Google Inc.
5 *
6 * This software may be used and distributed according to the terms of
7 * the GNU General Public License, incorporated herein by reference.
8 */
9 #include "thirdparty/xdiff/xdiff.h"
10 #include <inttypes.h>
11 #include <stdlib.h>
12
13 extern "C" {
14
15 int hunk_consumer(long a1, long a2, long b1, long b2, void *priv)
16 {
17 // TODO: probably also test returning -1 from this when things break?
18 return 0;
19 }
20
21 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
22 {
23 if (!Size) {
24 return 0;
25 }
26 // figure out a random point in [0, Size] to split our input.
27 size_t split = Data[0] / 255.0 * Size;
28
29 mmfile_t a, b;
30
31 // `a` input to diff is data[1:split]
32 a.ptr = (char *)Data + 1;
33 // which has len split-1
34 a.size = split - 1;
35 // `b` starts at the next byte after `a` ends
36 b.ptr = a.ptr + a.size;
37 b.size = Size - split;
38 xpparam_t xpp = {
39 XDF_INDENT_HEURISTIC, /* flags */
40 NULL, /* anchors */
41 0, /* anchors_nr */
42 };
43 xdemitconf_t xecfg = {
44 0, /* ctxlen */
45 0, /* interhunkctxlen */
46 XDL_EMIT_BDIFFHUNK, /* flags */
47 NULL, /* find_func */
48 NULL, /* find_func_priv */
49 hunk_consumer, /* hunk_consume_func */
50 };
51 xdemitcb_t ecb = {
52 NULL, /* priv */
53 NULL, /* outf */
54 };
55 xdl_diff(&a, &b, &xpp, &xecfg, &ecb);
56 return 0; // Non-zero return values are reserved for future use.
57 }
58
59 #ifdef HG_FUZZER_INCLUDE_MAIN
60 int main(int argc, char **argv)
61 {
62 const char data[] = "asdf";
63 return LLVMFuzzerTestOneInput((const uint8_t *)data, 4);
64 }
65 #endif
66
67 } // extern "C"
@@ -13,8 +13,28 b' bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o'
13 $$CXX $$CXXFLAGS -std=c++11 -I../../mercurial bdiff.cc \
13 $$CXX $$CXXFLAGS -std=c++11 -I../../mercurial bdiff.cc \
14 bdiff-oss-fuzz.o -lFuzzingEngine -o $$OUT/bdiff_fuzzer
14 bdiff-oss-fuzz.o -lFuzzingEngine -o $$OUT/bdiff_fuzzer
15
15
16 all: bdiff
16 x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
17 clang -g -O1 -fsanitize=fuzzer-no-link,address -c \
18 -o $@ \
19 $<
20
21 xdiff: xdiff.cc xdiffi.o xemit.o xmerge.o xprepare.o xutils.o
22 clang -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
23 -I../../mercurial xdiff.cc \
24 xdiffi.o xemit.o xmerge.o xprepare.o xutils.o -o xdiff
17
25
18 oss-fuzz: bdiff_fuzzer
26 fuzz-x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
27 $$CC $$CFLAGS -c \
28 -o $@ \
29 $<
30
31 xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xemit.o fuzz-xmerge.o fuzz-xprepare.o fuzz-xutils.o
32 $$CXX $$CXXFLAGS -std=c++11 -I../../mercurial xdiff.cc \
33 fuzz-xdiffi.o fuzz-xemit.o fuzz-xmerge.o fuzz-xprepare.o fuzz-xutils.o \
34 -lFuzzingEngine -o $$OUT/xdiff_fuzzer
35
36 all: bdiff xdiff
37
38 oss-fuzz: bdiff_fuzzer xdiff_fuzzer
19
39
20 .PHONY: all oss-fuzz
40 .PHONY: all oss-fuzz
General Comments 0
You need to be logged in to leave comments. Login now