abi_check.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #!/usr/bin/env python3
  2. """
  3. Purpose
  4. This script is a small wrapper around the abi-compliance-checker and
  5. abi-dumper tools, applying them to compare the ABI and API of the library
  6. files from two different Git revisions within an Mbed TLS repository.
  7. The results of the comparison are either formatted as HTML and stored at
  8. a configurable location, or are given as a brief list of problems.
  9. Returns 0 on success, 1 on ABI/API non-compliance, and 2 if there is an error
  10. while running the script. Note: must be run from Mbed TLS root.
  11. """
  12. # Copyright The Mbed TLS Contributors
  13. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  14. #
  15. # This file is provided under the Apache License 2.0, or the
  16. # GNU General Public License v2.0 or later.
  17. #
  18. # **********
  19. # Apache License 2.0:
  20. #
  21. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  22. # not use this file except in compliance with the License.
  23. # You may obtain a copy of the License at
  24. #
  25. # http://www.apache.org/licenses/LICENSE-2.0
  26. #
  27. # Unless required by applicable law or agreed to in writing, software
  28. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  29. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  30. # See the License for the specific language governing permissions and
  31. # limitations under the License.
  32. #
  33. # **********
  34. #
  35. # **********
  36. # GNU General Public License v2.0 or later:
  37. #
  38. # This program is free software; you can redistribute it and/or modify
  39. # it under the terms of the GNU General Public License as published by
  40. # the Free Software Foundation; either version 2 of the License, or
  41. # (at your option) any later version.
  42. #
  43. # This program is distributed in the hope that it will be useful,
  44. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46. # GNU General Public License for more details.
  47. #
  48. # You should have received a copy of the GNU General Public License along
  49. # with this program; if not, write to the Free Software Foundation, Inc.,
  50. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  51. #
  52. # **********
  53. import os
  54. import sys
  55. import traceback
  56. import shutil
  57. import subprocess
  58. import argparse
  59. import logging
  60. import tempfile
  61. import fnmatch
  62. from types import SimpleNamespace
  63. import xml.etree.ElementTree as ET
  64. class AbiChecker:
  65. """API and ABI checker."""
  66. def __init__(self, old_version, new_version, configuration):
  67. """Instantiate the API/ABI checker.
  68. old_version: RepoVersion containing details to compare against
  69. new_version: RepoVersion containing details to check
  70. configuration.report_dir: directory for output files
  71. configuration.keep_all_reports: if false, delete old reports
  72. configuration.brief: if true, output shorter report to stdout
  73. configuration.skip_file: path to file containing symbols and types to skip
  74. """
  75. self.repo_path = "."
  76. self.log = None
  77. self.verbose = configuration.verbose
  78. self._setup_logger()
  79. self.report_dir = os.path.abspath(configuration.report_dir)
  80. self.keep_all_reports = configuration.keep_all_reports
  81. self.can_remove_report_dir = not (os.path.exists(self.report_dir) or
  82. self.keep_all_reports)
  83. self.old_version = old_version
  84. self.new_version = new_version
  85. self.skip_file = configuration.skip_file
  86. self.brief = configuration.brief
  87. self.git_command = "git"
  88. self.make_command = "make"
  89. @staticmethod
  90. def check_repo_path():
  91. if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
  92. raise Exception("Must be run from Mbed TLS root")
  93. def _setup_logger(self):
  94. self.log = logging.getLogger()
  95. if self.verbose:
  96. self.log.setLevel(logging.DEBUG)
  97. else:
  98. self.log.setLevel(logging.INFO)
  99. self.log.addHandler(logging.StreamHandler())
  100. @staticmethod
  101. def check_abi_tools_are_installed():
  102. for command in ["abi-dumper", "abi-compliance-checker"]:
  103. if not shutil.which(command):
  104. raise Exception("{} not installed, aborting".format(command))
  105. def _get_clean_worktree_for_git_revision(self, version):
  106. """Make a separate worktree with version.revision checked out.
  107. Do not modify the current worktree."""
  108. git_worktree_path = tempfile.mkdtemp()
  109. if version.repository:
  110. self.log.debug(
  111. "Checking out git worktree for revision {} from {}".format(
  112. version.revision, version.repository
  113. )
  114. )
  115. fetch_output = subprocess.check_output(
  116. [self.git_command, "fetch",
  117. version.repository, version.revision],
  118. cwd=self.repo_path,
  119. stderr=subprocess.STDOUT
  120. )
  121. self.log.debug(fetch_output.decode("utf-8"))
  122. worktree_rev = "FETCH_HEAD"
  123. else:
  124. self.log.debug("Checking out git worktree for revision {}".format(
  125. version.revision
  126. ))
  127. worktree_rev = version.revision
  128. worktree_output = subprocess.check_output(
  129. [self.git_command, "worktree", "add", "--detach",
  130. git_worktree_path, worktree_rev],
  131. cwd=self.repo_path,
  132. stderr=subprocess.STDOUT
  133. )
  134. self.log.debug(worktree_output.decode("utf-8"))
  135. version.commit = subprocess.check_output(
  136. [self.git_command, "rev-parse", "HEAD"],
  137. cwd=git_worktree_path,
  138. stderr=subprocess.STDOUT
  139. ).decode("ascii").rstrip()
  140. self.log.debug("Commit is {}".format(version.commit))
  141. return git_worktree_path
  142. def _update_git_submodules(self, git_worktree_path, version):
  143. """If the crypto submodule is present, initialize it.
  144. if version.crypto_revision exists, update it to that revision,
  145. otherwise update it to the default revision"""
  146. update_output = subprocess.check_output(
  147. [self.git_command, "submodule", "update", "--init", '--recursive'],
  148. cwd=git_worktree_path,
  149. stderr=subprocess.STDOUT
  150. )
  151. self.log.debug(update_output.decode("utf-8"))
  152. if not (os.path.exists(os.path.join(git_worktree_path, "crypto"))
  153. and version.crypto_revision):
  154. return
  155. if version.crypto_repository:
  156. fetch_output = subprocess.check_output(
  157. [self.git_command, "fetch", version.crypto_repository,
  158. version.crypto_revision],
  159. cwd=os.path.join(git_worktree_path, "crypto"),
  160. stderr=subprocess.STDOUT
  161. )
  162. self.log.debug(fetch_output.decode("utf-8"))
  163. crypto_rev = "FETCH_HEAD"
  164. else:
  165. crypto_rev = version.crypto_revision
  166. checkout_output = subprocess.check_output(
  167. [self.git_command, "checkout", crypto_rev],
  168. cwd=os.path.join(git_worktree_path, "crypto"),
  169. stderr=subprocess.STDOUT
  170. )
  171. self.log.debug(checkout_output.decode("utf-8"))
  172. def _build_shared_libraries(self, git_worktree_path, version):
  173. """Build the shared libraries in the specified worktree."""
  174. my_environment = os.environ.copy()
  175. my_environment["CFLAGS"] = "-g -Og"
  176. my_environment["SHARED"] = "1"
  177. if os.path.exists(os.path.join(git_worktree_path, "crypto")):
  178. my_environment["USE_CRYPTO_SUBMODULE"] = "1"
  179. make_output = subprocess.check_output(
  180. [self.make_command, "lib"],
  181. env=my_environment,
  182. cwd=git_worktree_path,
  183. stderr=subprocess.STDOUT
  184. )
  185. self.log.debug(make_output.decode("utf-8"))
  186. for root, _dirs, files in os.walk(git_worktree_path):
  187. for file in fnmatch.filter(files, "*.so"):
  188. version.modules[os.path.splitext(file)[0]] = (
  189. os.path.join(root, file)
  190. )
  191. @staticmethod
  192. def _pretty_revision(version):
  193. if version.revision == version.commit:
  194. return version.revision
  195. else:
  196. return "{} ({})".format(version.revision, version.commit)
  197. def _get_abi_dumps_from_shared_libraries(self, version):
  198. """Generate the ABI dumps for the specified git revision.
  199. The shared libraries must have been built and the module paths
  200. present in version.modules."""
  201. for mbed_module, module_path in version.modules.items():
  202. output_path = os.path.join(
  203. self.report_dir, "{}-{}-{}.dump".format(
  204. mbed_module, version.revision, version.version
  205. )
  206. )
  207. abi_dump_command = [
  208. "abi-dumper",
  209. module_path,
  210. "-o", output_path,
  211. "-lver", self._pretty_revision(version),
  212. ]
  213. abi_dump_output = subprocess.check_output(
  214. abi_dump_command,
  215. stderr=subprocess.STDOUT
  216. )
  217. self.log.debug(abi_dump_output.decode("utf-8"))
  218. version.abi_dumps[mbed_module] = output_path
  219. def _cleanup_worktree(self, git_worktree_path):
  220. """Remove the specified git worktree."""
  221. shutil.rmtree(git_worktree_path)
  222. worktree_output = subprocess.check_output(
  223. [self.git_command, "worktree", "prune"],
  224. cwd=self.repo_path,
  225. stderr=subprocess.STDOUT
  226. )
  227. self.log.debug(worktree_output.decode("utf-8"))
  228. def _get_abi_dump_for_ref(self, version):
  229. """Generate the ABI dumps for the specified git revision."""
  230. git_worktree_path = self._get_clean_worktree_for_git_revision(version)
  231. self._update_git_submodules(git_worktree_path, version)
  232. self._build_shared_libraries(git_worktree_path, version)
  233. self._get_abi_dumps_from_shared_libraries(version)
  234. self._cleanup_worktree(git_worktree_path)
  235. def _remove_children_with_tag(self, parent, tag):
  236. children = parent.getchildren()
  237. for child in children:
  238. if child.tag == tag:
  239. parent.remove(child)
  240. else:
  241. self._remove_children_with_tag(child, tag)
  242. def _remove_extra_detail_from_report(self, report_root):
  243. for tag in ['test_info', 'test_results', 'problem_summary',
  244. 'added_symbols', 'affected']:
  245. self._remove_children_with_tag(report_root, tag)
  246. for report in report_root:
  247. for problems in report.getchildren()[:]:
  248. if not problems.getchildren():
  249. report.remove(problems)
  250. def _abi_compliance_command(self, mbed_module, output_path):
  251. """Build the command to run to analyze the library mbed_module.
  252. The report will be placed in output_path."""
  253. abi_compliance_command = [
  254. "abi-compliance-checker",
  255. "-l", mbed_module,
  256. "-old", self.old_version.abi_dumps[mbed_module],
  257. "-new", self.new_version.abi_dumps[mbed_module],
  258. "-strict",
  259. "-report-path", output_path,
  260. ]
  261. if self.skip_file:
  262. abi_compliance_command += ["-skip-symbols", self.skip_file,
  263. "-skip-types", self.skip_file]
  264. if self.brief:
  265. abi_compliance_command += ["-report-format", "xml",
  266. "-stdout"]
  267. return abi_compliance_command
  268. def _is_library_compatible(self, mbed_module, compatibility_report):
  269. """Test if the library mbed_module has remained compatible.
  270. Append a message regarding compatibility to compatibility_report."""
  271. output_path = os.path.join(
  272. self.report_dir, "{}-{}-{}.html".format(
  273. mbed_module, self.old_version.revision,
  274. self.new_version.revision
  275. )
  276. )
  277. try:
  278. subprocess.check_output(
  279. self._abi_compliance_command(mbed_module, output_path),
  280. stderr=subprocess.STDOUT
  281. )
  282. except subprocess.CalledProcessError as err:
  283. if err.returncode != 1:
  284. raise err
  285. if self.brief:
  286. self.log.info(
  287. "Compatibility issues found for {}".format(mbed_module)
  288. )
  289. report_root = ET.fromstring(err.output.decode("utf-8"))
  290. self._remove_extra_detail_from_report(report_root)
  291. self.log.info(ET.tostring(report_root).decode("utf-8"))
  292. else:
  293. self.can_remove_report_dir = False
  294. compatibility_report.append(
  295. "Compatibility issues found for {}, "
  296. "for details see {}".format(mbed_module, output_path)
  297. )
  298. return False
  299. compatibility_report.append(
  300. "No compatibility issues for {}".format(mbed_module)
  301. )
  302. if not (self.keep_all_reports or self.brief):
  303. os.remove(output_path)
  304. return True
  305. def get_abi_compatibility_report(self):
  306. """Generate a report of the differences between the reference ABI
  307. and the new ABI. ABI dumps from self.old_version and self.new_version
  308. must be available."""
  309. compatibility_report = ["Checking evolution from {} to {}".format(
  310. self._pretty_revision(self.old_version),
  311. self._pretty_revision(self.new_version)
  312. )]
  313. compliance_return_code = 0
  314. shared_modules = list(set(self.old_version.modules.keys()) &
  315. set(self.new_version.modules.keys()))
  316. for mbed_module in shared_modules:
  317. if not self._is_library_compatible(mbed_module,
  318. compatibility_report):
  319. compliance_return_code = 1
  320. for version in [self.old_version, self.new_version]:
  321. for mbed_module, mbed_module_dump in version.abi_dumps.items():
  322. os.remove(mbed_module_dump)
  323. if self.can_remove_report_dir:
  324. os.rmdir(self.report_dir)
  325. self.log.info("\n".join(compatibility_report))
  326. return compliance_return_code
  327. def check_for_abi_changes(self):
  328. """Generate a report of ABI differences
  329. between self.old_rev and self.new_rev."""
  330. self.check_repo_path()
  331. self.check_abi_tools_are_installed()
  332. self._get_abi_dump_for_ref(self.old_version)
  333. self._get_abi_dump_for_ref(self.new_version)
  334. return self.get_abi_compatibility_report()
  335. def run_main():
  336. try:
  337. parser = argparse.ArgumentParser(
  338. description=(
  339. """This script is a small wrapper around the
  340. abi-compliance-checker and abi-dumper tools, applying them
  341. to compare the ABI and API of the library files from two
  342. different Git revisions within an Mbed TLS repository.
  343. The results of the comparison are either formatted as HTML and
  344. stored at a configurable location, or are given as a brief list
  345. of problems. Returns 0 on success, 1 on ABI/API non-compliance,
  346. and 2 if there is an error while running the script.
  347. Note: must be run from Mbed TLS root."""
  348. )
  349. )
  350. parser.add_argument(
  351. "-v", "--verbose", action="store_true",
  352. help="set verbosity level",
  353. )
  354. parser.add_argument(
  355. "-r", "--report-dir", type=str, default="reports",
  356. help="directory where reports are stored, default is reports",
  357. )
  358. parser.add_argument(
  359. "-k", "--keep-all-reports", action="store_true",
  360. help="keep all reports, even if there are no compatibility issues",
  361. )
  362. parser.add_argument(
  363. "-o", "--old-rev", type=str, help="revision for old version.",
  364. required=True,
  365. )
  366. parser.add_argument(
  367. "-or", "--old-repo", type=str, help="repository for old version."
  368. )
  369. parser.add_argument(
  370. "-oc", "--old-crypto-rev", type=str,
  371. help="revision for old crypto submodule."
  372. )
  373. parser.add_argument(
  374. "-ocr", "--old-crypto-repo", type=str,
  375. help="repository for old crypto submodule."
  376. )
  377. parser.add_argument(
  378. "-n", "--new-rev", type=str, help="revision for new version",
  379. required=True,
  380. )
  381. parser.add_argument(
  382. "-nr", "--new-repo", type=str, help="repository for new version."
  383. )
  384. parser.add_argument(
  385. "-nc", "--new-crypto-rev", type=str,
  386. help="revision for new crypto version"
  387. )
  388. parser.add_argument(
  389. "-ncr", "--new-crypto-repo", type=str,
  390. help="repository for new crypto submodule."
  391. )
  392. parser.add_argument(
  393. "-s", "--skip-file", type=str,
  394. help=("path to file containing symbols and types to skip "
  395. "(typically \"-s identifiers\" after running "
  396. "\"tests/scripts/list-identifiers.sh --internal\")")
  397. )
  398. parser.add_argument(
  399. "-b", "--brief", action="store_true",
  400. help="output only the list of issues to stdout, instead of a full report",
  401. )
  402. abi_args = parser.parse_args()
  403. if os.path.isfile(abi_args.report_dir):
  404. print("Error: {} is not a directory".format(abi_args.report_dir))
  405. parser.exit()
  406. old_version = SimpleNamespace(
  407. version="old",
  408. repository=abi_args.old_repo,
  409. revision=abi_args.old_rev,
  410. commit=None,
  411. crypto_repository=abi_args.old_crypto_repo,
  412. crypto_revision=abi_args.old_crypto_rev,
  413. abi_dumps={},
  414. modules={}
  415. )
  416. new_version = SimpleNamespace(
  417. version="new",
  418. repository=abi_args.new_repo,
  419. revision=abi_args.new_rev,
  420. commit=None,
  421. crypto_repository=abi_args.new_crypto_repo,
  422. crypto_revision=abi_args.new_crypto_rev,
  423. abi_dumps={},
  424. modules={}
  425. )
  426. configuration = SimpleNamespace(
  427. verbose=abi_args.verbose,
  428. report_dir=abi_args.report_dir,
  429. keep_all_reports=abi_args.keep_all_reports,
  430. brief=abi_args.brief,
  431. skip_file=abi_args.skip_file
  432. )
  433. abi_check = AbiChecker(old_version, new_version, configuration)
  434. return_code = abi_check.check_for_abi_changes()
  435. sys.exit(return_code)
  436. except Exception: # pylint: disable=broad-except
  437. # Print the backtrace and exit explicitly so as to exit with
  438. # status 2, not 1.
  439. traceback.print_exc()
  440. sys.exit(2)
  441. if __name__ == "__main__":
  442. run_main()