File: fuzztools.py

package info (click to toggle)
mutagen 1.47.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,456 kB
  • sloc: python: 22,562; makefile: 50; sh: 29
file content (141 lines) | stat: -rw-r--r-- 3,640 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from io import BytesIO

from mutagen import File, Metadata
from mutagen import MutagenError
from mutagen.asf import ASF
from mutagen.apev2 import APEv2File, APEv2
from mutagen.flac import FLAC
from mutagen.easyid3 import EasyID3FileType, EasyID3
from mutagen.id3 import ID3FileType, ID3
from mutagen.mp3 import MP3
from mutagen.mp3 import EasyMP3
from mutagen.oggflac import OggFLAC
from mutagen.oggspeex import OggSpeex
from mutagen.oggtheora import OggTheora
from mutagen.oggvorbis import OggVorbis
from mutagen.oggopus import OggOpus
from mutagen.trueaudio import EasyTrueAudio
from mutagen.trueaudio import TrueAudio
from mutagen.wavpack import WavPack
from mutagen.easymp4 import EasyMP4
from mutagen.mp4 import MP4
from mutagen.musepack import Musepack
from mutagen.monkeysaudio import MonkeysAudio
from mutagen.optimfrog import OptimFROG
from mutagen.aiff import AIFF
from mutagen.aac import AAC
from mutagen.ac3 import AC3
from mutagen.smf import SMF
from mutagen.tak import TAK
from mutagen.dsf import DSF
from mutagen.wave import WAVE
from mutagen.dsdiff import DSDIFF


OPENERS = [
    MP3, TrueAudio, OggTheora, OggSpeex, OggVorbis, OggFLAC,
    FLAC, AIFF, APEv2File, MP4, ID3FileType, WavPack,
    Musepack, MonkeysAudio, OptimFROG, ASF, OggOpus, AC3,
    TAK, DSF, EasyMP3, EasyID3FileType, EasyTrueAudio, EasyMP4,
    File, SMF, AAC, EasyID3, ID3, APEv2, WAVE, DSDIFF]

# If you only want to test one:
# OPENERS = [AAC]


def run(opener, f):
    try:
        res = opener(f)
    except MutagenError:
        return

    # File is special and returns None if loading fails
    if opener is File and res is None:
        return

    # These can still fail because we might need to parse more data
    # to rewrite the file

    f.seek(0)
    try:
        res.save(f)
    except MutagenError:
        pass

    f.seek(0)
    res = opener(f)

    f.seek(0)
    try:
        res.delete(f)
    except MutagenError:
        pass

    # These can also save to empty files
    if isinstance(res, Metadata):
        f = BytesIO()
        res.save(f)
        f.seek(0)
        opener(f)
        f.seek(0)
        res.delete(f)


def run_all(data):
    f = BytesIO(data)
    [run(opener, f) for opener in OPENERS]


def group_crashes(result_path):
    """Re-checks all errors, and groups them by stack trace
    and error type.
    """

    crash_paths = []
    pattern = os.path.join(result_path, '**', 'crashes', '*')
    for path in glob.glob(pattern):
        if os.path.splitext(path)[-1] == ".txt":
            continue
        crash_paths.append(path)

    if not crash_paths:
        print("No crashes found")
        return

    def norm_exc():
        lines = traceback.format_exc().splitlines()
        if ":" in lines[-1]:
            lines[-1], message = lines[-1].split(":", 1)
        else:
            message = ""
        return "\n".join(lines), message.strip()

    traces = {}
    messages = {}
    for path in crash_paths:
        with open(path, "rb") as h:
            data = h.read()
        try:
            run_all(data)
        except Exception:
            trace, message = norm_exc()
            messages.setdefault(trace, set()).add(message)
            traces.setdefault(trace, []).append(path)

    for trace, paths in traces.items():
        print('-' * 80)
        print("\n".join(paths))
        print()
        print(textwrap.indent(trace, '    '))
        print(messages[trace])

    print("%d crashes with %d traces" % (len(crash_paths), len(traces)))


if __name__ == '__main__':
    import sys
    import glob
    import os
    import traceback
    import textwrap
    group_crashes(sys.argv[1])