From c73ebb85374164515eb9df6d619a5822b6568599 Mon Sep 17 00:00:00 2001 From: Max Dymond Date: Mon, 11 Sep 2017 20:51:58 +0100 Subject: ossfuzz: changes before merging the generated corpora Before merging in the oss-fuzz corpora from Google, there are some changes to the fuzzer. - Add a read corpus script, to display corpus files nicely. - Change the behaviour of the fuzzer so that TLV parse failures all now go down the same execution paths, which should reduce the size of the corpora. - Make unknown TLVs a failure to parse, which should decrease the size of the corpora as well. Closes #1881 --- tests/fuzz/read_corpus.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 tests/fuzz/read_corpus.py (limited to 'tests/fuzz/read_corpus.py') diff --git a/tests/fuzz/read_corpus.py b/tests/fuzz/read_corpus.py new file mode 100755 index 000000000..bb8fcedcd --- /dev/null +++ b/tests/fuzz/read_corpus.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# +# Simple script which reads corpus files. + +import argparse +import logging +import sys +import corpus +log = logging.getLogger(__name__) + + +def read_corpus(options): + with open(options.input, "rb") as f: + dec = corpus.TLVDecoder(f.read()) + for tlv in dec: + print(tlv) + + return ScriptRC.SUCCESS + + +def get_options(): + parser = argparse.ArgumentParser() + parser.add_argument("--input", required=True) + return parser.parse_args() + + +def setup_logging(): + """ + Set up logging from the command line options + """ + root_logger = logging.getLogger() + formatter = logging.Formatter("%(asctime)s %(levelname)-5.5s %(message)s") + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.setFormatter(formatter) + stdout_handler.setLevel(logging.DEBUG) + root_logger.addHandler(stdout_handler) + root_logger.setLevel(logging.DEBUG) + + +class ScriptRC(object): + """Enum for script return codes""" + SUCCESS = 0 + FAILURE = 1 + EXCEPTION = 2 + + +class ScriptException(Exception): + pass + + +def main(): + # Get the options from the user. + options = get_options() + + setup_logging() + + # Run main script. + try: + rc = read_corpus(options) + except Exception as e: + log.exception(e) + rc = ScriptRC.EXCEPTION + + log.info("Returning %d", rc) + return rc + + +if __name__ == '__main__': + sys.exit(main()) -- cgit v1.2.3