Commit 1089c462 authored by Erik Chen's avatar Erik Chen Committed by Commit Bot

Fix accounting error in memlog.

The implementaiton of HookRealloc needs to both log the free and the alloc.
However, the conditional was only checking to see whether it was okay to log an
alloc. This could result in missed logging for frees.

Change-Id: I26cf132d7ccac275c1b3e80955263a92162b1956
Bug: 853438
Reviewed-on: https://chromium-review.googlesource.com/1155308Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Erik Chen <erikchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579399}
parent 6fa8d815
......@@ -20,6 +20,7 @@
namespace heap_profiling {
class ScopedAllowAlloc;
class ScopedAllowRealloc;
} // namespace heap_profiling
namespace ui {
......@@ -166,6 +167,7 @@ class BASE_EXPORT ThreadLocalStorage {
friend class base::trace_event::MallocDumpProvider;
friend class debug::GlobalActivityTracker;
friend class heap_profiling::ScopedAllowAlloc;
friend class heap_profiling::ScopedAllowRealloc;
friend class ui::TLSDestructionCheckerForX11;
static bool HasBeenDestroyed();
......
......@@ -154,6 +154,28 @@ class ScopedAllowAlloc {
const bool allowed_;
};
// Realloc triggers both a free and an alloc.
class ScopedAllowRealloc {
public:
ScopedAllowRealloc()
: allow_free_(LIKELY(CanEnterAllocatorShim())),
allow_alloc_(LIKELY(allow_free_ &&
(!base::ThreadLocalStorage::HasBeenDestroyed()))) {
if (allow_free_)
SetEnteringAllocatorShim(true);
}
~ScopedAllowRealloc() {
if (allow_free_)
SetEnteringAllocatorShim(false);
}
bool allow_free() { return allow_free_; }
bool allow_alloc() { return allow_alloc_; }
private:
const bool allow_free_;
const bool allow_alloc_;
};
namespace {
using base::allocator::AllocatorDispatch;
......@@ -436,14 +458,16 @@ void* HookRealloc(const AllocatorDispatch* self,
void* address,
size_t size,
void* context) {
ScopedAllowAlloc allow_logging;
ScopedAllowRealloc allow_logging;
const AllocatorDispatch* const next = self->next;
void* ptr = next->realloc_function(next, address, size, context);
if (LIKELY(allow_logging)) {
if (LIKELY(allow_logging.allow_free())) {
AllocatorShimLogFree(address);
if (size > 0) // realloc(size == 0) means free()
// realloc(size == 0) means free()
if (size > 0 && LIKELY(allow_logging.allow_alloc()))
AllocatorShimLogAlloc(AllocatorType::kMalloc, ptr, size, nullptr);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment