Skip to content

Commit 28b102b

Browse files
happyCoder92copybara-github
authored andcommitted
MSAN mark memory initialized only when it's actually copied
This reduces performance for MSAN builds in exchange for better sanitizer coverage. PiperOrigin-RevId: 790741800 Change-Id: I05c653a648a86ce92149e9f3085609595f836335
1 parent d7443c2 commit 28b102b

5 files changed

Lines changed: 39 additions & 8 deletions

File tree

sandboxed_api/call.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ constexpr uint32_t kMsgRecvFd = 0x107;
4040
constexpr uint32_t kMsgClose = 0x108;
4141
constexpr uint32_t kMsgReallocate = 0x109;
4242
constexpr uint32_t kMsgStrlen = 0x10A;
43+
constexpr uint32_t kMsgMarkMemoryInit = 0x10B;
4344
// Return:
4445
constexpr uint32_t kMsgReturn = 0x201;
4546

sandboxed_api/client.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,6 @@ void HandleAllocMsg(const size_t size, FuncRet* ret) {
241241
VLOG(1) << "HandleAllocMsg: size=" << size;
242242

243243
const void* allocated = malloc(size);
244-
// Memory is copied to the pointer using an API that the memory sanitizer
245-
// is blind to (process_vm_writev). Mark the memory as initialized here, so
246-
// that the sandboxed code can still be tested using MSAN.
247-
ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(allocated, size);
248244

249245
ret->ret_type = v::Type::kPointer;
250246
ret->int_val = reinterpret_cast<uintptr_t>(allocated);
@@ -257,16 +253,20 @@ void HandleReallocMsg(uintptr_t ptr, size_t size, FuncRet* ret) {
257253
<< ")";
258254

259255
const void* reallocated = realloc(reinterpret_cast<void*>(ptr), size);
260-
// Memory is copied to the pointer using an API that the memory sanitizer
261-
// is blind to (process_vm_writev). Mark the memory as initialized here, so
262-
// that the sandboxed code can still be tested using MSAN.
263-
ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(reallocated, size);
264256

265257
ret->ret_type = v::Type::kPointer;
266258
ret->int_val = reinterpret_cast<uintptr_t>(reallocated);
267259
ret->success = true;
268260
}
269261

262+
void HandleMarkMemoryInit(uintptr_t ptr, size_t size, FuncRet* ret) {
263+
ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(reinterpret_cast<void*>(ptr), size);
264+
265+
ret->ret_type = v::Type::kVoid;
266+
ret->success = true;
267+
ret->int_val = 0ULL;
268+
}
269+
270270
// Handles requests to free memory previously allocated by HandleAllocMsg() and
271271
// HandleReallocMsg().
272272
void HandleFreeMsg(uintptr_t ptr, FuncRet* ret) {
@@ -399,6 +399,12 @@ void ServeRequest(sandbox2::Comms* comms) {
399399
VLOG(1) << "Received Client::kMsgStrlen message";
400400
HandleStrlen(comms, BytesAs<const char*>(bytes), &ret);
401401
break;
402+
case comms::kMsgMarkMemoryInit:
403+
VLOG(1) << "Received Client::kMsgMarkMemoryInit message";
404+
{
405+
auto req = BytesAs<comms::ReallocRequest>(bytes);
406+
HandleMarkMemoryInit(req.old_addr, req.size, &ret);
407+
}
402408
break;
403409
default:
404410
LOG(FATAL) << "Received unknown tag: " << tag;

sandboxed_api/rpcchannel.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,23 @@ absl::StatusOr<size_t> RPCChannel::Strlen(void* str) {
196196
return fret.int_val;
197197
}
198198

199+
absl::Status RPCChannel::MarkMemoryInit(void* addr, size_t size) {
200+
#ifdef MEMORY_SANITIZER
201+
absl::MutexLock lock(&mutex_);
202+
comms::ReallocRequest req = {
203+
.old_addr = reinterpret_cast<uintptr_t>(addr),
204+
.size = size,
205+
};
206+
if (!comms_->SendTLV(comms::kMsgMarkMemoryInit, sizeof(comms::ReallocRequest),
207+
&req)) {
208+
return absl::UnavailableError("Sending TLV value failed");
209+
}
210+
SAPI_ASSIGN_OR_RETURN(auto fret, Return(v::Type::kVoid));
211+
if (!fret.success) {
212+
return absl::UnavailableError("MarkMemoryInit() failed on the remote side");
213+
}
214+
#endif
215+
return absl::OkStatus();
216+
}
217+
199218
} // namespace sapi

sandboxed_api/rpcchannel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class RPCChannel {
4343
// Reallocates memory.
4444
absl::Status Reallocate(void* old_addr, size_t size, void** new_addr);
4545

46+
// Marks the memory as initialized (used with MSAN).
47+
absl::Status MarkMemoryInit(void* addr, size_t size);
48+
4649
// Frees memory.
4750
absl::Status Free(void* addr);
4851

sandboxed_api/var_abstract.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ absl::Status Var::TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) {
131131
return absl::UnavailableError("process_vm_writev: partial success");
132132
}
133133

134+
SAPI_RETURN_IF_ERROR(rpc_channel->MarkMemoryInit(GetRemote(), GetSize()));
135+
134136
return absl::OkStatus();
135137
}
136138

0 commit comments

Comments
 (0)