Skip to content

Enable Clang 23 lifetime safety warnings #1846

Backlog

Description

@chfast

Clang 23 adds a flow-sensitive lifetime analysis: the -Wlifetime-safety* warning groups, off by default and included in -Weverything. Clang 22 and older don't know these groups. Let's enable the reliable part with -Werror once CI builds with Clang 23 (branch ci/cpp-build-env-26).

Results of a full build with tests (Clang 23.1.6, libc++, Debug, x86-64 Linux):

  • -Wlifetime-safety-permissive (use-after-scope, return-stack-addr, dangling-field, dangling-global, use-after-free) and the -moved variants in -Wlifetime-safety-strict: no warnings. On small synthetic cases they do fire, so the silence means something.
  • -Wlifetime-safety-invalidation (in -Wlifetime-safety-strict): 5 warnings in test/, all false positives.
    • A container passed by reference and then appended to: test/utils/test_collector.cpp:51, test/utils/blockchaintest_loader.cpp:178, test/utils/statetest_loader.cpp:521, test/state/state.cpp:158. This is [LifetimeSafety] False positive after adding to an std::vector llvm/llvm-project#222537. A maintainer says invalidations have "a lot of false-positives right now and not adoptable atm", and LLVM main has renamed the group to -Wlifetime-safety-experimental-invalidation.
    • An assignment through a reference into a vector element (auto& log_data = receipts[0].logs[0].data; log_data = ...;): test/unittests/state_deposit_requests_test.cpp:29.
  • -Wlifetime-safety-suggestions (not in -Wlifetime-safety): 73 suggestions to add [[clang::lifetimebound]] (45 intra-TU, 26 intra-TU constructor, 2 cross-TU). Most are instruction handlers returning pos, constructors storing a pointer or reference, and accessors returning a member. They are mechanical and none of them is a bug.
  • The cc1 options -flifetime-safety-inference and -fexperimental-lifetime-safety-tu-analysis propagate the inferred lifetimebound across calls within a TU, which catches e.g. std::string s; return id(s);. They only take effect together. They also add 6 invalidation false positives:
    • a reference returned by emplace_back, even outside a loop: lib/evmone_precompiles/bls.cpp:221,226,272,277;
    • mutation through a reference into the object: test/state/state.cpp:384 and the chained append at test/utils/test_report.hpp:155.
  • The analysis would not have reported the only lifetime bug fixed so far, tracing: Keep copy of output path in InstructionCounter #1554 (a std::string_view member in InstructionCounter). On the pre-fix tracing.cpp it only suggests lifetimebound on the constructor parameter, alongside 4 identical suggestions for correct constructors in the same file. The dangling caller is in another TU, behind create_instruction_counter(), which returns std::unique_ptr<Tracer>.

Proposal:

  1. With Clang ≥ 23, enable -Wlifetime-safety -Wno-lifetime-safety-invalidation behind a compiler flag check, because Clang 17–22 and AppleClang don't support these flags.
  2. Enable -Wlifetime-safety-invalidation after [LifetimeSafety] False positive after adding to an std::vector llvm/llvm-project#222537 is fixed.
  3. Maybe: annotate a few view-returning APIs with [[clang::lifetimebound]] (e.g. ModArith::mod(), the bytes_view accessors); -Wlifetime-safety-validations then checks the annotations. Not for the instruction handlers.

To reproduce, inject the flags after project() with -DCMAKE_PROJECT_evmone_INCLUDE=<file>. CMake de-duplicates repeated -Xclang options, hence SHELL:.

add_compile_options(
    -Weverything
    "$<$<COMPILE_LANGUAGE:CXX>:SHELL:-Xclang -flifetime-safety-inference>"
    "$<$<COMPILE_LANGUAGE:CXX>:SHELL:-Xclang -fexperimental-lifetime-safety-tu-analysis>"
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions