Avoid unsequenced accesses to volatile variables #1998

pull hebasto wants to merge 2 commits into bitcoin-core:master from hebasto:261114-volatile changing 2 files +42 −36
  1. hebasto commented at 11:06 AM on November 14, 2026: member

    Since C11, if a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined (see https://en.cppreference.com/c/language/eval_order). Although this code targets C89, where the order of volatile reads is merely unspecified, this is exactly what happens with the volatile cond_negate in the master branch, because a read of such a variable is a side effect. Clang 24 warns about it with -Wunsequenced regardless of the language standard:

    $ cmake -B build -DCMAKE_C_COMPILER=clang-24
    $ cmake --build build
    [5/19] Building C object src/CMakeFiles/exhaustive_tests.dir/tests_exhaustive.c.o
    In file included from /home/hebasto/dev/secp256k1/secp256k1/src/tests_exhaustive.c:23:
    In file included from /home/hebasto/dev/secp256k1/secp256k1/src/secp256k1.c:27:
    In file included from /home/hebasto/dev/secp256k1/secp256k1/src/field_impl.h:14:
    In file included from /home/hebasto/dev/secp256k1/secp256k1/src/field_5x52_impl.h:13:
    /home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:116:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
      116 |     r0 = (r0 ^ cond_negate) - cond_negate;
          |                ~~~~~~~~~~~    ^
    /home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:117:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
      117 |     r1 = (r1 ^ cond_negate) - cond_negate;
          |                ~~~~~~~~~~~    ^
    /home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:118:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
      118 |     r2 = (r2 ^ cond_negate) - cond_negate;
          |                ~~~~~~~~~~~    ^
    /home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:119:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
      119 |     r3 = (r3 ^ cond_negate) - cond_negate;
          |                ~~~~~~~~~~~    ^
    /home/hebasto/dev/secp256k1/secp256k1/src/modinv64_impl.h:120:31: warning: unsequenced accesses to volatile qualified 'cond_negate' [-Wunsequenced]
      120 |     r4 = (r4 ^ cond_negate) - cond_negate;
          |                ~~~~~~~~~~~    ^
    5 warnings generated.
    <snip>
    

    Avoid such behavior by separating the access to a volatile variable, which still prevents compilers from generating branching, non-constant-time machine code, from the actual use of its value in expressions where the evaluation order is unspecified.

  2. hebasto referenced this in commit c282fc295e on Nov 14, 2026
  3. fametrano approved
  4. fametrano commented at 7:27 AM on November 15, 2026: contributor

    ACK 5b6aea409261f7b7d430f2e301c44c67d38eb388

    Both normalize functions stay branch-free with this change: in the generated code, the repeated loads of cond_negate become a single one, and neither version has a conditional branch.

    The -O2 disassembly of secp256k1.c differs between master and this PR in those two functions only, under clang 23.1.5 for arm64 and x86_64 and gcc 14.2 for aarch64, in the default and the int64 widemul builds. Under valgrind, the gcc builds of ctime_tests report no errors with the PR applied. The test reaches both functions: with an if (mask_negate) inserted into secp256k1_modinv64_normalize_62 or secp256k1_modinv32_normalize_30, valgrind reports a conditional jump on secret data in that function.

    A clang 24.0.0 snapshot of 2026-11-14 with -Wunsequenced warns on master at the 14 lines this PR changes, and nowhere else, examples included. With the PR applied, neither build has a warning.

  5. theStack added the label tweak/refactor on Nov 15, 2026
  6. theStack approved
  7. theStack commented at 12:10 AM on November 16, 2026: contributor

    ACK 5b6aea409261f7b7d430f2e301c44c67d38eb388

    Thanks for the fast fix!

  8. real-or-random commented at 8:58 AM on November 16, 2026: contributor

    Since C11, if a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined (see https://en.cppreference.com/c/language/eval_order). Although this code targets C89, where the order of such accesses is merely unspecified, this is exactly what happens with the volatile cond_negate in the master branch, because a read of such a variable is a side effect.

    I had to look this up, and here's what I learned: Unsequenced modifications were already UB (C89 6.3) (of course). What’s new in C11 (6.5p2) is that the rule is stated in terms of side effects rather than modifications. Since accessing a volatile object has been a side effect since C89 (5.1.2.3), unsequenced volatile reads went from unspecified order to UB.

  9. in src/modinv32_impl.h:68 in 5b6aea4092
      64 | @@ -65,6 +65,7 @@ static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int3
      65 |      int32_t r0 = r->v[0], r1 = r->v[1], r2 = r->v[2], r3 = r->v[3], r4 = r->v[4],
      66 |              r5 = r->v[5], r6 = r->v[6], r7 = r->v[7], r8 = r->v[8];
      67 |      volatile int32_t cond_add, cond_negate;
      68 | +    int32_t mask_negate;
    


    real-or-random commented at 9:00 AM on November 16, 2026:

    I think the naming here can really be improved because otherwise it's unclear to readers. We use a v prefix in other places (grep for volatile).

        int32_t cond_negate;
        volatile int32_t vcond_add, vcond_negate;
    

    hebasto commented at 11:51 AM on November 16, 2026:

    Thanks! Done in a separated refactor commit.

  10. in src/modinv32_impl.h:97 in 5b6aea4092
     101 | -    r4 = (r4 ^ cond_negate) - cond_negate;
     102 | -    r5 = (r5 ^ cond_negate) - cond_negate;
     103 | -    r6 = (r6 ^ cond_negate) - cond_negate;
     104 | -    r7 = (r7 ^ cond_negate) - cond_negate;
     105 | -    r8 = (r8 ^ cond_negate) - cond_negate;
     106 | +    mask_negate = cond_negate;
    


    real-or-random commented at 9:00 AM on November 16, 2026:

    Also, I think this entire thing so obscure that this assignment deserves a comment.


    hebasto commented at 9:18 AM on November 16, 2026:

    Sure. What's wording for that comment in your mind?


    real-or-random commented at 9:30 AM on November 16, 2026:

    You're right. I should have proposed one for a more efficient discussion.

    What about this: "Unsequenced reads to volatile variables are UB, so copy to a non-volatile variable first."


    hebasto commented at 11:52 AM on November 16, 2026:

    Thanks! Comments have been added.

  11. real-or-random commented at 9:01 AM on November 16, 2026: contributor

    Thanks for taking care of this. ACK of course but two nits I'd prefer to see addressed

  12. refactor: Prefix volatile variable names with "v"
    This follow the convention used for volatile variable elsewhere in this
    project.
    
    No behavior change.
    709e39c2fb
  13. Avoid unsequenced accesses to volatile variables
    Since C11, if a side effect on a scalar object is unsequenced relative
    to another side effect on the same scalar object, the behavior is
    undefined. Although this code targets C89, where the order of volatile
    reads is merely unspecified, this is exactly what happens with the
    volatile `cond_negate` in the master branch, because a read of such a
    variable is a side effect. Clang 24 warns about it with `-Wunsequenced`
    regardless of the language standard.
    
    Avoid such behavior by separating the access to a volatile variable,
    which still prevents compilers from generating branching,
    non-constant-time machine code, from the actual use of its value in
    expressions where the evaluation order is unspecified.
    e7a34ba3ec
  14. hebasto force-pushed on Nov 16, 2026
  15. hebasto commented at 11:51 AM on November 16, 2026: member

    Thanks for the review! The feedback has been addressed.

    Since C11, if a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined (see https://en.cppreference.com/c/language/eval_order). Although this code targets C89, where the order of such accesses is merely unspecified, this is exactly what happens with the volatile cond_negate in the master branch, because a read of such a variable is a side effect.

    I had to look this up, and here's what I learned: Unsequenced modifications were already UB (C89 6.3) (of course). What’s new in C11 (6.5p2) is that the rule is stated in terms of side effects rather than modifications. Since accessing a volatile object has been a side effect since C89 (5.1.2.3), unsequenced volatile reads went from unspecified order to UB.

    I've replaced "the order of such accesses" with "the order of volatile reads" in the commit message and the PR description.

  16. real-or-random approved
  17. real-or-random commented at 1:15 PM on November 16, 2026: contributor

    utACK e7a34ba3ecee49421404994eeeeb0383f3e2fbbf

  18. theStack approved
  19. theStack commented at 1:29 PM on November 16, 2026: contributor

    re-ACK e7a34ba3ecee49421404994eeeeb0383f3e2fbbf

  20. theStack merged this on Nov 16, 2026
  21. theStack closed this on Nov 16, 2026

  22. github-actions[bot] referenced this in commit 35743fcabc on Nov 16, 2026
  23. hebasto deleted the branch on Nov 16, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-02 10:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me