contrib/fuzz/bdiff.cc
author Matt Harbison <matt_harbison@yahoo.com>
Tue, 13 Oct 2020 16:44:57 -0400
changeset 45718 87c35b5a14eb
parent 43834 d37658efbec2
permissions -rw-r--r--
posix: avoid a leaked file descriptor in a unix domain socket exception case Differential Revision: https://phab.mercurial-scm.org/D9206
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35670
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
/*
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
 * bdiff.cc - fuzzer harness for bdiff.c
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
 *
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
 * Copyright 2018, Google Inc.
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
 *
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
 * This software may be used and distributed according to the terms of
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
 * the GNU General Public License, incorporated herein by reference.
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
 */
38206
fa0ddd5e8fff fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents: 35670
diff changeset
     9
#include <memory>
35670
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
#include <stdlib.h>
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
43831
5a9e2ae9899b fuzz: use a more standard approach to allow local builds of fuzzers
Augie Fackler <augie@google.com>
parents: 43827
diff changeset
    12
#include "FuzzedDataProvider.h"
38206
fa0ddd5e8fff fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents: 35670
diff changeset
    13
35670
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
extern "C" {
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
#include "bdiff.h"
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
43827
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43657
diff changeset
    17
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43657
diff changeset
    18
{
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43657
diff changeset
    19
	return 0;
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43657
diff changeset
    20
}
51a99e09c54b fuzz: always define LLVMFuzzerInitialize() even if we don't need it
Augie Fackler <augie@google.com>
parents: 43657
diff changeset
    21
35670
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
{
43657
dbc39f028c9f fuzz: migrate bdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 38206
diff changeset
    24
	FuzzedDataProvider provider(Data, Size);
dbc39f028c9f fuzz: migrate bdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 38206
diff changeset
    25
	std::string left = provider.ConsumeRandomLengthString(Size);
dbc39f028c9f fuzz: migrate bdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 38206
diff changeset
    26
	std::string right = provider.ConsumeRemainingBytesAsString();
35670
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
	struct bdiff_line *a, *b;
43657
dbc39f028c9f fuzz: migrate bdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 38206
diff changeset
    29
	int an = bdiff_splitlines(left.c_str(), left.size(), &a);
dbc39f028c9f fuzz: migrate bdiff fuzzer to use FuzzedDataProvider
Augie Fackler <augie@google.com>
parents: 38206
diff changeset
    30
	int bn = bdiff_splitlines(right.c_str(), right.size(), &b);
35670
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
	struct bdiff_hunk l;
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
	bdiff_diff(a, an, b, bn, &l);
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
	free(a);
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
	free(b);
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
	bdiff_freehunks(l.next);
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
	return 0; // Non-zero return values are reserved for future use.
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
}
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
2b9e2415f5b5 contrib: add some basic scaffolding for some fuzz test targets
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
} // extern "C"