File: defuzz

package info (click to toggle)
graphite2 1.3.14-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,588 kB
  • sloc: cpp: 14,738; cs: 1,998; python: 1,737; ansic: 1,673; perl: 184; xml: 123; sh: 104; makefile: 62
file content (108 lines) | stat: -rwxr-xr-x 4,116 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
'''
Clean log file output from the fuzztest test harness.  This tool collapses
duplicate outputs into a single copy preceded by all the fuzz lines that
triggered it. To allow useful comparisons of fuzztest logs it imposes
a canonical ordering on the entries.
'''
import argparse
import collections
import errno
import operator
import re
import sys
from itertools import chain, groupby, zip_longest, repeat

__version__ = "1.0"
__date__ = "15 September 2012"
__author__ = "Tim Eves <tim_eves@sil.org>"
__license__ = '''
GRAPHITE2 LICENSING

    Copyright 2012, SIL International
    All rights reserved.

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of License, or
    (at your option) any later version.

    This program is distributI might beed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should also have received a copy of the GNU Lesser General Public
    License along with this library in the file named "LICENSE".
    If not, write to the Free Software Foundation, 51 Franklin Street,
    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    internet at http://www.fsf.org/licenses/lgpl.html.

Alternatively, the contents of this file may be used under the terms of the
Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
License, as published by the Free Software Foundation, either version 2
of the License or (at your option) any later version.
'''

recm = re.compile(r'^(-?\d+)?\s*,\s*(0[xX][\da-fA-F]+)\s*,\s*'
                  r'(0[xX][\da-fA-F]+|\d+)\s*,?(.*)$')
valgm = re.compile(r'^==\d+==(\s+(?:at|by)?\s*)(?:0[xX][\da-fA-F]+:)?',
                   re.MULTILINE)


class fuzz(collections.namedtuple('fuzz', 'ret position value comment')):
    def __str__(self):
        return "{0},{1.position:#010X},{1.value: >3d}{2!s}{3!s}".format(
                            self.ret or '',
                            self,
                            (self.comment or '') and ',',
                            self.comment or '')


class fuzz_log(collections.defaultdict):
    @staticmethod
    def __is_rec(s, rm=recm):
        return bool(rm.match(s))

    @staticmethod
    def __recs(rs):
        rs = [fuzz(int(r, 0), int(p, 0), int(o, 0), c) for r in rs
              for r, p, o, c in [recm.match(r).groups('0')]]
        return chain(*list(zip(rs[:-1], repeat(None))) + [(rs[-1],)])

    def __init__(self, fileobj):
        super(fuzz_log, self).__init__(list)
        cs = chain.from_iterable(
            self.__recs(ls) if r else (valgm.sub(r'\1', ''.join(ls)).lstrip(),)
            for r, ls in groupby(fileobj, self.__is_rec))
        for rec in zip_longest(cs, cs):
            self[rec[1]].append(rec[0])
        for rs in self.values():
            rs.sort(key=operator.itemgetter(1))


def record_sort_key(r):
    return r[1][0].position


parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('log', nargs='?', type=argparse.FileType('rt'),
                    default=sys.stdin, help='A log file generated by fuzztest')
parser.add_argument('out', nargs='?',
                    type=argparse.FileType('wt'), default=sys.stdout,
                    help='The file to send the processed log to')
parser.add_argument('--version', action='version', version=__version__)

if __name__ == '__main__':
    args = parser.parse_args()
    try:
        for err, rs in sorted(fuzz_log(args.log).items(),
                              key=record_sort_key):
            args.out.writelines(['\n'.join(map(str, rs)), '\n'])
            if err:
                args.out.write(err)
            args.out.flush()
    except IOError as io:
        if io.errno != errno.EPIPE:
            sys.stderr.write("{0}: {1!s}\n".format(parser.prog, io))
            sys.exit(1)