Debian Codegraph @

Tools/build/smelly.py

raw
1 #!/usr/bin/env python
2 # Script checking that all symbols exported by libpython start with Py or _Py
3
4 import os.path
5 import subprocess
6 import sys
7 import sysconfig
8
9 ALLOWED_PREFIXES = ('Py', '_Py')
10 if sys.platform == 'darwin':
11 ALLOWED_PREFIXES += ('__Py',)
12
13 # mimalloc doesn't use static, but it's symbols are not exported
14 # from the shared library. They do show up in the static library
15 # before its linked into an executable.
16 ALLOWED_STATIC_PREFIXES = ('mi_', '_mi_')
17
18 # "Legacy": some old symbols are prefixed by "PY_".
19 EXCEPTIONS = frozenset({
20 'PY_TIMEOUT_MAX',
21 })
22
23 IGNORED_EXTENSION = "_ctypes_test"
24 # Ignore constructor and destructor functions
25 IGNORED_SYMBOLS = {'_init', '_fini'}
26
27
28 def is_local_symbol_type(symtype):
29 # Ignore local symbols.
30
31 # If lowercase, the symbol is usually local; if uppercase, the symbol
32 # is global (external). There are however a few lowercase symbols that
33 # are shown for special global symbols ("u", "v" and "w").
34 if symtype.islower() and symtype not in "uvw":
35 return True
36
37 # Ignore the initialized data section (d and D) and the BSS data
38 # section. For example, ignore "__bss_start (type: B)"
39 # and "_edata (type: D)".
40 if symtype in "bBdD":
41 return True
42
43 return False
44
45
46 def get_exported_symbols(library, dynamic=False):
47 print(f"Check that {library} only exports symbols starting with Py or _Py")
48
49 # Only look at dynamic symbols
50 args = ['nm', '--no-sort']
51 if dynamic:
52 args.append('--dynamic')
53 args.append(library)
54 print(f"+ {' '.join(args)}")
55 proc = subprocess.run(args, stdout=subprocess.PIPE, encoding='utf-8')
56 if proc.returncode:
57 sys.stdout.write(proc.stdout)
58 sys.exit(proc.returncode)
59
60 stdout = proc.stdout.rstrip()
61 if not stdout:
62 raise Exception("command output is empty")
63 return stdout
64
65
66 def get_smelly_symbols(stdout, dynamic=False):
67 smelly_symbols = []
68 python_symbols = []
69 local_symbols = []
70
71 for line in stdout.splitlines():
72 # Split line '0000000000001b80 D PyTextIOWrapper_Type'
73 if not line:
74 continue
75
76 parts = line.split(maxsplit=2)
77 if len(parts) < 3:
78 continue
79
80 symtype = parts[1].strip()
81 symbol = parts[-1]
82 result = f'{symbol} (type: {symtype})'
83
84 if (symbol.startswith(ALLOWED_PREFIXES) or
85 symbol in EXCEPTIONS or
86 (not dynamic and symbol.startswith(ALLOWED_STATIC_PREFIXES))):
87 python_symbols.append(result)
88 continue
89
90 if is_local_symbol_type(symtype):
91 local_symbols.append(result)
92 elif symbol in IGNORED_SYMBOLS:
93 local_symbols.append(result)
94 else:
95 smelly_symbols.append(result)
96
97 if local_symbols:
98 print(f"Ignore {len(local_symbols)} local symbols")
99 return smelly_symbols, python_symbols
100
101
102 def check_library(library, dynamic=False):
103 nm_output = get_exported_symbols(library, dynamic)
104 smelly_symbols, python_symbols = get_smelly_symbols(nm_output, dynamic)
105
106 if not smelly_symbols:
107 print(f"OK: no smelly symbol found ({len(python_symbols)} Python symbols)")
108 return 0
109
110 print()
111 smelly_symbols.sort()
112 for symbol in smelly_symbols:
113 print(f"Smelly symbol: {symbol}")
114
115 print()
116 print(f"ERROR: Found {len(smelly_symbols)} smelly symbols!")
117 return len(smelly_symbols)
118
119
120 def check_extensions():
121 print(__file__)
122 # This assumes pybuilddir.txt is in same directory as pyconfig.h.
123 # In the case of out-of-tree builds, we can't assume pybuilddir.txt is
124 # in the source folder.
125 config_dir = os.path.dirname(sysconfig.get_config_h_filename())
126 filename = os.path.join(config_dir, "pybuilddir.txt")
127 try:
128 with open(filename, encoding="utf-8") as fp:
129 pybuilddir = fp.readline()
130 except FileNotFoundError:
131 print(f"Cannot check extensions because {filename} does not exist")
132 return True
133
134 print(f"Check extension modules from {pybuilddir} directory")
135 builddir = os.path.join(config_dir, pybuilddir)
136 nsymbol = 0
137 for name in os.listdir(builddir):
138 if not name.endswith(".so"):
139 continue
140 if IGNORED_EXTENSION in name:
141 print()
142 print(f"Ignore extension: {name}")
143 continue
144
145 print()
146 filename = os.path.join(builddir, name)
147 nsymbol += check_library(filename, dynamic=True)
148
149 return nsymbol
150
151
152 def main():
153 nsymbol = 0
154
155 # static library
156 LIBRARY = sysconfig.get_config_var('LIBRARY')
157 if not LIBRARY:
158 raise Exception("failed to get LIBRARY variable from sysconfig")
159 if os.path.exists(LIBRARY):
160 nsymbol += check_library(LIBRARY)
161
162 # dynamic library
163 LDLIBRARY = sysconfig.get_config_var('LDLIBRARY')
164 if not LDLIBRARY:
165 raise Exception("failed to get LDLIBRARY variable from sysconfig")
166 if LDLIBRARY != LIBRARY:
167 print()
168 nsymbol += check_library(LDLIBRARY, dynamic=True)
169
170 # Check extension modules like _ssl.cpython-310d-x86_64-linux-gnu.so
171 nsymbol += check_extensions()
172
173 if nsymbol:
174 print()
175 print(f"ERROR: Found {nsymbol} smelly symbols in total!")
176 sys.exit(1)
177
178 print()
179 print(f"OK: all exported symbols of all libraries "
180 f"are prefixed with {' or '.join(map(repr, ALLOWED_PREFIXES))}")
181
182
183 if __name__ == "__main__":
184 main()