1 //
2 // Copyright 2020 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "absl/flags/reflection.h"
17
18 #include <assert.h>
19
20 #include <atomic>
21 #include <string>
22
23 #include "absl/base/config.h"
24 #include "absl/base/thread_annotations.h"
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/flags/commandlineflag.h"
27 #include "absl/flags/internal/private_handle_accessor.h"
28 #include "absl/flags/internal/registry.h"
29 #include "absl/flags/usage_config.h"
30 #include "absl/strings/str_cat.h"
31 #include "absl/strings/string_view.h"
32 #include "absl/synchronization/mutex.h"
33
34 namespace absl {
35 ABSL_NAMESPACE_BEGIN
36 namespace flags_internal {
37
38 // --------------------------------------------------------------------
39 // FlagRegistry
40 // A FlagRegistry singleton object holds all flag objects indexed by their
41 // names so that if you know a flag's name, you can access or set it. If the
42 // function is named FooLocked(), you must own the registry lock before
43 // calling the function; otherwise, you should *not* hold the lock, and the
44 // function will acquire it itself if needed.
45 // --------------------------------------------------------------------
46
47 class FlagRegistry {
48 public:
49 FlagRegistry() = default;
50 ~FlagRegistry() = default;
51
52 // Store a flag in this registry. Takes ownership of *flag.
53 void RegisterFlag(CommandLineFlag& flag, const char* filename);
54
Lock()55 void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION(lock_) { lock_.Lock(); }
Unlock()56 void Unlock() ABSL_UNLOCK_FUNCTION(lock_) { lock_.Unlock(); }
57
58 // Returns the flag object for the specified name, or nullptr if not found.
59 // Will emit a warning if a 'retired' flag is specified.
60 CommandLineFlag* FindFlag(absl::string_view name);
61
62 static FlagRegistry& GlobalRegistry(); // returns a singleton registry
63
64 private:
65 friend class flags_internal::FlagSaverImpl; // reads all the flags in order
66 // to copy them
67 friend void ForEachFlag(std::function<void(CommandLineFlag&)> visitor);
68 friend void FinalizeRegistry();
69
70 // The map from name to flag, for FindFlag().
71 using FlagMap = absl::flat_hash_map<absl::string_view, CommandLineFlag*>;
72 using FlagIterator = FlagMap::iterator;
73 using FlagConstIterator = FlagMap::const_iterator;
74 FlagMap flags_;
75 std::vector<CommandLineFlag*> flat_flags_;
76 std::atomic<bool> finalized_flags_{false};
77
78 absl::Mutex lock_;
79
80 // Disallow
81 FlagRegistry(const FlagRegistry&);
82 FlagRegistry& operator=(const FlagRegistry&);
83 };
84
85 namespace {
86
87 class FlagRegistryLock {
88 public:
FlagRegistryLock(FlagRegistry & fr)89 explicit FlagRegistryLock(FlagRegistry& fr) : fr_(fr) { fr_.Lock(); }
~FlagRegistryLock()90 ~FlagRegistryLock() { fr_.Unlock(); }
91
92 private:
93 FlagRegistry& fr_;
94 };
95
96 } // namespace
97
FindFlag(absl::string_view name)98 CommandLineFlag* FlagRegistry::FindFlag(absl::string_view name) {
99 if (finalized_flags_.load(std::memory_order_acquire)) {
100 // We could save some gcus here if we make `Name()` be non-virtual.
101 // We could move the `const char*` name to the base class.
102 auto it = std::partition_point(
103 flat_flags_.begin(), flat_flags_.end(),
104 [=](CommandLineFlag* f) { return f->Name() < name; });
105 if (it != flat_flags_.end() && (*it)->Name() == name) return *it;
106 }
107
108 FlagRegistryLock frl(*this);
109 auto it = flags_.find(name);
110 return it != flags_.end() ? it->second : nullptr;
111 }
112
RegisterFlag(CommandLineFlag & flag,const char * filename)113 void FlagRegistry::RegisterFlag(CommandLineFlag& flag, const char* filename) {
114 if (filename != nullptr &&
115 flag.Filename() != GetUsageConfig().normalize_filename(filename)) {
116 flags_internal::ReportUsageError(
117 absl::StrCat(
118 "Inconsistency between flag object and registration for flag '",
119 flag.Name(),
120 "', likely due to duplicate flags or an ODR violation. Relevant "
121 "files: ",
122 flag.Filename(), " and ", filename),
123 true);
124 std::exit(1);
125 }
126
127 FlagRegistryLock registry_lock(*this);
128
129 std::pair<FlagIterator, bool> ins =
130 flags_.insert(FlagMap::value_type(flag.Name(), &flag));
131 if (ins.second == false) { // means the name was already in the map
132 CommandLineFlag& old_flag = *ins.first->second;
133 if (flag.IsRetired() != old_flag.IsRetired()) {
134 // All registrations must agree on the 'retired' flag.
135 flags_internal::ReportUsageError(
136 absl::StrCat(
137 "Retired flag '", flag.Name(), "' was defined normally in file '",
138 (flag.IsRetired() ? old_flag.Filename() : flag.Filename()), "'."),
139 true);
140 } else if (flags_internal::PrivateHandleAccessor::TypeId(flag) !=
141 flags_internal::PrivateHandleAccessor::TypeId(old_flag)) {
142 flags_internal::ReportUsageError(
143 absl::StrCat("Flag '", flag.Name(),
144 "' was defined more than once but with "
145 "differing types. Defined in files '",
146 old_flag.Filename(), "' and '", flag.Filename(), "'."),
147 true);
148 } else if (old_flag.IsRetired()) {
149 return;
150 } else if (old_flag.Filename() != flag.Filename()) {
151 flags_internal::ReportUsageError(
152 absl::StrCat("Flag '", flag.Name(),
153 "' was defined more than once (in files '",
154 old_flag.Filename(), "' and '", flag.Filename(), "')."),
155 true);
156 } else {
157 flags_internal::ReportUsageError(
158 absl::StrCat(
159 "Something is wrong with flag '", flag.Name(), "' in file '",
160 flag.Filename(), "'. One possibility: file '", flag.Filename(),
161 "' is being linked both statically and dynamically into this "
162 "executable. e.g. some files listed as srcs to a test and also "
163 "listed as srcs of some shared lib deps of the same test."),
164 true);
165 }
166 // All cases above are fatal, except for the retired flags.
167 std::exit(1);
168 }
169 }
170
GlobalRegistry()171 FlagRegistry& FlagRegistry::GlobalRegistry() {
172 static FlagRegistry* global_registry = new FlagRegistry;
173 return *global_registry;
174 }
175
176 // --------------------------------------------------------------------
177
ForEachFlag(std::function<void (CommandLineFlag &)> visitor)178 void ForEachFlag(std::function<void(CommandLineFlag&)> visitor) {
179 FlagRegistry& registry = FlagRegistry::GlobalRegistry();
180
181 if (registry.finalized_flags_.load(std::memory_order_acquire)) {
182 for (const auto& i : registry.flat_flags_) visitor(*i);
183 }
184
185 FlagRegistryLock frl(registry);
186 for (const auto& i : registry.flags_) visitor(*i.second);
187 }
188
189 // --------------------------------------------------------------------
190
RegisterCommandLineFlag(CommandLineFlag & flag,const char * filename)191 bool RegisterCommandLineFlag(CommandLineFlag& flag, const char* filename) {
192 FlagRegistry::GlobalRegistry().RegisterFlag(flag, filename);
193 return true;
194 }
195
FinalizeRegistry()196 void FinalizeRegistry() {
197 auto& registry = FlagRegistry::GlobalRegistry();
198 FlagRegistryLock frl(registry);
199 if (registry.finalized_flags_.load(std::memory_order_relaxed)) {
200 // Was already finalized. Ignore the second time.
201 return;
202 }
203 registry.flat_flags_.reserve(registry.flags_.size());
204 for (const auto& f : registry.flags_) {
205 registry.flat_flags_.push_back(f.second);
206 }
207 std::sort(std::begin(registry.flat_flags_), std::end(registry.flat_flags_),
208 [](const CommandLineFlag* lhs, const CommandLineFlag* rhs) {
209 return lhs->Name() < rhs->Name();
210 });
211 registry.flags_.clear();
212 registry.finalized_flags_.store(true, std::memory_order_release);
213 }
214
215 // --------------------------------------------------------------------
216
217 namespace {
218
219 class RetiredFlagObj final : public CommandLineFlag {
220 public:
RetiredFlagObj(const char * name,FlagFastTypeId type_id)221 constexpr RetiredFlagObj(const char* name, FlagFastTypeId type_id)
222 : name_(name), type_id_(type_id) {}
223
224 private:
Name() const225 absl::string_view Name() const override { return name_; }
Filename() const226 std::string Filename() const override {
227 OnAccess();
228 return "RETIRED";
229 }
TypeId() const230 FlagFastTypeId TypeId() const override { return type_id_; }
Help() const231 std::string Help() const override {
232 OnAccess();
233 return "";
234 }
IsRetired() const235 bool IsRetired() const override { return true; }
IsSpecifiedOnCommandLine() const236 bool IsSpecifiedOnCommandLine() const override {
237 OnAccess();
238 return false;
239 }
DefaultValue() const240 std::string DefaultValue() const override {
241 OnAccess();
242 return "";
243 }
CurrentValue() const244 std::string CurrentValue() const override {
245 OnAccess();
246 return "";
247 }
248
249 // Any input is valid
ValidateInputValue(absl::string_view) const250 bool ValidateInputValue(absl::string_view) const override {
251 OnAccess();
252 return true;
253 }
254
SaveState()255 std::unique_ptr<flags_internal::FlagStateInterface> SaveState() override {
256 return nullptr;
257 }
258
ParseFrom(absl::string_view,flags_internal::FlagSettingMode,flags_internal::ValueSource,std::string &)259 bool ParseFrom(absl::string_view, flags_internal::FlagSettingMode,
260 flags_internal::ValueSource, std::string&) override {
261 OnAccess();
262 return false;
263 }
264
CheckDefaultValueParsingRoundtrip() const265 void CheckDefaultValueParsingRoundtrip() const override { OnAccess(); }
266
Read(void *) const267 void Read(void*) const override { OnAccess(); }
268
OnAccess() const269 void OnAccess() const {
270 flags_internal::ReportUsageError(
271 absl::StrCat("Accessing retired flag '", name_, "'"), false);
272 }
273
274 // Data members
275 const char* const name_;
276 const FlagFastTypeId type_id_;
277 };
278
279 } // namespace
280
Retire(const char * name,FlagFastTypeId type_id,char * buf)281 void Retire(const char* name, FlagFastTypeId type_id, char* buf) {
282 static_assert(sizeof(RetiredFlagObj) == kRetiredFlagObjSize, "");
283 static_assert(alignof(RetiredFlagObj) == kRetiredFlagObjAlignment, "");
284 auto* flag = ::new (static_cast<void*>(buf))
285 flags_internal::RetiredFlagObj(name, type_id);
286 FlagRegistry::GlobalRegistry().RegisterFlag(*flag, nullptr);
287 }
288
289 // --------------------------------------------------------------------
290
291 class FlagSaverImpl {
292 public:
293 FlagSaverImpl() = default;
294 FlagSaverImpl(const FlagSaverImpl&) = delete;
295 void operator=(const FlagSaverImpl&) = delete;
296
297 // Saves the flag states from the flag registry into this object.
298 // It's an error to call this more than once.
SaveFromRegistry()299 void SaveFromRegistry() {
300 assert(backup_registry_.empty()); // call only once!
301 flags_internal::ForEachFlag([&](CommandLineFlag& flag) {
302 if (auto flag_state =
303 flags_internal::PrivateHandleAccessor::SaveState(flag)) {
304 backup_registry_.emplace_back(std::move(flag_state));
305 }
306 });
307 }
308
309 // Restores the saved flag states into the flag registry.
RestoreToRegistry()310 void RestoreToRegistry() {
311 for (const auto& flag_state : backup_registry_) {
312 flag_state->Restore();
313 }
314 }
315
316 private:
317 std::vector<std::unique_ptr<flags_internal::FlagStateInterface>>
318 backup_registry_;
319 };
320
321 } // namespace flags_internal
322
FlagSaver()323 FlagSaver::FlagSaver() : impl_(new flags_internal::FlagSaverImpl) {
324 impl_->SaveFromRegistry();
325 }
326
~FlagSaver()327 FlagSaver::~FlagSaver() {
328 if (!impl_) return;
329
330 impl_->RestoreToRegistry();
331 delete impl_;
332 }
333
334 // --------------------------------------------------------------------
335
FindCommandLineFlag(absl::string_view name)336 CommandLineFlag* FindCommandLineFlag(absl::string_view name) {
337 if (name.empty()) return nullptr;
338 flags_internal::FlagRegistry& registry =
339 flags_internal::FlagRegistry::GlobalRegistry();
340 return registry.FindFlag(name);
341 }
342
343 // --------------------------------------------------------------------
344
GetAllFlags()345 absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags() {
346 absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> res;
347 flags_internal::ForEachFlag([&](CommandLineFlag& flag) {
348 if (!flag.IsRetired()) res.insert({flag.Name(), &flag});
349 });
350 return res;
351 }
352
353 ABSL_NAMESPACE_END
354 } // namespace absl
355