Add a self-check for miscompiled `memcmp` and remove `secp256k1_memcmp_var` #2007

pull hebasto wants to merge 3 commits into bitcoin-core:master from hebasto:261121-memcmp-selfcheck changing 25 files +292 −295
  1. hebasto commented at 8:17 PM on November 21, 2026: member

    secp256k1_memcmp_var was introduced in 6173839c90553385171d560be8a17cbe167e3bef to avoid GCC bug 95189, which may miscompile memcmp. This PR follows the suggestion in #823 (comment) to get rid of the function and to guard against affected compilers with a self-check instead.

    Closes #823.

  2. selftest: Add check for miscompiled `memcmp`
    GCC 9.2 to 9.5, 10.1 and 10.2 may miscompile `memcmp` when optimizing
    with `-O2` or `-O3`. The bug is fixed in GCC 10.3, but not in the GCC 9
    series. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189.
    
    Add a self-check that runs a known test case for this bug. On affected
    compilers, building with `-fno-builtin-memcmp` works around the bug.
    
    A runtime check is used as it works regardless of how the library is
    built, i.e., with CMake, with Autotools, or without any build system.
    
    This is a step towards removing `secp256k1_memcmp_var`, see #823.
    c7edddcde8
  3. Replace `secp256k1_memcmp_var()` with `memcmp()`
    `secp256k1_memcmp_var()` was introduced to avoid a GCC bug that may
    miscompile `memcmp`. See commit 6173839c90553385171d560be8a17cbe167e3bef.
    As of the parent commit, the self-test detects affected compilers at
    runtime, so the workaround is no longer needed at the call sites.
    8e3e9d7722
  4. refactor: Remove unused `secp256k1_memcmp_var()`
    Also drop the guideline to use it from CONTRIBUTING.md.
    947bd145c1
  5. Yudis-bit commented at 2:06 AM on November 22, 2026: contributor

    ACK 947bd145c13e674f00a7c614313efb0d3e902d71

    I checked the memcmp selftest on GCC 9 and 10: it catches 9.2 through 10.2 at -O2/-O3 and passes on 10.3 or with -fno-builtin-memcmp. Also built with all modules on GCC and Clang and ran ctest and Valgrind ctime_tests locally.

  6. theStack commented at 9:18 PM on November 22, 2026: contributor

    Concept ACK

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

    ACK 947bd145c13e674f00a7c614313efb0d3e902d71

    I think we can safely do this now.

    • GCC 9 is really old
    • If you're on GCC 10, you can probably upgrade to >= 10.3
    • Debian Bullseye (still shipping 10.2) is EOL
    • If none of the above helps you, -fno-builtin-memcmp is a simple workaround.
  9. real-or-random added the label needs-changelog on Nov 23, 2026
  10. real-or-random added the label tweak/refactor on Nov 23, 2026
  11. hebasto commented at 9:40 AM on November 23, 2026: member
    • Debian Bullseye (still shipping 10.2) is EOL

    I believe that gcc-10 is patched on Debian Bullseye.

  12. theStack approved
  13. theStack commented at 6:34 PM on November 23, 2026: contributor

    ACK 947bd145c13e674f00a7c614313efb0d3e902d71

  14. real-or-random merged this on Nov 23, 2026
  15. real-or-random closed this on Nov 23, 2026

  16. hebasto deleted the branch on Nov 23, 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