Case 06: Symbol Visibility Leak¶
| Field | Value |
|---|---|
| Verdict | 🔴 BREAKING |
| Category | Breaking |
| Classification | Rule |
| Platforms | Linux |
| Flags | ABI break, Bad practice |
Detected ChangeKinds |
func_visibility_changed |
| Source files | catalog/cases/case06_visibility/ |
| Rule family | visibility |
| Subject | Symbol visibility scoping |
Category: Visibility | Verdict: 🔴 BREAKING (bad practice)
Verdict and consumer impact¶
bad.c (v1) was compiled without -fvisibility=hidden and unintentionally
exports two internal helpers (internal_helper, another_impl) as part of
its public ABI surface, alongside the real public_api. good.c (v2) fixes
this: it builds with -fvisibility=hidden and marks both helpers
visibility("hidden"). The helpers keep their definitions and external
linkage — this is a pure visibility mutation, not a removal. From a
consumer's point of view the effect is still the same as a removal: any
binary that resolved internal_helper or another_impl at load time (even
by accident, having reached past the intended public API) fails to load
against v2.
Old/new diff¶
| bad.c (v1) | good.c (v2) |
|---|---|
__attribute__((visibility("default"))) int public_api(int x) |
__attribute__((visibility("default"))) int public_api(int x) |
__attribute__((visibility("default"))) int internal_helper(int x) |
__attribute__((visibility("hidden"))) int internal_helper(int x) |
__attribute__((visibility("default"))) int another_impl(int x) |
__attribute__((visibility("hidden"))) int another_impl(int x) |
abicheck command¶
gcc -shared -fPIC -g bad.c -o libfoo_v1.so
gcc -shared -fPIC -g -fvisibility=hidden good.c -o libfoo_v2.so
abicheck compare libfoo_v1.so libfoo_v2.so
Expected abicheck finding¶
Verdict: BREAKING (exit 4)
- func_visibility_changed: Function visibility changed to hidden:
internal_helper (public -> hidden)
- func_visibility_changed: Function visibility changed to hidden:
another_impl (public -> hidden)
> Symbol hidden from dynamic linking; old binaries can't find it at
load time.
Minimum evidence¶
min_evidence: L0 — both findings come straight from the .dynsym exported-
symbol set: internal_helper and another_impl move from an exported
binding to no binding at all.
No debug info or headers are required; abicheck's DWARF-aware path (used
here since the binaries were built with -g) additionally confirms
internal_helper still has external linkage in the source, distinguishing a
genuine visibility change from a function that was deleted outright.
Why abicheck catches it¶
The dynamic symbol table is authoritative L0 evidence for what's actually
callable at runtime. abicheck diffs the exported-symbol sets directly, and
(via dwarf_snapshot.py) can tell a symbol that disappeared from .dynsym
while remaining externally-linked in DWARF apart from one that was deleted
from the source — surfacing the former as func_visibility_changed instead
of a plain removal.
Runtime failure demonstration¶
Severity: CRITICAL
Scenario: an old consumer that (incorrectly) linked against the leaked
helpers. app.c is linked against libv1.so only and calls
public_api, internal_helper and another_impl directly; the
runtime-smoke harness then substitutes libv2.so under the same name.
gcc -shared -fPIC -g bad.c -o libv1.so
gcc -shared -fPIC -g -fvisibility=hidden good.c -o libv2.so
gcc -g app.c -L. -lv1 -Wl,-rpath,'$ORIGIN' -o app
./app # v1: prints the three results, exit 0
cp libv2.so libv1.so
./app # v2: symbol lookup error: undefined symbol: internal_helper
The baseline run touches only the installed library, so a baseline failure
means a broken fixture, not the demonstration. After the swap the dynamic
loader refuses to start the program before main() runs.
Safe redesign¶
Add -fvisibility=hidden to build flags and annotate every intended public
function with __attribute__((visibility("default"))). Use a FOO_EXPORT
macro:
#define FOO_EXPORT __attribute__((visibility("default")))
FOO_EXPORT int public_api(void); // exported
static int internal_helper(void); // or just leave it static
Real-world example: Qt, GCC libstdc++, LLVM, and most large C++ projects
gate their public API with visibility macros (Q_DECL_EXPORT,
_GLIBCXX_VISIBILITY) precisely to avoid this. -fvisibility=hidden is
standard practice since GCC 4.
Cross-tool comparison¶
abidiff also flags this as an ABI change (the removed/hidden symbols),
though it doesn't carry abicheck's VISIBILITY_LEAK-style framing of the
bad practice on v1 alone — it only ever compares the two binaries.
References¶
Source files¶
CMakeLists.txtapp.cbad.cgood.c
See also: Compatibility Catalog · All BREAKING cases · Category: Breaking · Rule: Symbol visibility changed · Subject: Symbol visibility scoping.