blob: e8e7a67745667829894eed351ad5665235507d22 [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 Ferris55a89a42016-04-07 17:14:53 -070039FreeTrackData::FreeTrackData(DebugData* debug, const Config& config)
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070040 : OptionData(debug), backtrace_num_frames_(config.free_track_backtrace_num_frames()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080041 cmp_mem_.resize(4096);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070042 memset(cmp_mem_.data(), config.fill_free_value(), cmp_mem_.size());
Christopher Ferris63860cb2015-11-16 17:30:32 -080043}
44
Christopher Ferris55a89a42016-04-07 17:14:53 -070045void FreeTrackData::LogFreeError(const Header* header, const uint8_t* pointer) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080046 error_log(LOG_DIVIDER);
47 error_log("+++ ALLOCATION %p USED AFTER FREE", pointer);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070048 uint8_t fill_free_value = debug_->config().fill_free_value();
Christopher Ferris63860cb2015-11-16 17:30:32 -080049 for (size_t i = 0; i < header->usable_size; i++) {
50 if (pointer[i] != fill_free_value) {
Christopher Ferrisea26b332016-04-15 14:13:52 -070051 error_log(" allocation[%zu] = 0x%02x (expected 0x%02x)", i, pointer[i], fill_free_value);
Christopher Ferris63860cb2015-11-16 17:30:32 -080052 }
53 }
Christopher Ferris7993b802016-01-28 18:35:05 -080054 auto back_iter = backtraces_.find(header);
55 if (back_iter != backtraces_.end()) {
56 const BacktraceHeader* back_header = back_iter->second;
57 error_log("Backtrace at time of free:");
58 backtrace_log(&back_header->frames[0], back_header->num_frames);
Christopher Ferris63860cb2015-11-16 17:30:32 -080059 }
60 error_log(LOG_DIVIDER);
61}
62
Christopher Ferris55a89a42016-04-07 17:14:53 -070063void FreeTrackData::VerifyAndFree(const Header* header) {
64 const void* pointer = debug_->GetPointer(header);
Christopher Ferrisd0919622016-03-15 22:39:39 -070065 if (header->tag != DEBUG_FREE_TAG) {
66 error_log(LOG_DIVIDER);
67 error_log("+++ ALLOCATION %p HAS CORRUPTED HEADER TAG 0x%x AFTER FREE", pointer, header->tag);
68 error_log(LOG_DIVIDER);
69 } else {
70 const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer);
71 size_t bytes = header->usable_size;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070072 bytes = (bytes < debug_->config().fill_on_free_bytes()) ? bytes
73 : debug_->config().fill_on_free_bytes();
Christopher Ferrisd0919622016-03-15 22:39:39 -070074 while (bytes > 0) {
75 size_t bytes_to_cmp = (bytes < cmp_mem_.size()) ? bytes : cmp_mem_.size();
76 if (memcmp(memory, cmp_mem_.data(), bytes_to_cmp) != 0) {
Christopher Ferris55a89a42016-04-07 17:14:53 -070077 LogFreeError(header, reinterpret_cast<const uint8_t*>(pointer));
Christopher Ferrisd0919622016-03-15 22:39:39 -070078 break;
79 }
80 bytes -= bytes_to_cmp;
81 memory = &memory[bytes_to_cmp];
Christopher Ferris63860cb2015-11-16 17:30:32 -080082 }
Christopher Ferris63860cb2015-11-16 17:30:32 -080083 }
Christopher Ferrisd0919622016-03-15 22:39:39 -070084
Christopher Ferris7993b802016-01-28 18:35:05 -080085 auto back_iter = backtraces_.find(header);
86 if (back_iter != backtraces_.end()) {
87 g_dispatch->free(reinterpret_cast<void*>(back_iter->second));
88 backtraces_.erase(header);
89 }
Christopher Ferris63860cb2015-11-16 17:30:32 -080090 g_dispatch->free(header->orig_pointer);
91}
92
Christopher Ferris55a89a42016-04-07 17:14:53 -070093void FreeTrackData::Add(const Header* header) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080094 pthread_mutex_lock(&mutex_);
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070095 if (list_.size() == debug_->config().free_track_allocations()) {
Christopher Ferris7993b802016-01-28 18:35:05 -080096 const Header* old_header = list_.back();
Christopher Ferris55a89a42016-04-07 17:14:53 -070097 VerifyAndFree(old_header);
Christopher Ferris63860cb2015-11-16 17:30:32 -080098 list_.pop_back();
99 }
100
Christopher Ferris7993b802016-01-28 18:35:05 -0800101 if (backtrace_num_frames_ > 0) {
102 BacktraceHeader* back_header = reinterpret_cast<BacktraceHeader*>(
103 g_dispatch->malloc(sizeof(BacktraceHeader) + backtrace_num_frames_ * sizeof(uintptr_t)));
104 if (back_header) {
105 back_header->num_frames = backtrace_get(&back_header->frames[0], backtrace_num_frames_);
106 backtraces_[header] = back_header;
107 }
108 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800109 list_.push_front(header);
110
111 pthread_mutex_unlock(&mutex_);
112}
113
Christopher Ferris55a89a42016-04-07 17:14:53 -0700114void FreeTrackData::VerifyAll() {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800115 for (const auto& header : list_) {
Christopher Ferris55a89a42016-04-07 17:14:53 -0700116 VerifyAndFree(header);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800117 }
118 list_.clear();
119}
Christopher Ferris7993b802016-01-28 18:35:05 -0800120
121void FreeTrackData::LogBacktrace(const Header* header) {
Christopher Ferris7993b802016-01-28 18:35:05 -0800122 auto back_iter = backtraces_.find(header);
123 if (back_iter == backtraces_.end()) {
124 return;
125 }
126
127 error_log("Backtrace of original free:");
128 backtrace_log(&back_iter->second->frames[0], back_iter->second->num_frames);
129}