Repository navigation
Description
Found by comparing the semantics described in the x86-64 System V ABI with how CBMC implements them. An AI agent found this bug, reproduced it with CBMC 5783f86 and drafted this report. A second agent tried to refute it and searched upstream for duplicates. The methodology is described here.
What went wrong: Wrong result (sizeof/_Alignof of structs with unnamed bit-fields), so CBMC reports VERIFICATION SUCCESSFUL where the real program fails.
Version: cbmc 6.11.0 and develop 6.11.0-29-g266f43809, --64 (default x86-64 Linux). Also with --z3.
Preconditions: default configuration; the struct has an unnamed bit-field (including int :0) whose declared type is more strictly aligned than the named members.
Input
prog.c:
#include <assert.h>
void reach_error(void) { assert(0); }
struct S { char c; int : 0; char d; };
int main(void) {
if (sizeof(struct S) == 5) reach_error();
return 0;
}prog_oob.c:
struct A { char c; int : 4; };
struct A a[3];
int main(void) {
char *b = (char *)a;
b[7] = 1;
return 0;
}Command
./run.sh # CBMC=cbmc CC=gcc by default
Output
AddressSanitizer: global-buffer-overflow
native prog.c exit: 134 (134 = reach_error reached)
cbmc prog.c: VERIFICATION SUCCESSFUL
cbmc prog_oob.c: VERIFICATION SUCCESSFUL
(prog_oob.c is run with --bounds-check --pointer-check; prog.c with --64.)
Expected
gcc and clang on x86-64 give sizeof(struct S) == 5, _Alignof(struct S) == 1 and sizeof(struct A) == 2. System V AMD64 psABI §3.1.2: "Unnamed bit-fields' types do not affect the alignment of a structure or union." CBMC gives alignment 4 (sizes 8 and 4), so reach_error() is called natively but proved unreachable, and the 6-byte a is treated as 12 bytes so the write at b[7] passes the bounds check. Named bit-fields (int x:4) are correct.
In add_padding in src/ansi-c/padding.cpp, every ID_c_bit_field member (zero-width ones fall through to the generic path) raises max_alignment, with no check that the member is anonymous. The member offsets are right (offsetof(S, d) == 4); only the struct alignment, size and tail padding are wrong.
Reproducer files
prog.c
/* Zero-width unnamed bit-field: forces d to the next int boundary (offset 4), but does not
raise the alignment: gcc/clang x86-64 give sizeof == 5, _Alignof == 1. */
#include <assert.h>
void reach_error(void) { assert(0); }
struct S { char c; int : 0; char d; };
int main(void) {
if (sizeof(struct S) == 5) reach_error();
return 0;
}prog_oob.c
/* Out-of-bounds write: natively sizeof(a) == 6 (x86-64), so b[7] is outside a (ASan: global-buffer-overflow). */
struct A { char c; int : 4; };
struct A a[3];
int main(void) {
char *b = (char *)a;
b[7] = 1;
return 0;
}run.sh
#!/bin/bash
# Exit 1 if CBMC verifies a program that fails natively (bug), 0 if CBMC agrees, 2 if a tool is missing.
CBMC=${CBMC:-cbmc}; CC=${CC:-gcc}
D=$(cd "$(dirname "$0")" && pwd)
command -v "$CBMC" >/dev/null && "$CBMC" --version >/dev/null 2>&1 || { echo "tool not found: $CBMC"; exit 2; }
command -v "$CC" >/dev/null || { echo "tool not found: $CC"; exit 2; }
W=$(mktemp -d); trap 'rm -rf "$W"' EXIT; ulimit -c 0
$CC -w -fsanitize=address "$D/prog_oob.c" -o "$W/o" || exit 2
"$W/o" 2>&1 | grep -m1 -o 'AddressSanitizer: [a-z-]*'
$CC -w "$D/prog.c" -o "$W/a" || exit 2
"$W/a" 2>/dev/null; echo "native prog.c exit: $? (134 = reach_error reached)"
bug=0
r=$("$CBMC" --64 "$D/prog.c" 2>&1 | grep -m1 '^VERIFICATION'); echo "cbmc prog.c: $r"
case "$r" in *SUCCESSFUL*) bug=1;; *FAILED*) ;; *) exit 2;; esac
r=$("$CBMC" --64 --bounds-check --pointer-check "$D/prog_oob.c" 2>&1 | grep -m1 '^VERIFICATION'); echo "cbmc prog_oob.c: $r"
case "$r" in *SUCCESSFUL*) bug=1;; *FAILED*) ;; *) exit 2;; esac
exit $bugIf this is intended behaviour or a duplicate, feel free to close it, and I'll use that as feedback to improve future reports. For questions, or to have this re-checked on a newer version of CBMC, mention @JeroenRobben or email [email protected].
Activity
Fix alignment of structs with unnamed bit-fields
C front-end: unnamed bit-fields and alignment on all System V targets