https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128909
Bug ID: 128909
Summary: [contracts] precondition of a templated function
silently not checked when its declaration comes from a
precompiled header; outcome depends on ASLR
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: filip.sajdak at gmail dot com
Target Milestone: ---
Host: aarch64-unknown-linux-gnu
Target: aarch64-unknown-linux-gnu
Build: aarch64-unknown-linux-gnu
A precondition (P2900 contracts) on a templated function is silently not
checked when the declaration comes from a precompiled header. Whether it is
dropped depends on the addresses the compiler had when the .gch was written.
Seen with 16.2.0 (releases/gcc-16.2.0, 78d4ac73dd) and with trunk 9eb44775b9
(2026-11-19) on aarch64-unknown-linux-gnu (Ubuntu 24.04,
kernel.randomize_va_space = 2), both configured with --enable-languages=c,c++
--disable-bootstrap --disable-multilib --enable-checking=release.
Reproducer:
box.hpp:
#pragma once
template <int N> struct box {
bool built = false;
void unmount() pre(built) {}
};
main.cpp:
#include "box.hpp"
int main() { box<7>{}.unmount(); }
The precondition is false, so the program must stop with a contract violation
(the default semantic is enforce).
Command lines and output:
Without a precompiled header:
g++ -std=c++26 -fcontracts main.cpp -o nopch -lstdc++exp && ./nopch
contract violation in function void box<N>::unmount() [with int N = 7] at
box.hpp:4: built [assertion_kind: pre, semantic: enforce, mode:
predicate_false, ...]
The exit status is 134. 16.2.0 needs -lstdc++exp for the default violation
handler. -std=c++26 alone gives the same results as -std=c++26 -fcontracts.
With a precompiled header, found by name next to the header (box.hpp.gch, no
-include):
g++ -std=c++26 -fcontracts -x c++-header box.hpp -o box.hpp.gch
g++ -std=c++26 -fcontracts -Winvalid-pch -H main.cpp -o pch -lstdc++exp &&
./pch
-H prints ! box.hpp.gch, so the header was used. The program usually exits with
status 0 and prints nothing: the precondition was not checked.
There is no warning at any step.
The outcome depends on the addresses when the .gch is written:
Each row builds the .gch afresh N times. Each .gch is used by one compile of
main.cpp, and the program is run. 134 means the precondition was checked; 0
means it was dropped.
Compiler Address randomisation Exit statuses
16.2.0 on (default) 0 0 134 134 0 0 0 0 0 0 0 0 0
0 0 0
16.2.0 off for both steps (setarch -R) 0 0 0 0 0 0 0 0
trunk 9eb44775 on (default) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
trunk 9eb44775 off for both steps (setarch -R) 134 134 134 134 134 134 134
134
One .gch, once written, gives the same result every time it is used: eight
compiles with the same file gave eight times the same exit status.
So the result is fixed when the .gch is written, and it depends on the
addresses the compiler had then.
Which declarations are affected:
Each case is a header and a main that breaks the contract, built as above. The
results are with -std=c++26 -fcontracts at -O0, with the PCH:
Case 16.2.0
trunk
Member of template <int N> struct box, pre dropped
dropped
Member of template <class T> struct box, pre dropped
dropped
Static member of a class template, pre on a non-dependent parameter dropped
dropped
Member function template of a plain class, pre dropped
dropped
Function template, pre dropped
dropped
Member of a class template, post(r: r > 0) checked
dropped
Member of a class template, contract_assert in the body checked
checked
Member of a plain class, pre checked
checked
Inline non-template function, pre checked
checked
Without the PCH, every case is checked on both compilers.
The same table at -O2, with -std=gnu++26 -g, and with -std=c++26 alone, varies
in the cases marked "dropped". For example the function template was checked at
-O2 on 16.2.0, and post was dropped on 16.2.0 at -O2. This is consistent with
the address dependence above.
The three "checked" cases at the bottom were checked in every run: those whose
contract is part of a body that was already parsed into the header.
-fcontract-evaluation-semantic was left at its default; other semantics were
not tried.
A guess at the cause:
This is a guess from reading gcc/cp/contracts.cc and pt.cc of 16.2.0; it was
not confirmed with a debugger.
The contract specifiers of a function are kept in a side table, static GTY(())
hash_map<tree, contract_decl> *contract_decl_map, keyed by the FUNCTION_DECL.
An instantiation reads the specifiers of its pattern from that table
(get_fn_contract_specifiers in tsubst_function_decl and in the instantiation of
the body, pt.cc).
A non-template function has its checks in its body before the header is
written, so it does not need the table later. This matches the cases that
survive.
If a lookup in the table after loading the .gch misses some of the time, the
instantiation sees no contract and emits none. That fits a result that depends
on the addresses at the time of writing.
A table keyed by pointer whose layout is not recomputed for the addresses the
objects get in the .gch would behave this way. Which part does not happen,
whether the reordering at write time or the hashing at load time, was not
found.
Trunk has the same table. Its later contract changes, such as the rework of
constexpr handling on 2026-11-11, do not remove it.
Not checked:
- x86-64 and other hosts; macOS, where the problem was first seen with GCC
16.2.0 from Homebrew.
- -fcontract-evaluation-semantic=observe and the other semantics.
- Modules instead of a precompiled header.
- A build with --enable-checking=yes.
This report was prepared with AI assistance. The compilers were built and the
cases were run on a machine of mine. No code is proposed.