Skip to content

cmake: incorrect assumption that Debug build type will not use optimisations #30800

Closed
dashpay/dash
#7696

Description

@fanquake

CMakeLists.txt currently states:

bitcoin/CMakeLists.txt

Lines 487 to 491 in 9cb9651

# When the build configuration is Debug, all optimizations are disabled.
# However, _FORTIFY_SOURCE requires that there is some level of optimization,
# otherwise it does nothing and just creates a compiler warning.
# Since _FORTIFY_SOURCE is a no-op without optimizations, do not enable it
# when the build configuration is Debug.

However that isn't always true. If, for example, depends is built with DEBUG=1, and CMake is configured with -DCMAKE_BUILD_TYPE=Debug, -O1 will actually be used. This is confusing, not just because the documentation is incorrect, but when making a change to switch the build type to Debug in oss-fuzz, it's caused unexpected results: google/oss-fuzz#12443 (comment).

Rather than assuming that builds with the Debug build type don't use optimisations, the check should be fixed to actually check what optimisation level is being used.

i.e:

make -C depends/ NO_QT=1 NO_WALLET=1 NO_ZMQ=1 NO_UPNP=1 -j17 DEBUG=1
cmake -B build --toolchain=/root/ci_scratch/depends/x86_64-pc-linux-gnu/toolchain.cmake -DCMAKE_BUILD_TYPE=Debug
<snip>
C++ compiler flags .................... -pipe -std=c++20 -O0 -ftrapv -O1 -g3

(-O1 is last, so is what will be used).

Activity

  1. ryanofsky commented on Sep 3, 2024

    @ryanofsky
    Contributor

    Not sure if this makes sense, but if the problem is that specifying _FORTIFY_SOURCE without optimizations triggers a compiler warning, maybe there should just be a check that drops _FORTIFY_SOURCE when it triggers the warning. That might avoid problems if there is a discrepancy between the way our cmake code detects optimizations and the way _FORTIFY_SOURCE implementation does.

  2. hebasto commented on Sep 3, 2024

    @hebasto
    Member

    However that isn't always true. If, for example, depends is built with DEBUG=1, and CMake is configured with -DCMAKE_BUILD_TYPE=Debug, -O1 will actually be used.

    I think we should first prioritize coordinating the optimization and debugging flags between the depends subsystem and the main build system:

    1. Should the optimization and debugging flags coincide, or are they allowed to differ?
    2. If the optimization and debugging can differ between the depends subsystem and in the main build system, which should take precedence?

    UPD. #29796 is related.

  3. fanquake commented on Sep 4, 2024

    @fanquake
    MemberAuthor

    I think we should first prioritize coordinating the optimization and debugging flags between the depends subsystem and the main build system:

    I'm not sure? This hardening feature should be turned on if optimisations are being used, and hardening isn't disabled. Whether or not depends flags override CMake defaults, or if depends defaults match CMake defaults seems tangential.

  4. fanquake commented on Sep 5, 2024

    @fanquake
    MemberAuthor

    Not sure if this makes sense, but if the problem is that specifying _FORTIFY_SOURCE without optimizations triggers a compiler warning, maybe there should just be a check that drops _FORTIFY_SOURCE when it triggers the warning. That might avoid problems if there is a discrepancy between the way our cmake code detects optimizations and the way _FORTIFY_SOURCE implementation does.

    That could be a good approach, but there's also the possiblity that FORTIFY will warn if the selected level will be treated as a lower level (i.e 3 selected, but will be treated as 2), depending on the libc, platform etc, which would cause it's usage to be inadvertantly disabled. I've opted to implement what is (hopefully) a straightforward enough check for optimisations in #30824, which is the same test used by glibc.

  5. added a commit that references this issue on Sep 9, 2024
    94bc3c4
  6. locked and limited conversation to collaborators on Sep 9, 2025
  7. added a commit that references this issue on Sep 18, 2026
    f1f64f7
  8. added a commit that references this issue on Sep 19, 2026
    86771f6
  9. added a commit that references this issue on Nov 17, 2026
    70afb28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions