llvm-project
427 строк · 13.4 Кб
1#!/usr/bin/env python
2#
3# Given a previous good compile narrow down miscompiles.
4# Expects two directories named "before" and "after" each containing a set of
5# assembly or object files where the "after" version is assumed to be broken.
6# You also have to provide a script called "link_test". It is called with a
7# list of files which should be linked together and result tested. "link_test"
8# should returns with exitcode 0 if the linking and testing succeeded.
9#
10# If a response file is provided, only the object files that are listed in the
11# file are inspected. In addition, the "link_test" is called with a temporary
12# response file representing one iteration of bisection.
13#
14# abtest.py operates by taking all files from the "before" directory and
15# in each step replacing one of them with a file from the "bad" directory.
16#
17# Additionally you can perform the same steps with a single .s file. In this
18# mode functions are identified by " -- Begin function FunctionName" and
19# " -- End function" markers. The abtest.py then takes all
20# function from the file in the "before" directory and replaces one function
21# with the corresponding function from the "bad" file in each step.
22#
23# Example usage to identify miscompiled files:
24# 1. Create a link_test script, make it executable. Simple Example:
25# clang "$@" -o /tmp/test && /tmp/test || echo "PROBLEM"
26# 2. Run the script to figure out which files are miscompiled:
27# > ./abtest.py
28# somefile.s: ok
29# someotherfile.s: skipped: same content
30# anotherfile.s: failed: './link_test' exitcode != 0
31# ...
32# Example usage to identify miscompiled functions inside a file:
33# 3. Run the tests on a single file (assuming before/file.s and
34# after/file.s exist)
35# > ./abtest.py file.s
36# funcname1 [0/XX]: ok
37# funcname2 [1/XX]: ok
38# funcname3 [2/XX]: skipped: same content
39# funcname4 [3/XX]: failed: './link_test' exitcode != 0
40# ...
41from fnmatch import filter
42from sys import stderr
43import argparse
44import filecmp
45import os
46import subprocess
47import sys
48import tempfile
49
50# Specify LINKTEST via `--test`. Default value is './link_test'.
51LINKTEST = ""
52ESCAPE = "\033[%sm"
53BOLD = ESCAPE % "1"
54RED = ESCAPE % "31"
55NORMAL = ESCAPE % "0"
56FAILED = RED + "failed" + NORMAL
57
58
59def find(dir, file_filter=None):
60files = [walkdir[0] + "/" + file for walkdir in os.walk(dir) for file in walkdir[2]]
61if file_filter is not None:
62files = filter(files, file_filter)
63return sorted(files)
64
65
66def error(message):
67stderr.write("Error: %s\n" % (message,))
68
69
70def warn(message):
71stderr.write("Warning: %s\n" % (message,))
72
73
74def info(message):
75stderr.write("Info: %s\n" % (message,))
76
77
78def announce_test(name):
79stderr.write("%s%s%s: " % (BOLD, name, NORMAL))
80stderr.flush()
81
82
83def announce_result(result):
84stderr.write(result)
85stderr.write("\n")
86stderr.flush()
87
88
89def format_namelist(l):
90result = ", ".join(l[0:3])
91if len(l) > 3:
92result += "... (%d total)" % len(l)
93return result
94
95
96def check_sanity(choices, perform_test):
97announce_test("sanity check A")
98all_a = {name: a_b[0] for name, a_b in choices}
99res_a = perform_test(all_a)
100if res_a is not True:
101error("Picking all choices from A failed to pass the test")
102sys.exit(1)
103
104announce_test("sanity check B (expecting failure)")
105all_b = {name: a_b[1] for name, a_b in choices}
106res_b = perform_test(all_b)
107if res_b is not False:
108error("Picking all choices from B did unexpectedly pass the test")
109sys.exit(1)
110
111
112def check_sequentially(choices, perform_test):
113known_good = set()
114all_a = {name: a_b[0] for name, a_b in choices}
115n = 1
116for name, a_b in sorted(choices):
117picks = dict(all_a)
118picks[name] = a_b[1]
119announce_test("checking %s [%d/%d]" % (name, n, len(choices)))
120n += 1
121res = perform_test(picks)
122if res is True:
123known_good.add(name)
124return known_good
125
126
127def check_bisect(choices, perform_test):
128known_good = set()
129if len(choices) == 0:
130return known_good
131
132choice_map = dict(choices)
133all_a = {name: a_b[0] for name, a_b in choices}
134
135def test_partition(partition, upcoming_partition):
136# Compute the maximum number of checks we have to do in the worst case.
137max_remaining_steps = len(partition) * 2 - 1
138if upcoming_partition is not None:
139max_remaining_steps += len(upcoming_partition) * 2 - 1
140for x in partitions_to_split:
141max_remaining_steps += (len(x) - 1) * 2
142
143picks = dict(all_a)
144for x in partition:
145picks[x] = choice_map[x][1]
146announce_test(
147"checking %s [<=%d remaining]"
148% (format_namelist(partition), max_remaining_steps)
149)
150res = perform_test(picks)
151if res is True:
152known_good.update(partition)
153elif len(partition) > 1:
154partitions_to_split.insert(0, partition)
155
156# TODO:
157# - We could optimize based on the knowledge that when splitting a failed
158# partition into two and one side checks out okay then we can deduce that
159# the other partition must be a failure.
160all_choice_names = [name for name, _ in choices]
161partitions_to_split = [all_choice_names]
162while len(partitions_to_split) > 0:
163partition = partitions_to_split.pop()
164
165middle = len(partition) // 2
166left = partition[0:middle]
167right = partition[middle:]
168
169if len(left) > 0:
170test_partition(left, right)
171assert len(right) > 0
172test_partition(right, None)
173
174return known_good
175
176
177def extract_functions(file):
178functions = []
179in_function = None
180for line in open(file):
181marker = line.find(" -- Begin function ")
182if marker != -1:
183if in_function is not None:
184warn("Missing end of function %s" % (in_function,))
185funcname = line[marker + 19 : -1]
186in_function = funcname
187text = line
188continue
189
190marker = line.find(" -- End function")
191if marker != -1:
192text += line
193functions.append((in_function, text))
194in_function = None
195continue
196
197if in_function is not None:
198text += line
199return functions
200
201
202def replace_functions(source, dest, replacements):
203out = open(dest, "w")
204skip = False
205in_function = None
206for line in open(source):
207marker = line.find(" -- Begin function ")
208if marker != -1:
209if in_function is not None:
210warn("Missing end of function %s" % (in_function,))
211funcname = line[marker + 19 : -1]
212in_function = funcname
213replacement = replacements.get(in_function)
214if replacement is not None:
215out.write(replacement)
216skip = True
217else:
218marker = line.find(" -- End function")
219if marker != -1:
220in_function = None
221if skip:
222skip = False
223continue
224
225if not skip:
226out.write(line)
227
228
229def testrun(files):
230linkline = "%s %s" % (
231LINKTEST,
232" ".join(files),
233)
234res = subprocess.call(linkline, shell=True)
235if res != 0:
236announce_result(FAILED + ": '%s' exitcode != 0" % LINKTEST)
237return False
238else:
239announce_result("ok")
240return True
241
242
243def prepare_files(gooddir, baddir, rspfile):
244files_a = []
245files_b = []
246
247if rspfile is not None:
248
249def get_basename(name):
250# remove prefix
251if name.startswith(gooddir):
252return name[len(gooddir) :]
253if name.startswith(baddir):
254return name[len(baddir) :]
255assert False, ""
256
257with open(rspfile, "r") as rf:
258for line in rf.read().splitlines():
259for obj in line.split():
260assert not os.path.isabs(obj), "TODO: support abs path"
261files_a.append(gooddir + "/" + obj)
262files_b.append(baddir + "/" + obj)
263else:
264get_basename = lambda name: os.path.basename(name)
265files_a = find(gooddir, "*")
266files_b = find(baddir, "*")
267
268basenames_a = set(map(get_basename, files_a))
269basenames_b = set(map(get_basename, files_b))
270
271for name in files_b:
272basename = get_basename(name)
273if basename not in basenames_a:
274warn("There is no corresponding file to '%s' in %s" % (name, gooddir))
275choices = []
276skipped = []
277for name in files_a:
278basename = get_basename(name)
279if basename not in basenames_b:
280warn("There is no corresponding file to '%s' in %s" % (name, baddir))
281
282file_a = gooddir + "/" + basename
283file_b = baddir + "/" + basename
284if filecmp.cmp(file_a, file_b):
285skipped.append(basename)
286continue
287
288choice = (basename, (file_a, file_b))
289choices.append(choice)
290
291if len(skipped) > 0:
292info("Skipped (same content): %s" % format_namelist(skipped))
293
294def perform_test(picks):
295files = []
296# Note that we iterate over files_a so we don't change the order
297# (cannot use `picks` as it is a dictionary without order)
298for x in files_a:
299basename = get_basename(x)
300picked = picks.get(basename)
301if picked is None:
302assert basename in skipped
303files.append(x)
304else:
305files.append(picked)
306
307# If response file is used, create a temporary response file for the
308# picked files.
309if rspfile is not None:
310with tempfile.NamedTemporaryFile("w", suffix=".rsp", delete=False) as tf:
311tf.write(" ".join(files))
312tf.flush()
313ret = testrun([tf.name])
314os.remove(tf.name)
315return ret
316
317return testrun(files)
318
319return perform_test, choices
320
321
322def prepare_functions(to_check, gooddir, goodfile, badfile):
323files_good = find(gooddir, "*")
324
325functions_a = extract_functions(goodfile)
326functions_a_map = dict(functions_a)
327functions_b_map = dict(extract_functions(badfile))
328
329for name in functions_b_map.keys():
330if name not in functions_a_map:
331warn("Function '%s' missing from good file" % name)
332choices = []
333skipped = []
334for name, candidate_a in functions_a:
335candidate_b = functions_b_map.get(name)
336if candidate_b is None:
337warn("Function '%s' missing from bad file" % name)
338continue
339if candidate_a == candidate_b:
340skipped.append(name)
341continue
342choice = name, (candidate_a, candidate_b)
343choices.append(choice)
344
345if len(skipped) > 0:
346info("Skipped (same content): %s" % format_namelist(skipped))
347
348combined_file = "/tmp/combined2.s"
349files = []
350found_good_file = False
351for c in files_good:
352if os.path.basename(c) == to_check:
353found_good_file = True
354files.append(combined_file)
355continue
356files.append(c)
357assert found_good_file
358
359def perform_test(picks):
360for name, x in picks.items():
361assert x == functions_a_map[name] or x == functions_b_map[name]
362replace_functions(goodfile, combined_file, picks)
363return testrun(files)
364
365return perform_test, choices
366
367
368def main():
369parser = argparse.ArgumentParser()
370parser.add_argument("--a", dest="dir_a", default="before")
371parser.add_argument("--b", dest="dir_b", default="after")
372parser.add_argument("--rsp", default=None)
373parser.add_argument("--test", default="./link_test")
374parser.add_argument("--insane", help="Skip sanity check", action="store_true")
375parser.add_argument(
376"--seq", help="Check sequentially instead of bisection", action="store_true"
377)
378parser.add_argument("file", metavar="file", nargs="?")
379config = parser.parse_args()
380
381gooddir = config.dir_a
382baddir = config.dir_b
383rspfile = config.rsp
384global LINKTEST
385LINKTEST = config.test
386
387# Preparation phase: Creates a dictionary mapping names to a list of two
388# choices each. The bisection algorithm will pick one choice for each name
389# and then run the perform_test function on it.
390if config.file is not None:
391goodfile = gooddir + "/" + config.file
392badfile = baddir + "/" + config.file
393perform_test, choices = prepare_functions(
394config.file, gooddir, goodfile, badfile
395)
396else:
397perform_test, choices = prepare_files(gooddir, baddir, rspfile)
398
399info("%d bisection choices" % len(choices))
400
401# "Checking whether build environment is sane ..."
402if not config.insane:
403if not os.access(LINKTEST, os.X_OK):
404error("Expect '%s' to be present and executable" % (LINKTEST,))
405exit(1)
406
407check_sanity(choices, perform_test)
408
409if config.seq:
410known_good = check_sequentially(choices, perform_test)
411else:
412known_good = check_bisect(choices, perform_test)
413
414stderr.write("")
415if len(known_good) != len(choices):
416stderr.write("== Failing ==\n")
417for name, _ in choices:
418if name not in known_good:
419stderr.write("%s\n" % name)
420else:
421# This shouldn't happen when the sanity check works...
422# Maybe link_test isn't deterministic?
423stderr.write("Could not identify failing parts?!?")
424
425
426if __name__ == "__main__":
427main()
428