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
Unref() const48 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 if (ref_.load(std::memory_order_acquire) == 1 ||
53 ref_.fetch_sub(1, std::memory_order_acq_rel) - 1 == 0) {
54 delete this;
55 }
56 }
57
FindPayloadIndexByUrl(const Payloads * payloads,absl::string_view type_url)58 static absl::optional<size_t> FindPayloadIndexByUrl(
59 const Payloads* payloads, absl::string_view type_url) {
60 if (payloads == nullptr) return absl::nullopt;
61
62 for (size_t i = 0; i < payloads->size(); ++i) {
63 if ((*payloads)[i].type_url == type_url) return i;
64 }
65
66 return absl::nullopt;
67 }
68
GetPayload(absl::string_view type_url) const69 absl::optional<absl::Cord> StatusRep::GetPayload(
70 absl::string_view type_url) const {
71 absl::optional<size_t> index =
72 status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url);
73 if (index.has_value()) return (*payloads_)[index.value()].payload;
74
75 return absl::nullopt;
76 }
77
SetPayload(absl::string_view type_url,absl::Cord payload)78 void StatusRep::SetPayload(absl::string_view type_url, absl::Cord payload) {
79 if (payloads_ == nullptr) {
80 payloads_ = absl::make_unique<status_internal::Payloads>();
81 }
82
83 absl::optional<size_t> index =
84 status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url);
85 if (index.has_value()) {
86 (*payloads_)[index.value()].payload = std::move(payload);
87 return;
88 }
89
90 payloads_->push_back({std::string(type_url), std::move(payload)});
91 }
92
ErasePayload(absl::string_view type_url)93 StatusRep::EraseResult StatusRep::ErasePayload(absl::string_view type_url) {
94 absl::optional<size_t> index =
95 status_internal::FindPayloadIndexByUrl(payloads_.get(), type_url);
96 if (!index.has_value()) return {false, Status::PointerToRep(this)};
97 payloads_->erase(payloads_->begin() + index.value());
98 if (payloads_->empty() && message_.empty()) {
99 // Special case: If this can be represented inlined, it MUST be inlined
100 // (== depends on this behavior).
101 EraseResult result = {true, Status::CodeToInlinedRep(code_)};
102 Unref();
103 return result;
104 }
105 return {true, Status::PointerToRep(this)};
106 }
107
ForEachPayload(absl::FunctionRef<void (absl::string_view,const absl::Cord &)> visitor) const108 void StatusRep::ForEachPayload(
109 absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor)
110 const {
111 if (auto* payloads = payloads_.get()) {
112 bool in_reverse =
113 payloads->size() > 1 && reinterpret_cast<uintptr_t>(payloads) % 13 > 6;
114
115 for (size_t index = 0; index < payloads->size(); ++index) {
116 const auto& elem =
117 (*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 visitor(std::string(elem.type_url), elem.payload);
127 #endif // NDEBUG
128 }
129 }
130 }
131
ToString(StatusToStringMode mode) const132 std::string StatusRep::ToString(StatusToStringMode mode) const {
133 std::string text;
134 absl::StrAppend(&text, absl::StatusCodeToString(code()), ": ", message());
135
136 const bool with_payload = (mode & StatusToStringMode::kWithPayload) ==
137 StatusToStringMode::kWithPayload;
138
139 if (with_payload) {
140 status_internal::StatusPayloadPrinter printer =
141 status_internal::GetStatusPayloadPrinter();
142 this->ForEachPayload([&](absl::string_view type_url,
143 const absl::Cord& payload) {
144 absl::optional<std::string> result;
145 if (printer) result = printer(type_url, payload);
146 absl::StrAppend(
147 &text, " [", type_url, "='",
148 result.has_value() ? *result : absl::CHexEscape(std::string(payload)),
149 "']");
150 });
151 }
152
153 return text;
154 }
155
operator ==(const StatusRep & other) const156 bool StatusRep::operator==(const StatusRep& other) const {
157 assert(this != &other);
158 if (code_ != other.code_) return false;
159 if (message_ != other.message_) return false;
160 const status_internal::Payloads* this_payloads = payloads_.get();
161 const status_internal::Payloads* other_payloads = other.payloads_.get();
162
163 const status_internal::Payloads no_payloads;
164 const status_internal::Payloads* larger_payloads =
165 this_payloads ? this_payloads : &no_payloads;
166 const status_internal::Payloads* smaller_payloads =
167 other_payloads ? other_payloads : &no_payloads;
168 if (larger_payloads->size() < smaller_payloads->size()) {
169 std::swap(larger_payloads, smaller_payloads);
170 }
171 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 for (const auto& payload : *larger_payloads) {
175
176 bool found = false;
177 for (const auto& other_payload : *smaller_payloads) {
178 if (payload.type_url == other_payload.type_url) {
179 if (payload.payload != other_payload.payload) {
180 return false;
181 }
182 found = true;
183 break;
184 }
185 }
186 if (!found) return false;
187 }
188 return true;
189 }
190
CloneAndUnref() const191 absl::Nonnull<StatusRep*> StatusRep::CloneAndUnref() const {
192 // Optimization: no need to create a clone if we already have a refcount of 1.
193 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 return const_cast<StatusRep*>(this);
200 }
201 std::unique_ptr<status_internal::Payloads> payloads;
202 if (payloads_) {
203 payloads = absl::make_unique<status_internal::Payloads>(*payloads_);
204 }
205 auto* new_rep = new StatusRep(code_, message_, std::move(payloads));
206 Unref();
207 return new_rep;
208 }
209
210 // Convert canonical code to a value known to this binary.
MapToLocalCode(int value)211 absl::StatusCode MapToLocalCode(int value) {
212 absl::StatusCode code = static_cast<absl::StatusCode>(value);
213 switch (code) {
214 case absl::StatusCode::kOk:
215 case absl::StatusCode::kCancelled:
216 case absl::StatusCode::kUnknown:
217 case absl::StatusCode::kInvalidArgument:
218 case absl::StatusCode::kDeadlineExceeded:
219 case absl::StatusCode::kNotFound:
220 case absl::StatusCode::kAlreadyExists:
221 case absl::StatusCode::kPermissionDenied:
222 case absl::StatusCode::kResourceExhausted:
223 case absl::StatusCode::kFailedPrecondition:
224 case absl::StatusCode::kAborted:
225 case absl::StatusCode::kOutOfRange:
226 case absl::StatusCode::kUnimplemented:
227 case absl::StatusCode::kInternal:
228 case absl::StatusCode::kUnavailable:
229 case absl::StatusCode::kDataLoss:
230 case absl::StatusCode::kUnauthenticated:
231 return code;
232 default:
233 return absl::StatusCode::kUnknown;
234 }
235 }
236
MakeCheckFailString(absl::Nonnull<const absl::Status * > status,absl::Nonnull<const char * > prefix)237 absl::Nonnull<std::string*> MakeCheckFailString(
238 absl::Nonnull<const absl::Status*> status,
239 absl::Nonnull<const char*> prefix) {
240 return new std::string(
241 absl::StrCat(prefix, " (",
242 status->ToString(StatusToStringMode::kWithEverything), ")"));
243 }
244
245 } // namespace status_internal
246
247 ABSL_NAMESPACE_END
248 } // namespace absl
249