Repository navigation
Conversation
Prevent optimizing compilers (in particular Clang 21 on x86-64 and AArch64) from propagating the 0 / all-ones range of `mask` and `fastMask` into secret-dependent branches or `memset` calls. Uses an empty inline-assembly barrier on GCC/Clang when `LTC_NO_ASM` and `__STRICT_ANSI__` are not defined, with a typed `volatile` fallback otherwise, and undefines the local macro at EOF for `AMALGAM=1` builds. Fixes: libtom#832
sjaeckel
reviewed
Dec 3, 2026
sjaeckel
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR :)
Basically LGTM.
If you agree with the proposal, please amend the commit and also add a Fixes: 5c0b1b4bf6ee ("add copy_or_zeromem()") to the commit message.
| #if !defined(__STRICT_ANSI__) && (defined(__GNUC__) || defined(__clang__)) && !defined(LTC_NO_ASM) | ||
| #define LTC_VALUE_BARRIER(type, x) __asm__ ("" : "+r" (x)) | ||
| #else | ||
| #define LTC_VALUE_BARRIER(type, x) do { volatile type ltc_vb_ = (x); (x) = ltc_vb_; } while (0) |
Member
There was a problem hiding this comment.
Suggested change
| #define LTC_VALUE_BARRIER(type, x) do { volatile type ltc_vb_ = (x); (x) = ltc_vb_; } while (0) | |
| #define LTC_VALUE_BARRIER(type, x) do { volatile type LTC_TMPVAR(vb) = (x); (x) = LTC_TMPVAR(vb); } while (0) |
WDYT?
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.
Fixes: #832
Adds a portable compiler barrier on the bitmasks in
copy_or_zeromemso optimizing compilers do not turn the branchless masking loop into a secret-dependent branch or amemsetcall.Verified branchless output across optimization levels on x86-64 and ARM64 (with and without inline assembly enabled) and ran the test suite under Clang and GCC.
Checklist
Fixes:tag to the commit message