Repository navigation
Backlog
Description
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-movedvariants 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 intest/, 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.
- A container passed by reference and then appended to:
-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 returningpos, 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-inferenceand-fexperimental-lifetime-safety-tu-analysispropagate the inferredlifetimeboundacross 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:384and the chainedappendattest/utils/test_report.hpp:155.
- a reference returned by
- 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_viewmember inInstructionCounter). On the pre-fixtracing.cppit only suggestslifetimeboundon the constructor parameter, alongside 4 identical suggestions for correct constructors in the same file. The dangling caller is in another TU, behindcreate_instruction_counter(), which returnsstd::unique_ptr<Tracer>.
Proposal:
- With Clang ≥ 23, enable
-Wlifetime-safety -Wno-lifetime-safety-invalidationbehind a compiler flag check, because Clang 17–22 and AppleClang don't support these flags. - Enable
-Wlifetime-safety-invalidationafter [LifetimeSafety] False positive after adding to an std::vector llvm/llvm-project#222537 is fixed. - Maybe: annotate a few view-returning APIs with
[[clang::lifetimebound]](e.g.ModArith::mod(), thebytes_viewaccessors);-Wlifetime-safety-validationsthen 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
Labels
No labels
Activity
cmake: Enable the Clang lifetime safety warnings
cmake: Enable the Clang lifetime safety warnings
cmake: Enable the Clang lifetime safety warnings
cmake: Enable the Clang lifetime safety warnings
cmake: Enable the Clang lifetime safety warnings
cmake: Enable the Clang lifetime safety warnings (#1859)