check-kabi 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/python3
  2. #
  3. # check-kabi - Red Hat kABI reference checking tool
  4. #
  5. # We use this script to check against reference Module.kabi files.
  6. #
  7. # Author: Jon Masters <[email protected]>
  8. # Copyright (C) 2007-2009 Red Hat, Inc.
  9. #
  10. # This software may be freely redistributed under the terms of the GNU
  11. # General Public License (GPL).
  12. # Changelog:
  13. #
  14. # 2018/06/01 - Update for python3 by Petr Oros.
  15. # 2009/08/15 - Updated for use in RHEL6.
  16. # 2007/06/13 - Initial rewrite in python by Jon Masters.
  17. __author__ = "Jon Masters <[email protected]>"
  18. __version__ = "2.0"
  19. __date__ = "2009/08/15"
  20. __copyright__ = "Copyright (C) 2007-2009 Red Hat, Inc"
  21. __license__ = "GPL"
  22. import getopt
  23. import string
  24. import sys
  25. true = 1
  26. false = 0
  27. def load_symvers(symvers, filename):
  28. """Load a Module.symvers file."""
  29. symvers_file = open(filename, "r")
  30. while true:
  31. in_line = symvers_file.readline()
  32. if in_line == "":
  33. break
  34. if in_line == "\n":
  35. continue
  36. checksum, symbol, directory, type, *ns = in_line.split()
  37. ns = ns[0] if ns else None
  38. symvers[symbol] = in_line[0:-1]
  39. def load_kabi(kabi, filename):
  40. """Load a Module.kabi file."""
  41. kabi_file = open(filename, "r")
  42. while true:
  43. in_line = kabi_file.readline()
  44. if in_line == "":
  45. break
  46. if in_line == "\n":
  47. continue
  48. checksum, symbol, directory, type, *ns = in_line.split()
  49. ns = ns[0] if ns else None
  50. kabi[symbol] = in_line[0:-1]
  51. def check_kabi(symvers, kabi):
  52. """Check Module.kabi and Module.symvers files."""
  53. fail = 0
  54. warn = 0
  55. changed_symbols = []
  56. moved_symbols = []
  57. ns_symbols = []
  58. for symbol in kabi:
  59. abi_hash, abi_sym, abi_dir, abi_type, *abi_ns = kabi[symbol].split()
  60. abi_ns = abi_ns[0] if abi_ns else None
  61. if symbol in symvers:
  62. sym_hash, sym_sym, sym_dir, sym_type, *sym_ns = symvers[symbol].split()
  63. sym_ns = sym_ns[0] if sym_ns else None
  64. if abi_hash != sym_hash:
  65. fail = 1
  66. changed_symbols.append(symbol)
  67. if abi_dir != sym_dir:
  68. warn = 1
  69. moved_symbols.append(symbol)
  70. if abi_ns != sym_ns:
  71. warn = 1
  72. ns_symbols.append(symbol)
  73. else:
  74. fail = 1
  75. changed_symbols.append(symbol)
  76. if fail:
  77. print("*** ERROR - ABI BREAKAGE WAS DETECTED ***")
  78. print("")
  79. print("The following symbols have been changed (this will cause an ABI breakage):")
  80. print("")
  81. for symbol in changed_symbols:
  82. print(symbol)
  83. print("")
  84. if warn:
  85. print("*** WARNING - ABI SYMBOLS MOVED ***")
  86. if moved_symbols:
  87. print("")
  88. print("The following symbols moved (typically caused by moving a symbol from being")
  89. print("provided by the kernel vmlinux out to a loadable module):")
  90. print("")
  91. for symbol in moved_symbols:
  92. print(symbol)
  93. print("")
  94. if ns_symbols:
  95. print("")
  96. print("The following symbols changed symbol namespaces:")
  97. print("")
  98. for symbol in ns_symbols:
  99. print(symbol)
  100. print("")
  101. """Halt the build, if we got errors and/or warnings. In either case,
  102. double-checkig is required to avoid introducing / concealing
  103. KABI inconsistencies."""
  104. if fail or warn:
  105. sys.exit(1)
  106. sys.exit(0)
  107. def usage():
  108. print("""
  109. check-kabi: check Module.kabi and Module.symvers files.
  110. check-kabi [ -k Module.kabi ] [ -s Module.symvers ]
  111. """)
  112. if __name__ == "__main__":
  113. symvers_file = ""
  114. kabi_file = ""
  115. opts, args = getopt.getopt(sys.argv[1:], 'hk:s:')
  116. for o, v in opts:
  117. if o == "-s":
  118. symvers_file = v
  119. if o == "-h":
  120. usage()
  121. sys.exit(0)
  122. if o == "-k":
  123. kabi_file = v
  124. if (symvers_file == "") or (kabi_file == ""):
  125. usage()
  126. sys.exit(1)
  127. symvers = {}
  128. kabi = {}
  129. load_symvers(symvers, symvers_file)
  130. load_kabi(kabi, kabi_file)
  131. check_kabi(symvers, kabi)