2
# pylint: disable=C0103,C0114,C0115,C0116,C0321
3
######################################################################
4
# DESCRIPTION: Fuzzer dictionary generator
6
# Copyright 2019-2019 by Eric Rippey. This program is free software; you
7
# can redistribute it and/or modify it under the terms of either the GNU Lesser
8
# General Public License Version 3 or the Perl Artistic License Version 2.0.
9
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
10
######################################################################
12
# Attempts to pull a list of keywords out of the Flex input
13
# These are then put in a dictionary of "interesting" sequences
14
# This will be used to help the fuzzer pick interesting inputs more quickly.
16
from subprocess import getstatusoutput
21
# any(a) => (a->bool)->[a]->[a]
22
# Does the same think as Haskell's takewhile.
33
# any(a) => (a->bool)->[a]->[a]
34
# Basically, the opposite thing from skipwhile
35
while len(a) and f(a[0]):
41
# printable(a) => [a]->void
46
def write_file(filename, contents):
48
with open(filename, "w", encoding="utf8") as fh:
54
if len(s) == 0: return None
55
part = skip_while(lambda x: x != '"', s)
56
if len(part) == 0 or part[0] != '"': return None
57
literal_part = take_while(lambda x: x != '"', part[1:])
58
return ''.join(filter(lambda x: x != '\\', literal_part))
62
status, output = getstatusoutput('flex -T ../../src/verilog.l')
65
lines = output.splitlines()
66
lines = take_while(lambda x: 'beginning dump of nfa' not in x, lines)
67
tokens = set(filter(lambda x: x, map(parse_line, lines)))
69
dirname = 'dictionary'
70
r = system('mkdir -p ' + dirname)
72
for i, token in enumerate(tokens):
73
write_file(dirname + '/' + str(i), token)
76
if __name__ == '__main__':