/src/abseil-cpp/absl/status/internal/status_internal.cc
Line | Count | Source |
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/base/nullability.h" |
31 | | #include "absl/debugging/stacktrace.h" |
32 | | #include "absl/debugging/symbolize.h" |
33 | | #include "absl/memory/memory.h" |
34 | | #include "absl/status/status.h" |
35 | | #include "absl/status/status_payload_printer.h" |
36 | | #include "absl/strings/cord.h" |
37 | | #include "absl/strings/escaping.h" |
38 | | #include "absl/strings/str_cat.h" |
39 | | #include "absl/strings/str_format.h" |
40 | | #include "absl/strings/str_split.h" |
41 | | #include "absl/strings/string_view.h" |
42 | | #include "absl/types/optional.h" |
43 | | |
44 | | namespace absl { |
45 | | ABSL_NAMESPACE_BEGIN |
46 | | namespace status_internal { |
47 | | |
48 | 644k | void StatusRep::Unref() const { |
49 | | // Fast path: if ref==1, there is no need for a RefCountDec (since |
50 | | // this is the only reference and therefore no other thread is |
51 | | // allowed to be mucking with r). |
52 | 644k | if (ref_.load(std::memory_order_acquire) == 1 || |
53 | 429k | ref_.fetch_sub(1, std::memory_order_acq_rel) - 1 == 0) { |
54 | 429k | delete this; |
55 | 429k | } |
56 | 644k | } |
57 | | |
58 | | static absl::optional<size_t> FindPayloadIndexByUrl( |
59 | 0 | const Payloads* payloads, absl::string_view type_url) { |
60 | 0 | if (payloads == nullptr) return absl::nullopt; |
61 | | |
62 | 0 | for (size_t i = 0; i < payloads->size(); ++i) { |
63 | 0 | if ((*payloads)[i].type_url == type_url) return i; |
64 | 0 | } |
65 | | |
66 | 0 | return absl::nullopt; |
67 | 0 | } |
68 | | |
69 | | absl::optional<absl::Cord> StatusRep::GetPayload( |
70 | 0 | absl::string_view type_url) const { |
71 | 0 | absl::optional<size_t> index = |
72 | 0 | status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url); |
73 | 0 | if (index.has_value()) return (*payloads_)[index.value()].payload; |
74 | | |
75 | 0 | return absl::nullopt; |
76 | 0 | } |
77 | | |
78 | 0 | void StatusRep::SetPayload(absl::string_view type_url, absl::Cord payload) { |
79 | 0 | if (payloads_ == nullptr) { |
80 | 0 | payloads_ = absl::make_unique<status_internal::Payloads>(); |
81 | 0 | } |
82 | |
|
83 | 0 | absl::optional<size_t> index = |
84 | 0 | status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url); |
85 | 0 | if (index.has_value()) { |
86 | 0 | (*payloads_)[index.value()].payload = std::move(payload); |
87 | 0 | return; |
88 | 0 | } |
89 | | |
90 | 0 | payloads_->push_back({std::string(type_url), std::move(payload)}); |
91 | 0 | } |
92 | | |
93 | 0 | StatusRep::EraseResult StatusRep::ErasePayload(absl::string_view type_url) { |
94 | 0 | absl::optional<size_t> index = |
95 | 0 | status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url); |
96 | 0 | if (!index.has_value()) return {false, Status::PointerToRep(this)}; |
97 | 0 | payloads_->erase(payloads_->begin() + index.value()); |
98 | 0 | if (payloads_->empty() && message_.empty()) { |
99 | | // Special case: If this can be represented inlined, it MUST be inlined |
100 | | // (== depends on this behavior). |
101 | 0 | EraseResult result = {true, Status::CodeToInlinedRep(code_)}; |
102 | 0 | Unref(); |
103 | 0 | return result; |
104 | 0 | } |
105 | 0 | return {true, Status::PointerToRep(this)}; |
106 | 0 | } |
107 | | |
108 | | void StatusRep::ForEachPayload( |
109 | | absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor) |
110 | 0 | const { |
111 | 0 | if (auto* payloads = payloads_.get()) { |
112 | 0 | bool in_reverse = |
113 | 0 | payloads->size() > 1 && reinterpret_cast<uintptr_t>(payloads) % 13 > 6; |
114 | |
|
115 | 0 | for (size_t index = 0; index < payloads->size(); ++index) { |
116 | 0 | const auto& elem = |
117 | 0 | (*payloads)[in_reverse ? payloads->size() - 1 - index : index]; |
118 | |
|
119 | | #ifdef NDEBUG |
120 | | visitor(elem.type_url, elem.payload); |
121 | | #else |
122 | | // In debug mode invalidate the type url to prevent users from relying on |
123 | | // this string lifetime. |
124 | | |
125 | | // NOLINTNEXTLINE intentional extra conversion to force temporary. |
126 | 0 | visitor(std::string(elem.type_url), elem.payload); |
127 | 0 | #endif // NDEBUG |
128 | 0 | } |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | 0 | std::string StatusRep::ToString(StatusToStringMode mode) const { |
133 | 0 | std::string text; |
134 | 0 | absl::StrAppend(&text, absl::StatusCodeToString(code()), ": ", message()); |
135 | |
|
136 | 0 | const bool with_payload = (mode & StatusToStringMode::kWithPayload) == |
137 | 0 | StatusToStringMode::kWithPayload; |
138 | |
|
139 | 0 | if (with_payload) { |
140 | 0 | status_internal::StatusPayloadPrinter printer = |
141 | 0 | status_internal::GetStatusPayloadPrinter(); |
142 | 0 | this->ForEachPayload([&](absl::string_view type_url, |
143 | 0 | const absl::Cord& payload) { |
144 | 0 | absl::optional<std::string> result; |
145 | 0 | if (printer) result = printer(type_url, payload); |
146 | 0 | absl::StrAppend( |
147 | 0 | &text, " [", type_url, "='", |
148 | 0 | result.has_value() ? *result : absl::CHexEscape(std::string(payload)), |
149 | 0 | "']"); |
150 | 0 | }); |
151 | 0 | } |
152 | |
|
153 | 0 | return text; |
154 | 0 | } |
155 | | |
156 | 0 | bool StatusRep::operator==(const StatusRep& other) const { |
157 | 0 | assert(this != &other); |
158 | 0 | if (code_ != other.code_) return false; |
159 | 0 | if (message_ != other.message_) return false; |
160 | 0 | const status_internal::Payloads* this_payloads = payloads_.get(); |
161 | 0 | const status_internal::Payloads* other_payloads = other.payloads_.get(); |
162 | |
|
163 | 0 | const status_internal::Payloads no_payloads; |
164 | 0 | const status_internal::Payloads* larger_payloads = |
165 | 0 | this_payloads ? this_payloads : &no_payloads; |
166 | 0 | const status_internal::Payloads* smaller_payloads = |
167 | 0 | other_payloads ? other_payloads : &no_payloads; |
168 | 0 | if (larger_payloads->size() < smaller_payloads->size()) { |
169 | 0 | std::swap(larger_payloads, smaller_payloads); |
170 | 0 | } |
171 | 0 | if ((larger_payloads->size() - smaller_payloads->size()) > 1) return false; |
172 | | // Payloads can be ordered differently, so we can't just compare payload |
173 | | // vectors. |
174 | 0 | for (const auto& payload : *larger_payloads) { |
175 | |
|
176 | 0 | bool found = false; |
177 | 0 | for (const auto& other_payload : *smaller_payloads) { |
178 | 0 | if (payload.type_url == other_payload.type_url) { |
179 | 0 | if (payload.payload != other_payload.payload) { |
180 | 0 | return false; |
181 | 0 | } |
182 | 0 | found = true; |
183 | 0 | break; |
184 | 0 | } |
185 | 0 | } |
186 | 0 | if (!found) return false; |
187 | 0 | } |
188 | 0 | return true; |
189 | 0 | } |
190 | | |
191 | 0 | absl::Nonnull<StatusRep*> StatusRep::CloneAndUnref() const { |
192 | | // Optimization: no need to create a clone if we already have a refcount of 1. |
193 | 0 | if (ref_.load(std::memory_order_acquire) == 1) { |
194 | | // All StatusRep instances are heap allocated and mutable, therefore this |
195 | | // const_cast will never cast away const from a stack instance. |
196 | | // |
197 | | // CloneAndUnref is the only method that doesn't involve an external cast to |
198 | | // get a mutable StatusRep* from the uintptr_t rep stored in Status. |
199 | 0 | return const_cast<StatusRep*>(this); |
200 | 0 | } |
201 | 0 | std::unique_ptr<status_internal::Payloads> payloads; |
202 | 0 | if (payloads_) { |
203 | 0 | payloads = absl::make_unique<status_internal::Payloads>(*payloads_); |
204 | 0 | } |
205 | 0 | auto* new_rep = new StatusRep(code_, message_, std::move(payloads)); |
206 | 0 | Unref(); |
207 | 0 | return new_rep; |
208 | 0 | } |
209 | | |
210 | | // Convert canonical code to a value known to this binary. |
211 | 562k | absl::StatusCode MapToLocalCode(int value) { |
212 | 562k | absl::StatusCode code = static_cast<absl::StatusCode>(value); |
213 | 562k | switch (code) { |
214 | 101k | case absl::StatusCode::kOk: |
215 | 101k | case absl::StatusCode::kCancelled: |
216 | 101k | case absl::StatusCode::kUnknown: |
217 | 282k | case absl::StatusCode::kInvalidArgument: |
218 | 282k | case absl::StatusCode::kDeadlineExceeded: |
219 | 519k | case absl::StatusCode::kNotFound: |
220 | 519k | case absl::StatusCode::kAlreadyExists: |
221 | 519k | case absl::StatusCode::kPermissionDenied: |
222 | 519k | case absl::StatusCode::kResourceExhausted: |
223 | 519k | case absl::StatusCode::kFailedPrecondition: |
224 | 554k | case absl::StatusCode::kAborted: |
225 | 554k | case absl::StatusCode::kOutOfRange: |
226 | 554k | case absl::StatusCode::kUnimplemented: |
227 | 554k | case absl::StatusCode::kInternal: |
228 | 562k | case absl::StatusCode::kUnavailable: |
229 | 562k | case absl::StatusCode::kDataLoss: |
230 | 562k | case absl::StatusCode::kUnauthenticated: |
231 | 562k | return code; |
232 | 0 | default: |
233 | 0 | return absl::StatusCode::kUnknown; |
234 | 562k | } |
235 | 562k | } |
236 | | |
237 | | absl::Nonnull<std::string*> MakeCheckFailString( |
238 | | absl::Nonnull<const absl::Status*> status, |
239 | 0 | absl::Nonnull<const char*> prefix) { |
240 | 0 | return new std::string( |
241 | 0 | absl::StrCat(prefix, " (", |
242 | 0 | status->ToString(StatusToStringMode::kWithEverything), ")")); |
243 | 0 | } |
244 | | |
245 | | } // namespace status_internal |
246 | | |
247 | | ABSL_NAMESPACE_END |
248 | | } // namespace absl |