llvm-project
214 строк · 7.3 Кб
1#!/usr/bin/env python3
2# ===----------------------------------------------------------------------===##
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8# ===----------------------------------------------------------------------===##
9"""Script to bisect over files in an rsp file.
10
11This is mostly used for detecting which file contains a miscompile between two
12compiler revisions. It does this by bisecting over an rsp file. Between two
13build directories, this script will make the rsp file reference the current
14build directory's version of some set of the rsp's object files/libraries, and
15reference the other build directory's version of the same files for the
16remaining set of object files/libraries.
17
18Build the target in two separate directories with the two compiler revisions,
19keeping the rsp file around since ninja by default deletes the rsp file after
20building.
21$ ninja -d keeprsp mytarget
22
23Create a script to build the target and run an interesting test. Get the
24command to build the target via
25$ ninja -t commands | grep mytarget
26The command to build the target should reference the rsp file.
27This script doesn't care if the test script returns 0 or 1 for specifically the
28successful or failing test, just that the test script returns a different
29return code for success vs failure.
30Since the command that `ninja -t commands` is run from the build directory,
31usually the test script cd's to the build directory.
32
33$ rsp_bisect.py --test=path/to/test_script --rsp=path/to/build/target.rsp
34--other_rel_path=../Other
35where --other_rel_path is the relative path from the first build directory to
36the other build directory. This is prepended to files in the rsp.
37
38
39For a full example, if the foo target is suspected to contain a miscompile in
40some file, have two different build directories, buildgood/ and buildbad/ and
41run
42$ ninja -d keeprsp foo
43in both so we have two versions of all relevant object files that may contain a
44miscompile, one built by a good compiler and one by a bad compiler.
45
46In buildgood/, run
47$ ninja -t commands | grep '-o .*foo'
48to get the command to link the files together. It may look something like
49clang -o foo @foo.rsp
50
51Now create a test script that runs the link step and whatever test reproduces a
52miscompile and returns a non-zero exit code when there is a miscompile. For
53example
54```
55#!/bin/bash
56# immediately bail out of script if any command returns a non-zero return code
57set -e
58clang -o foo @foo.rsp
59./foo
60```
61
62With buildgood/ as the working directory, run
63$ path/to/llvm-project/llvm/utils/rsp_bisect.py \
64--test=path/to/test_script --rsp=./foo.rsp --other_rel_path=../buildbad/
65If rsp_bisect is successful, it will print the first file in the rsp file that
66when using the bad build directory's version causes the test script to return a
67different return code. foo.rsp.0 and foo.rsp.1 will also be written. foo.rsp.0
68will be a copy of foo.rsp with the relevant file using the version in
69buildgood/, and foo.rsp.1 will be a copy of foo.rsp with the relevant file
70using the version in buildbad/.
71
72"""
73
74import argparse
75import os
76import subprocess
77import sys
78
79
80def is_path(s):
81return "/" in s
82
83
84def run_test(test):
85"""Runs the test and returns whether it was successful or not."""
86return subprocess.run([test], capture_output=True).returncode == 0
87
88
89def modify_rsp(rsp_entries, other_rel_path, modify_after_num):
90"""Create a modified rsp file for use in bisection.
91
92Returns a new list from rsp.
93For each file in rsp after the first modify_after_num files, prepend
94other_rel_path.
95"""
96ret = []
97for r in rsp_entries:
98if is_path(r):
99if modify_after_num == 0:
100r = os.path.join(other_rel_path, r)
101else:
102modify_after_num -= 1
103ret.append(r)
104assert modify_after_num == 0
105return ret
106
107
108def test_modified_rsp(test, modified_rsp_entries, rsp_path):
109"""Write the rsp file to disk and run the test."""
110with open(rsp_path, "w") as f:
111f.write(" ".join(modified_rsp_entries))
112return run_test(test)
113
114
115def bisect(test, zero_result, rsp_entries, num_files_in_rsp, other_rel_path, rsp_path):
116"""Bisect over rsp entries.
117
118Args:
119zero_result: the test result when modify_after_num is 0.
120
121Returns:
122The index of the file in the rsp file where the test result changes.
123"""
124lower = 0
125upper = num_files_in_rsp
126while lower != upper - 1:
127assert lower < upper - 1
128mid = int((lower + upper) / 2)
129assert lower != mid and mid != upper
130print("Trying {} ({}-{})".format(mid, lower, upper))
131result = test_modified_rsp(
132test, modify_rsp(rsp_entries, other_rel_path, mid), rsp_path
133)
134if zero_result == result:
135lower = mid
136else:
137upper = mid
138return upper
139
140
141def main():
142parser = argparse.ArgumentParser()
143parser.add_argument(
144"--test", help="Binary to test if current setup is good or bad", required=True
145)
146parser.add_argument("--rsp", help="rsp file", required=True)
147parser.add_argument(
148"--other-rel-path",
149help="Relative path from current build directory to other build "
150+ 'directory, e.g. from "out/Default" to "out/Other" specify "../Other"',
151required=True,
152)
153args = parser.parse_args()
154
155with open(args.rsp, "r") as f:
156rsp_entries = f.read()
157rsp_entries = rsp_entries.split()
158num_files_in_rsp = sum(1 for a in rsp_entries if is_path(a))
159if num_files_in_rsp == 0:
160print("No files in rsp?")
161return 1
162print("{} files in rsp".format(num_files_in_rsp))
163
164try:
165print("Initial testing")
166test0 = test_modified_rsp(
167args.test, modify_rsp(rsp_entries, args.other_rel_path, 0), args.rsp
168)
169test_all = test_modified_rsp(
170args.test,
171modify_rsp(rsp_entries, args.other_rel_path, num_files_in_rsp),
172args.rsp,
173)
174
175if test0 == test_all:
176print("Test returned same exit code for both build directories")
177return 1
178
179print("First build directory returned " + ("0" if test_all else "1"))
180
181result = bisect(
182args.test,
183test0,
184rsp_entries,
185num_files_in_rsp,
186args.other_rel_path,
187args.rsp,
188)
189print(
190"First file change: {} ({})".format(
191list(filter(is_path, rsp_entries))[result - 1], result
192)
193)
194
195rsp_out_0 = args.rsp + ".0"
196rsp_out_1 = args.rsp + ".1"
197with open(rsp_out_0, "w") as f:
198f.write(" ".join(modify_rsp(rsp_entries, args.other_rel_path, result - 1)))
199with open(rsp_out_1, "w") as f:
200f.write(" ".join(modify_rsp(rsp_entries, args.other_rel_path, result)))
201print(
202"Bisection point rsp files written to {} and {}".format(
203rsp_out_0, rsp_out_1
204)
205)
206finally:
207# Always make sure to write the original rsp file contents back so it's
208# less of a pain to rerun this script.
209with open(args.rsp, "w") as f:
210f.write(" ".join(rsp_entries))
211
212
213if __name__ == "__main__":
214sys.exit(main())
215