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(const Config & config)39 FreeTrackData::FreeTrackData(const Config& config)
40     : 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(DebugData & debug,const Header * header,const uint8_t * pointer)45 void 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("  allocation[%zu] = 0x%02x (expected 0x%02x)", i, pointer[i], fill_free_value);
55     }
56   }
57   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);
62   }
63   error_log(LOG_DIVIDER);
64 }
65 
VerifyAndFree(DebugData & debug,const Header * header,const void * pointer)66 void FreeTrackData::VerifyAndFree(DebugData& debug, const Header* header,
67                                   const void* pointer) {
68   ScopedDisableDebugCalls disable;
69 
70   if (header->tag != DEBUG_FREE_TAG) {
71     error_log(LOG_DIVIDER);
72     error_log("+++ ALLOCATION %p HAS CORRUPTED HEADER TAG 0x%x AFTER FREE", pointer, header->tag);
73     error_log(LOG_DIVIDER);
74   } else {
75     const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer);
76     size_t bytes = header->usable_size;
77     bytes = (bytes < debug.config().fill_on_free_bytes) ? bytes : debug.config().fill_on_free_bytes;
78     while (bytes > 0) {
79       size_t bytes_to_cmp = (bytes < cmp_mem_.size()) ? bytes : cmp_mem_.size();
80       if (memcmp(memory, cmp_mem_.data(), bytes_to_cmp) != 0) {
81         LogFreeError(debug, header, reinterpret_cast<const uint8_t*>(pointer));
82         break;
83       }
84       bytes -= bytes_to_cmp;
85       memory = &memory[bytes_to_cmp];
86     }
87   }
88 
89   auto back_iter = backtraces_.find(header);
90   if (back_iter != backtraces_.end()) {
91     g_dispatch->free(reinterpret_cast<void*>(back_iter->second));
92     backtraces_.erase(header);
93   }
94   g_dispatch->free(header->orig_pointer);
95 }
96 
Add(DebugData & debug,const Header * header)97 void FreeTrackData::Add(DebugData& debug, const Header* header) {
98   // Make sure the stl calls below don't call the debug_XXX functions.
99   ScopedDisableDebugCalls disable;
100 
101   pthread_mutex_lock(&mutex_);
102   if (list_.size() == debug.config().free_track_allocations) {
103     const Header* old_header = list_.back();
104     VerifyAndFree(debug, old_header, debug.GetPointer(old_header));
105     list_.pop_back();
106   }
107 
108   if (backtrace_num_frames_ > 0) {
109     BacktraceHeader* back_header = reinterpret_cast<BacktraceHeader*>(
110       g_dispatch->malloc(sizeof(BacktraceHeader) + backtrace_num_frames_ * sizeof(uintptr_t)));
111     if (back_header) {
112       back_header->num_frames = backtrace_get(&back_header->frames[0], backtrace_num_frames_);
113       backtraces_[header] = back_header;
114     }
115   }
116   list_.push_front(header);
117 
118   pthread_mutex_unlock(&mutex_);
119 }
120 
VerifyAll(DebugData & debug)121 void FreeTrackData::VerifyAll(DebugData& debug) {
122   // Make sure the stl calls below don't call the debug_XXX functions.
123   ScopedDisableDebugCalls disable;
124 
125   for (const auto& header : list_) {
126     VerifyAndFree(debug, header, debug.GetPointer(header));
127   }
128   list_.clear();
129 }
130 
LogBacktrace(const Header * header)131 void FreeTrackData::LogBacktrace(const Header* header) {
132   ScopedDisableDebugCalls disable;
133 
134   auto back_iter = backtraces_.find(header);
135   if (back_iter == backtraces_.end()) {
136     return;
137   }
138 
139   error_log("Backtrace of original free:");
140   backtrace_log(&back_iter->second->frames[0], back_iter->second->num_frames);
141 }
142