contrib/fuzz/xdiff.cc
author Jun Wu <quark@fb.com>
Tue, 06 Mar 2018 19:31:17 -0800
changeset 36765 04d64163039a
parent 36679 624cbd1477a6
child 38173 fa0ddd5e8fff
permissions -rw-r--r--
fuzz: fix xdiff build Recent xdiff code cleanups removed some files and changed some structures. Update fuzz code. Differential Revision: https://phab.mercurial-scm.org/D2707
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
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
extern "C" {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
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
    16
{
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
	// 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
    18
	return 0;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
}
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
{
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
	if (!Size) {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
		return 0;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
	}
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
	// figure out a random point in [0, Size] to split our input.
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
	size_t split = Data[0] / 255.0 * Size;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
	mmfile_t a, b;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
	// `a` input to diff is data[1:split]
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
	a.ptr = (char *)Data + 1;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
	// which has len split-1
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
	a.size = split - 1;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
	// `b` starts at the next byte after `a` ends
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
	b.ptr = a.ptr + a.size;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
	b.size = Size - split;
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
	xpparam_t xpp = {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
	    XDF_INDENT_HEURISTIC, /* flags */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
	};
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
	xdemitconf_t xecfg = {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
	    XDL_EMIT_BDIFFHUNK, /* flags */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
	    hunk_consumer,      /* hunk_consume_func */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
	};
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
	xdemitcb_t ecb = {
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
	    NULL, /* priv */
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
	};
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
	xdl_diff(&a, &b, &xpp, &xecfg, &ecb);
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
	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
    50
}
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
#ifdef HG_FUZZER_INCLUDE_MAIN
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
int main(int argc, char **argv)
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
{
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
	const char data[] = "asdf";
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
	return LLVMFuzzerTestOneInput((const uint8_t *)data, 4);
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
#endif
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
624cbd1477a6 fuzz: add a fuzzer for xdiff
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
} // extern "C"