Skip to content

Commit

Permalink
Better error message on mismatching Send/Recv
Browse files Browse the repository at this point in the history
Report tag mismatch before reporting length mismatch.

PiperOrigin-RevId: 662084983
Change-Id: Ie1c8fdca5488a2ddbbcc2ba2100b42de65959843
  • Loading branch information
happyCoder92 authored and copybara-github committed Aug 12, 2024
1 parent e5c5bdb commit 5954e8e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
7 changes: 1 addition & 6 deletions sandboxed_api/rpcchannel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,9 @@ absl::StatusOr<FuncRet> RPCChannel::Return(v::Type exp_type) {
uint32_t tag;
size_t len;
FuncRet ret;
if (!comms_->RecvTLV(&tag, &len, &ret, sizeof(ret))) {
if (!comms_->RecvTLV(&tag, &len, &ret, sizeof(ret), comms::kMsgReturn)) {
return absl::UnavailableError("Receiving TLV value failed");
}
if (tag != comms::kMsgReturn) {
LOG(ERROR) << "tag != comms::kMsgReturn (" << absl::StrCat(absl::Hex(tag))
<< " != " << absl::StrCat(absl::Hex(comms::kMsgReturn)) << ")";
return absl::UnavailableError("Received TLV has incorrect tag");
}
if (len != sizeof(FuncRet)) {
LOG(ERROR) << "len != sizeof(FuncReturn) (" << len
<< " != " << sizeof(FuncRet) << ")";
Expand Down
14 changes: 8 additions & 6 deletions sandboxed_api/sandbox2/comms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <cstdlib>
#include <cstring>
#include <memory>
#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -618,11 +619,16 @@ bool Comms::RecvTLVGeneric(uint32_t* tag, T* value) {
}

bool Comms::RecvTLV(uint32_t* tag, size_t* length, void* buffer,
size_t buffer_size) {
size_t buffer_size, std::optional<uint32_t> expected_tag) {
if (!RecvTL(tag, length)) {
return false;
}

if (expected_tag.has_value() && *tag != *expected_tag) {
SAPI_RAW_LOG(ERROR, "Expected tag: 0x%08x, got: 0x%x", *expected_tag, *tag);
return false;
}

if (*length == 0) {
return true;
}
Expand All @@ -639,14 +645,10 @@ bool Comms::RecvTLV(uint32_t* tag, size_t* length, void* buffer,
bool Comms::RecvInt(void* buffer, size_t len, uint32_t tag) {
uint32_t received_tag;
size_t received_length;
if (!RecvTLV(&received_tag, &received_length, buffer, len)) {
if (!RecvTLV(&received_tag, &received_length, buffer, len, tag)) {
return false;
}

if (received_tag != tag) {
SAPI_RAW_LOG(ERROR, "Expected tag: 0x%08x, got: 0x%x", tag, received_tag);
return false;
}
if (received_length != len) {
SAPI_RAW_LOG(ERROR, "Expected length: %zu, got: %zu", len, received_length);
return false;
Expand Down
4 changes: 3 additions & 1 deletion sandboxed_api/sandbox2/comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <variant>
Expand Down Expand Up @@ -144,7 +145,8 @@ class Comms {
// by std::string.
bool RecvTLV(uint32_t* tag, std::string* value);
// Receives a TLV value into a specified buffer without allocating memory.
bool RecvTLV(uint32_t* tag, size_t* length, void* buffer, size_t buffer_size);
bool RecvTLV(uint32_t* tag, size_t* length, void* buffer, size_t buffer_size,
std::optional<uint32_t> expected_tag = std::nullopt);

// Sends/receives various types of data.
bool RecvUint8(uint8_t* v) { return RecvIntGeneric(v, kTagUint8); }
Expand Down

0 comments on commit 5954e8e

Please sign in to comment.