blob: 3466861c7687573add903d5ce53732ab6fe15593 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stdint.h>
30
31#include "backtrace.h"
32#include "Config.h"
33#include "DebugData.h"
34#include "debug_disable.h"
35#include "debug_log.h"
36#include "FreeTrackData.h"
37#include "malloc_debug.h"
38
39FreeTrackData::FreeTrackData(const Config& config) {
40 cmp_mem_.resize(4096);
41 memset(cmp_mem_.data(), config.fill_free_value, cmp_mem_.size());
42}
43
44void FreeTrackData::LogFreeError(DebugData& debug, const Header* header,
45 const uint8_t* pointer) {
46 ScopedDisableDebugCalls disable;
47
48 error_log(LOG_DIVIDER);
49 error_log("+++ ALLOCATION %p USED AFTER FREE", pointer);
50 uint8_t fill_free_value = debug.config().fill_free_value;
51 for (size_t i = 0; i < header->usable_size; i++) {
52 if (pointer[i] != fill_free_value) {
53 error_log(" pointer[%zu] = 0x%02x (expected 0x%02x)", i, pointer[i], fill_free_value);
54 }
55 }
56 if (debug.config().options & BACKTRACE) {
57 BacktraceHeader* back_header = debug.GetFreeBacktrace(header);
58 if (back_header->num_frames > 0) {
59 error_log("Backtrace at time of free:");
60 backtrace_log(&back_header->frames[0], back_header->num_frames);
61 }
62 }
63 error_log(LOG_DIVIDER);
64}
65
66void FreeTrackData::VerifyAndFree(DebugData& debug, const Header* header,
67 const void* pointer) {
68 const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer);
69 size_t bytes = header->usable_size;
70 bytes = (bytes < debug.config().fill_on_free_bytes) ? bytes : debug.config().fill_on_free_bytes;
71 while (bytes > 0) {
72 size_t bytes_to_cmp = (bytes < cmp_mem_.size()) ? bytes : cmp_mem_.size();
73 if (memcmp(memory, cmp_mem_.data(), bytes_to_cmp) != 0) {
74 LogFreeError(debug, header, reinterpret_cast<const uint8_t*>(pointer));
75 break;
76 }
77 bytes -= bytes_to_cmp;
78 memory = &memory[bytes_to_cmp];
79 }
80 g_dispatch->free(header->orig_pointer);
81}
82
83void FreeTrackData::Add(DebugData& debug, const Header* header) {
84 // Make sure the stl calls below don't call the debug_XXX functions.
85 ScopedDisableDebugCalls disable;
86
87 pthread_mutex_lock(&mutex_);
88 if (list_.size() == debug.config().free_track_allocations) {
89 VerifyAndFree(debug, list_.back(), debug.GetPointer(list_.back()));
90 list_.pop_back();
91 }
92
93 list_.push_front(header);
94
95 pthread_mutex_unlock(&mutex_);
96}
97
98void FreeTrackData::VerifyAll(DebugData& debug) {
99 // Make sure the stl calls below don't call the debug_XXX functions.
100 ScopedDisableDebugCalls disable;
101
102 for (const auto& header : list_) {
103 VerifyAndFree(debug, header, debug.GetPointer(header));
104 }
105 list_.clear();
106}