• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Abseil Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/status/internal/status_internal.h"
16 
17 #include <atomic>
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstdio>
22 #include <cstring>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 
27 #include "absl/base/attributes.h"
28 #include "absl/base/config.h"
29 #include "absl/base/macros.h"
30 #include "absl/debugging/stacktrace.h"
31 #include "absl/debugging/symbolize.h"
32 #include "absl/memory/memory.h"
33 #include "absl/status/status.h"
34 #include "absl/status/status_payload_printer.h"
35 #include "absl/strings/cord.h"
36 #include "absl/strings/escaping.h"
37 #include "absl/strings/str_cat.h"
38 #include "absl/strings/str_format.h"
39 #include "absl/strings/str_split.h"
40 #include "absl/strings/string_view.h"
41 #include "absl/types/optional.h"
42 
43 namespace absl {
44 ABSL_NAMESPACE_BEGIN
45 namespace status_internal {
46 
Unref() const47 void StatusRep::Unref() const {
48   // Fast path: if ref==1, there is no need for a RefCountDec (since
49   // this is the only reference and therefore no other thread is
50   // allowed to be mucking with r).
51   if (ref_.load(std::memory_order_acquire) == 1 ||
52       ref_.fetch_sub(1, std::memory_order_acq_rel) - 1 == 0) {
53     delete this;
54   }
55 }
56 
FindPayloadIndexByUrl(const Payloads * payloads,absl::string_view type_url)57 static absl::optional<size_t> FindPayloadIndexByUrl(
58     const Payloads* payloads, absl::string_view type_url) {
59   if (payloads == nullptr) return absl::nullopt;
60 
61   for (size_t i = 0; i < payloads->size(); ++i) {
62     if ((*payloads)[i].type_url == type_url) return i;
63   }
64 
65   return absl::nullopt;
66 }
67 
GetPayload(absl::string_view type_url) const68 absl::optional<absl::Cord> StatusRep::GetPayload(
69     absl::string_view type_url) const {
70   absl::optional<size_t> index =
71       status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url);
72   if (index.has_value()) return (*payloads_)[index.value()].payload;
73 
74   return absl::nullopt;
75 }
76 
SetPayload(absl::string_view type_url,absl::Cord payload)77 void StatusRep::SetPayload(absl::string_view type_url, absl::Cord payload) {
78   if (payloads_ == nullptr) {
79     payloads_ = absl::make_unique<status_internal::Payloads>();
80   }
81 
82   absl::optional<size_t> index =
83       status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url);
84   if (index.has_value()) {
85     (*payloads_)[index.value()].payload = std::move(payload);
86     return;
87   }
88 
89   payloads_->push_back({std::string(type_url), std::move(payload)});
90 }
91 
ErasePayload(absl::string_view type_url)92 StatusRep::EraseResult StatusRep::ErasePayload(absl::string_view type_url) {
93   absl::optional<size_t> index =
94       status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url);
95   if (!index.has_value()) return {false, Status::PointerToRep(this)};
96   payloads_->erase(payloads_->begin() + index.value());
97   if (payloads_->empty() && message_.empty()) {
98     // Special case: If this can be represented inlined, it MUST be inlined
99     // (== depends on this behavior).
100     EraseResult result = {true, Status::CodeToInlinedRep(code_)};
101     Unref();
102     return result;
103   }
104   return {true, Status::PointerToRep(this)};
105 }
106 
ForEachPayload(absl::FunctionRef<void (absl::string_view,const absl::Cord &)> visitor) const107 void StatusRep::ForEachPayload(
108     absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor)
109     const {
110   if (auto* payloads = payloads_.get()) {
111     bool in_reverse =
112         payloads->size() > 1 && reinterpret_cast<uintptr_t>(payloads) % 13 > 6;
113 
114     for (size_t index = 0; index < payloads->size(); ++index) {
115       const auto& elem =
116           (*payloads)[in_reverse ? payloads->size() - 1 - index : index];
117 
118 #ifdef NDEBUG
119       visitor(elem.type_url, elem.payload);
120 #else
121       // In debug mode invalidate the type url to prevent users from relying on
122       // this string lifetime.
123 
124       // NOLINTNEXTLINE intentional extra conversion to force temporary.
125       visitor(std::string(elem.type_url), elem.payload);
126 #endif  // NDEBUG
127     }
128   }
129 }
130 
ToString(StatusToStringMode mode) const131 std::string StatusRep::ToString(StatusToStringMode mode) const {
132   std::string text;
133   absl::StrAppend(&text, absl::StatusCodeToString(code()), ": ", message());
134 
135   const bool with_payload = (mode & StatusToStringMode::kWithPayload) ==
136                             StatusToStringMode::kWithPayload;
137 
138   if (with_payload) {
139     status_internal::StatusPayloadPrinter printer =
140         status_internal::GetStatusPayloadPrinter();
141     this->ForEachPayload([&](absl::string_view type_url,
142                              const absl::Cord& payload) {
143       absl::optional<std::string> result;
144       if (printer) result = printer(type_url, payload);
145       absl::StrAppend(
146           &text, " [", type_url, "='",
147           result.has_value() ? *result : absl::CHexEscape(std::string(payload)),
148           "']");
149     });
150   }
151 
152   return text;
153 }
154 
operator ==(const StatusRep & other) const155 bool StatusRep::operator==(const StatusRep& other) const {
156   assert(this != &other);
157   if (code_ != other.code_) return false;
158   if (message_ != other.message_) return false;
159   const status_internal::Payloads* this_payloads = payloads_.get();
160   const status_internal::Payloads* other_payloads = other.payloads_.get();
161 
162   const status_internal::Payloads no_payloads;
163   const status_internal::Payloads* larger_payloads =
164       this_payloads ? this_payloads : &no_payloads;
165   const status_internal::Payloads* smaller_payloads =
166       other_payloads ? other_payloads : &no_payloads;
167   if (larger_payloads->size() < smaller_payloads->size()) {
168     std::swap(larger_payloads, smaller_payloads);
169   }
170   if ((larger_payloads->size() - smaller_payloads->size()) > 1) return false;
171   // Payloads can be ordered differently, so we can't just compare payload
172   // vectors.
173   for (const auto& payload : *larger_payloads) {
174 
175     bool found = false;
176     for (const auto& other_payload : *smaller_payloads) {
177       if (payload.type_url == other_payload.type_url) {
178         if (payload.payload != other_payload.payload) {
179           return false;
180         }
181         found = true;
182         break;
183       }
184     }
185     if (!found) return false;
186   }
187   return true;
188 }
189 
CloneAndUnref() const190 StatusRep* StatusRep::CloneAndUnref() const {
191   // Optimization: no need to create a clone if we already have a refcount of 1.
192   if (ref_.load(std::memory_order_acquire) == 1) {
193     // All StatusRep instances are heap allocated and mutable, therefore this
194     // const_cast will never cast away const from a stack instance.
195     //
196     // CloneAndUnref is the only method that doesn't involve an external cast to
197     // get a mutable StatusRep* from the uintptr_t rep stored in Status.
198     return const_cast<StatusRep*>(this);
199   }
200   std::unique_ptr<status_internal::Payloads> payloads;
201   if (payloads_) {
202     payloads = absl::make_unique<status_internal::Payloads>(*payloads_);
203   }
204   auto* new_rep = new StatusRep(code_, message_, std::move(payloads));
205   Unref();
206   return new_rep;
207 }
208 
209 // Convert canonical code to a value known to this binary.
MapToLocalCode(int value)210 absl::StatusCode MapToLocalCode(int value) {
211   absl::StatusCode code = static_cast<absl::StatusCode>(value);
212   switch (code) {
213     case absl::StatusCode::kOk:
214     case absl::StatusCode::kCancelled:
215     case absl::StatusCode::kUnknown:
216     case absl::StatusCode::kInvalidArgument:
217     case absl::StatusCode::kDeadlineExceeded:
218     case absl::StatusCode::kNotFound:
219     case absl::StatusCode::kAlreadyExists:
220     case absl::StatusCode::kPermissionDenied:
221     case absl::StatusCode::kResourceExhausted:
222     case absl::StatusCode::kFailedPrecondition:
223     case absl::StatusCode::kAborted:
224     case absl::StatusCode::kOutOfRange:
225     case absl::StatusCode::kUnimplemented:
226     case absl::StatusCode::kInternal:
227     case absl::StatusCode::kUnavailable:
228     case absl::StatusCode::kDataLoss:
229     case absl::StatusCode::kUnauthenticated:
230       return code;
231     default:
232       return absl::StatusCode::kUnknown;
233   }
234 }
235 
MakeCheckFailString(const absl::Status * status,const char * prefix)236 std::string* MakeCheckFailString(const absl::Status* status,
237                                  const char* prefix) {
238   return new std::string(
239       absl::StrCat(prefix, " (",
240                    status->ToString(StatusToStringMode::kWithEverything), ")"));
241 }
242 
243 }  // namespace status_internal
244 
245 ABSL_NAMESPACE_END
246 }  // namespace absl
247