blob: 3ac54bf3ddffad8aed1f834eb19ad1bbcc0c1d2a [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
Christopher Ferris7993b802016-01-28 18:35:05 -080039FreeTrackData::FreeTrackData(const Config& config)
40 : backtrace_num_frames_(config.free_track_backtrace_num_frames) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080041 cmp_mem_.resize(4096);
42 memset(cmp_mem_.data(), config.fill_free_value, cmp_mem_.size());
43}
44
45void FreeTrackData::LogFreeError(DebugData& debug, const Header* header,
46 const uint8_t* pointer) {
47 ScopedDisableDebugCalls disable;
48
49 error_log(LOG_DIVIDER);
50 error_log("+++ ALLOCATION %p USED AFTER FREE", pointer);
51 uint8_t fill_free_value = debug.config().fill_free_value;
52 for (size_t i = 0; i < header->usable_size; i++) {
53 if (pointer[i] != fill_free_value) {
54 error_log(" pointer[%zu] = 0x%02x (expected 0x%02x)", i, pointer[i], fill_free_value);
55 }
56 }
Christopher Ferris7993b802016-01-28 18:35:05 -080057 auto back_iter = backtraces_.find(header);
58 if (back_iter != backtraces_.end()) {
59 const BacktraceHeader* back_header = back_iter->second;
60 error_log("Backtrace at time of free:");
61 backtrace_log(&back_header->frames[0], back_header->num_frames);
Christopher Ferris63860cb2015-11-16 17:30:32 -080062 }
63 error_log(LOG_DIVIDER);
64}
65
66void FreeTrackData::VerifyAndFree(DebugData& debug, const Header* header,
67 const void* pointer) {
Christopher Ferris7993b802016-01-28 18:35:05 -080068 ScopedDisableDebugCalls disable;
69
Christopher Ferris63860cb2015-11-16 17:30:32 -080070 const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer);
71 size_t bytes = header->usable_size;
72 bytes = (bytes < debug.config().fill_on_free_bytes) ? bytes : debug.config().fill_on_free_bytes;
73 while (bytes > 0) {
74 size_t bytes_to_cmp = (bytes < cmp_mem_.size()) ? bytes : cmp_mem_.size();
75 if (memcmp(memory, cmp_mem_.data(), bytes_to_cmp) != 0) {
76 LogFreeError(debug, header, reinterpret_cast<const uint8_t*>(pointer));
77 break;
78 }
79 bytes -= bytes_to_cmp;
80 memory = &memory[bytes_to_cmp];
81 }
Christopher Ferris7993b802016-01-28 18:35:05 -080082 auto back_iter = backtraces_.find(header);
83 if (back_iter != backtraces_.end()) {
84 g_dispatch->free(reinterpret_cast<void*>(back_iter->second));
85 backtraces_.erase(header);
86 }
Christopher Ferris63860cb2015-11-16 17:30:32 -080087 g_dispatch->free(header->orig_pointer);
88}
89
90void FreeTrackData::Add(DebugData& debug, const Header* header) {
91 // Make sure the stl calls below don't call the debug_XXX functions.
92 ScopedDisableDebugCalls disable;
93
94 pthread_mutex_lock(&mutex_);
95 if (list_.size() == debug.config().free_track_allocations) {
Christopher Ferris7993b802016-01-28 18:35:05 -080096 const Header* old_header = list_.back();
97 VerifyAndFree(debug, old_header, debug.GetPointer(old_header));
Christopher Ferris63860cb2015-11-16 17:30:32 -080098 list_.pop_back();
99 }
100
Christopher Ferris7993b802016-01-28 18:35:05 -0800101 // Only log the free backtrace if we are using the free track feature.
102 if (backtrace_num_frames_ > 0) {
103 BacktraceHeader* back_header = reinterpret_cast<BacktraceHeader*>(
104 g_dispatch->malloc(sizeof(BacktraceHeader) + backtrace_num_frames_ * sizeof(uintptr_t)));
105 if (back_header) {
106 back_header->num_frames = backtrace_get(&back_header->frames[0], backtrace_num_frames_);
107 backtraces_[header] = back_header;
108 }
109 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800110 list_.push_front(header);
111
112 pthread_mutex_unlock(&mutex_);
113}
114
115void FreeTrackData::VerifyAll(DebugData& debug) {
116 // Make sure the stl calls below don't call the debug_XXX functions.
117 ScopedDisableDebugCalls disable;
118
119 for (const auto& header : list_) {
120 VerifyAndFree(debug, header, debug.GetPointer(header));
121 }
122 list_.clear();
123}
Christopher Ferris7993b802016-01-28 18:35:05 -0800124
125void FreeTrackData::LogBacktrace(const Header* header) {
126 ScopedDisableDebugCalls disable;
127
128 auto back_iter = backtraces_.find(header);
129 if (back_iter == backtraces_.end()) {
130 return;
131 }
132
133 error_log("Backtrace of original free:");
134 backtrace_log(&back_iter->second->frames[0], back_iter->second->num_frames);
135}