4 # 1) Update the 'decls' list below with your fuzzing configuration.
5 # 2) Run with the clang binary as the command-line argument.
16 def __init__(self, text, depends=[], provides=[], conflicts=[]):
18 self.depends = depends
19 self.provides = provides
20 self.conflicts = conflicts
22 def valid(self, model):
23 for i in self.depends:
24 if i not in model.decls:
26 for i in self.conflicts:
31 def apply(self, model, name):
32 for i in self.provides:
34 model.source += self.text % {'name': name}
37 Decl('struct X { int n; };\n', provides=['X'], conflicts=['X']),
38 Decl('static_assert(X{.n=1}.n == 1, "");\n', depends=['X']),
39 Decl('X %(name)s;\n', depends=['X']),
47 def write(self, path, contents):
48 self.fs[path] = contents
51 for f, s in self.fs.items():
52 if self.prevfs.get(f) != s:
61 self.prevfs, self.fs = self.fs, {}
74 return 'n' + str(self.i)
77 fs.write('module.modulemap',
78 ''.join('module %s { header "%s.h" export * }\n' % (m, m)
79 for m in self.modules.keys()))
81 for m, (s, _) in self.modules.items():
82 fs.write('%s.h' % m, s)
84 fs.write('main.cc', self.source)
87 return subprocess.call([clang, '-std=c++11', '-c', '-fmodules', 'main.cc', '-o', '/dev/null']) != 0
94 for d in mutations(model):
99 except KeyboardInterrupt:
103 sys.stdout.write('\nReducing:\n')
108 assert m, 'got a failure with no steps; broken clang binary?'
109 i = random.choice(range(len(m)))
118 sys.stdout.write('.')
120 except KeyboardInterrupt:
121 # FIXME: Clean out output directory first.
127 i = int(random.uniform(0, len(options) + none_opts))
128 if i >= len(options):
132 def mutations(model):
133 options = [create_module, add_top_level_decl]
134 for opt in choose(options):
135 yield opt(model, options)
137 def create_module(model, options):
138 n = model.make_name()
140 model.modules[n] = (model.source, model.decls)
141 (model.source, model.decls) = ('', {})
142 options += [lambda model, options: add_import(model, options, n)]
145 def add_top_level_decl(model, options):
146 n = model.make_name()
147 d = random.choice([decl for decl in decls if decl.valid(model)])
149 if not d.valid(model):
154 def add_import(model, options, module_name):
156 if module_name in model.modules:
157 model.source += '#include "%s.h"\n' % module_name
158 model.decls.update(model.modules[module_name][1])
161 sys.stdout.write('Finding bug: ')
165 sys.stdout.write('.')