|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Verifies a binary uses only dynamic symbols from whitelisted library versions. |
| 4 | +# Prints the disallowed symbols and their versions on failure. |
| 5 | +# Usage: lib_compat_test.py bin/clangd --lib=GLIBC_2.18 |
| 6 | + |
| 7 | +import argparse |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | + |
| 11 | +parser = argparse.ArgumentParser() |
| 12 | +parser.add_argument("binary") |
| 13 | +parser.add_argument( |
| 14 | + "--lib", |
| 15 | + action="append", |
| 16 | + default=[], |
| 17 | + help="Whitelist a library, e.g. GLIBC_2.18 or GLIBC", |
| 18 | +) |
| 19 | +parser.add_argument( |
| 20 | + "--sym", action="append", default=[], help="Whitelist a symbol, e.g. crc32" |
| 21 | +) |
| 22 | +args = parser.parse_args() |
| 23 | + |
| 24 | +# Parses GLIBC_2.3 into ("GLIBC", [2,3]) |
| 25 | +# Parses GLIBC into ("GLIBC", None) |
| 26 | +def parse_version(version): |
| 27 | + parts = version.rsplit("_", 1) |
| 28 | + if len(parts) == 1: |
| 29 | + return (version, None) |
| 30 | + try: |
| 31 | + return (parts[0], [int(p) for p in parts[1].split(".")]) |
| 32 | + except ValueError: |
| 33 | + return (version, None) |
| 34 | + |
| 35 | + |
| 36 | +lib_versions = dict([parse_version(v) for v in args.lib]) |
| 37 | + |
| 38 | +# Determines whether all symbols with version 'lib' are acceptable. |
| 39 | +# A versioned library is name_x.y.z by convention. |
| 40 | +def accept_lib(lib): |
| 41 | + (lib, ver) = parse_version(lib) |
| 42 | + if not lib in lib_versions: # Non-whitelisted library. |
| 43 | + return False |
| 44 | + if lib_versions[lib] is None: # Unrestricted version |
| 45 | + return True |
| 46 | + if ver is None: # Lib has non-numeric version, library restricts version. |
| 47 | + return False |
| 48 | + return ver <= lib_versions[lib] |
| 49 | + |
| 50 | + |
| 51 | +# Determines whether an optionally-versioned symbol is acceptable. |
| 52 | +# A versioned symbol is symbol@version as output by nm. |
| 53 | +def accept_symbol(sym): |
| 54 | + if sym in args.sym: |
| 55 | + return True |
| 56 | + split = sym.split("@", 1) |
| 57 | + return (split[0] in args.sym) or (len(split) == 2 and accept_lib(split[1])) |
| 58 | + |
| 59 | + |
| 60 | +# Run nm to find the undefined symbols, and check whether each is acceptable. |
| 61 | +nm = subprocess.run( |
| 62 | + ["nm", "-uD", "--with-symbol-version", args.binary], |
| 63 | + stdout=subprocess.PIPE, |
| 64 | + text=True, |
| 65 | +) |
| 66 | +nm.check_returncode() |
| 67 | +status = 0 |
| 68 | +for line in nm.stdout.splitlines(): |
| 69 | + # line = " U foo@GLIBC_2.3" |
| 70 | + parts = line.split() |
| 71 | + if len(parts) != 2: |
| 72 | + print("Unparseable nm output: ", line, file=sys.stderr) |
| 73 | + status = 2 |
| 74 | + continue |
| 75 | + if parts[0] == "w": # Weak-undefined symbol, not actually required. |
| 76 | + continue |
| 77 | + if not accept_symbol(parts[1]): |
| 78 | + print(parts[1]) |
| 79 | + status = 1 |
| 80 | +if status == 1: |
| 81 | + print( |
| 82 | + "Binary depends on disallowed symbols above. Use some combination of:\n" |
| 83 | + " - relax the whitelist by adding --lib and --sym flags to this test\n" |
| 84 | + " - force older symbol versions by updating lib_compat.h\n" |
| 85 | + " - avoid dynamic dependencies by changing CMake configuration\n" |
| 86 | + " - remove bad dependencies from the code", |
| 87 | + file=sys.stderr, |
| 88 | + ) |
| 89 | +sys.exit(status) |
0 commit comments