Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: base/trace_event/process_memory_dump.cc

Issue 2006943003: [tracing] Sanitize process memory dumps for background mode (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@whitelist_mdp
Patch Set: Fix stripping. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/process_memory_dump.h" 5 #include "base/trace_event/process_memory_dump.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/process/process_metrics.h" 12 #include "base/process/process_metrics.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/trace_event/heap_profiler_heap_dump_writer.h" 14 #include "base/trace_event/heap_profiler_heap_dump_writer.h"
15 #include "base/trace_event/memory_infra_background_whitelist.h"
15 #include "base/trace_event/process_memory_totals.h" 16 #include "base/trace_event/process_memory_totals.h"
16 #include "base/trace_event/trace_event_argument.h" 17 #include "base/trace_event/trace_event_argument.h"
17 #include "build/build_config.h" 18 #include "build/build_config.h"
18 19
19 #if defined(OS_IOS) 20 #if defined(OS_IOS)
20 #include <sys/sysctl.h> 21 #include <sys/sysctl.h>
21 #endif 22 #endif
22 23
23 #if defined(OS_POSIX) 24 #if defined(OS_POSIX)
24 #include <sys/mman.h> 25 #include <sys/mman.h>
(...skipping 16 matching lines...) Expand all
41 } 42 }
42 43
43 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED) 44 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED)
44 size_t GetSystemPageCount(size_t mapped_size, size_t page_size) { 45 size_t GetSystemPageCount(size_t mapped_size, size_t page_size) {
45 return (mapped_size + page_size - 1) / page_size; 46 return (mapped_size + page_size - 1) / page_size;
46 } 47 }
47 #endif 48 #endif
48 49
49 } // namespace 50 } // namespace
50 51
52 // static
53 bool ProcessMemoryDump::is_black_hole_non_fatal_for_testing_ = false;
54
51 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED) 55 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED)
52 // static 56 // static
53 size_t ProcessMemoryDump::GetSystemPageSize() { 57 size_t ProcessMemoryDump::GetSystemPageSize() {
54 #if defined(OS_IOS) 58 #if defined(OS_IOS)
55 // On iOS, getpagesize() returns the user page sizes, but for allocating 59 // On iOS, getpagesize() returns the user page sizes, but for allocating
56 // arrays for mincore(), kernel page sizes is needed. sysctlbyname() should 60 // arrays for mincore(), kernel page sizes is needed. sysctlbyname() should
57 // be used for this. Refer to crbug.com/542671 and Apple rdar://23651782 61 // be used for this. Refer to crbug.com/542671 and Apple rdar://23651782
58 int pagesize; 62 int pagesize;
59 size_t pagesize_len; 63 size_t pagesize_len;
60 int status = sysctlbyname("vm.pagesize", NULL, &pagesize_len, nullptr, 0); 64 int status = sysctlbyname("vm.pagesize", NULL, &pagesize_len, nullptr, 0);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 DCHECK(!failure); 145 DCHECK(!failure);
142 if (failure) { 146 if (failure) {
143 total_resident_size = 0; 147 total_resident_size = 0;
144 LOG(ERROR) << "CountResidentBytes failed. The resident size is invalid"; 148 LOG(ERROR) << "CountResidentBytes failed. The resident size is invalid";
145 } 149 }
146 return total_resident_size; 150 return total_resident_size;
147 } 151 }
148 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) 152 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED)
149 153
150 ProcessMemoryDump::ProcessMemoryDump( 154 ProcessMemoryDump::ProcessMemoryDump(
151 scoped_refptr<MemoryDumpSessionState> session_state) 155 scoped_refptr<MemoryDumpSessionState> session_state,
156 const MemoryDumpArgs& dump_args)
152 : has_process_totals_(false), 157 : has_process_totals_(false),
153 has_process_mmaps_(false), 158 has_process_mmaps_(false),
154 session_state_(std::move(session_state)) {} 159 session_state_(std::move(session_state)),
160 dump_args_(dump_args) {}
155 161
156 ProcessMemoryDump::~ProcessMemoryDump() {} 162 ProcessMemoryDump::~ProcessMemoryDump() {}
157 163
158 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump( 164 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump(
159 const std::string& absolute_name) { 165 const std::string& absolute_name) {
160 return AddAllocatorDumpInternal( 166 return AddAllocatorDumpInternal(
161 WrapUnique(new MemoryAllocatorDump(absolute_name, this))); 167 WrapUnique(new MemoryAllocatorDump(absolute_name, this)));
162 } 168 }
163 169
164 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump( 170 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump(
165 const std::string& absolute_name, 171 const std::string& absolute_name,
166 const MemoryAllocatorDumpGuid& guid) { 172 const MemoryAllocatorDumpGuid& guid) {
167 return AddAllocatorDumpInternal( 173 return AddAllocatorDumpInternal(
168 WrapUnique(new MemoryAllocatorDump(absolute_name, this, guid))); 174 WrapUnique(new MemoryAllocatorDump(absolute_name, this, guid)));
169 } 175 }
170 176
171 MemoryAllocatorDump* ProcessMemoryDump::AddAllocatorDumpInternal( 177 MemoryAllocatorDump* ProcessMemoryDump::AddAllocatorDumpInternal(
172 std::unique_ptr<MemoryAllocatorDump> mad) { 178 std::unique_ptr<MemoryAllocatorDump> mad) {
179 // In background mode return the black hole dump, if invalid dump name is
180 // given.
181 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND &&
182 !IsMemoryAllocatorDumpNameWhitelisted(mad->absolute_name())) {
183 return GetBlackHoleMad();
184 }
185
173 auto insertion_result = allocator_dumps_.insert( 186 auto insertion_result = allocator_dumps_.insert(
174 std::make_pair(mad->absolute_name(), std::move(mad))); 187 std::make_pair(mad->absolute_name(), std::move(mad)));
175 MemoryAllocatorDump* inserted_mad = insertion_result.first->second.get(); 188 MemoryAllocatorDump* inserted_mad = insertion_result.first->second.get();
176 DCHECK(insertion_result.second) << "Duplicate name: " 189 DCHECK(insertion_result.second) << "Duplicate name: "
177 << inserted_mad->absolute_name(); 190 << inserted_mad->absolute_name();
178 return inserted_mad; 191 return inserted_mad;
179 } 192 }
180 193
181 MemoryAllocatorDump* ProcessMemoryDump::GetAllocatorDump( 194 MemoryAllocatorDump* ProcessMemoryDump::GetAllocatorDump(
182 const std::string& absolute_name) const { 195 const std::string& absolute_name) const {
183 auto it = allocator_dumps_.find(absolute_name); 196 auto it = allocator_dumps_.find(absolute_name);
184 return it == allocator_dumps_.end() ? nullptr : it->second.get(); 197 if (it != allocator_dumps_.end())
198 return it->second.get();
199 if (black_hole_mad_)
200 return black_hole_mad_.get();
201 return nullptr;
185 } 202 }
186 203
187 MemoryAllocatorDump* ProcessMemoryDump::GetOrCreateAllocatorDump( 204 MemoryAllocatorDump* ProcessMemoryDump::GetOrCreateAllocatorDump(
188 const std::string& absolute_name) { 205 const std::string& absolute_name) {
189 MemoryAllocatorDump* mad = GetAllocatorDump(absolute_name); 206 MemoryAllocatorDump* mad = GetAllocatorDump(absolute_name);
190 return mad ? mad : CreateAllocatorDump(absolute_name); 207 return mad ? mad : CreateAllocatorDump(absolute_name);
191 } 208 }
192 209
193 MemoryAllocatorDump* ProcessMemoryDump::CreateSharedGlobalAllocatorDump( 210 MemoryAllocatorDump* ProcessMemoryDump::CreateSharedGlobalAllocatorDump(
194 const MemoryAllocatorDumpGuid& guid) { 211 const MemoryAllocatorDumpGuid& guid) {
212 // Global dumps are disabled in background mode.
213 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND)
214 return GetBlackHoleMad();
215
195 // A shared allocator dump can be shared within a process and the guid could 216 // A shared allocator dump can be shared within a process and the guid could
196 // have been created already. 217 // have been created already.
197 MemoryAllocatorDump* mad = GetSharedGlobalAllocatorDump(guid); 218 MemoryAllocatorDump* mad = GetSharedGlobalAllocatorDump(guid);
198 if (mad) { 219 if (mad) {
199 // The weak flag is cleared because this method should create a non-weak 220 // The weak flag is cleared because this method should create a non-weak
200 // dump. 221 // dump.
201 mad->clear_flags(MemoryAllocatorDump::Flags::WEAK); 222 mad->clear_flags(MemoryAllocatorDump::Flags::WEAK);
202 return mad; 223 return mad;
203 } 224 }
204 return CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid); 225 return CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid);
205 } 226 }
206 227
207 MemoryAllocatorDump* ProcessMemoryDump::CreateWeakSharedGlobalAllocatorDump( 228 MemoryAllocatorDump* ProcessMemoryDump::CreateWeakSharedGlobalAllocatorDump(
208 const MemoryAllocatorDumpGuid& guid) { 229 const MemoryAllocatorDumpGuid& guid) {
230 // Global dumps are disabled in background mode.
231 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND)
232 return GetBlackHoleMad();
233
209 MemoryAllocatorDump* mad = GetSharedGlobalAllocatorDump(guid); 234 MemoryAllocatorDump* mad = GetSharedGlobalAllocatorDump(guid);
210 if (mad) 235 if (mad)
211 return mad; 236 return mad;
212 mad = CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid); 237 mad = CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid);
213 mad->set_flags(MemoryAllocatorDump::Flags::WEAK); 238 mad->set_flags(MemoryAllocatorDump::Flags::WEAK);
214 return mad; 239 return mad;
215 } 240 }
216 241
217 MemoryAllocatorDump* ProcessMemoryDump::GetSharedGlobalAllocatorDump( 242 MemoryAllocatorDump* ProcessMemoryDump::GetSharedGlobalAllocatorDump(
218 const MemoryAllocatorDumpGuid& guid) const { 243 const MemoryAllocatorDumpGuid& guid) const {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 } 346 }
322 347
323 void ProcessMemoryDump::AddOwnershipEdge( 348 void ProcessMemoryDump::AddOwnershipEdge(
324 const MemoryAllocatorDumpGuid& source, 349 const MemoryAllocatorDumpGuid& source,
325 const MemoryAllocatorDumpGuid& target) { 350 const MemoryAllocatorDumpGuid& target) {
326 AddOwnershipEdge(source, target, 0 /* importance */); 351 AddOwnershipEdge(source, target, 0 /* importance */);
327 } 352 }
328 353
329 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source, 354 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source,
330 const std::string& target_node_name) { 355 const std::string& target_node_name) {
356 // Do not create new dumps for suballocations in background mode.
357 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND)
358 return;
359
331 std::string child_mad_name = target_node_name + "/__" + source.ToString(); 360 std::string child_mad_name = target_node_name + "/__" + source.ToString();
332 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name); 361 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name);
333 AddOwnershipEdge(source, target_child_mad->guid()); 362 AddOwnershipEdge(source, target_child_mad->guid());
334 } 363 }
335 364
365 MemoryAllocatorDump* ProcessMemoryDump::GetBlackHoleMad() {
366 DCHECK(is_black_hole_non_fatal_for_testing_);
367 if (!black_hole_mad_)
368 black_hole_mad_.reset(new MemoryAllocatorDump("discarded", this));
369 return black_hole_mad_.get();
370 }
371
336 } // namespace trace_event 372 } // namespace trace_event
337 } // namespace base 373 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/process_memory_dump.h ('k') | base/trace_event/process_memory_dump_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698