Skip to content

Case 07: Struct Layout Change

Field Value
Verdict 🔴 BREAKING
Category Breaking
Classification Rule
Platforms Linux
Flags ABI break, API break
Detected ChangeKinds type_size_changed
Source files catalog/cases/case07_struct_layout/
Rule family embedded-type-size-increased
Subject Struct and type layout changes

Category: Type Layout | Verdict: 🔴 BREAKING

Verdict and consumer impact

Code compiled against v1 allocates sizeof(Point) = 8 bytes. v2's Point is 12 bytes. Stack/heap allocations are undersized; the z field reads/writes outside the allocated region. Any binary passing Point by value is broken without recompilation.

Old/new diff

v1.c v2.c
struct Point { int x; int y; }; struct Point { int x; int y; int z; };

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: BREAKING (exit 4)

- type_size_changed: Size changed: Point (64 -> 96 bits)
  > Old code allocates or copies the type with the old size;
    heap/stack corruption, out-of-bounds access.
  Affected symbols: get_x, init_point

Additions:
- type_field_added_compatible: Field added: Point::z
  > Field appended without changing existing offsets; old code works
    but won't initialize the new field.

Minimum evidence

min_evidence: L1 — DWARF's struct-layout info (DW_TAG_structure_type size + member offsets) is enough to detect the size change; no public headers required.

Why abicheck catches it

DWARF records each struct's total byte size and every member's offset for both versions; abicheck compares them directly from debug info.

Runtime failure demonstration

Severity: CRITICAL

Scenario: an old consumer allocates Point with v1's layout (8 bytes) and lets init_point() initialise it; v2's init_point() also stores z at offset 8 — past the consumer's allocation.

gcc -shared -fPIC -g v1.c -o libv1.so
gcc -g app.c -I. -L. -lv1 -Wl,-rpath,'$ORIGIN' -o app
./app            # sizeof(Point) as compiled = 8, p={1,2}        exit 0
gcc -shared -fPIC -g v2.c -o libv1.so     # swap in v2, no recompile
./app            # CORRUPTION: library wrote 1 byte(s) past ...  exit 1

The witness heap-allocates the object with the v1 size, followed by a 32-byte guard region filled with 0xA5, and asserts the guard afterwards. That makes the overrun visible deterministically under GCC and Clang, Debug and Release, without depending on stack layout, optimisation level or a stack-protector canary (the original stack-canary witness stayed silent under some Clang builds). An independent ASan build of the same consumer reports heap-buffer-overflow after the swap.

Safe redesign

Never add fields to public structs. Use the opaque-pointer (PIMPL) idiom: expose struct Point* and allocate/free through library functions, so the struct layout is hidden from callers.

Real-world example: the C standard library's FILE* is a classic opaque handle — callers never see the struct layout; all access is through fopen/fread/fclose. This pattern keeps the ABI stable across libc versions even as the internal FILE struct changes.

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: $?"   # → 4

Note on abidiff 2.4.0: Struct layout changes return exit 4 (not 12), but the change is semantically breaking — all callers allocate the old size and pass wrong-length data.

References


Source files

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

See also: Compatibility Catalog · All BREAKING cases · Category: Breaking · Rule: Embedded type size increased · Subject: Struct and type layout changes.