Skip to content

Case 30: Field Qualifier Changes (const, volatile)

Field Value
Verdict 🟠 API_BREAK
Category API Break
Classification Rule
Platforms Linux, macOS, Windows
Flags API break
Detected ChangeKinds field_became_const, field_became_volatile
Source files catalog/cases/case30_field_qualifiers/
Rule family field-qualifiers
Subject Struct and type layout changes

Category: Type Qualifiers | Verdict: 🟠 API_BREAK

Verdict and consumer impact

The binary layout of struct SensorConfig is unchanged — const and volatile don't affect size, alignment, or offsets, so an already-built consumer binary keeps linking and running against v2 unmodified. The break is at recompilation: sample_rate becomes const, so the consumer's assignment to it no longer compiles (and, for C++ consumers, the implicit default constructor and copy/move assignment become deleted). abicheck reports exactly that fact — API_BREAK (field_became_const); a project that promises source compatibility gates it.

raw_value becoming volatile is a different kind of change: it does not break the build, but every recompiled access must now reach memory (e.g. memory-mapped hardware state), so code that relied on cached reads changes behaviour — reported separately as a risk (field_became_volatile). Earlier releases also reported both fields as type_field_type_changed (BREAKING), turning a recompile-only change into a layout claim; the qualifier change is not a field type change in the layout sense.

Old/new diff

v1.h v2.h
int sample_rate; const int sample_rate;
int raw_value; volatile int raw_value;
int cache_hits; (unchanged) int cache_hits;

abicheck command

gcc -shared -fPIC -g v1.c -o libfoo_v1.so
gcc -shared -fPIC -g v2.c -o libfoo_v2.so
abicheck compare libfoo_v1.so libfoo_v2.so

Expected abicheck finding

Verdict: API_BREAK (exit 2)

- field_became_const: Field became const: SensorConfig::sample_rate
- field_became_volatile: Field became volatile: SensorConfig::raw_value
- inline_function_removed: SensorConfig() / operator= (implicit C++ members
  deleted by the const field; never exported)

type_field_type_changed and func_removed must not appear.

Minimum evidence

min_evidence: L1 — DWARF wraps a qualified field's type DIE in DW_TAG_const_type/DW_TAG_volatile_type, so the qualifier change is visible directly in debug info; -g alone (no public headers) reaches the same API_BREAK.

Why abicheck catches it

abicheck resolves each struct member's type for both sides. A member whose type differs only by a by-value const/volatile keeps its size and offset, so the layout detectors stay silent and the change is reported by the dedicated qualifier kinds: field_became_const (API_BREAK: writes no longer compile), field_became_volatile / field_lost_volatile (risk), field_lost_const (compatible). The C++ special members a const member implicitly deletes are compiler-generated and never exported, so their disappearance is inline_function_removed (a source break), not a missing symbol.

Runtime failure demonstration

Severity: MODERATE (semantic break, not crash)

Scenario: compile app against v1 headers, swap in the v2 .so without recompiling.

# Build old library + app
gcc -shared -fPIC -g v1.c -o libfoo.so
gcc -g app.c -I. -L. -lfoo -Wl,-rpath,. -o app
./app
# → sensor_read = 42
# → OK (semantic break only: field qualifiers changed, binary runs identically)

# Swap in new library (no recompile)
gcc -shared -fPIC -g v2.c -o libfoo.so
./app
# → sensor_read = 42
# → OK (semantic break only: field qualifiers changed, binary runs identically)

Why MODERATE, not CRITICAL: the struct's binary layout is unchanged, so the already-compiled app keeps running with identical output — there's no crash or corruption to observe here. The break only surfaces on recompilation: code that writes to sample_rate becomes a compile error against v2.h, and code relying on non-volatile caching of raw_value can silently diverge from hardware-mapped reality once actually rebuilt against the new header.

Source break verification (recompiling against v2 fails):

sed 's/#include "v1.h"/#include "v2.h"/' app.c > /tmp/app_v2_test.c
gcc -g /tmp/app_v2_test.c -I. -L. -lfoo -Wl,-rpath,. -o app_v2
# → error: assignment of read-only member 'sample_rate'
rm -f /tmp/app_v2_test.c

(This app doesn't write to sample_rate, so this step only demonstrates the compile-time contract; a version that assigns cfg.sample_rate = ... would fail to build against v2.h.)

Safe redesign

Don't add const to a field of a public struct unless it was always documented as read-only, and don't add volatile to an existing field — both are contract changes that recompiled callers must account for. If a field must become immutable, provide setter/getter functions and hide the struct behind an opaque pointer instead of relying on the field-qualifier system; introduce a new struct (or bump the major version) if volatile semantics are genuinely required.

Cross-tool comparison

abidw --out-file v1.xml libfoo_v1.so
abidw --out-file v2.xml libfoo_v2.so
abidiff v1.xml v2.xml
echo "exit: $?"

References


Source files

  • CMakeLists.txt
  • app.c
  • v1.c
  • v1.h
  • v2.c
  • v2.h

See also: Compatibility Catalog · All API_BREAK cases · Category: API Break · Rule: Field type qualifiers changed · Subject: Struct and type layout changes.