Skip to content

Consider adding -Werror=unguarded-availability-new to compiler flags for Apple platforms #100384

Description

@indygreg

Feature or enhancement

Runtime crashes like #97897 could be prevented by turning clang's unguaded-availability-new warning into a fatal compile error. This can be done by adding -Werror=unguarded-availability-new to compiler flags.

Essentially what this warning does is cross reference undefined symbols against symbols defined in the targeted Apple SDK. If it sees a symbol introduced in a newer SDK version and that symbol isn't weakly referenced/linked, you get a warning. -Werror upconverts it to a fatal compiler error.

If you add this flag to release builds, the compiler prevents you from shipping binaries that ship unguarded symbol usage for targeted SDK versions. i.e. it prevents run-time crashes when binaries run on older Apple OS versions.

If you enable this setting today, you may find the 3.8 branch isn't properly gating use of mkfifoat and mknodat. (Although this may be an oddity from python-build-standalone and not a CPython bug.)

cc @ned-deily


The advantages of having this additional warning is to catch regressions such as:

Linked PRs

Activity

  1. indygreg commented on Dec 21, 2022

    @indygreg
    ContributorAuthor

    Ignore my comment about 3.8. 3.8 doesn't have properly weak linking support at all (at least not in posixmodule.c). The mkfifoat and mknodat errors were just clang catching these symbols falling through a gap in python-build-standalone's build system, which attempts to disable all optional symbols on 3.8 to work around the lack of weak referencing. I think this demonstrates the value of -Werror=unguarded-availability-new, as without this, I probably would have shipped binaries that segfault at runtime.

  2. ronaldoussoren commented on Dec 21, 2022

    @ronaldoussoren
    Contributor

    3.9 and later should have weak linking support in posixmodule, this was introduced while working on support for arm64 and Universal 2 binaries. That said, newer SDKs can introduce new APIs that require changes to that.

  3. added
    buildThe build process and cross-build
    on Dec 20, 2024
  4. zanieb commented on Dec 21, 2024

    @zanieb
    Contributor

    Copying over some of my commentary from #128135

    Here's an initial patch 36c7da4

  5. picnixz commented on Dec 21, 2024

    @picnixz
    Member

    What we could do is integrate this new flag as part of our CI without necessarily making it part of the configure script. At least it could help without affecting local configurations? cc @hugovk

  6. ned-deily commented on Dec 21, 2024

    @ned-deily
    Member

    Instead of CFLAGS, try adding this to CFLAGS_NODIST. "CFLAGS_NODIST is used for building the interpreter and stdlib C extensions. Use it when a compiler flag should not be part of CFLAGS once Python is installed."

  7. zanieb commented on Dec 21, 2024

    @zanieb
    Contributor

    Ah cool, thanks @ned-deily. I was hoping there was something like that.

    Do you have any other concerns before I post the patch as a pull request?

    I think my most important open question is

    This flag is clang-only, will this help prevent regressions upstream or just avoid shipping bad code downstream?

    Is clang being used in CI?

  8. indygreg commented on Dec 21, 2024

    @indygreg
    ContributorAuthor

    The Xcode C toolchain is clang. There are an extremely small number of people not using clang when targeting Apple platforms. I'm pretty sure CPython's build system implicitly requires clang for Apple targets.

  9. picnixz commented on Dec 21, 2024

    @picnixz
    Member

    We use clang on build bots sanitizers (UBSan, TSan and ASan) so yes we use clang. And for the JIT as well since this requires LLVM.

  10. hugovk commented on Dec 21, 2024

    @hugovk
    Member

    What we could do is integrate this new flag as part of our CI without necessarily making it part of the configure script. At least it could help without affecting local configurations? cc @hugovk

    Like with this tooling?

    https://devguide.python.org/development-tools/warnings/

  11. zanieb commented on Dec 21, 2024

    @zanieb
    Contributor

    I think the concern about it affecting users is addressed by using CFLAGS_NODIST, is there a different concern you have that makes this better as a warning than an error?

  12. ned-deily commented on Dec 22, 2024

    @ned-deily
    Member

    I'm not sure if we should be using unguarded-availability or unguarded-availability-new?

    If I understand the implications correctly, I think we should stick with unguarded-availability until there is a demonstrated need to restrict to unguarded-availability-new.

    Also we specifically now check and report on supported platform / compiler combinations (see AC_MSG_CHECKING([for PEP 11 support tier]) in configure.ac) which for macOS is clang for tier 1 support. That said, we should be careful to not deliberately break gcc builds; presumably the AX_CHECK_COMPILE_FLAG([-Wunguarded-availability] ... test in the patch should take care of that.

  13. added a commit that references this issue on Dec 22, 2024
  14. added a commit that references this issue on Dec 23, 2024
  15. added a commit that references this issue on Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-macbuildThe build process and cross-buildtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions