Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

status: Fix ASAN error in Status payload handling #10906

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bazel/repository_locations.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ REPOSITORY_LOCATIONS = dict(
urls = ["https://commondatastorage.googleapis.com/chromium-boringssl-docs/fips/boringssl-66005f41fbc3529ffe8d007708756720529da20d.tar.xz"],
),
com_google_absl = dict(
sha256 = "2693730730247afb0e7cb2d41664ac2af3ad75c79944efd266be40ba944179b9",
strip_prefix = "abseil-cpp-06f0e767d13d4d68071c4fc51e25724e0fc8bc74",
# 2020-03-03
urls = ["https://github.com/abseil/abseil-cpp/archive/06f0e767d13d4d68071c4fc51e25724e0fc8bc74.tar.gz"],
sha256 = "14ee08e2089c2a9b6bf27e1d10abc5629c69c4d0bab4b78ec5b65a29ea1c2af7",
strip_prefix = "abseil-cpp-cf3a1998e9d41709d4141e2f13375993cba1130e",
# 2020-03-05
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to bump up to most recent export?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to push this fix as quickly as possible and want to minimize the amount of changes to absl to avoid dealing with any gremlins.

urls = ["https://github.com/abseil/abseil-cpp/archive/cf3a1998e9d41709d4141e2f13375993cba1130e.tar.gz"],
),
com_github_apache_thrift = dict(
sha256 = "7d59ac4fdcb2c58037ebd4a9da5f9a49e3e034bf75b3f26d9fe48ba3d8806e6b",
Expand Down
36 changes: 23 additions & 13 deletions source/common/http/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@ struct PrematureResponsePayload : public EnvoyStatusPayload {
};

template <typename T> void storePayload(absl::Status& status, const T& payload) {
status.SetPayload(
EnvoyPayloadUrl,
absl::Cord(absl::string_view(reinterpret_cast<const char*>(&payload), sizeof(payload))));
absl::Cord cord(absl::string_view(reinterpret_cast<const char*>(&payload), sizeof(payload)));
cord.Flatten(); // Flatten ahead of time for easier access later.
status.SetPayload(EnvoyPayloadUrl, std::move(cord));
}

template <typename T = EnvoyStatusPayload> const T* getPayload(const absl::Status& status) {
auto payload = status.GetPayload(EnvoyPayloadUrl);
ASSERT(payload.has_value(), "Must have payload");
auto data = payload.value().Flatten();
ASSERT(data.length() >= sizeof(T), "Invalid payload length");
return reinterpret_cast<const T*>(data.data());
template <typename T = EnvoyStatusPayload> const T& getPayload(const absl::Status& status) {
// The only way to get a reference to the payload owned by the absl::Status is through the
// ForEachPayload method. All other methods create a copy of the payload, which is not convenient
// for peeking at the payload value.
const T* payload = nullptr;
status.ForEachPayload([&payload](absl::string_view url, const absl::Cord& cord) {
if (url == EnvoyPayloadUrl) {
ASSERT(!payload); // Status API guarantees to have one payload with given URL
auto data = cord.TryFlat();
ASSERT(data.has_value()); // EnvoyPayloadUrl cords are flattened ahead of time
ASSERT(data.value().length() >= sizeof(T), "Invalid payload length");
payload = reinterpret_cast<const T*>(data.value().data());
}
});
ASSERT(payload);
return *payload;
}

} // namespace
Expand Down Expand Up @@ -94,7 +104,7 @@ Status codecClientError(absl::string_view message) {

// Methods for checking and extracting error information
StatusCode getStatusCode(const Status& status) {
return status.ok() ? StatusCode::Ok : getPayload(status)->status_code_;
return status.ok() ? StatusCode::Ok : getPayload(status).status_code_;
}

bool isCodecProtocolError(const Status& status) {
Expand All @@ -110,10 +120,10 @@ bool isPrematureResponseError(const Status& status) {
}

Http::Code getPrematureResponseHttpCode(const Status& status) {
const auto* payload = getPayload<PrematureResponsePayload>(status);
ASSERT(payload->status_code_ == StatusCode::PrematureResponseError,
const auto& payload = getPayload<PrematureResponsePayload>(status);
ASSERT(payload.status_code_ == StatusCode::PrematureResponseError,
"Must be PrematureResponseError");
return payload->http_code_;
return payload.http_code_;
}

bool isCodecClientError(const Status& status) {
Expand Down