Refactor malloc debug.
Changes
- Refactor the code so that only guards require creating a special header
for every pointer allocated.
- Store only a single copy of every backtrace. This saves memory so that
turning on the backtrace option doesn't result in 10X memory usage.
- Added new option track_allocs that only verifies pointers are valid for
free/malloc_usable_size/realloc.
- Remove suffix from test names.
- Add the TRACK_ALLOCS options to all guard options.
- Add new option verify_pointers that is a lightweight way to verify
pointers that are passed to allocation routines.
- Do auto-formatting of the code.
- Updated documentation for all of these changes.
Bug: 74361929
Test: Ran unit tests.
Test: Ran libmemunreachable unit tests.
Test: Ran an app with backtrace enabled.
Change-Id: I3246c48ae4f9811f64622d90d0a9b4d9d818702c
diff --git a/libc/malloc_debug/GuardData.cpp b/libc/malloc_debug/GuardData.cpp
index d6cef85..f9a2dca 100644
--- a/libc/malloc_debug/GuardData.cpp
+++ b/libc/malloc_debug/GuardData.cpp
@@ -31,13 +31,13 @@
#include <vector>
-#include "backtrace.h"
#include "Config.h"
+#include "DebugData.h"
+#include "GuardData.h"
+#include "backtrace.h"
#include "debug_disable.h"
#include "debug_log.h"
-#include "DebugData.h"
#include "malloc_debug.h"
-#include "GuardData.h"
GuardData::GuardData(DebugData* debug_data, int init_value, size_t num_bytes)
: OptionData(debug_data) {
@@ -48,8 +48,8 @@
void GuardData::LogFailure(const Header* header, const void* pointer, const void* data) {
error_log(LOG_DIVIDER);
- error_log("+++ ALLOCATION %p SIZE %zu HAS A CORRUPTED %s GUARD", pointer,
- header->real_size(), GetTypeName());
+ error_log("+++ ALLOCATION %p SIZE %zu HAS A CORRUPTED %s GUARD", pointer, header->size,
+ GetTypeName());
// Log all of the failing bytes.
const uint8_t* expected = cmp_mem_.data();
@@ -70,7 +70,7 @@
}
FrontGuardData::FrontGuardData(DebugData* debug_data, const Config& config, size_t* offset)
- : GuardData(debug_data, config.front_guard_value(), config.front_guard_bytes()) {
+ : GuardData(debug_data, config.front_guard_value(), config.front_guard_bytes()) {
// Create a buffer for fast comparisons of the front guard.
cmp_mem_.resize(config.front_guard_bytes());
memset(cmp_mem_.data(), config.front_guard_value(), cmp_mem_.size());
@@ -88,8 +88,7 @@
}
RearGuardData::RearGuardData(DebugData* debug_data, const Config& config)
- : GuardData(debug_data, config.rear_guard_value(), config.rear_guard_bytes()) {
-}
+ : GuardData(debug_data, config.rear_guard_value(), config.rear_guard_bytes()) {}
bool RearGuardData::Valid(const Header* header) {
return GuardData::Valid(debug_->GetRearGuard(header));