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 <platform/bionic/macros.h>
42 #include <unwindstack/LocalUnwinder.h>
43 
44 #include "OptionData.h"
45 #include "UnwindBacktrace.h"
46 
47 extern bool* g_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) {
93     return GetEncodedSize(*g_zygote_child, size);
94   }
95   static size_t GetEncodedSize(bool child_alloc, size_t size) {
96     return size | ((child_alloc) ? (1U << 31) : 0);
97   }
98   static size_t MaxSize() { return (1U << 31) - 1; }
99 };
100 
101 struct FreePointerInfoType {
102   uintptr_t pointer;
103   size_t hash_index;
104 };
105 
106 struct ListInfoType {
107   uintptr_t pointer;
108   size_t num_allocations;
109   size_t size;
110   bool zygote_child_alloc;
111   FrameInfoType* frame_info;
112   std::vector<unwindstack::LocalFrameData>* backtrace_info;
113 };
114 
115 class PointerData : public OptionData {
116  public:
117   explicit PointerData(DebugData* debug_data);
118   virtual ~PointerData() = default;
119 
120   bool Initialize(const Config& config);
121 
122   inline size_t alloc_offset() { return alloc_offset_; }
123 
124   bool ShouldBacktrace() { return backtrace_enabled_ == 1; }
125   void ToggleBacktraceEnabled() { backtrace_enabled_.fetch_xor(1); }
126 
127   void EnableDumping() { backtrace_dump_ = true; }
128   bool ShouldDumpAndReset() {
129     bool expected = true;
130     return backtrace_dump_.compare_exchange_strong(expected, false);
131   }
132 
133   void PrepareFork();
134   void PostForkParent();
135   void PostForkChild();
136 
137   static size_t AddBacktrace(size_t num_frames);
138   static void RemoveBacktrace(size_t hash_index);
139 
140   static void Add(const void* pointer, size_t size);
141   static void Remove(const void* pointer);
142 
143   typedef std::unordered_map<uintptr_t, PointerInfoType>::iterator iterator;
144   static iterator begin() { return pointers_.begin(); }
145   static iterator end() { return pointers_.end(); }
146 
147   static void* AddFreed(const void* pointer);
148   static void LogFreeError(const FreePointerInfoType& info, size_t usable_size);
149   static void LogFreeBacktrace(const void* ptr);
150   static void VerifyFreedPointer(const FreePointerInfoType& info);
151   static void VerifyAllFreed();
152 
153   static void GetAllocList(std::vector<ListInfoType>* list);
154   static void LogLeaks();
155   static void DumpLiveToFile(int fd);
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);
166   static void LogBacktrace(size_t hash_index);
167 
168   static void GetList(std::vector<ListInfoType>* list, bool only_with_backtrace);
169   static void GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace);
170 
171   size_t alloc_offset_ = 0;
172   std::vector<uint8_t> cmp_mem_;
173 
174   static std::atomic_uint8_t backtrace_enabled_;
175 
176   static std::atomic_bool backtrace_dump_;
177 
178   static std::mutex pointer_mutex_;
179   static std::unordered_map<uintptr_t, PointerInfoType> pointers_;
180 
181   static std::mutex frame_mutex_;
182   static std::unordered_map<FrameKeyType, size_t> key_to_index_;
183   static std::unordered_map<size_t, FrameInfoType> frames_;
184   static std::unordered_map<size_t, std::vector<unwindstack::LocalFrameData>> backtraces_info_;
185   static size_t cur_hash_index_;
186 
187   static std::mutex free_pointer_mutex_;
188   static std::deque<FreePointerInfoType> free_pointers_;
189 
190   BIONIC_DISALLOW_COPY_AND_ASSIGN(PointerData);
191 };
192