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 
FreeTrackData(DebugData * debug,const Config & config)39 FreeTrackData::FreeTrackData(DebugData* debug, const Config& config)
40     : OptionData(debug), backtrace_num_frames_(config.free_track_backtrace_num_frames) {
41   cmp_mem_.resize(4096);
42   memset(cmp_mem_.data(), config.fill_free_value, cmp_mem_.size());
43 }
44 
LogFreeError(const Header * header,const uint8_t * pointer)45 void FreeTrackData::LogFreeError(const Header* header, const uint8_t* pointer) {
46   error_log(LOG_DIVIDER);
47   error_log("+++ ALLOCATION %p USED AFTER FREE", pointer);
48   uint8_t fill_free_value = debug_->config().fill_free_value;
49   for (size_t i = 0; i < header->usable_size; i++) {
50     if (pointer[i] != fill_free_value) {
51       error_log("  allocation[%zu] = 0x%02x (expected 0x%02x)", i, pointer[i], fill_free_value);
52     }
53   }
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);
59   }
60   error_log(LOG_DIVIDER);
61 }
62 
VerifyAndFree(const Header * header)63 void FreeTrackData::VerifyAndFree(const Header* header) {
64   const void* pointer = debug_->GetPointer(header);
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;
72     bytes = (bytes < debug_->config().fill_on_free_bytes) ? bytes
73         : debug_->config().fill_on_free_bytes;
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) {
77         LogFreeError(header, reinterpret_cast<const uint8_t*>(pointer));
78         break;
79       }
80       bytes -= bytes_to_cmp;
81       memory = &memory[bytes_to_cmp];
82     }
83   }
84 
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   }
90   g_dispatch->free(header->orig_pointer);
91 }
92 
Add(const Header * header)93 void FreeTrackData::Add(const Header* header) {
94   pthread_mutex_lock(&mutex_);
95   if (list_.size() == debug_->config().free_track_allocations) {
96     const Header* old_header = list_.back();
97     VerifyAndFree(old_header);
98     list_.pop_back();
99   }
100 
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   }
109   list_.push_front(header);
110 
111   pthread_mutex_unlock(&mutex_);
112 }
113 
VerifyAll()114 void FreeTrackData::VerifyAll() {
115   for (const auto& header : list_) {
116     VerifyAndFree(header);
117   }
118   list_.clear();
119 }
120 
LogBacktrace(const Header * header)121 void FreeTrackData::LogBacktrace(const Header* header) {
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 }
130