Debian Codegraph @

tests/fuzztest

raw
1 #!/usr/bin/env python3
2 '''
3 A multiprocess fuzz test framework that generates corrupt fonts to run a user
4 supplied test harness against. This will check each byte in every table of a
5 TTF or subset if specified by overwriting with a random or user specified
6 value. It can also re-run tests using previous output logs as input.
7 '''
8 __version__ = "1.0"
9 __date__ = "4 April 2012"
10 __author__ = "Tim Eves <[email protected]>"
11 __license__ ='''
12 SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
13 Copyright 2010, SIL International, All rights reserved.
14 '''
15 import collections, operator, struct
16 from contextlib import closing, contextmanager
17 from itertools import chain, cycle, repeat, starmap
18 from random import uniform, seed
19
20 import multiprocessing.dummy as multiprocessing
21 import shutil, tempfile
22 import os, re, resource, sys, time
23 from subprocess import call
24
25 from optparse import OptionParser, OptionGroup
26
27
28
29 tablerecord = collections.namedtuple("tablerecord", "tag start end")
30
31 class font:
32 __recm = re.compile('^(?:-?\d+)?\s*,\s*(0[xX][\da-fA-F]+)\s*,\s*(0[xX][\da-fA-F]+|\d+)\s*')
33 random_bytes = map(int, starmap(uniform, repeat((0,256))))
34
35 @staticmethod
36 def __initialise_worker(fnt):
37 self = multiprocessing.current_process()
38 self.count = 0
39 self.tmpdir = fnt._tmpdir
40 self.file = tempfile.NamedTemporaryFile('r+b', suffix='.ttf', prefix="font", dir=self.tmpdir)
41 self.nolog = open('/dev/null','wt')
42 with open(fnt._font_path, "rb") as font_file:
43 shutil.copyfileobj(font_file, self.file)
44
45 def __init__(self, font_path, inclusion, exclusion):
46 fpath = os.path.splitext(os.path.basename(font_path))[0]
47 self._tmpdir = tempfile.mkdtemp(prefix='fuzztest-' + fpath + '-')
48 self._font_path = font_path
49 self.__table_dir = self.read_tabledir()
50 inclusion = set(inclusion or (tr.tag for tr in self.__table_dir)) - set(exclusion)
51 self.__table_dir = filter(lambda tr: tr.tag in inclusion, self.read_tabledir())
52 if not opts.quiet :
53 sys.stdout.write("Font: {0}\n".format(fpath))
54 sys.stdout.write("tables tested: {0!s}\n".format(','.join(tr.tag for tr in self.__table_dir)))
55 self.pool = multiprocessing.Pool(initializer=self.__initialise_worker, initargs=[self], processes=opts.jobs)
56
57 def close(self):
58 self.pool.terminate()
59 self.pool.join()
60 #shutil.rmtree(self._tmpdir, True)
61
62 def table_from_offset(self, o):
63 return next((tr for tr in self.__table_dir if tr.start <= o < tr.end), None)
64
65 def tests_per_pass(self):
66 return sum(tr.end-tr.start for tr in self.__table_dir)
67
68 def input(self,path):
69 return [(int(m.group(1),0), int(m.group(2),0)) for m in map(self.__recm.match, open(path, "rt")) if m]
70
71
72 def linear(self, value=[], start=0, passes=0):
73 tables = chain.from_iterable(repeat(self.__table_dir, passes)) if passes else cycle(self.__table_dir)
74 locs = chain.from_iterable(xrange(tr.start, tr.end) for tr in tables)
75 vals = cycle(value) if value else self.random_bytes
76 next(slice(locs, start, start),None)
77 next(slice(vals, start, start),None)
78 return zip(locs, vals)
79
80 def position(self, loc, val=[]):
81 return zip(repeat(loc), val if val else [next(random_bytes)])
82
83 def read_tabledir(self):
84 import mmap
85 _offset_table = b">i4H"
86 _table_record = b">4s3L"
87 with open(self._font_path, "rb") as font:
88 sfnt = mmap.mmap(font.fileno(), 0, access=mmap.ACCESS_READ)
89 num_tables = struct.unpack_from(_offset_table, sfnt)[1]
90 off = struct.calcsize(_offset_table)
91 tdir = [tablerecord(b'',0,off + num_tables*struct.calcsize(_table_record))]
92 for s in range(off, tdir[0].end, struct.calcsize(_table_record)):
93 tag,cs,o,l = struct.unpack_from(_table_record, sfnt, s)
94 tdir.append(tablerecord(tag, o, o+l))
95 tdir.sort(key=operator.itemgetter(1))
96 return tdir
97
98
99 def rlimit() :
100 if opts.timeout :
101 resource.setrlimit(resource.RLIMIT_CPU, (opts.timeout, opts.timeout))
102 if opts.memory :
103 mem = opts.memory * 1024 * 1024
104 resource.setrlimit(resource.RLIMIT_AS, (mem, mem))
105
106 @contextmanager
107 def bug(f, offset, value):
108 f.seek(offset, os.SEEK_SET)
109 orig = ord(f.read(1))
110 f.seek(offset, os.SEEK_SET)
111 f.write(bytes([value]))
112 #f.write(chr(value))
113 f.flush()
114 yield (offset, orig)
115 f.seek(offset, os.SEEK_SET)
116 f.write(bytes([orig]))
117 #f.write(chr(orig))
118 f.flush()
119
120 def test(cr):
121 try:
122 self = multiprocessing.current_process()
123 log_path = "{0}.err.log.{1:d}".format(self.file.name, self.count)
124 log_path = os.path.join(self.tmpdir, log_path)
125 with bug(self.file, *cr):
126 with open(log_path, "wt") as log:
127 proc = args[:]
128 proc[proc.index('{}')] = self.file.name
129 retval = call(proc,
130 stdout=self.nolog, stderr=log, preexec_fn=rlimit, close_fds = True)
131 self.count += 1
132 self.count %= 10000
133 if not log.tell(): os.unlink(log.name)
134 return (retval, cr, log.tell() and log.name)
135 except KeyboardInterrupt:
136 return (0, cr, log_path)
137
138 class estimator:
139 def __init__(self, max):
140 self.__base = time.time()
141 self.__max = float(max)
142 self.__m = 0
143 self.__t = 1
144 self.__st = self.__base
145
146 def sample(self, current):
147 ct = time.time()
148 if (ct - self.__st <= opts.status):
149 return None
150 x = current / (ct-self.__base)
151 k = (ct-self.__base)/opts.status+1
152 m = self.__m + (x - self.__m)/k
153 s = (k-1) and (self.__t + (x - self.__m)*(x - m)) / (k-1)
154 m = bayes(x, s, self.__m, self.__t)
155 self.__t = s
156 self.__m = m
157 self.__st = ct
158 return (x, time.ctime(ct + (self.__max - current)/m) if m else 'never')
159
160 def bayes(x, s, m, t):
161 st = s + t
162 return m*s/st + x*t/st
163
164
165 predefined_tablesets = {
166 'fontdir' : [''],
167 'required' : ['cmap','glyf','head','hhea','hmtx','loca','maxp','name','post'],
168 'graphite' : ['Feat','Glat','Gloc','Sile','Silf','Sill'],
169 'volt' : ['TSIV','TSID','TSIP','TSIS'],
170 'opentype' : ['BASE','DSIG','GDEF','GPOS','GSUB','LTSH','OS/2','PCLT','VDMX','cvt','fpgm','prep','gasp','hdmx','kern','vhea','vmtx'],
171 'advtypo' : ['acnt','cvar','bdat','bloc','bsln','feat','fdsc','fvar','gvar','just','kern','lcar','mort','morx','opbd','prop','trak','vhea','vmtx']
172 }
173
174 def tableset(tags):
175 return [(t+' ')[:4] for t in chain.from_iterable(predefined_tablesets.get(n,[n]) for n in tags)]
176
177 parser = OptionParser(usage="usage: %prog -f font [options] -- command\n" + __doc__, version = __version__)
178 parser.add_option("-l", "--logfile", help="Log results to this file")
179 parser.add_option("-f", "--font", help="Required font file to corrupt")
180 parser.add_option("-p", "--position", help="Specifies position to corrupt (hex)")
181 parser.add_option("-v", "--value", default=[], help="Specifies value(s) to use (default: varying random number)")
182 parser.add_option("-i", "--input", help=".log file to read test values from")
183 parser.add_option("-V", "--verbose", action="store_true", help="Be noisy")
184 parser.add_option("-q", "--quiet", action="store_true", help="Be quiet, don't print to stdout")
185 parser.add_option("-t", "--timeout", type="int", help="limit subprocess time in seconds")
186 parser.add_option("--memory", type="int", help="limit subprocess address space in MB")
187 parser.add_option("-s","--status",type='int',help='update status every STATUS seconds (default: None)')
188 parser.add_option("-P","--passes",type='int', default=0, help='Run this many passes (default: forever)')
189 parser.add_option("-j","--jobs",type='int', default=None, help='Number of subprocesses to run in parallel (default: cpu count)')
190 parser.add_option("-r","--random",help="Seed the random number generator with the given string")
191 parser.add_option("--valgrind",action="store_true",help="Run tests with valgrind and report errors")
192 parser.add_option("-k","--keep",action="store_true",help="Keep going. Don't return error status")
193 testset = OptionGroup(parser, 'Test sets',
194 'Explicitly specify which tables to include or exclude from testing. '
195 'By default all tables (including the font directory) are included.'
196 ' If both include and exclude sets are supplied any excluded tables'
197 ' are removed from the specified inclusion set.'
198 ' Sets may be either table tags or predefined tablesets from this list: ' + ', '.join(sorted(predefined_tablesets)))
199 testset.add_option("-x", "--exclude", default=[], help="A comma separated list of tables to exclude")
200 testset.add_option("-I", "--include", default=[], help="A comma spearated list of the only tables to test")
201 parser.add_option_group(testset)
202
203 CHUNK_SIZE=10
204
205 if __name__ == '__main__':
206 (opts, args) = parser.parse_args()
207
208 if opts.random : seed(opts.random)
209 if opts.valgrind :
210 args = ["valgrind", "-q"] + args
211 opts.verbose = True
212 opts.position = opts.position and int(opts.position,0)
213 opts.value = opts.value and [int(n,0) for n in opts.value.split(',')]
214 opts.exclude = opts.exclude and tableset(opts.exclude.split(','))
215 opts.include = opts.include and tableset(opts.include.split(','))
216 if opts.quiet : opts.status = None
217
218 if '{}' not in args: args += ['{}']
219
220 try:
221 exitrv = 0
222 log = open(opts.logfile, "at") if opts.logfile else sys.stdout
223 with closing(font(opts.font, opts.include, opts.exclude)) as fnt:
224 if opts.input: cs = fnt.input(opts.input)
225 elif opts.position: cs = fnt.position(opts.position, opts.value)
226 else: cs = fnt.linear(opts.value, opts.position, opts.passes)
227
228 if opts.input or opts.position:
229 opts.passes = 1
230 total_tests = len(cs)
231 else:
232 total_tests = fnt.tests_per_pass()
233
234 if opts.status:
235 sys.stdout.write("tests per pass: {0}\t\tpasses: {1}\n".format(total_tests, opts.passes if opts.passes else 'until killed'))
236 sys.stdout.write("% {0}complete\ttests per second\testimated time to {0}complete\n".format('' if opts.passes else 'pass '))
237 total_tests *= opts.passes or 1
238
239 estimate = estimator(total_tests)
240 for count,(rv,cr,errlog) in enumerate(fnt.pool.imap_unordered(test, cs, CHUNK_SIZE)):
241 try:
242 if opts.status and count % CHUNK_SIZE == 0:
243 sam = estimate.sample(count)
244 if sam:
245 pc = 100*count // total_tests
246 sys.stdout.write("{0: 3d}%\t\t{1: 8.2f}\t\t{2}\r".format(pc, sam[0], sam[1]))
247 sys.stdout.flush()
248 if rv < 0 or errlog :
249 exitrv = 2
250 if rv < 0 :
251 (t,s,_) = fnt.table_from_offset(cr[0]) or (0,0,fontlen)
252 log.write("{0:d},{1[0]:#010X},{1[1]: >3d},{2!s}{3:#010X}\n".format(rv, cr, t + '+' if t else '', cr[0]-s))
253 elif opts.verbose and errlog:
254 (t,s,_) = fnt.table_from_offset(cr[0]) or (0,0,fontlen)
255 log.write(",{0[0]:#010X},{0[1]: >3d},{1!s}{2:#010X}\n".format(cr, t + '+' if t else '', cr[0]-s))
256 log.flush()
257 if errlog :
258 with open(errlog,'r') as logpart: shutil.copyfileobj(logpart, log)
259 os.unlink(errlog)
260 except KeyboardInterrupt: fnt.pool.close()
261 log.flush()
262 except IOError as io:
263 sys.stderr.write("{0}: {1!s}\n".format(os.path.basename(sys.argv[0]), io))
264 sys.exit(1)
265 except KeyboardInterrupt:
266 sys.stdout.write('\n')
267 sys.exit(exitrv)
268 if opts.status :
269 sys.stdout.write("Finished at {0}\n".format(time.ctime()))
270 if not opts.keep :
271 sys.exit(exitrv)