ART: Fix arena allocation for valgrind.
Move the zero-initialization check after marking the newly
allocated chunk as defined and check only the allocated
space without the red zone. Also mark unallocated space as
inaccessible instead of just undefined.
Change-Id: I74fc65f5b53acb74cec4e5a0146f41dacf4a1470
diff --git a/runtime/base/scoped_arena_allocator.cc b/runtime/base/scoped_arena_allocator.cc
index 31f96e4..90c6ee3 100644
--- a/runtime/base/scoped_arena_allocator.cc
+++ b/runtime/base/scoped_arena_allocator.cc
@@ -91,16 +91,19 @@
}
void* ArenaStack::AllocWithMemoryTool(size_t bytes, ArenaAllocKind kind) {
+ // We mark all memory for a newly retrieved arena as inaccessible and then
+ // mark only the actually allocated memory as defined. That leaves red zones
+ // and padding between allocations marked as inaccessible.
size_t rounded_bytes = RoundUp(bytes + kMemoryToolRedZoneBytes, 8);
uint8_t* ptr = top_ptr_;
if (UNLIKELY(static_cast<size_t>(top_end_ - ptr) < rounded_bytes)) {
ptr = AllocateFromNextArena(rounded_bytes);
CHECK(ptr != nullptr) << "Failed to allocate memory";
+ MEMORY_TOOL_MAKE_NOACCESS(ptr, top_end_);
}
CurrentStats()->RecordAlloc(bytes, kind);
top_ptr_ = ptr + rounded_bytes;
MEMORY_TOOL_MAKE_UNDEFINED(ptr, bytes);
- MEMORY_TOOL_MAKE_NOACCESS(ptr + bytes, rounded_bytes - bytes);
return ptr;
}