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.
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.
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.
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.
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:
29 # someotherfile.s: skipped: same content
30 # anotherfile.s: failed: './link_test' exitcode != 0
32 # Example usage to identify miscompiled functions inside a file:
33 # 3. Run the tests on a single file (assuming before/file.s and
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
41 from fnmatch
import filter
42 from sys
import stderr
50 # Specify LINKTEST via `--test`. Default value is './link_test'.
56 FAILED
= RED
+ "failed" + NORMAL
59 def find(dir, file_filter
=None):
62 for walkdir
in os
.walk(dir)
63 for file in walkdir
[2]
65 if file_filter
is not None:
66 files
= filter(files
, file_filter
)
71 stderr
.write("Error: %s\n" % (message
,))
75 stderr
.write("Warning: %s\n" % (message
,))
79 stderr
.write("Info: %s\n" % (message
,))
82 def announce_test(name
):
83 stderr
.write("%s%s%s: " % (BOLD
, name
, NORMAL
))
87 def announce_result(result
):
93 def format_namelist(l
):
94 result
= ", ".join(l
[0:3])
96 result
+= "... (%d total)" % len(l
)
100 def check_sanity(choices
, perform_test
):
101 announce_test("sanity check A")
102 all_a
= {name
: a_b
[0] for name
, a_b
in choices
}
103 res_a
= perform_test(all_a
)
104 if res_a
is not True:
105 error("Picking all choices from A failed to pass the test")
108 announce_test("sanity check B (expecting failure)")
109 all_b
= {name
: a_b
[1] for name
, a_b
in choices
}
110 res_b
= perform_test(all_b
)
111 if res_b
is not False:
112 error("Picking all choices from B did unexpectedly pass the test")
116 def check_sequentially(choices
, perform_test
):
118 all_a
= {name
: a_b
[0] for name
, a_b
in choices
}
120 for name
, a_b
in sorted(choices
):
123 announce_test("checking %s [%d/%d]" % (name
, n
, len(choices
)))
125 res
= perform_test(picks
)
131 def check_bisect(choices
, perform_test
):
133 if len(choices
) == 0:
136 choice_map
= dict(choices
)
137 all_a
= {name
: a_b
[0] for name
, a_b
in choices
}
139 def test_partition(partition
, upcoming_partition
):
140 # Compute the maximum number of checks we have to do in the worst case.
141 max_remaining_steps
= len(partition
) * 2 - 1
142 if upcoming_partition
is not None:
143 max_remaining_steps
+= len(upcoming_partition
) * 2 - 1
144 for x
in partitions_to_split
:
145 max_remaining_steps
+= (len(x
) - 1) * 2
149 picks
[x
] = choice_map
[x
][1]
150 announce_test("checking %s [<=%d remaining]" %
151 (format_namelist(partition
), max_remaining_steps
))
152 res
= perform_test(picks
)
154 known_good
.update(partition
)
155 elif len(partition
) > 1:
156 partitions_to_split
.insert(0, partition
)
159 # - We could optimize based on the knowledge that when splitting a failed
160 # partition into two and one side checks out okay then we can deduce that
161 # the other partition must be a failure.
162 all_choice_names
= [name
for name
, _
in choices
]
163 partitions_to_split
= [all_choice_names
]
164 while len(partitions_to_split
) > 0:
165 partition
= partitions_to_split
.pop()
167 middle
= len(partition
) // 2
168 left
= partition
[0:middle
]
169 right
= partition
[middle
:]
172 test_partition(left
, right
)
173 assert len(right
) > 0
174 test_partition(right
, None)
179 def extract_functions(file):
182 for line
in open(file):
183 marker
= line
.find(" -- Begin function ")
185 if in_function
is not None:
186 warn("Missing end of function %s" % (in_function
,))
187 funcname
= line
[marker
+ 19:-1]
188 in_function
= funcname
192 marker
= line
.find(" -- End function")
195 functions
.append((in_function
, text
))
199 if in_function
is not None:
204 def replace_functions(source
, dest
, replacements
):
205 out
= open(dest
, "w")
208 for line
in open(source
):
209 marker
= line
.find(" -- Begin function ")
211 if in_function
is not None:
212 warn("Missing end of function %s" % (in_function
,))
213 funcname
= line
[marker
+ 19:-1]
214 in_function
= funcname
215 replacement
= replacements
.get(in_function
)
216 if replacement
is not None:
217 out
.write(replacement
)
220 marker
= line
.find(" -- End function")
232 linkline
= "%s %s" % (LINKTEST
, " ".join(files
),)
233 res
= subprocess
.call(linkline
, shell
=True)
235 announce_result(FAILED
+ ": '%s' exitcode != 0" % LINKTEST
)
238 announce_result("ok")
242 def prepare_files(gooddir
, baddir
, rspfile
):
246 if rspfile
is not None:
247 def get_basename(name
):
249 if name
.startswith(gooddir
):
250 return name
[len(gooddir
):]
251 if name
.startswith(baddir
):
252 return name
[len(baddir
):]
255 with
open(rspfile
, "r") as rf
:
256 for line
in rf
.read().splitlines():
257 for obj
in line
.split():
258 assert not os
.path
.isabs(obj
), "TODO: support abs path"
259 files_a
.append(gooddir
+ "/" + obj
)
260 files_b
.append(baddir
+ "/" + obj
)
262 get_basename
= lambda name
: os
.path
.basename(name
)
263 files_a
= find(gooddir
, "*")
264 files_b
= find(baddir
, "*")
266 basenames_a
= set(map(get_basename
, files_a
))
267 basenames_b
= set(map(get_basename
, files_b
))
270 basename
= get_basename(name
)
271 if basename
not in basenames_a
:
272 warn("There is no corresponding file to '%s' in %s" %
277 basename
= get_basename(name
)
278 if basename
not in basenames_b
:
279 warn("There is no corresponding file to '%s' in %s" %
282 file_a
= gooddir
+ "/" + basename
283 file_b
= baddir
+ "/" + basename
284 if filecmp
.cmp(file_a
, file_b
):
285 skipped
.append(basename
)
288 choice
= (basename
, (file_a
, file_b
))
289 choices
.append(choice
)
292 info("Skipped (same content): %s" % format_namelist(skipped
))
294 def perform_test(picks
):
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)
299 basename
= get_basename(x
)
300 picked
= picks
.get(basename
)
302 assert basename
in skipped
307 # If response file is used, create a temporary response file for the
309 if rspfile
is not None:
310 with tempfile
.NamedTemporaryFile('w', suffix
='.rsp',
312 tf
.write(" ".join(files
))
314 ret
= testrun([tf
.name
])
318 return testrun(files
)
320 return perform_test
, choices
323 def prepare_functions(to_check
, gooddir
, goodfile
, badfile
):
324 files_good
= find(gooddir
, "*")
326 functions_a
= extract_functions(goodfile
)
327 functions_a_map
= dict(functions_a
)
328 functions_b_map
= dict(extract_functions(badfile
))
330 for name
in functions_b_map
.keys():
331 if name
not in functions_a_map
:
332 warn("Function '%s' missing from good file" % name
)
335 for name
, candidate_a
in functions_a
:
336 candidate_b
= functions_b_map
.get(name
)
337 if candidate_b
is None:
338 warn("Function '%s' missing from bad file" % name
)
340 if candidate_a
== candidate_b
:
343 choice
= name
, (candidate_a
, candidate_b
)
344 choices
.append(choice
)
347 info("Skipped (same content): %s" % format_namelist(skipped
))
349 combined_file
= '/tmp/combined2.s'
351 found_good_file
= False
353 if os
.path
.basename(c
) == to_check
:
354 found_good_file
= True
355 files
.append(combined_file
)
358 assert found_good_file
360 def perform_test(picks
):
361 for name
, x
in picks
.items():
362 assert x
== functions_a_map
[name
] or x
== functions_b_map
[name
]
363 replace_functions(goodfile
, combined_file
, picks
)
364 return testrun(files
)
365 return perform_test
, choices
369 parser
= argparse
.ArgumentParser()
370 parser
.add_argument('--a', dest
='dir_a', default
='before')
371 parser
.add_argument('--b', dest
='dir_b', default
='after')
372 parser
.add_argument('--rsp', default
=None)
373 parser
.add_argument('--test', default
='./link_test')
374 parser
.add_argument('--insane', help='Skip sanity check',
376 parser
.add_argument('--seq',
377 help='Check sequentially instead of bisection',
379 parser
.add_argument('file', metavar
='file', nargs
='?')
380 config
= parser
.parse_args()
382 gooddir
= config
.dir_a
383 baddir
= config
.dir_b
386 LINKTEST
= config
.test
388 # Preparation phase: Creates a dictionary mapping names to a list of two
389 # choices each. The bisection algorithm will pick one choice for each name
390 # and then run the perform_test function on it.
391 if config
.file is not None:
392 goodfile
= gooddir
+ "/" + config
.file
393 badfile
= baddir
+ "/" + config
.file
394 perform_test
, choices
= prepare_functions(config
.file, gooddir
,
397 perform_test
, choices
= prepare_files(gooddir
, baddir
, rspfile
)
399 info("%d bisection choices" % len(choices
))
401 # "Checking whether build environment is sane ..."
402 if not config
.insane
:
403 if not os
.access(LINKTEST
, os
.X_OK
):
404 error("Expect '%s' to be present and executable" % (LINKTEST
,))
407 check_sanity(choices
, perform_test
)
410 known_good
= check_sequentially(choices
, perform_test
)
412 known_good
= check_bisect(choices
, perform_test
)
415 if len(known_good
) != len(choices
):
416 stderr
.write("== Failing ==\n")
417 for name
, _
in choices
:
418 if name
not in known_good
:
419 stderr
.write("%s\n" % name
)
421 # This shouldn't happen when the sanity check works...
422 # Maybe link_test isn't deterministic?
423 stderr
.write("Could not identify failing parts?!?")
426 if __name__
== '__main__':