Use size_t/uintptr_t instead of uintptr_t or uint64_t where appropriate

PiperOrigin-RevId: 332449107
Change-Id: I623c320c7f31bb73b92799dfbeb9a1e8ce0cdb3b
This commit is contained in:
Wiktor Garbacz 2020-09-18 07:44:34 -07:00 committed by Copybara-Service
parent c33f1fb03e
commit f91f843f50
9 changed files with 49 additions and 50 deletions

View File

@ -23,8 +23,8 @@ namespace sapi {
namespace comms { namespace comms {
struct ReallocRequest { struct ReallocRequest {
uint64_t old_addr; uintptr_t old_addr;
uint64_t size; size_t size;
}; };
// Types of TAGs used with Comms channel. // Types of TAGs used with Comms channel.

View File

@ -229,7 +229,7 @@ void HandleCallMsg(const FuncCall& call, FuncRet* ret) {
} }
// Handles requests to allocate memory inside the sandboxee. // Handles requests to allocate memory inside the sandboxee.
void HandleAllocMsg(const uintptr_t size, FuncRet* ret) { void HandleAllocMsg(const size_t size, FuncRet* ret) {
VLOG(1) << "HandleAllocMsg: size=" << size; VLOG(1) << "HandleAllocMsg: size=" << size;
ret->ret_type = v::Type::kPointer; ret->ret_type = v::Type::kPointer;
@ -237,16 +237,15 @@ void HandleAllocMsg(const uintptr_t size, FuncRet* ret) {
// Memory is copied to the pointer using an API that the memory sanitizer // Memory is copied to the pointer using an API that the memory sanitizer
// is blind to (process_vm_writev). Initialize the memory here so that // is blind to (process_vm_writev). Initialize the memory here so that
// the sandboxed code can still be tested with the memory sanitizer. // the sandboxed code can still be tested with the memory sanitizer.
ret->int_val = ret->int_val = reinterpret_cast<uintptr_t>(calloc(1, size));
reinterpret_cast<uintptr_t>(calloc(1, static_cast<size_t>(size)));
#else #else
ret->int_val = reinterpret_cast<uintptr_t>(malloc(static_cast<size_t>(size))); ret->int_val = reinterpret_cast<uintptr_t>(malloc(size));
#endif #endif
ret->success = true; ret->success = true;
} }
// Like HandleAllocMsg(), but handles requests to reallocate memory. // Like HandleAllocMsg(), but handles requests to reallocate memory.
void HandleReallocMsg(uintptr_t ptr, uintptr_t size, FuncRet* ret) { void HandleReallocMsg(uintptr_t ptr, size_t size, FuncRet* ret) {
VLOG(1) << "HandleReallocMsg(" << absl::StrCat(absl::Hex(ptr)) << ", " << size VLOG(1) << "HandleReallocMsg(" << absl::StrCat(absl::Hex(ptr)) << ", " << size
<< ")"; << ")";
#ifdef MEMORY_SANITIZER #ifdef MEMORY_SANITIZER
@ -363,14 +362,13 @@ void ServeRequest(sandbox2::Comms* comms) {
break; break;
case comms::kMsgAllocate: case comms::kMsgAllocate:
VLOG(1) << "Client::kMsgAllocate"; VLOG(1) << "Client::kMsgAllocate";
HandleAllocMsg(BytesAs<uintptr_t>(bytes), &ret); HandleAllocMsg(BytesAs<size_t>(bytes), &ret);
break; break;
case comms::kMsgReallocate: case comms::kMsgReallocate:
VLOG(1) << "Client::kMsgReallocate"; VLOG(1) << "Client::kMsgReallocate";
{ {
auto req = BytesAs<comms::ReallocRequest>(bytes); auto req = BytesAs<comms::ReallocRequest>(bytes);
HandleReallocMsg(static_cast<uintptr_t>(req.old_addr), HandleReallocMsg(req.old_addr, req.size, &ret);
static_cast<uintptr_t>(req.size), &ret);
} }
break; break;
case comms::kMsgFree: case comms::kMsgFree:

View File

@ -134,7 +134,7 @@ TEST(StringopTest, RawStringLength) {
ASSERT_THAT(sandbox.Init(), IsOk()); ASSERT_THAT(sandbox.Init(), IsOk());
StringopApi api(&sandbox); StringopApi api(&sandbox);
SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string()); SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string());
SAPI_ASSERT_OK_AND_ASSIGN(uint64_t len, SAPI_ASSERT_OK_AND_ASSIGN(size_t len,
sandbox.rpc_channel()->Strlen(target_mem_ptr)); sandbox.rpc_channel()->Strlen(target_mem_ptr));
EXPECT_THAT(len, Eq(10)); EXPECT_THAT(len, Eq(10));
} }
@ -144,7 +144,7 @@ TEST(StringopTest, RawStringReading) {
ASSERT_THAT(sandbox.Init(), IsOk()); ASSERT_THAT(sandbox.Init(), IsOk());
StringopApi api(&sandbox); StringopApi api(&sandbox);
SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string()); SAPI_ASSERT_OK_AND_ASSIGN(void* target_mem_ptr, api.get_raw_c_string());
SAPI_ASSERT_OK_AND_ASSIGN(uint64_t len, SAPI_ASSERT_OK_AND_ASSIGN(size_t len,
sandbox.rpc_channel()->Strlen(target_mem_ptr)); sandbox.rpc_channel()->Strlen(target_mem_ptr));
EXPECT_THAT(len, Eq(10)); EXPECT_THAT(len, Eq(10));

View File

@ -37,7 +37,7 @@ absl::Status RPCChannel::Call(const FuncCall& call, uint32_t tag, FuncRet* ret,
absl::StatusOr<FuncRet> RPCChannel::Return(v::Type exp_type) { absl::StatusOr<FuncRet> RPCChannel::Return(v::Type exp_type) {
uint32_t tag; uint32_t tag;
uint64_t len; size_t len;
FuncRet ret; FuncRet ret;
if (!comms_->RecvTLV(&tag, &len, &ret, sizeof(ret))) { if (!comms_->RecvTLV(&tag, &len, &ret, sizeof(ret))) {
return absl::UnavailableError("Receiving TLV value failed"); return absl::UnavailableError("Receiving TLV value failed");
@ -78,9 +78,10 @@ absl::Status RPCChannel::Allocate(size_t size, void** addr) {
absl::Status RPCChannel::Reallocate(void* old_addr, size_t size, absl::Status RPCChannel::Reallocate(void* old_addr, size_t size,
void** new_addr) { void** new_addr) {
absl::MutexLock lock(&mutex_); absl::MutexLock lock(&mutex_);
comms::ReallocRequest req; comms::ReallocRequest req = {
req.old_addr = reinterpret_cast<uint64_t>(old_addr); .old_addr = reinterpret_cast<uintptr_t>(old_addr),
req.size = size; .size = size,
};
if (!comms_->SendTLV(comms::kMsgReallocate, sizeof(comms::ReallocRequest), if (!comms_->SendTLV(comms::kMsgReallocate, sizeof(comms::ReallocRequest),
&req)) { &req)) {
@ -100,7 +101,7 @@ absl::Status RPCChannel::Reallocate(void* old_addr, size_t size,
absl::Status RPCChannel::Free(void* addr) { absl::Status RPCChannel::Free(void* addr) {
absl::MutexLock lock(&mutex_); absl::MutexLock lock(&mutex_);
uint64_t remote = reinterpret_cast<uint64_t>(addr); uintptr_t remote = reinterpret_cast<uintptr_t>(addr);
if (!comms_->SendTLV(comms::kMsgFree, sizeof(remote), &remote)) { if (!comms_->SendTLV(comms::kMsgFree, sizeof(remote), &remote)) {
return absl::UnavailableError("Sending TLV value failed"); return absl::UnavailableError("Sending TLV value failed");
} }
@ -193,7 +194,7 @@ absl::Status RPCChannel::Close(int remote_fd) {
return absl::OkStatus(); return absl::OkStatus();
} }
absl::StatusOr<uint64_t> RPCChannel::Strlen(void* str) { absl::StatusOr<size_t> RPCChannel::Strlen(void* str) {
absl::MutexLock lock(&mutex_); absl::MutexLock lock(&mutex_);
if (!comms_->SendTLV(comms::kMsgStrlen, sizeof(str), &str)) { if (!comms_->SendTLV(comms::kMsgStrlen, sizeof(str), &str)) {
return absl::UnavailableError("Sending TLV value failed"); return absl::UnavailableError("Sending TLV value failed");

View File

@ -61,7 +61,7 @@ class RPCChannel {
absl::Status Close(int remote_fd); absl::Status Close(int remote_fd);
// Returns length of a null-terminated c-style string (invokes strlen). // Returns length of a null-terminated c-style string (invokes strlen).
absl::StatusOr<uint64_t> Strlen(void* str); absl::StatusOr<size_t> Strlen(void* str);
sandbox2::Comms* comms() const { return comms_; } sandbox2::Comms* comms() const { return comms_; }

View File

@ -394,7 +394,7 @@ absl::Status Sandbox::TransferFromSandboxee(v::Var* var) {
} }
absl::StatusOr<std::string> Sandbox::GetCString(const v::RemotePtr& str, absl::StatusOr<std::string> Sandbox::GetCString(const v::RemotePtr& str,
uint64_t max_length) { size_t max_length) {
if (!is_active()) { if (!is_active()) {
return absl::UnavailableError("Sandbox not active"); return absl::UnavailableError("Sandbox not active");
} }

View File

@ -103,8 +103,8 @@ class Sandbox {
absl::Status TransferFromSandboxee(v::Var* var); absl::Status TransferFromSandboxee(v::Var* var);
absl::StatusOr<std::string> GetCString(const v::RemotePtr& str, absl::StatusOr<std::string> GetCString(const v::RemotePtr& str,
uint64_t max_length = 10ULL size_t max_length = 10ULL
<< 20 /* 10 MiB*/ << 20 /* 10 MiB*/
); );
// Waits until the sandbox terminated and returns the result. // Waits until the sandbox terminated and returns the result.

View File

@ -228,7 +228,7 @@ void Comms::Terminate() {
} }
} }
bool Comms::SendTLV(uint32_t tag, uint64_t length, const void* value) { bool Comms::SendTLV(uint32_t tag, size_t length, const void* value) {
if (length > GetMaxMsgSize()) { if (length > GetMaxMsgSize()) {
SAPI_RAW_LOG(ERROR, "Maximum TLV message size exceeded: (%u > %u)", length, SAPI_RAW_LOG(ERROR, "Maximum TLV message size exceeded: (%u > %u)", length,
GetMaxMsgSize()); GetMaxMsgSize());
@ -296,7 +296,7 @@ bool Comms::RecvBytes(std::vector<uint8_t>* buffer) {
return true; return true;
} }
bool Comms::SendBytes(const uint8_t* v, uint64_t len) { bool Comms::SendBytes(const uint8_t* v, size_t len) {
return SendTLV(kTagBytes, len, v); return SendTLV(kTagBytes, len, v);
} }
@ -417,7 +417,7 @@ bool Comms::SendFD(int fd) {
int* fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); int* fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
fds[0] = fd; fds[0] = fd;
InternalTLV tlv = {kTagFd, sizeof(tlv.val), 0}; InternalTLV tlv = {kTagFd, 0};
iovec iov; iovec iov;
iov.iov_base = &tlv; iov.iov_base = &tlv;
@ -512,8 +512,8 @@ socklen_t Comms::CreateSockaddrUn(sockaddr_un* sun) {
return slen; return slen;
} }
bool Comms::Send(const void* data, uint64_t len) { bool Comms::Send(const void* data, size_t len) {
uint64_t total_sent = 0; size_t total_sent = 0;
const char* bytes = reinterpret_cast<const char*>(data); const char* bytes = reinterpret_cast<const char*>(data);
const auto op = [bytes, len, &total_sent](int fd) -> ssize_t { const auto op = [bytes, len, &total_sent](int fd) -> ssize_t {
PotentiallyBlockingRegion region; PotentiallyBlockingRegion region;
@ -545,8 +545,8 @@ bool Comms::Send(const void* data, uint64_t len) {
return true; return true;
} }
bool Comms::Recv(void* data, uint64_t len) { bool Comms::Recv(void* data, size_t len) {
uint64_t total_recv = 0; size_t total_recv = 0;
char* bytes = reinterpret_cast<char*>(data); char* bytes = reinterpret_cast<char*>(data);
const auto op = [bytes, len, &total_recv](int fd) -> ssize_t { const auto op = [bytes, len, &total_recv](int fd) -> ssize_t {
PotentiallyBlockingRegion region; PotentiallyBlockingRegion region;
@ -574,7 +574,7 @@ bool Comms::Recv(void* data, uint64_t len) {
} }
// Internal helper method (low level). // Internal helper method (low level).
bool Comms::RecvTL(uint32_t* tag, uint64_t* length) { bool Comms::RecvTL(uint32_t* tag, size_t* length) {
if (!Recv(reinterpret_cast<uint8_t*>(tag), sizeof(*tag))) { if (!Recv(reinterpret_cast<uint8_t*>(tag), sizeof(*tag))) {
return false; return false;
} }
@ -611,7 +611,7 @@ bool Comms::RecvTLV(uint32_t* tag, std::string* value) {
template <typename T> template <typename T>
bool Comms::RecvTLVGeneric(uint32_t* tag, T* value) { bool Comms::RecvTLVGeneric(uint32_t* tag, T* value) {
absl::MutexLock lock(&tlv_recv_transmission_mutex_); absl::MutexLock lock(&tlv_recv_transmission_mutex_);
uint64_t length; size_t length;
if (!RecvTL(tag, &length)) { if (!RecvTL(tag, &length)) {
return false; return false;
} }
@ -620,28 +620,29 @@ bool Comms::RecvTLVGeneric(uint32_t* tag, T* value) {
return length == 0 || Recv(reinterpret_cast<uint8_t*>(value->data()), length); return length == 0 || Recv(reinterpret_cast<uint8_t*>(value->data()), length);
} }
bool Comms::RecvTLV(uint32_t* tag, uint64_t* length, void* buffer, bool Comms::RecvTLV(uint32_t* tag, size_t* length, void* buffer,
uint64_t buffer_size) { size_t buffer_size) {
absl::MutexLock lock(&tlv_recv_transmission_mutex_); absl::MutexLock lock(&tlv_recv_transmission_mutex_);
if (!RecvTL(tag, length)) { if (!RecvTL(tag, length)) {
return false; return false;
} }
if (*length == 0) {
return true;
}
if (*length > buffer_size) { if (*length > buffer_size) {
SAPI_RAW_LOG(ERROR, "Buffer size too small (0x%x > 0x%x)", *length, SAPI_RAW_LOG(ERROR, "Buffer size too small (0x%x > 0x%x)", *length,
buffer_size); buffer_size);
return false; return false;
} else if (*length > 0) {
if (!Recv(reinterpret_cast<uint8_t*>(buffer), *length)) {
return false;
}
} }
return true;
return Recv(reinterpret_cast<uint8_t*>(buffer), *length);
} }
bool Comms::RecvInt(void* buffer, uint64_t len, uint32_t tag) { bool Comms::RecvInt(void* buffer, size_t len, uint32_t tag) {
uint32_t received_tag; uint32_t received_tag;
uint64_t received_length; size_t received_length;
if (!RecvTLV(&received_tag, &received_length, buffer, len)) { if (!RecvTLV(&received_tag, &received_length, buffer, len)) {
return false; return false;
} }

View File

@ -60,7 +60,7 @@ class Comms {
static constexpr uint32_t kTagFd = 0X80000201; static constexpr uint32_t kTagFd = 0X80000201;
// Any payload size above this limit will LOG(WARNING). // Any payload size above this limit will LOG(WARNING).
static constexpr uint64_t kWarnMsgSize = (256ULL << 20); static constexpr size_t kWarnMsgSize = (256ULL << 20);
// Sandbox2-specific convention where FD=1023 is always passed to the // Sandbox2-specific convention where FD=1023 is always passed to the
// sandboxed process as a communication channel (encapsulated in the // sandboxed process as a communication channel (encapsulated in the
@ -103,9 +103,9 @@ class Comms {
// Note: The actual size is "unlimited", although the Buffer API is more // Note: The actual size is "unlimited", although the Buffer API is more
// efficient for large transfers. There is an arbitrary limit to ~2GiB to // efficient for large transfers. There is an arbitrary limit to ~2GiB to
// avoid protobuf serialization issues. // avoid protobuf serialization issues.
uint64_t GetMaxMsgSize() const { return std::numeric_limits<int32_t>::max(); } size_t GetMaxMsgSize() const { return std::numeric_limits<int32_t>::max(); }
bool SendTLV(uint32_t tag, uint64_t length, const void* value); bool SendTLV(uint32_t tag, size_t length, const void* value);
// Receive a TLV structure, the memory for the value will be allocated // Receive a TLV structure, the memory for the value will be allocated
// by std::vector. // by std::vector.
bool RecvTLV(uint32_t* tag, std::vector<uint8_t>* value); bool RecvTLV(uint32_t* tag, std::vector<uint8_t>* value);
@ -113,7 +113,7 @@ class Comms {
// by std::string. // by std::string.
bool RecvTLV(uint32_t* tag, std::string* value); bool RecvTLV(uint32_t* tag, std::string* value);
// Receives a TLV value into a specified buffer without allocating memory. // Receives a TLV value into a specified buffer without allocating memory.
bool RecvTLV(uint32_t* tag, uint64_t* length, void* buffer, uint64_t buffer_size); bool RecvTLV(uint32_t* tag, size_t* length, void* buffer, size_t buffer_size);
// Sends/receives various types of data. // Sends/receives various types of data.
bool RecvUint8(uint8_t* v) { return RecvIntGeneric(v, kTagUint8); } bool RecvUint8(uint8_t* v) { return RecvIntGeneric(v, kTagUint8); }
@ -138,7 +138,7 @@ class Comms {
bool SendString(const std::string& v); bool SendString(const std::string& v);
bool RecvBytes(std::vector<uint8_t>* buffer); bool RecvBytes(std::vector<uint8_t>* buffer);
bool SendBytes(const uint8_t* v, uint64_t len); bool SendBytes(const uint8_t* v, size_t len);
bool SendBytes(const std::vector<uint8_t>& buffer); bool SendBytes(const std::vector<uint8_t>& buffer);
// Receives remote process credentials. // Receives remote process credentials.
@ -183,19 +183,18 @@ class Comms {
struct ABSL_ATTRIBUTE_PACKED InternalTLV { struct ABSL_ATTRIBUTE_PACKED InternalTLV {
uint32_t tag; uint32_t tag;
uint32_t len; uint32_t len;
uint64_t val;
}; };
// Fills sockaddr_un struct with proper values. // Fills sockaddr_un struct with proper values.
socklen_t CreateSockaddrUn(sockaddr_un* sun); socklen_t CreateSockaddrUn(sockaddr_un* sun);
// Support for EINTR and size completion. // Support for EINTR and size completion.
bool Send(const void* data, uint64_t len); bool Send(const void* data, size_t len);
bool Recv(void* data, uint64_t len); bool Recv(void* data, size_t len);
// Receives tag and length. Assumes that the `tlv_transmission_mutex_` mutex // Receives tag and length. Assumes that the `tlv_transmission_mutex_` mutex
// is locked. // is locked.
bool RecvTL(uint32_t* tag, uint64_t* length) bool RecvTL(uint32_t* tag, size_t* length)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(tlv_recv_transmission_mutex_); ABSL_EXCLUSIVE_LOCKS_REQUIRED(tlv_recv_transmission_mutex_);
// T has to be a ContiguousContainer // T has to be a ContiguousContainer
@ -203,7 +202,7 @@ class Comms {
bool RecvTLVGeneric(uint32_t* tag, T* value); bool RecvTLVGeneric(uint32_t* tag, T* value);
// Receives arbitrary integers. // Receives arbitrary integers.
bool RecvInt(void* buffer, uint64_t len, uint32_t tag); bool RecvInt(void* buffer, size_t len, uint32_t tag);
template <typename T> template <typename T>
bool RecvIntGeneric(T* output, uint32_t tag) { bool RecvIntGeneric(T* output, uint32_t tag) {