| 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> | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 36 | #include <functional> | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 37 | #include <mutex> | 
 | 38 | #include <string> | 
 | 39 | #include <unordered_map> | 
 | 40 | #include <vector> | 
 | 41 |  | 
| Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 42 | #include <platform/bionic/macros.h> | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 43 | #include <unwindstack/Unwinder.h> | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 44 |  | 
 | 45 | #include "OptionData.h" | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 46 | #include "UnwindBacktrace.h" | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 47 |  | 
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 48 | extern bool* g_zygote_child; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 49 |  | 
 | 50 | // Forward declarations. | 
 | 51 | class Config; | 
 | 52 |  | 
 | 53 | struct FrameKeyType { | 
 | 54 |   size_t num_frames; | 
 | 55 |   uintptr_t* frames; | 
 | 56 |  | 
 | 57 |   bool operator==(const FrameKeyType& comp) const { | 
 | 58 |     if (num_frames != comp.num_frames) return false; | 
 | 59 |     for (size_t i = 0; i < num_frames; i++) { | 
 | 60 |       if (frames[i] != comp.frames[i]) { | 
 | 61 |         return false; | 
 | 62 |       } | 
 | 63 |     } | 
 | 64 |     return true; | 
 | 65 |   } | 
 | 66 | }; | 
 | 67 |  | 
 | 68 | namespace std { | 
 | 69 | template <> | 
 | 70 | struct hash<FrameKeyType> { | 
 | 71 |   std::size_t operator()(const FrameKeyType& key) const { | 
 | 72 |     std::size_t cur_hash = key.frames[0]; | 
 | 73 |     // Limit the number of frames to speed up hashing. | 
 | 74 |     size_t max_frames = (key.num_frames > 5) ? 5 : key.num_frames; | 
 | 75 |     for (size_t i = 1; i < max_frames; i++) { | 
 | 76 |       cur_hash ^= key.frames[i]; | 
 | 77 |     } | 
 | 78 |     return cur_hash; | 
 | 79 |   } | 
 | 80 | }; | 
 | 81 | };  // namespace std | 
 | 82 |  | 
 | 83 | struct FrameInfoType { | 
 | 84 |   size_t references = 0; | 
 | 85 |   std::vector<uintptr_t> frames; | 
 | 86 | }; | 
 | 87 |  | 
 | 88 | struct PointerInfoType { | 
 | 89 |   size_t size; | 
 | 90 |   size_t hash_index; | 
 | 91 |   size_t RealSize() const { return size & ~(1U << 31); } | 
 | 92 |   bool ZygoteChildAlloc() const { return size & (1U << 31); } | 
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 93 |   static size_t GetEncodedSize(size_t size) { | 
 | 94 |     return GetEncodedSize(*g_zygote_child, size); | 
 | 95 |   } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 96 |   static size_t GetEncodedSize(bool child_alloc, size_t size) { | 
 | 97 |     return size | ((child_alloc) ? (1U << 31) : 0); | 
 | 98 |   } | 
 | 99 |   static size_t MaxSize() { return (1U << 31) - 1; } | 
 | 100 | }; | 
 | 101 |  | 
 | 102 | struct FreePointerInfoType { | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 103 |   uintptr_t mangled_ptr; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 104 |   size_t hash_index; | 
 | 105 | }; | 
 | 106 |  | 
 | 107 | struct ListInfoType { | 
 | 108 |   uintptr_t pointer; | 
 | 109 |   size_t num_allocations; | 
 | 110 |   size_t size; | 
 | 111 |   bool zygote_child_alloc; | 
 | 112 |   FrameInfoType* frame_info; | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 113 |   std::vector<unwindstack::FrameData>* backtrace_info; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 114 | }; | 
 | 115 |  | 
 | 116 | class PointerData : public OptionData { | 
 | 117 |  public: | 
| Chih-Hung Hsieh | 770032d | 2019-01-02 10:59:48 -0800 | [diff] [blame] | 118 |   explicit PointerData(DebugData* debug_data); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 119 |   virtual ~PointerData() = default; | 
 | 120 |  | 
 | 121 |   bool Initialize(const Config& config); | 
 | 122 |  | 
 | 123 |   inline size_t alloc_offset() { return alloc_offset_; } | 
 | 124 |  | 
 | 125 |   bool ShouldBacktrace() { return backtrace_enabled_ == 1; } | 
 | 126 |   void ToggleBacktraceEnabled() { backtrace_enabled_.fetch_xor(1); } | 
 | 127 |  | 
 | 128 |   void EnableDumping() { backtrace_dump_ = true; } | 
 | 129 |   bool ShouldDumpAndReset() { | 
 | 130 |     bool expected = true; | 
 | 131 |     return backtrace_dump_.compare_exchange_strong(expected, false); | 
 | 132 |   } | 
 | 133 |  | 
 | 134 |   void PrepareFork(); | 
 | 135 |   void PostForkParent(); | 
 | 136 |   void PostForkChild(); | 
 | 137 |  | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 138 |   static void IteratePointers(std::function<void(uintptr_t pointer)> fn); | 
 | 139 |  | 
| Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 140 |   static size_t AddBacktrace(size_t num_frames, size_t size_bytes); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 141 |   static void RemoveBacktrace(size_t hash_index); | 
 | 142 |  | 
 | 143 |   static void Add(const void* pointer, size_t size); | 
 | 144 |   static void Remove(const void* pointer); | 
 | 145 |  | 
| Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 146 |   static void* AddFreed(const void* pointer, size_t size_bytes); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 147 |   static void LogFreeError(const FreePointerInfoType& info, size_t usable_size); | 
 | 148 |   static void LogFreeBacktrace(const void* ptr); | 
 | 149 |   static void VerifyFreedPointer(const FreePointerInfoType& info); | 
 | 150 |   static void VerifyAllFreed(); | 
 | 151 |  | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 152 |   static void GetAllocList(std::vector<ListInfoType>* list); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 153 |   static void LogLeaks(); | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 154 |   static void DumpLiveToFile(int fd); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 155 |  | 
 | 156 |   static void GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory, | 
 | 157 |                       size_t* backtrace_size); | 
 | 158 |  | 
 | 159 |   static size_t GetFrames(const void* pointer, uintptr_t* frames, size_t max_frames); | 
 | 160 |  | 
 | 161 |   static bool Exists(const void* pointer); | 
 | 162 |  | 
 | 163 |  private: | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 164 |   // Only keep mangled pointers in internal data structures. This avoids | 
 | 165 |   // problems where libmemunreachable finds these pointers and thinks they | 
 | 166 |   // are not unreachable. | 
 | 167 |   static inline uintptr_t ManglePointer(uintptr_t pointer) { return pointer ^ UINTPTR_MAX; } | 
 | 168 |   static inline uintptr_t DemanglePointer(uintptr_t pointer) { return pointer ^ UINTPTR_MAX; } | 
 | 169 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 170 |   static std::string GetHashString(uintptr_t* frames, size_t num_frames); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 171 |   static void LogBacktrace(size_t hash_index); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 172 |  | 
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 173 |   static void GetList(std::vector<ListInfoType>* list, bool only_with_backtrace); | 
 | 174 |   static void GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace); | 
 | 175 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 176 |   size_t alloc_offset_ = 0; | 
 | 177 |   std::vector<uint8_t> cmp_mem_; | 
 | 178 |  | 
 | 179 |   static std::atomic_uint8_t backtrace_enabled_; | 
 | 180 |  | 
 | 181 |   static std::atomic_bool backtrace_dump_; | 
 | 182 |  | 
 | 183 |   static std::mutex pointer_mutex_; | 
 | 184 |   static std::unordered_map<uintptr_t, PointerInfoType> pointers_; | 
 | 185 |  | 
 | 186 |   static std::mutex frame_mutex_; | 
 | 187 |   static std::unordered_map<FrameKeyType, size_t> key_to_index_; | 
 | 188 |   static std::unordered_map<size_t, FrameInfoType> frames_; | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 189 |   static std::unordered_map<size_t, std::vector<unwindstack::FrameData>> backtraces_info_; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 190 |   static size_t cur_hash_index_; | 
 | 191 |  | 
 | 192 |   static std::mutex free_pointer_mutex_; | 
 | 193 |   static std::deque<FreePointerInfoType> free_pointers_; | 
 | 194 |  | 
| Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 195 |   BIONIC_DISALLOW_COPY_AND_ASSIGN(PointerData); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 196 | }; |