|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2026 LiveKit |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +""" |
| 17 | +Verify that liblivekit's exported ABI does not leak private dependency symbols. |
| 18 | +
|
| 19 | +The LiveKit SDK statically links several private dependencies (spdlog, fmt, |
| 20 | +google::protobuf, absl). When those symbols escape the dynamic symbol table |
| 21 | +of liblivekit.{so,dylib,dll}, they collide at runtime with the same libraries |
| 22 | +loaded elsewhere in the host process (a common failure mode is ROS 2's |
| 23 | +rcl_logging_spdlog ABI-clashing with our vendored spdlog and crashing inside |
| 24 | +spdlog::pattern_formatter). |
| 25 | +
|
| 26 | +This script lists exported defined symbols from the supplied shared library |
| 27 | +using the platform-appropriate tool and fails (exit code 1) if any of them |
| 28 | +match a forbidden pattern. |
| 29 | +
|
| 30 | +Usage: |
| 31 | + python3 check_no_private_symbols.py <path-to-shared-library> |
| 32 | +
|
| 33 | +Optional environment variables: |
| 34 | + LIVEKIT_SYMBOL_CHECK_VERBOSE=1 Print every leaked symbol (default: print |
| 35 | + up to 20 examples). |
| 36 | + LIVEKIT_SYMBOL_CHECK_EXTRA_FORBIDDEN=foo,bar Additional comma-separated |
| 37 | + patterns to forbid. |
| 38 | +""" |
| 39 | + |
| 40 | +import os |
| 41 | +import re |
| 42 | +import shutil |
| 43 | +import subprocess |
| 44 | +import sys |
| 45 | +from pathlib import Path |
| 46 | + |
| 47 | +# Substring patterns that must NOT appear in any exported symbol after |
| 48 | +# demangling. We use plain-substring semantics for readability; if you need a |
| 49 | +# regex, switch to re.search. |
| 50 | +DEFAULT_FORBIDDEN = [ |
| 51 | + "spdlog::", |
| 52 | + "fmt::v", |
| 53 | + "google::protobuf", |
| 54 | + "absl::", |
| 55 | +] |
| 56 | + |
| 57 | +MAX_REPORTED_LEAKS = 20 |
| 58 | + |
| 59 | + |
| 60 | +def _which_or_die(name: str) -> str: |
| 61 | + path = shutil.which(name) |
| 62 | + if not path: |
| 63 | + sys.stderr.write(f"error: required tool '{name}' not found on PATH\n") |
| 64 | + sys.exit(2) |
| 65 | + return path |
| 66 | + |
| 67 | + |
| 68 | +def _list_exports_macos(lib: Path) -> list[str]: |
| 69 | + nm = _which_or_die("nm") |
| 70 | + cxxfilt = shutil.which("c++filt") |
| 71 | + # -gU: external (global) defined symbols. |
| 72 | + raw = subprocess.run( |
| 73 | + [nm, "-gU", str(lib)], |
| 74 | + check=True, |
| 75 | + capture_output=True, |
| 76 | + text=True, |
| 77 | + ).stdout |
| 78 | + if cxxfilt: |
| 79 | + raw = subprocess.run( |
| 80 | + [cxxfilt], |
| 81 | + input=raw, |
| 82 | + check=True, |
| 83 | + capture_output=True, |
| 84 | + text=True, |
| 85 | + ).stdout |
| 86 | + return raw.splitlines() |
| 87 | + |
| 88 | + |
| 89 | +def _list_exports_linux(lib: Path) -> list[str]: |
| 90 | + nm = _which_or_die("nm") |
| 91 | + cxxfilt = shutil.which("c++filt") |
| 92 | + # -D: dynamic symbols (i.e., what's actually visible to the dynamic linker) |
| 93 | + # --defined-only: drop UND entries |
| 94 | + raw = subprocess.run( |
| 95 | + [nm, "-D", "--defined-only", str(lib)], |
| 96 | + check=True, |
| 97 | + capture_output=True, |
| 98 | + text=True, |
| 99 | + ).stdout |
| 100 | + if cxxfilt: |
| 101 | + raw = subprocess.run( |
| 102 | + [cxxfilt], |
| 103 | + input=raw, |
| 104 | + check=True, |
| 105 | + capture_output=True, |
| 106 | + text=True, |
| 107 | + ).stdout |
| 108 | + return raw.splitlines() |
| 109 | + |
| 110 | + |
| 111 | +def _find_dumpbin_via_vswhere() -> str | None: |
| 112 | + """Locate dumpbin.exe under the latest installed Visual Studio. |
| 113 | +
|
| 114 | + GitHub-hosted Windows runners (and standard local VS installs) don't add |
| 115 | + dumpbin to PATH unless the user opens a Developer Command Prompt. vswhere |
| 116 | + ships at a fixed location with every Visual Studio install since 2017 and |
| 117 | + is the supported way to discover the install tree from a vanilla shell. |
| 118 | + """ |
| 119 | + program_files_x86 = os.environ.get( |
| 120 | + "ProgramFiles(x86)", r"C:\Program Files (x86)" |
| 121 | + ) |
| 122 | + vswhere = Path(program_files_x86) / "Microsoft Visual Studio" / "Installer" \ |
| 123 | + / "vswhere.exe" |
| 124 | + if not vswhere.exists(): |
| 125 | + return None |
| 126 | + try: |
| 127 | + proc = subprocess.run( |
| 128 | + [ |
| 129 | + str(vswhere), |
| 130 | + "-latest", |
| 131 | + "-products", "*", |
| 132 | + "-requires", "Microsoft.VisualStudio.Component.VC.Tools.x86.x64", |
| 133 | + "-property", "installationPath", |
| 134 | + ], |
| 135 | + check=True, |
| 136 | + capture_output=True, |
| 137 | + text=True, |
| 138 | + ) |
| 139 | + except subprocess.CalledProcessError: |
| 140 | + return None |
| 141 | + install_path = proc.stdout.strip() |
| 142 | + if not install_path: |
| 143 | + return None |
| 144 | + msvc_root = Path(install_path) / "VC" / "Tools" / "MSVC" |
| 145 | + if not msvc_root.is_dir(): |
| 146 | + return None |
| 147 | + # Pick the highest-versioned toolchain present (lexicographic order matches |
| 148 | + # version order for dotted MSVC versions like "14.44.35207"). |
| 149 | + for version_dir in sorted(msvc_root.iterdir(), reverse=True): |
| 150 | + candidate = version_dir / "bin" / "Hostx64" / "x64" / "dumpbin.exe" |
| 151 | + if candidate.exists(): |
| 152 | + return str(candidate) |
| 153 | + return None |
| 154 | + |
| 155 | + |
| 156 | +def _list_exports_windows(lib: Path) -> list[str]: |
| 157 | + # dumpbin ships with MSVC; it understands import libs (.lib) and DLLs. |
| 158 | + dumpbin = shutil.which("dumpbin") or _find_dumpbin_via_vswhere() |
| 159 | + if not dumpbin: |
| 160 | + sys.stderr.write( |
| 161 | + "error: 'dumpbin' not on PATH and could not be located via " |
| 162 | + "vswhere; run from a Visual Studio Developer command prompt or " |
| 163 | + "ensure dumpbin.exe is available\n" |
| 164 | + ) |
| 165 | + sys.exit(2) |
| 166 | + raw = subprocess.run( |
| 167 | + [dumpbin, "/exports", str(lib)], |
| 168 | + check=True, |
| 169 | + capture_output=True, |
| 170 | + text=True, |
| 171 | + ).stdout |
| 172 | + # dumpbin output lines for export entries look like |
| 173 | + # " 1 0 00001000 ?foo@@YAHXZ = ?foo@@YAHXZ (int __cdecl foo(void))" |
| 174 | + # We keep all of stdout: the substring search will only fire on actual |
| 175 | + # symbol names, headers/footers are harmless. |
| 176 | + return raw.splitlines() |
| 177 | + |
| 178 | + |
| 179 | +def _list_exports(lib: Path) -> list[str]: |
| 180 | + if sys.platform == "darwin": |
| 181 | + return _list_exports_macos(lib) |
| 182 | + if sys.platform.startswith("linux"): |
| 183 | + return _list_exports_linux(lib) |
| 184 | + if os.name == "nt" or sys.platform == "win32": |
| 185 | + return _list_exports_windows(lib) |
| 186 | + sys.stderr.write(f"error: unsupported platform '{sys.platform}'\n") |
| 187 | + sys.exit(2) |
| 188 | + |
| 189 | + |
| 190 | +def main(argv: list[str]) -> int: |
| 191 | + if len(argv) != 2: |
| 192 | + sys.stderr.write(__doc__ or "") |
| 193 | + return 2 |
| 194 | + |
| 195 | + lib = Path(argv[1]).resolve() |
| 196 | + if not lib.exists(): |
| 197 | + sys.stderr.write(f"error: library not found: {lib}\n") |
| 198 | + return 2 |
| 199 | + |
| 200 | + forbidden = list(DEFAULT_FORBIDDEN) |
| 201 | + extra = os.environ.get("LIVEKIT_SYMBOL_CHECK_EXTRA_FORBIDDEN", "") |
| 202 | + if extra: |
| 203 | + forbidden.extend(p for p in extra.split(",") if p) |
| 204 | + |
| 205 | + verbose = bool(os.environ.get("LIVEKIT_SYMBOL_CHECK_VERBOSE")) |
| 206 | + |
| 207 | + print(f"[symbol-check] library : {lib}") |
| 208 | + print(f"[symbol-check] platform: {sys.platform}") |
| 209 | + print(f"[symbol-check] forbidden patterns: {forbidden}") |
| 210 | + |
| 211 | + lines = _list_exports(lib) |
| 212 | + print(f"[symbol-check] {len(lines)} lines of nm/dumpbin output") |
| 213 | + |
| 214 | + # Group leaks by pattern for a tidy summary. |
| 215 | + leaks_by_pattern: dict[str, list[str]] = {p: [] for p in forbidden} |
| 216 | + for line in lines: |
| 217 | + for pat in forbidden: |
| 218 | + if pat in line: |
| 219 | + leaks_by_pattern[pat].append(line.rstrip()) |
| 220 | + |
| 221 | + total_leaks = sum(len(v) for v in leaks_by_pattern.values()) |
| 222 | + if total_leaks == 0: |
| 223 | + print("[symbol-check] OK: no forbidden symbols exported") |
| 224 | + return 0 |
| 225 | + |
| 226 | + print(f"[symbol-check] FAIL: {total_leaks} forbidden symbol(s) exported") |
| 227 | + for pat, hits in leaks_by_pattern.items(): |
| 228 | + if not hits: |
| 229 | + continue |
| 230 | + print(f" pattern {pat!r}: {len(hits)} hit(s)") |
| 231 | + shown = hits if verbose else hits[:MAX_REPORTED_LEAKS] |
| 232 | + for h in shown: |
| 233 | + print(f" {h}") |
| 234 | + if not verbose and len(hits) > MAX_REPORTED_LEAKS: |
| 235 | + print(f" ... and {len(hits) - MAX_REPORTED_LEAKS} more " |
| 236 | + "(set LIVEKIT_SYMBOL_CHECK_VERBOSE=1 to see all)") |
| 237 | + |
| 238 | + print( |
| 239 | + "\nliblivekit must not re-export private dependency symbols.\n" |
| 240 | + "If you intentionally added a public symbol that triggered this, mark\n" |
| 241 | + "it with LIVEKIT_API in include/livekit/visibility.h and rebuild.\n" |
| 242 | + ) |
| 243 | + return 1 |
| 244 | + |
| 245 | + |
| 246 | +if __name__ == "__main__": |
| 247 | + sys.exit(main(sys.argv)) |
0 commit comments