Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Darryl Green | 7869680 | 2018-04-06 11:23:22 +0100 | [diff] [blame] | 2 | """ |
| 3 | This file is part of Mbed TLS (https://tls.mbed.org) |
| 4 | |
| 5 | Copyright (c) 2018, Arm Limited, All Rights Reserved |
| 6 | |
| 7 | Purpose |
| 8 | |
| 9 | This script is a small wrapper around the abi-compliance-checker and |
| 10 | abi-dumper tools, applying them to compare the ABI and API of the library |
| 11 | files from two different Git revisions within an Mbed TLS repository. |
| 12 | The results of the comparison are formatted as HTML and stored at |
| 13 | a configurable location. Returns 0 on success, 1 on ABI/API non-compliance, |
| 14 | and 2 if there is an error while running the script. |
Darryl Green | 418527b | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 15 | Note: must be run from Mbed TLS root. |
Darryl Green | 7869680 | 2018-04-06 11:23:22 +0100 | [diff] [blame] | 16 | """ |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 17 | |
| 18 | import os |
| 19 | import sys |
| 20 | import traceback |
| 21 | import shutil |
| 22 | import subprocess |
| 23 | import argparse |
| 24 | import logging |
| 25 | import tempfile |
| 26 | |
| 27 | |
| 28 | class AbiChecker(object): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 29 | """API and ABI checker.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 30 | |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 31 | def __init__(self, report_dir, old_repo, old_rev, new_repo, new_rev, |
Darryl Green | 668063b | 2019-02-20 15:01:56 +0000 | [diff] [blame^] | 32 | keep_all_reports, skip_file=None): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 33 | """Instantiate the API/ABI checker. |
| 34 | |
| 35 | report_dir: directory for output files |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 36 | old_repo: repository for git revision to compare against |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 37 | old_rev: reference git revision to compare against |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 38 | new_repo: repository for git revision to check |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 39 | new_rev: git revision to check |
| 40 | keep_all_reports: if false, delete old reports |
Darryl Green | 668063b | 2019-02-20 15:01:56 +0000 | [diff] [blame^] | 41 | skip_file: path to file containing symbols and types to skip |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 42 | """ |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 43 | self.repo_path = "." |
| 44 | self.log = None |
| 45 | self.setup_logger() |
| 46 | self.report_dir = os.path.abspath(report_dir) |
| 47 | self.keep_all_reports = keep_all_reports |
| 48 | self.should_keep_report_dir = os.path.isdir(self.report_dir) |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 49 | self.old_repo = old_repo |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 50 | self.old_rev = old_rev |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 51 | self.new_repo = new_repo |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 52 | self.new_rev = new_rev |
Darryl Green | 668063b | 2019-02-20 15:01:56 +0000 | [diff] [blame^] | 53 | self.skip_file = skip_file |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 54 | self.mbedtls_modules = ["libmbedcrypto", "libmbedtls", "libmbedx509"] |
| 55 | self.old_dumps = {} |
| 56 | self.new_dumps = {} |
| 57 | self.git_command = "git" |
| 58 | self.make_command = "make" |
| 59 | |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 60 | @staticmethod |
| 61 | def check_repo_path(): |
Darryl Green | a6f430f | 2018-03-15 10:12:06 +0000 | [diff] [blame] | 62 | current_dir = os.path.realpath('.') |
| 63 | root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 64 | if current_dir != root_dir: |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 65 | raise Exception("Must be run from Mbed TLS root") |
| 66 | |
| 67 | def setup_logger(self): |
| 68 | self.log = logging.getLogger() |
| 69 | self.log.setLevel(logging.INFO) |
| 70 | self.log.addHandler(logging.StreamHandler()) |
| 71 | |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 72 | @staticmethod |
| 73 | def check_abi_tools_are_installed(): |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 74 | for command in ["abi-dumper", "abi-compliance-checker"]: |
| 75 | if not shutil.which(command): |
| 76 | raise Exception("{} not installed, aborting".format(command)) |
| 77 | |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 78 | def get_clean_worktree_for_git_revision(self, remote_repo, git_rev): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 79 | """Make a separate worktree with git_rev checked out. |
| 80 | Do not modify the current worktree.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 81 | git_worktree_path = tempfile.mkdtemp() |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 82 | if remote_repo: |
| 83 | self.log.info( |
| 84 | "Checking out git worktree for revision {} from {}".format( |
| 85 | git_rev, remote_repo |
| 86 | ) |
| 87 | ) |
| 88 | fetch_process = subprocess.Popen( |
| 89 | [self.git_command, "fetch", remote_repo, git_rev], |
| 90 | cwd=self.repo_path, |
| 91 | stdout=subprocess.PIPE, |
| 92 | stderr=subprocess.STDOUT |
| 93 | ) |
| 94 | fetch_output, _ = fetch_process.communicate() |
| 95 | self.log.info(fetch_output.decode("utf-8")) |
| 96 | if fetch_process.returncode != 0: |
| 97 | raise Exception("Fetching revision failed, aborting") |
| 98 | worktree_rev = "FETCH_HEAD" |
| 99 | else: |
| 100 | self.log.info( |
| 101 | "Checking out git worktree for revision {}".format(git_rev) |
| 102 | ) |
| 103 | worktree_rev = git_rev |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 104 | worktree_process = subprocess.Popen( |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 105 | [self.git_command, "worktree", "add", "--detach", |
| 106 | git_worktree_path, worktree_rev], |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 107 | cwd=self.repo_path, |
| 108 | stdout=subprocess.PIPE, |
| 109 | stderr=subprocess.STDOUT |
| 110 | ) |
| 111 | worktree_output, _ = worktree_process.communicate() |
| 112 | self.log.info(worktree_output.decode("utf-8")) |
| 113 | if worktree_process.returncode != 0: |
| 114 | raise Exception("Checking out worktree failed, aborting") |
| 115 | return git_worktree_path |
| 116 | |
Jaeden Amero | 4cd4b4b | 2018-11-02 16:35:09 +0000 | [diff] [blame] | 117 | def update_git_submodules(self, git_worktree_path): |
| 118 | process = subprocess.Popen( |
| 119 | [self.git_command, "submodule", "update", "--init", '--recursive'], |
| 120 | cwd=git_worktree_path, |
| 121 | stdout=subprocess.PIPE, |
| 122 | stderr=subprocess.STDOUT |
| 123 | ) |
| 124 | output, _ = process.communicate() |
| 125 | self.log.info(output.decode("utf-8")) |
| 126 | if process.returncode != 0: |
| 127 | raise Exception("git submodule update failed, aborting") |
| 128 | |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 129 | def build_shared_libraries(self, git_worktree_path): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 130 | """Build the shared libraries in the specified worktree.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 131 | my_environment = os.environ.copy() |
| 132 | my_environment["CFLAGS"] = "-g -Og" |
| 133 | my_environment["SHARED"] = "1" |
| 134 | make_process = subprocess.Popen( |
| 135 | self.make_command, |
| 136 | env=my_environment, |
| 137 | cwd=git_worktree_path, |
| 138 | stdout=subprocess.PIPE, |
| 139 | stderr=subprocess.STDOUT |
| 140 | ) |
| 141 | make_output, _ = make_process.communicate() |
| 142 | self.log.info(make_output.decode("utf-8")) |
| 143 | if make_process.returncode != 0: |
| 144 | raise Exception("make failed, aborting") |
| 145 | |
| 146 | def get_abi_dumps_from_shared_libraries(self, git_ref, git_worktree_path): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 147 | """Generate the ABI dumps for the specified git revision. |
| 148 | It must be checked out in git_worktree_path and the shared libraries |
| 149 | must have been built.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 150 | abi_dumps = {} |
| 151 | for mbed_module in self.mbedtls_modules: |
| 152 | output_path = os.path.join( |
| 153 | self.report_dir, "{}-{}.dump".format(mbed_module, git_ref) |
| 154 | ) |
| 155 | abi_dump_command = [ |
| 156 | "abi-dumper", |
| 157 | os.path.join( |
| 158 | git_worktree_path, "library", mbed_module + ".so"), |
| 159 | "-o", output_path, |
| 160 | "-lver", git_ref |
| 161 | ] |
| 162 | abi_dump_process = subprocess.Popen( |
| 163 | abi_dump_command, |
| 164 | stdout=subprocess.PIPE, |
| 165 | stderr=subprocess.STDOUT |
| 166 | ) |
| 167 | abi_dump_output, _ = abi_dump_process.communicate() |
| 168 | self.log.info(abi_dump_output.decode("utf-8")) |
| 169 | if abi_dump_process.returncode != 0: |
| 170 | raise Exception("abi-dumper failed, aborting") |
| 171 | abi_dumps[mbed_module] = output_path |
| 172 | return abi_dumps |
| 173 | |
| 174 | def cleanup_worktree(self, git_worktree_path): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 175 | """Remove the specified git worktree.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 176 | shutil.rmtree(git_worktree_path) |
| 177 | worktree_process = subprocess.Popen( |
| 178 | [self.git_command, "worktree", "prune"], |
| 179 | cwd=self.repo_path, |
| 180 | stdout=subprocess.PIPE, |
| 181 | stderr=subprocess.STDOUT |
| 182 | ) |
| 183 | worktree_output, _ = worktree_process.communicate() |
| 184 | self.log.info(worktree_output.decode("utf-8")) |
| 185 | if worktree_process.returncode != 0: |
| 186 | raise Exception("Worktree cleanup failed, aborting") |
| 187 | |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 188 | def get_abi_dump_for_ref(self, remote_repo, git_rev): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 189 | """Generate the ABI dumps for the specified git revision.""" |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 190 | git_worktree_path = self.get_clean_worktree_for_git_revision( |
| 191 | remote_repo, git_rev |
| 192 | ) |
Jaeden Amero | 4cd4b4b | 2018-11-02 16:35:09 +0000 | [diff] [blame] | 193 | self.update_git_submodules(git_worktree_path) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 194 | self.build_shared_libraries(git_worktree_path) |
| 195 | abi_dumps = self.get_abi_dumps_from_shared_libraries( |
| 196 | git_rev, git_worktree_path |
| 197 | ) |
| 198 | self.cleanup_worktree(git_worktree_path) |
| 199 | return abi_dumps |
| 200 | |
| 201 | def get_abi_compatibility_report(self): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 202 | """Generate a report of the differences between the reference ABI |
| 203 | and the new ABI. ABI dumps from self.old_rev and self.new_rev must |
| 204 | be available.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 205 | compatibility_report = "" |
| 206 | compliance_return_code = 0 |
| 207 | for mbed_module in self.mbedtls_modules: |
| 208 | output_path = os.path.join( |
| 209 | self.report_dir, "{}-{}-{}.html".format( |
| 210 | mbed_module, self.old_rev, self.new_rev |
| 211 | ) |
| 212 | ) |
| 213 | abi_compliance_command = [ |
| 214 | "abi-compliance-checker", |
| 215 | "-l", mbed_module, |
| 216 | "-old", self.old_dumps[mbed_module], |
| 217 | "-new", self.new_dumps[mbed_module], |
| 218 | "-strict", |
| 219 | "-report-path", output_path |
| 220 | ] |
Darryl Green | 668063b | 2019-02-20 15:01:56 +0000 | [diff] [blame^] | 221 | if self.skip_file: |
| 222 | abi_compliance_command += ["-skip-symbols", self.skip_file, |
| 223 | "-skip-types", self.skip_file] |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 224 | abi_compliance_process = subprocess.Popen( |
| 225 | abi_compliance_command, |
| 226 | stdout=subprocess.PIPE, |
| 227 | stderr=subprocess.STDOUT |
| 228 | ) |
| 229 | abi_compliance_output, _ = abi_compliance_process.communicate() |
| 230 | self.log.info(abi_compliance_output.decode("utf-8")) |
| 231 | if abi_compliance_process.returncode == 0: |
| 232 | compatibility_report += ( |
| 233 | "No compatibility issues for {}\n".format(mbed_module) |
| 234 | ) |
| 235 | if not self.keep_all_reports: |
| 236 | os.remove(output_path) |
| 237 | elif abi_compliance_process.returncode == 1: |
| 238 | compliance_return_code = 1 |
| 239 | self.should_keep_report_dir = True |
| 240 | compatibility_report += ( |
| 241 | "Compatibility issues found for {}, " |
| 242 | "for details see {}\n".format(mbed_module, output_path) |
| 243 | ) |
| 244 | else: |
| 245 | raise Exception( |
| 246 | "abi-compliance-checker failed with a return code of {}," |
| 247 | " aborting".format(abi_compliance_process.returncode) |
| 248 | ) |
| 249 | os.remove(self.old_dumps[mbed_module]) |
| 250 | os.remove(self.new_dumps[mbed_module]) |
| 251 | if not self.should_keep_report_dir and not self.keep_all_reports: |
| 252 | os.rmdir(self.report_dir) |
| 253 | self.log.info(compatibility_report) |
| 254 | return compliance_return_code |
| 255 | |
| 256 | def check_for_abi_changes(self): |
Gilles Peskine | 9df1763 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 257 | """Generate a report of ABI differences |
| 258 | between self.old_rev and self.new_rev.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 259 | self.check_repo_path() |
| 260 | self.check_abi_tools_are_installed() |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 261 | self.old_dumps = self.get_abi_dump_for_ref(self.old_repo, self.old_rev) |
| 262 | self.new_dumps = self.get_abi_dump_for_ref(self.new_repo, self.new_rev) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 263 | return self.get_abi_compatibility_report() |
| 264 | |
| 265 | |
| 266 | def run_main(): |
| 267 | try: |
| 268 | parser = argparse.ArgumentParser( |
| 269 | description=( |
Darryl Green | 418527b | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 270 | """This script is a small wrapper around the |
| 271 | abi-compliance-checker and abi-dumper tools, applying them |
| 272 | to compare the ABI and API of the library files from two |
| 273 | different Git revisions within an Mbed TLS repository. |
| 274 | The results of the comparison are formatted as HTML and stored |
| 275 | at a configurable location. Returns 0 on success, 1 on ABI/API |
| 276 | non-compliance, and 2 if there is an error while running the |
| 277 | script. Note: must be run from Mbed TLS root.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 278 | ) |
| 279 | ) |
| 280 | parser.add_argument( |
Darryl Green | 418527b | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 281 | "-r", "--report-dir", type=str, default="reports", |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 282 | help="directory where reports are stored, default is reports", |
| 283 | ) |
| 284 | parser.add_argument( |
Darryl Green | 418527b | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 285 | "-k", "--keep-all-reports", action="store_true", |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 286 | help="keep all reports, even if there are no compatibility issues", |
| 287 | ) |
| 288 | parser.add_argument( |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 289 | "-o", "--old-rev", type=str, |
| 290 | help=("revision for old version." |
| 291 | "Can include repository before revision"), |
| 292 | required=True, nargs="+" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 293 | ) |
| 294 | parser.add_argument( |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 295 | "-n", "--new-rev", type=str, |
| 296 | help=("revision for new version" |
| 297 | "Can include repository before revision"), |
| 298 | required=True, nargs="+" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 299 | ) |
Darryl Green | 668063b | 2019-02-20 15:01:56 +0000 | [diff] [blame^] | 300 | parser.add_argument( |
| 301 | "-s", "--skip-file", type=str, |
| 302 | help="path to file containing symbols and types to skip" |
| 303 | ) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 304 | abi_args = parser.parse_args() |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 305 | if len(abi_args.old_rev) == 1: |
| 306 | old_repo = None |
| 307 | old_rev = abi_args.old_rev[0] |
| 308 | elif len(abi_args.old_rev) == 2: |
| 309 | old_repo = abi_args.old_rev[0] |
| 310 | old_rev = abi_args.old_rev[1] |
| 311 | else: |
| 312 | raise Exception("Too many arguments passed for old version") |
| 313 | if len(abi_args.new_rev) == 1: |
| 314 | new_repo = None |
| 315 | new_rev = abi_args.new_rev[0] |
| 316 | elif len(abi_args.new_rev) == 2: |
| 317 | new_repo = abi_args.new_rev[0] |
| 318 | new_rev = abi_args.new_rev[1] |
| 319 | else: |
| 320 | raise Exception("Too many arguments passed for new version") |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 321 | abi_check = AbiChecker( |
Darryl Green | 5a301f0 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 322 | abi_args.report_dir, old_repo, old_rev, |
Darryl Green | 668063b | 2019-02-20 15:01:56 +0000 | [diff] [blame^] | 323 | new_repo, new_rev, abi_args.keep_all_reports, |
| 324 | abi_args.skip_file |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 325 | ) |
| 326 | return_code = abi_check.check_for_abi_changes() |
| 327 | sys.exit(return_code) |
Gilles Peskine | afd19dd | 2019-02-25 21:39:42 +0100 | [diff] [blame] | 328 | except Exception: # pylint: disable=broad-except |
| 329 | # Print the backtrace and exit explicitly so as to exit with |
| 330 | # status 2, not 1. |
Darryl Green | a6f430f | 2018-03-15 10:12:06 +0000 | [diff] [blame] | 331 | traceback.print_exc() |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 332 | sys.exit(2) |
| 333 | |
| 334 | |
| 335 | if __name__ == "__main__": |
| 336 | run_main() |