contrib/fuzz/xdiff.cc
author Augie Fackler <augie@google.com>
Fri, 06 Dec 2019 15:19:47 -0500
changeset 43813 5a9e2ae9899b
parent 43809 51a99e09c54b
child 43816 d37658efbec2
permissions -rw-r--r--
fuzz: use a more standard approach to allow local builds of fuzzers This is taken from the (improved since we started fuzzing) guide on ideal integrations. Rather than have our own wonky targets for building outside the fuzzer universe, we have a driver program we carry along and use when we're not using LibFuzzer. This will let us jettison a fair amount of goo. contrib/fuzz/standalone_fuzz_target_runner.cc is https://github.com/google/oss-fuzz/ file projects/example/my-api-repo/standalone from git revision c4579d9358a73ea5dbcc99cb985de1f2bf76dcf7, reformatted with out clang-format settings and a no-check-code comment added. It allows running a single test input through a fuzzer, rather than performing ongoing fuzzing as libfuzzer would. contrib/fuzz/FuzzedDataProvider.h is https://github.com/llvm/llvm-project/ file /compiler-rt/include/fuzzer/FuzzedDataProvider.h from git revision a44ef027ebca1598892ea9b104d6189aeb3bc2f0, reformatted with our clang-format settings and a no-check-code comment added. We can discard this if we instead want to add an hghave check for a new enough llvm that includes FuzzedDataProvder.h in the fuzzer headers. Differential Revision: https://phab.mercurial-scm.org/D7564
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36679
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
/*
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
 * xdiff.cc - fuzzer harness for thirdparty/xdiff
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
 *
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
 * Copyright 2018, Google Inc.
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
 *
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
 * This software may be used and distributed according to the terms of
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
 * the GNU General Public License, incorporated herein by reference.
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
 */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
#include "thirdparty/xdiff/xdiff.h"
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
#include <inttypes.h>
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
#include <stdlib.h>
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
43813
5a9e2ae9899b fuzz: use a more standard approach to allow local builds of fuzzers
Augie Fackler <augie@google.com>
parents: 43809
diff changeset
    13
#include "FuzzedDataProvider.h"
38173
fa0ddd5e8fff fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents: 36765
diff changeset
    14
36679
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
extern "C" {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
43809
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43614
diff changeset
    17
int LLVMFuzzerInitialize(int *argc, char ***argv)
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43614
diff changeset
    18
{
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43614
diff changeset
    19
	return 0;
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43614
diff changeset
    20
}
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43614
diff changeset
    21
36679
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
int hunk_consumer(long a1, long a2, long b1, long b2, void *priv)
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
{
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
	// TODO: probably also test returning -1 from this when things break?
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
	return 0;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
}
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
{
41139
2e60a77b7058 xdiff: don't attempt to use fuzzer inputs larger than 100k
Augie Fackler <augie@google.com>
parents: 38173
diff changeset
    30
	// Don't allow fuzzer inputs larger than 100k, since we'll just bog
2e60a77b7058 xdiff: don't attempt to use fuzzer inputs larger than 100k
Augie Fackler <augie@google.com>
parents: 38173
diff changeset
    31
	// down and not accomplish much.
2e60a77b7058 xdiff: don't attempt to use fuzzer inputs larger than 100k
Augie Fackler <augie@google.com>
parents: 38173
diff changeset
    32
	if (Size > 100000) {
2e60a77b7058 xdiff: don't attempt to use fuzzer inputs larger than 100k
Augie Fackler <augie@google.com>
parents: 38173
diff changeset
    33
		return 0;
2e60a77b7058 xdiff: don't attempt to use fuzzer inputs larger than 100k
Augie Fackler <augie@google.com>
parents: 38173
diff changeset
    34
	}
43614
78df32a8b6f4 fuzz: migrate xdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 41139
diff changeset
    35
	FuzzedDataProvider provider(Data, Size);
78df32a8b6f4 fuzz: migrate xdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 41139
diff changeset
    36
	std::string left = provider.ConsumeRandomLengthString(Size);
78df32a8b6f4 fuzz: migrate xdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 41139
diff changeset
    37
	std::string right = provider.ConsumeRemainingBytesAsString();
36679
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
	mmfile_t a, b;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
43614
78df32a8b6f4 fuzz: migrate xdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 41139
diff changeset
    40
	a.ptr = (char *)left.c_str();
78df32a8b6f4 fuzz: migrate xdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 41139
diff changeset
    41
	a.size = left.size();
78df32a8b6f4 fuzz: migrate xdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 41139
diff changeset
    42
	b.ptr = (char *)right.c_str();
78df32a8b6f4 fuzz: migrate xdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 41139
diff changeset
    43
	b.size = right.size();
36679
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
	xpparam_t xpp = {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
	    XDF_INDENT_HEURISTIC, /* flags */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
	};
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
	xdemitconf_t xecfg = {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
	    XDL_EMIT_BDIFFHUNK, /* flags */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
	    hunk_consumer,      /* hunk_consume_func */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
	};
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
	xdemitcb_t ecb = {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
	    NULL, /* priv */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
	};
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
	xdl_diff(&a, &b, &xpp, &xecfg, &ecb);
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
	return 0; // Non-zero return values are reserved for future use.
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
}
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    58
#ifdef HG_FUZZER_INCLUDE_MAIN
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
int main(int argc, char **argv)
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
{
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
	const char data[] = "asdf";
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    62
	return LLVMFuzzerTestOneInput((const uint8_t *)data, 4);
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    63
}
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
#endif
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
} // extern "C"