| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -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 | #pragma once | 
|  | 30 |  | 
|  | 31 | #include <stdint.h> | 
|  | 32 | #include <stdio.h> | 
|  | 33 |  | 
|  | 34 | #include <atomic> | 
|  | 35 | #include <deque> | 
|  | 36 | #include <mutex> | 
|  | 37 | #include <string> | 
|  | 38 | #include <unordered_map> | 
|  | 39 | #include <vector> | 
|  | 40 |  | 
|  | 41 | #include <private/bionic_macros.h> | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 42 | #include <unwindstack/LocalUnwinder.h> | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 43 |  | 
|  | 44 | #include "OptionData.h" | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 45 | #include "UnwindBacktrace.h" | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 46 |  | 
|  | 47 | extern int* g_malloc_zygote_child; | 
|  | 48 |  | 
|  | 49 | // Forward declarations. | 
|  | 50 | class Config; | 
|  | 51 |  | 
|  | 52 | struct FrameKeyType { | 
|  | 53 | size_t num_frames; | 
|  | 54 | uintptr_t* frames; | 
|  | 55 |  | 
|  | 56 | bool operator==(const FrameKeyType& comp) const { | 
|  | 57 | if (num_frames != comp.num_frames) return false; | 
|  | 58 | for (size_t i = 0; i < num_frames; i++) { | 
|  | 59 | if (frames[i] != comp.frames[i]) { | 
|  | 60 | return false; | 
|  | 61 | } | 
|  | 62 | } | 
|  | 63 | return true; | 
|  | 64 | } | 
|  | 65 | }; | 
|  | 66 |  | 
|  | 67 | namespace std { | 
|  | 68 | template <> | 
|  | 69 | struct hash<FrameKeyType> { | 
|  | 70 | std::size_t operator()(const FrameKeyType& key) const { | 
|  | 71 | std::size_t cur_hash = key.frames[0]; | 
|  | 72 | // Limit the number of frames to speed up hashing. | 
|  | 73 | size_t max_frames = (key.num_frames > 5) ? 5 : key.num_frames; | 
|  | 74 | for (size_t i = 1; i < max_frames; i++) { | 
|  | 75 | cur_hash ^= key.frames[i]; | 
|  | 76 | } | 
|  | 77 | return cur_hash; | 
|  | 78 | } | 
|  | 79 | }; | 
|  | 80 | };  // namespace std | 
|  | 81 |  | 
|  | 82 | struct FrameInfoType { | 
|  | 83 | size_t references = 0; | 
|  | 84 | std::vector<uintptr_t> frames; | 
|  | 85 | }; | 
|  | 86 |  | 
|  | 87 | struct PointerInfoType { | 
|  | 88 | size_t size; | 
|  | 89 | size_t hash_index; | 
|  | 90 | size_t RealSize() const { return size & ~(1U << 31); } | 
|  | 91 | bool ZygoteChildAlloc() const { return size & (1U << 31); } | 
|  | 92 | static size_t GetEncodedSize(size_t size) { return GetEncodedSize(*g_malloc_zygote_child, size); } | 
|  | 93 | static size_t GetEncodedSize(bool child_alloc, size_t size) { | 
|  | 94 | return size | ((child_alloc) ? (1U << 31) : 0); | 
|  | 95 | } | 
|  | 96 | static size_t MaxSize() { return (1U << 31) - 1; } | 
|  | 97 | }; | 
|  | 98 |  | 
|  | 99 | struct FreePointerInfoType { | 
|  | 100 | uintptr_t pointer; | 
|  | 101 | size_t hash_index; | 
|  | 102 | }; | 
|  | 103 |  | 
|  | 104 | struct ListInfoType { | 
|  | 105 | uintptr_t pointer; | 
|  | 106 | size_t num_allocations; | 
|  | 107 | size_t size; | 
|  | 108 | bool zygote_child_alloc; | 
|  | 109 | FrameInfoType* frame_info; | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 110 | std::vector<unwindstack::LocalFrameData>* backtrace_info; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 111 | }; | 
|  | 112 |  | 
|  | 113 | class PointerData : public OptionData { | 
|  | 114 | public: | 
|  | 115 | PointerData(DebugData* debug_data); | 
|  | 116 | virtual ~PointerData() = default; | 
|  | 117 |  | 
|  | 118 | bool Initialize(const Config& config); | 
|  | 119 |  | 
|  | 120 | inline size_t alloc_offset() { return alloc_offset_; } | 
|  | 121 |  | 
|  | 122 | bool ShouldBacktrace() { return backtrace_enabled_ == 1; } | 
|  | 123 | void ToggleBacktraceEnabled() { backtrace_enabled_.fetch_xor(1); } | 
|  | 124 |  | 
|  | 125 | void EnableDumping() { backtrace_dump_ = true; } | 
|  | 126 | bool ShouldDumpAndReset() { | 
|  | 127 | bool expected = true; | 
|  | 128 | return backtrace_dump_.compare_exchange_strong(expected, false); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | void PrepareFork(); | 
|  | 132 | void PostForkParent(); | 
|  | 133 | void PostForkChild(); | 
|  | 134 |  | 
|  | 135 | static void GetList(std::vector<ListInfoType>* list, bool only_with_backtrace); | 
|  | 136 | static void GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace); | 
|  | 137 |  | 
|  | 138 | static size_t AddBacktrace(size_t num_frames); | 
|  | 139 | static void RemoveBacktrace(size_t hash_index); | 
|  | 140 |  | 
|  | 141 | static void Add(const void* pointer, size_t size); | 
|  | 142 | static void Remove(const void* pointer); | 
|  | 143 |  | 
|  | 144 | typedef std::unordered_map<uintptr_t, PointerInfoType>::iterator iterator; | 
|  | 145 | static iterator begin() { return pointers_.begin(); } | 
|  | 146 | static iterator end() { return pointers_.end(); } | 
|  | 147 |  | 
|  | 148 | static void* AddFreed(const void* pointer); | 
|  | 149 | static void LogFreeError(const FreePointerInfoType& info, size_t usable_size); | 
|  | 150 | static void LogFreeBacktrace(const void* ptr); | 
|  | 151 | static void VerifyFreedPointer(const FreePointerInfoType& info); | 
|  | 152 | static void VerifyAllFreed(); | 
|  | 153 |  | 
|  | 154 | static void LogLeaks(); | 
|  | 155 | static void DumpLiveToFile(FILE* fp); | 
|  | 156 |  | 
|  | 157 | static void GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory, | 
|  | 158 | size_t* backtrace_size); | 
|  | 159 |  | 
|  | 160 | static size_t GetFrames(const void* pointer, uintptr_t* frames, size_t max_frames); | 
|  | 161 |  | 
|  | 162 | static bool Exists(const void* pointer); | 
|  | 163 |  | 
|  | 164 | private: | 
|  | 165 | static std::string GetHashString(uintptr_t* frames, size_t num_frames); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 166 | static void LogBacktrace(size_t hash_index); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 167 |  | 
|  | 168 | size_t alloc_offset_ = 0; | 
|  | 169 | std::vector<uint8_t> cmp_mem_; | 
|  | 170 |  | 
|  | 171 | static std::atomic_uint8_t backtrace_enabled_; | 
|  | 172 |  | 
|  | 173 | static std::atomic_bool backtrace_dump_; | 
|  | 174 |  | 
|  | 175 | static std::mutex pointer_mutex_; | 
|  | 176 | static std::unordered_map<uintptr_t, PointerInfoType> pointers_; | 
|  | 177 |  | 
|  | 178 | static std::mutex frame_mutex_; | 
|  | 179 | static std::unordered_map<FrameKeyType, size_t> key_to_index_; | 
|  | 180 | static std::unordered_map<size_t, FrameInfoType> frames_; | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 181 | static std::unordered_map<size_t, std::vector<unwindstack::LocalFrameData>> backtraces_info_; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 182 | static size_t cur_hash_index_; | 
|  | 183 |  | 
|  | 184 | static std::mutex free_pointer_mutex_; | 
|  | 185 | static std::deque<FreePointerInfoType> free_pointers_; | 
|  | 186 |  | 
|  | 187 | DISALLOW_COPY_AND_ASSIGN(PointerData); | 
|  | 188 | }; |