| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 39 | FreeTrackData::FreeTrackData(DebugData* debug, const Config& config) | 
|  | 40 | : OptionData(debug), backtrace_num_frames_(config.free_track_backtrace_num_frames) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 41 | cmp_mem_.resize(4096); | 
|  | 42 | memset(cmp_mem_.data(), config.fill_free_value, cmp_mem_.size()); | 
|  | 43 | } | 
|  | 44 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 45 | void FreeTrackData::LogFreeError(const Header* header, const uint8_t* pointer) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 46 | error_log(LOG_DIVIDER); | 
|  | 47 | error_log("+++ ALLOCATION %p USED AFTER FREE", pointer); | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 48 | uint8_t fill_free_value = debug_->config().fill_free_value; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 49 | for (size_t i = 0; i < header->usable_size; i++) { | 
|  | 50 | if (pointer[i] != fill_free_value) { | 
| Christopher Ferris | ea26b33 | 2016-04-15 14:13:52 -0700 | [diff] [blame] | 51 | error_log("  allocation[%zu] = 0x%02x (expected 0x%02x)", i, pointer[i], fill_free_value); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 52 | } | 
|  | 53 | } | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 54 | 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 Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 59 | } | 
|  | 60 | error_log(LOG_DIVIDER); | 
|  | 61 | } | 
|  | 62 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 63 | void FreeTrackData::VerifyAndFree(const Header* header) { | 
|  | 64 | const void* pointer = debug_->GetPointer(header); | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 65 | 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 Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 72 | bytes = (bytes < debug_->config().fill_on_free_bytes) ? bytes | 
|  | 73 | : debug_->config().fill_on_free_bytes; | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 74 | 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 Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 77 | LogFreeError(header, reinterpret_cast<const uint8_t*>(pointer)); | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 78 | break; | 
|  | 79 | } | 
|  | 80 | bytes -= bytes_to_cmp; | 
|  | 81 | memory = &memory[bytes_to_cmp]; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 82 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 83 | } | 
| Christopher Ferris | d091962 | 2016-03-15 22:39:39 -0700 | [diff] [blame] | 84 |  | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 85 | 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 Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 90 | g_dispatch->free(header->orig_pointer); | 
|  | 91 | } | 
|  | 92 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 93 | void FreeTrackData::Add(const Header* header) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 94 | pthread_mutex_lock(&mutex_); | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 95 | if (list_.size() == debug_->config().free_track_allocations) { | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 96 | const Header* old_header = list_.back(); | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 97 | VerifyAndFree(old_header); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 98 | list_.pop_back(); | 
|  | 99 | } | 
|  | 100 |  | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 101 | 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 Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 109 | list_.push_front(header); | 
|  | 110 |  | 
|  | 111 | pthread_mutex_unlock(&mutex_); | 
|  | 112 | } | 
|  | 113 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 114 | void FreeTrackData::VerifyAll() { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 115 | for (const auto& header : list_) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 116 | VerifyAndFree(header); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 117 | } | 
|  | 118 | list_.clear(); | 
|  | 119 | } | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 120 |  | 
|  | 121 | void FreeTrackData::LogBacktrace(const Header* header) { | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 122 | 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 | } |