With GCC 16, what do -fhardened, _FORTIFY_SOURCE, _GLIBCXX_ASSERTIONS and -fstack-protector-strong add to a production C++ build?
answer
- cheap enough to ship
- checks that abort, not reports
- fortify needs the optimizer
- assertions on by default at -O0 since 15
- one flag since GCC 14
basics
~20 sThey are low-cost protections meant to ship, unlike sanitizers: bounds-checked string and memory calls, library precondition assertions, stack canaries. -fhardened (GCC 14+, GNU/Linux) enables them plus zeroed locals, PIE, RELRO and stack-clash protection. _FORTIFY_SOURCE only takes effect when optimizing.
solid answer
~40 s`-D_FORTIFY_SOURCE=2` or `=3` makes glibc swap calls such as `memcpy` and `strcpy` for checked `__*_chk` versions when the destination size is known; GCC computes that size with its object-size built-ins, and glibc only enables the checked versions in optimized builds. `-D_GLIBCXX_ASSERTIONS` turns on libstdc++'s lightweight precondition checks, such as bounds checks and null smart-pointer dereference; since GCC 15 it is defined by default at `-O0` but not when optimizing. `-fstack-protector-strong` puts a guard value in functions with local arrays or whose local addresses are taken, and exits if it was overwritten. `-fhardened`, added in GCC 14 and GNU/Linux only, enables `-D_FORTIFY_SOURCE=3` (2 on glibc older than 2.35), `-D_GLIBCXX_ASSERTIONS`, `-ftrivial-auto-var-init=zero`, `-fPIE -pie`, `-Wl,-z,relro,-z,now`, `-fstack-protector-strong`, `-fstack-clash-protection` and, on x86 GNU/Linux, `-fcf-protection=full`, without changing the ABI.
code
bash · 9 lines# Release build on GNU/Linux with GCC 16: one flag for the hardening set
g++ -std=c++23 -O2 -fhardened -c matcher.cpp
# The exact set this GCC installation enables
g++ --help=hardened
# The same protections spelled out, for compilers older than GCC 14
g++ -std=c++23 -O2 -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS \
-fstack-protector-strong -fstack-clash-protection -fPIE -c matcher.cppgo deeper
Know that hardening flags are cheap protections for shipped builds, while sanitizers are heavier tools for tests.
Explain what fortify, library assertions and stack protectors each check, and why fortify needs optimization.
Choose a release hardening set, know -fhardened's contents, platform limit and skip-if-specified rule, and measure its cost on hot paths.
Decide which hardening becomes the default for every shipped binary and how exceptions for latency-critical code are approved.
## Hardening is not sanitizing Sanitizers are **test-time** tools: heavy instrumentation, detailed reports, a 2x or larger slowdown. Hardening flags are **ship-time** protections: small checks that stop the program when a known class of memory corruption happens, at a cost low enough for release builds. The GCC manual says of `-fhardened` that it "is intended to be used in production builds, not merely in debug builds". ## The pieces **`_FORTIFY_SOURCE`** is a macro that glibc's headers act on. When it is defined, calls such as `memcpy`, `strcpy` or `sprintf` go to checked variants that receive the destination's size. GCC's documentation describes the machinery: - `__builtin_object_size` returns the bytes left in the object a pointer points to, "if known at compile time", or `(size_t) -1` when it is not. - Built-ins such as `__builtin___memcpy_chk` take that size as an extra argument; with an unknown size they become the plain call, with a known safe size the check disappears, and when GCC can see an overflow at compile time "it issues a warning". - `__builtin_dynamic_object_size` can return a size computed at run time, at some cost; level 3 lets glibc use such sizes. glibc's headers enable the fortified wrappers only when the compiler is optimizing, so `-D_FORTIFY_SOURCE=2` in an `-O0` build adds nothing. Fortify belongs in the optimized build. **`_GLIBCXX_ASSERTIONS`** enables libstdc++'s lightweight checks of function preconditions, "such as bounds checking in strings and null pointer checks when dereferencing smart pointers", and out-of-bounds indices on `std::vector`. It is a subset of the Debug Mode checks, without Debug Mode's ABI and complexity changes. Since GCC 15, libstdc++ defines it by default when compiling without optimization; `_GLIBCXX_NO_ASSERTIONS` prevents that. With optimization it is off unless you define it. **`-fstack-protector-strong`** adds a guard value to functions that have local arrays or take the address of locals, and checks it on return. "If a guard check fails, an error message is printed and the program exits." `-fstack-protector` covers fewer functions, `-fstack-protector-all` covers all of them at higher cost. ## -fhardened: one flag for the set `-fhardened` arrived in GCC 14. In GCC 16 it enables: | Option | Protects against | |---|---| | `-D_FORTIFY_SOURCE=3` (`=2` with glibc older than 2.35) | overflowing calls to string and memory functions | | `-D_GLIBCXX_ASSERTIONS` | violated library preconditions, such as out-of-range indices | | `-ftrivial-auto-var-init=zero` | reading uninitialized locals | | `-fPIE -pie`, `-Wl,-z,relro,-z,now` | fixed addresses and writable relocation tables | | `-fstack-protector-strong` | stack buffer overwrites | | `-fstack-clash-protection` | stack growth jumping over the guard page | | `-fcf-protection=full` (x86 GNU/Linux only) | control-flow hijacking | Rules from the manual: 1. It changes code generation **"without affecting its ABI"**, so hardened and unhardened objects link together. 2. It is **only supported on GNU/Linux targets**. 3. It only enables an option **"if it wasn't already specified"**: `-fhardened -fstack-protector` gives plain `-fstack-protector`, and `-Whardened`, on by default, warns about it. 4. `--help=hardened` prints the exact list for your installation. 5. The precise list "may change between major releases". ## What it costs and what it does not do - Each check is small, but they add up, and `_FORTIFY_SOURCE=3` may compute sizes at run time; measure hot paths. - None of these **report** bugs the way sanitizers do; they **stop** the program, often with only a short message. Finding the root cause is still a sanitizer or debugger job. - Zero-initialized locals make behaviour predictable, but GCC "still considers" them uninitialized for warnings and optimization, so they hide symptoms rather than fix code.
- Why does -D_FORTIFY_SOURCE=2 do nothing in a debug build?`_FORTIFY_SOURCE` is acted on by glibc's headers, and they only switch to the checked `__*_chk` functions when the compiler is optimizing. An `-O0` build therefore gets the plain functions with no checks. Put fortify in the optimized build you ship, which is also where `-fhardened` is meant to be used.
- Should a release build define _GLIBCXX_DEBUG instead of _GLIBCXX_ASSERTIONS?No. Debug Mode changes container layout and algorithmic complexity, so it is a testing tool, and all code linked together must agree on it. `_GLIBCXX_ASSERTIONS` is the lightweight subset, with no ABI change, that `-fhardened` itself turns on for production.
- -D_FORTIFY_SOURCE=2 protects -O0 debug builds too
- -fhardened replaces running sanitizers in testing
- _GLIBCXX_ASSERTIONS changes the ABI like Debug Mode
- -fhardened overrides an explicit -fstack-protector on the command line
- Zero-initialized locals mean GCC stops warning about uninitialized use
Assumes GCC 16.2: -fhardened exists since GCC 14, libstdc++ defines _GLIBCXX_ASSERTIONS at -O0 by default since GCC 15, and the -fhardened list may change between major releases.