ForceZero: use a compiler barrier instead of a CPU fence - #11429
Merged
JacobBarthelmeh merged 2 commits intoSep 14, 2026
Merged
Conversation
ForceZero() bracketed its stores with XFENCE(), which resolves to a full CPU memory fence on every current compiler branch (mfence or lock-or on x86, dmb on ARM64), so each wipe of per-record state paid two fences even in single-threaded builds. The wipe only needs to survive dead-store elimination. Cross-thread visibility of the zeroed memory is not part of ForceZero's contract; a caller that shares the buffer orders it with a lock or an atomic release. Add WC_BARRIER_DATA(ptr), named after the same construct in the Linux kernel: an empty asm statement that takes the pointer as an input operand and clobbers memory, so the compiler must assume the buffer is read afterwards. ForceZero() uses it before and after the wipe. The trailing barrier keeps the zeroing stores alive. The leading one makes the compiler materialize the buffer's live contents at mem first; without it GCC 13 at -O3 kept a filled local in a different stack slot that the wipe never touched. Neither barrier costs an instruction. Non-GNU compilers and WOLFSSL_NO_ASM builds fall back to WC_BARRIER(), which keeps their previous behavior. XFENCE() itself is unchanged for the speculation-barrier call sites in asn.c and ssl_sess.c. On x86-64 with GCC 13 at -O2 the zeroing stores stay in the object code and the two lock-or fences per call are gone. The ChangeLog records the behavioral change.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The early return can bypass the trailing wipe barrier, and the ChangeLog needs a fallback-configuration qualification.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR replaces ForceZero() CPU fences with compiler barriers on supported builds while preserving fallbacks.
Changes:
- Adds
WC_BARRIER_DATA(). - Updates
ForceZero()barrier usage. - Documents the synchronization behavior change.
Review findings:
wolfcrypt/src/misc.c:794— moderate (2 votes): an early return can bypass the trailing barrier.ChangeLog.md:4— nit (1 vote): qualify the no-fence claim for fallback configurations.
File summaries
| File | Summary |
|---|---|
wolfssl/wolfcrypt/wc_port.h |
Defines compiler/data barriers and fallbacks. |
wolfcrypt/src/misc.c |
Uses barriers around memory wiping. |
ChangeLog.md |
Documents the behavioral change. |
Review details
Suppressed comments (1)
ChangeLog.md:8
- This entry is too broad for the fallback configurations: when
__GNUC__is unavailable orWOLFSSL_NO_ASMis set,WC_BARRIER_DATA()falls back toWC_BARRIER(), whose default implementation invokesXFENCE()(wc_port.h:2013-2017). Those builds can therefore still issue CPU fences, so qualify the no-fence behavior to the GNU-asm default or document the fallback exception.
* **Behavioral change (`ForceZero()` issues no CPU fences)**: the wipe is
kept alive by a compiler barrier that takes the buffer address, which also
keeps it from being optimized away for buffers that never leave the inlined
code. A caller that needs the zeroed memory to be visible to another core
must order it itself with a lock or an atomic release.
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Member
Author
|
retest this please |
The alignment loop returned early when the buffer was shorter than the bytes needed to reach alignment, skipping the trailing WC_BARRIER_DATA(). Only that barrier keeps the wipe from being dropped as a dead store, so a short unaligned buffer could go unwiped. Fold the length check into the loop condition instead.
JacobBarthelmeh
approved these changes
Sep 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ForceZero()bracketed its wipe withXFENCE(), which resolves to a full CPU memory fence (mfence/lock-or on x86, dmb on ARM64) even in single-threaded builds, so every wipe paid two fences unnecessarily.The wipe only needs to survive dead-store elimination; cross-thread visibility isn't part of its contract, since a caller sharing the buffer already orders it with a lock or atomic release.
WC_BARRIER_DATA(ptr), named after the Linux kernel construct: an empty asm statement taking the pointer as input and clobbering memory, forcing the compiler to assume the buffer is read afterward.ForceZero()now uses this barrier before and after the wipe instead ofXFENCE(). The trailing barrier keeps the zeroing stores alive; the leading one forces the compiler to materialize the buffer's live contents first (needed to avoid GCC 13 at -O3 keeping a filled local in an untouched stack slot).WOLFSSL_NO_ASMbuilds fall back toWC_BARRIER(), preserving previous behavior.XFENCE()itself is unchanged for the speculation-barrier call sites in asn.c and ssl_sess.c.On x86-64 with GCC 13 at -O2, the zeroing stores remain in the object code and the two lock-or fences per call are eliminated. The ChangeLog records this behavioral change.