##// END OF EJS Templates
fuzzutil: make it possible to use absl when C++17 isn't supported...
Augie Fackler -
r38192:36d55f90 default
parent child Browse files
Show More
@@ -1,46 +1,51 b''
1 fuzzutil.o: fuzzutil.cc fuzzutil.h
1 fuzzutil.o: fuzzutil.cc fuzzutil.h
2 $$CXX $$CXXFLAGS -g -O1 -fsanitize=fuzzer-no-link,address \
2 $$CXX $$CXXFLAGS -g -O1 -fsanitize=fuzzer-no-link,address \
3 -std=c++17 \
3 -std=c++17 \
4 -I../../mercurial -c -o fuzzutil.o fuzzutil.cc
4 -I../../mercurial -c -o fuzzutil.o fuzzutil.cc
5
5
6 fuzzutil-oss-fuzz.o: fuzzutil.cc fuzzutil.h
7 $$CXX $$CXXFLAGS -std=c++17 \
8 -I../../mercurial -c -o fuzzutil-oss-fuzz.o fuzzutil.cc
9
6 bdiff.o: ../../mercurial/bdiff.c
10 bdiff.o: ../../mercurial/bdiff.c
7 $$CC $$CFLAGS -fsanitize=fuzzer-no-link,address -c -o bdiff.o \
11 $$CC $$CFLAGS -fsanitize=fuzzer-no-link,address -c -o bdiff.o \
8 ../../mercurial/bdiff.c
12 ../../mercurial/bdiff.c
9
13
10 bdiff: bdiff.cc bdiff.o fuzzutil.o
14 bdiff: bdiff.cc bdiff.o fuzzutil.o
11 $$CXX $$CXXFLAGS -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
15 $$CXX $$CXXFLAGS -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
12 -std=c++17 \
16 -std=c++17 \
13 -I../../mercurial bdiff.cc bdiff.o fuzzutil.o -o bdiff
17 -I../../mercurial bdiff.cc bdiff.o fuzzutil.o -o bdiff
14
18
15 bdiff-oss-fuzz.o: ../../mercurial/bdiff.c
19 bdiff-oss-fuzz.o: ../../mercurial/bdiff.c
16 $$CC $$CFLAGS -c -o bdiff-oss-fuzz.o ../../mercurial/bdiff.c
20 $$CC $$CFLAGS -c -o bdiff-oss-fuzz.o ../../mercurial/bdiff.c
17
21
18 bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o fuzzutil.o
22 bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o
19 $$CXX $$CXXFLAGS -std=c++17 -I../../mercurial bdiff.cc \
23 $$CXX $$CXXFLAGS -std=c++17 -I../../mercurial bdiff.cc \
20 bdiff-oss-fuzz.o fuzzutil.o -lFuzzingEngine -o $$OUT/bdiff_fuzzer
24 bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o -lFuzzingEngine -o \
25 $$OUT/bdiff_fuzzer
21
26
22 x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
27 x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
23 $$CC -g -O1 -fsanitize=fuzzer-no-link,address -c \
28 $$CC -g -O1 -fsanitize=fuzzer-no-link,address -c \
24 -o $@ \
29 -o $@ \
25 $<
30 $<
26
31
27 xdiff: xdiff.cc xdiffi.o xprepare.o xutils.o fuzzutil.o
32 xdiff: xdiff.cc xdiffi.o xprepare.o xutils.o fuzzutil.o
28 $$CXX $$CXXFLAGS -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
33 $$CXX $$CXXFLAGS -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
29 -I../../mercurial xdiff.cc \
34 -I../../mercurial xdiff.cc \
30 xdiffi.o xprepare.o xutils.o fuzzutil.o -o xdiff
35 xdiffi.o xprepare.o xutils.o fuzzutil.o -o xdiff
31
36
32 fuzz-x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
37 fuzz-x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
33 $$CC $$CFLAGS -c \
38 $$CC $$CFLAGS -c \
34 -o $@ \
39 -o $@ \
35 $<
40 $<
36
41
37 xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil.o
42 xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o
38 $$CXX $$CXXFLAGS -std=c++17 -I../../mercurial xdiff.cc \
43 $$CXX $$CXXFLAGS -std=c++17 -I../../mercurial xdiff.cc \
39 fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil.o \
44 fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o \
40 -lFuzzingEngine -o $$OUT/xdiff_fuzzer
45 -lFuzzingEngine -o $$OUT/xdiff_fuzzer
41
46
42 all: bdiff xdiff
47 all: bdiff xdiff
43
48
44 oss-fuzz: bdiff_fuzzer xdiff_fuzzer
49 oss-fuzz: bdiff_fuzzer xdiff_fuzzer
45
50
46 .PHONY: all oss-fuzz
51 .PHONY: all oss-fuzz
@@ -1,26 +1,26 b''
1 #include "fuzzutil.h"
1 #include "fuzzutil.h"
2
2
3 #include <utility>
3 #include <utility>
4
4
5 std::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size)
5 contrib::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size)
6 {
6 {
7 if (!Size) {
7 if (!Size) {
8 return std::nullopt;
8 return contrib::nullopt;
9 }
9 }
10 // figure out a random point in [0, Size] to split our input.
10 // figure out a random point in [0, Size] to split our input.
11 size_t left_size = (Data[0] / 255.0) * (Size - 1);
11 size_t left_size = (Data[0] / 255.0) * (Size - 1);
12
12
13 // Copy inputs to new allocations so if bdiff over-reads
13 // Copy inputs to new allocations so if bdiff over-reads
14 // AddressSanitizer can detect it.
14 // AddressSanitizer can detect it.
15 std::unique_ptr<char[]> left(new char[left_size]);
15 std::unique_ptr<char[]> left(new char[left_size]);
16 memcpy(left.get(), Data + 1, left_size);
16 memcpy(left.get(), Data + 1, left_size);
17 // right starts at the next byte after left ends
17 // right starts at the next byte after left ends
18 size_t right_size = Size - (left_size + 1);
18 size_t right_size = Size - (left_size + 1);
19 std::unique_ptr<char[]> right(new char[right_size]);
19 std::unique_ptr<char[]> right(new char[right_size]);
20 memcpy(right.get(), Data + 1 + left_size, right_size);
20 memcpy(right.get(), Data + 1 + left_size, right_size);
21 LOG(2) << "inputs are " << left_size << " and " << right_size
21 LOG(2) << "inputs are " << left_size << " and " << right_size
22 << " bytes" << std::endl;
22 << " bytes" << std::endl;
23 two_inputs result = {std::move(right), right_size, std::move(left),
23 two_inputs result = {std::move(right), right_size, std::move(left),
24 left_size};
24 left_size};
25 return result;
25 return result;
26 }
26 }
@@ -1,24 +1,47 b''
1 #ifndef CONTRIB_FUZZ_FUZZUTIL_H
1 #ifndef CONTRIB_FUZZ_FUZZUTIL_H
2 #define CONTRIB_FUZZ_FUZZUTIL_H
2 #define CONTRIB_FUZZ_FUZZUTIL_H
3 #include <iostream>
3 #include <iostream>
4 #include <memory>
4 #include <memory>
5 #include <stdint.h>
6
7 /* Try and use std::optional, but failing that assume we'll have a
8 * workable https://abseil.io/ install on the include path to get
9 * their backport of std::optional. */
10 #ifdef __has_include
11 #if __has_include(<optional>) && __cplusplus >= 201703L
5 #include <optional>
12 #include <optional>
6 #include <stdint.h>
13 #define CONTRIB_FUZZ_HAVE_STD_OPTIONAL
14 #endif
15 #endif
16 #ifdef CONTRIB_FUZZ_HAVE_STD_OPTIONAL
17 namespace contrib
18 {
19 using std::nullopt;
20 using std::optional;
21 } /* namespace contrib */
22 #else
23 #include "third_party/absl/types/optional.h"
24 namespace contrib
25 {
26 using absl::nullopt;
27 using absl::optional;
28 } /* namespace contrib */
29 #endif
7
30
8 /* set DEBUG to 1 for a few debugging prints, or 2 for a lot */
31 /* set DEBUG to 1 for a few debugging prints, or 2 for a lot */
9 #define DEBUG 0
32 #define DEBUG 0
10 #define LOG(level) \
33 #define LOG(level) \
11 if (level <= DEBUG) \
34 if (level <= DEBUG) \
12 std::cout
35 std::cout
13
36
14 struct two_inputs {
37 struct two_inputs {
15 std::unique_ptr<char[]> right;
38 std::unique_ptr<char[]> right;
16 size_t right_size;
39 size_t right_size;
17 std::unique_ptr<char[]> left;
40 std::unique_ptr<char[]> left;
18 size_t left_size;
41 size_t left_size;
19 };
42 };
20
43
21 /* Split a non-zero-length input into two inputs. */
44 /* Split a non-zero-length input into two inputs. */
22 std::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size);
45 contrib::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size);
23
46
24 #endif /* CONTRIB_FUZZ_FUZZUTIL_H */
47 #endif /* CONTRIB_FUZZ_FUZZUTIL_H */
General Comments 0
You need to be logged in to leave comments. Login now