Skip to content

Commit ff48b1e

Browse files
rikvanrielmeta-codesync[bot]
authored andcommitted
Annotate net-new Snapshot-borrow accessors with [[clang::lifetimebound]]
Summary: Adds [[clang::lifetimebound]] to the borrow-returning folly::observer snapshot accessors NOT already covered by the D109704537 stack, so the compiler (-Wdangling / -Wreturn-stack-address) catches consumers that bind a reference/pointer into a snapshot that does not outlive it: - TLObserver::getSnapshotRef() / operator*() (Observer.h) - HazptrObserver::HazptrSnapshot operator*/->/get (HazptrObserver.h) - CoreCachedObserver::CoreCachedSnapshot operator*/->/get (CoreCachedObserver.h) Snapshot::operator*/->/get are annotated separately by the D109704537 stack. The single consumer this surfaced -- luna/quorumlib QuorumConfig::getConfigName returning a const std::string& into a temporary snapshot -- is fixed in the parent diff D109961109; with it in the stack, full CI is green. Reviewed By: r-barnes Differential Revision: D109953065 fbshipit-source-id: 8e9cb2d93b658960e0a7d1f94867910664be9741
1 parent 5b42321 commit ff48b1e

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎folly/observer/CoreCachedObserver.h‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <memory>
2020

21+
#include <folly/CppAttributes.h>
2122
#include <folly/concurrency/CoreCachedSharedPtr.h>
2223
#include <folly/observer/Observer.h>
2324
#include <folly/observer/detail/ObserverManager.h>
@@ -31,9 +32,15 @@ class CoreCachedObserver {
3132
explicit CoreCachedSnapshot(std::shared_ptr<const T> data)
3233
: data_(std::move(data)) {}
3334

34-
const T& operator*() const { return *get(); }
35-
const T* operator->() const { return get(); }
36-
const T* get() const { return data_.get(); }
35+
const T& operator*() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
36+
return *get();
37+
}
38+
const T* operator->() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
39+
return get();
40+
}
41+
const T* get() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
42+
return data_.get();
43+
}
3744

3845
std::shared_ptr<const T> getShared() const& { return data_; }
3946
std::shared_ptr<const T> getShared() && { return std::move(data_); }

‎folly/observer/HazptrObserver.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <atomic>
2020
#include <memory>
2121

22+
#include <folly/CppAttributes.h>
2223
#include <folly/Synchronized.h>
2324
#include <folly/observer/Observer.h>
2425
#include <folly/observer/detail/ObserverManager.h>
@@ -57,9 +58,13 @@ class HazptrObserver {
5758
ptr_ = get(holder_).protect(state)->snapshot_.get();
5859
}
5960

60-
const T& operator*() const { return *get(); }
61-
const T* operator->() const { return get(); }
62-
const T* get() const { return ptr_; }
61+
const T& operator*() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
62+
return *get();
63+
}
64+
const T* operator->() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
65+
return get();
66+
}
67+
const T* get() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] { return ptr_; }
6368

6469
private:
6570
static void make(hazptr_holder<Atom>& holder, hazptr_domain<Atom>& domain) {

‎folly/observer/Observer.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <atomic>
2020
#include <memory>
2121

22+
#include <folly/CppAttributes.h>
2223
#include <folly/SharedMutex.h>
2324
#include <folly/ThreadLocal.h>
2425
#include <folly/observer/Observer-pre.h>
@@ -418,8 +419,10 @@ class TLObserver {
418419
TLObserver(const TLObserver<T>& other);
419420
TLObserver(TLObserver<T>&& other) noexcept;
420421

421-
const Snapshot<T>& getSnapshotRef() const;
422-
const Snapshot<T>& operator*() const { return getSnapshotRef(); }
422+
const Snapshot<T>& getSnapshotRef() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]];
423+
const Snapshot<T>& operator*() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
424+
return getSnapshotRef();
425+
}
423426

424427
/**
425428
* Invoke a function with the current observed value. The snapshot is held

0 commit comments

Comments
 (0)