##// 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 1 fuzzutil.o: fuzzutil.cc fuzzutil.h
2 2 $$CXX $$CXXFLAGS -g -O1 -fsanitize=fuzzer-no-link,address \
3 3 -std=c++17 \
4 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 10 bdiff.o: ../../mercurial/bdiff.c
7 11 $$CC $$CFLAGS -fsanitize=fuzzer-no-link,address -c -o bdiff.o \
8 12 ../../mercurial/bdiff.c
9 13
10 14 bdiff: bdiff.cc bdiff.o fuzzutil.o
11 15 $$CXX $$CXXFLAGS -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
12 16 -std=c++17 \
13 17 -I../../mercurial bdiff.cc bdiff.o fuzzutil.o -o bdiff
14 18
15 19 bdiff-oss-fuzz.o: ../../mercurial/bdiff.c
16 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 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 27 x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
23 28 $$CC -g -O1 -fsanitize=fuzzer-no-link,address -c \
24 29 -o $@ \
25 30 $<
26 31
27 32 xdiff: xdiff.cc xdiffi.o xprepare.o xutils.o fuzzutil.o
28 33 $$CXX $$CXXFLAGS -DHG_FUZZER_INCLUDE_MAIN=1 -g -O1 -fsanitize=fuzzer-no-link,address \
29 34 -I../../mercurial xdiff.cc \
30 35 xdiffi.o xprepare.o xutils.o fuzzutil.o -o xdiff
31 36
32 37 fuzz-x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
33 38 $$CC $$CFLAGS -c \
34 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 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 45 -lFuzzingEngine -o $$OUT/xdiff_fuzzer
41 46
42 47 all: bdiff xdiff
43 48
44 49 oss-fuzz: bdiff_fuzzer xdiff_fuzzer
45 50
46 51 .PHONY: all oss-fuzz
@@ -1,26 +1,26 b''
1 1 #include "fuzzutil.h"
2 2
3 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 7 if (!Size) {
8 return std::nullopt;
8 return contrib::nullopt;
9 9 }
10 10 // figure out a random point in [0, Size] to split our input.
11 11 size_t left_size = (Data[0] / 255.0) * (Size - 1);
12 12
13 13 // Copy inputs to new allocations so if bdiff over-reads
14 14 // AddressSanitizer can detect it.
15 15 std::unique_ptr<char[]> left(new char[left_size]);
16 16 memcpy(left.get(), Data + 1, left_size);
17 17 // right starts at the next byte after left ends
18 18 size_t right_size = Size - (left_size + 1);
19 19 std::unique_ptr<char[]> right(new char[right_size]);
20 20 memcpy(right.get(), Data + 1 + left_size, right_size);
21 21 LOG(2) << "inputs are " << left_size << " and " << right_size
22 22 << " bytes" << std::endl;
23 23 two_inputs result = {std::move(right), right_size, std::move(left),
24 24 left_size};
25 25 return result;
26 26 }
@@ -1,24 +1,47 b''
1 1 #ifndef CONTRIB_FUZZ_FUZZUTIL_H
2 2 #define CONTRIB_FUZZ_FUZZUTIL_H
3 3 #include <iostream>
4 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 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 31 /* set DEBUG to 1 for a few debugging prints, or 2 for a lot */
9 32 #define DEBUG 0
10 33 #define LOG(level) \
11 34 if (level <= DEBUG) \
12 35 std::cout
13 36
14 37 struct two_inputs {
15 38 std::unique_ptr<char[]> right;
16 39 size_t right_size;
17 40 std::unique_ptr<char[]> left;
18 41 size_t left_size;
19 42 };
20 43
21 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 47 #endif /* CONTRIB_FUZZ_FUZZUTIL_H */
General Comments 0
You need to be logged in to leave comments. Login now