Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/metrics/persistent_histogram_allocator.h" | 5 #include "base/metrics/persistent_histogram_allocator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 &histogram_data_ptr->logged_metadata); | 561 &histogram_data_ptr->logged_metadata); |
| 562 DCHECK(histogram); | 562 DCHECK(histogram); |
| 563 histogram->SetFlags(histogram_data_ptr->flags); | 563 histogram->SetFlags(histogram_data_ptr->flags); |
| 564 RecordCreateHistogramResult(CREATE_HISTOGRAM_SUCCESS); | 564 RecordCreateHistogramResult(CREATE_HISTOGRAM_SUCCESS); |
| 565 return histogram; | 565 return histogram; |
| 566 } | 566 } |
| 567 | 567 |
| 568 // Copy the histogram_data to local storage because anything in persistent | 568 // Copy the histogram_data to local storage because anything in persistent |
| 569 // memory cannot be trusted as it could be changed at any moment by a | 569 // memory cannot be trusted as it could be changed at any moment by a |
| 570 // malicious actor that shares access. The contents of histogram_data are | 570 // malicious actor that shares access. The contents of histogram_data are |
| 571 // validated below; the local copy is to ensure that the contents cannot | 571 // validated below; the local copy of relevant fields is to ensure that the |
| 572 // be externally changed between validation and use. | 572 // contents cannot be externally changed between validation and use. A direct |
| 573 PersistentHistogramData histogram_data = *histogram_data_ptr; | 573 // copy isn't possible because of the counts_ref field that must be read |
| 574 // using an atomic operation. | |
| 575 PersistentHistogramData histogram_data; | |
|
Alexei Svitkine (slow)
2017/05/11 20:20:07
Can you add a CopyTo() function instead? This way,
bcwhite
2017/05/11 20:23:16
I looked at that path... but it was going to get
Alexei Svitkine (slow)
2017/05/11 20:27:55
That SGTM.
bcwhite
2017/05/11 21:03:29
Done.
| |
| 576 histogram_data.histogram_type = histogram_data_ptr->histogram_type; | |
| 577 histogram_data.flags = histogram_data_ptr->flags; | |
| 578 histogram_data.minimum = histogram_data_ptr->minimum; | |
| 579 histogram_data.maximum = histogram_data_ptr->maximum; | |
| 580 histogram_data.bucket_count = histogram_data_ptr->bucket_count; | |
| 581 histogram_data.ranges_ref = histogram_data_ptr->ranges_ref; | |
| 582 histogram_data.ranges_checksum = histogram_data_ptr->ranges_checksum; | |
| 574 | 583 |
| 575 HistogramBase::Sample* ranges_data = | 584 HistogramBase::Sample* ranges_data = |
| 576 memory_allocator_->GetAsArray<HistogramBase::Sample>( | 585 memory_allocator_->GetAsArray<HistogramBase::Sample>( |
| 577 histogram_data.ranges_ref, kTypeIdRangesArray, | 586 histogram_data.ranges_ref, kTypeIdRangesArray, |
| 578 PersistentMemoryAllocator::kSizeAny); | 587 PersistentMemoryAllocator::kSizeAny); |
| 579 | 588 |
| 580 const uint32_t max_buckets = | 589 const uint32_t max_buckets = |
| 581 std::numeric_limits<uint32_t>::max() / sizeof(HistogramBase::Sample); | 590 std::numeric_limits<uint32_t>::max() / sizeof(HistogramBase::Sample); |
| 582 size_t required_bytes = | 591 size_t required_bytes = |
| 583 (histogram_data.bucket_count + 1) * sizeof(HistogramBase::Sample); | 592 (histogram_data.bucket_count + 1) * sizeof(HistogramBase::Sample); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 599 NOTREACHED(); | 608 NOTREACHED(); |
| 600 return nullptr; | 609 return nullptr; |
| 601 } | 610 } |
| 602 const BucketRanges* ranges = | 611 const BucketRanges* ranges = |
| 603 StatisticsRecorder::RegisterOrDeleteDuplicateRanges( | 612 StatisticsRecorder::RegisterOrDeleteDuplicateRanges( |
| 604 created_ranges.release()); | 613 created_ranges.release()); |
| 605 | 614 |
| 606 size_t counts_bytes = | 615 size_t counts_bytes = |
| 607 CalculateRequiredCountsBytes(histogram_data.bucket_count); | 616 CalculateRequiredCountsBytes(histogram_data.bucket_count); |
| 608 PersistentMemoryAllocator::Reference counts_ref = | 617 PersistentMemoryAllocator::Reference counts_ref = |
| 609 subtle::NoBarrier_Load(&histogram_data.counts_ref); | 618 subtle::NoBarrier_Load(&histogram_data_ptr->counts_ref); |
| 610 if (counts_bytes == 0 || | 619 if (counts_bytes == 0 || |
| 611 (counts_ref != 0 && | 620 (counts_ref != 0 && |
| 612 memory_allocator_->GetAllocSize(counts_ref) < counts_bytes)) { | 621 memory_allocator_->GetAllocSize(counts_ref) < counts_bytes)) { |
| 613 RecordCreateHistogramResult(CREATE_HISTOGRAM_INVALID_COUNTS_ARRAY); | 622 RecordCreateHistogramResult(CREATE_HISTOGRAM_INVALID_COUNTS_ARRAY); |
| 614 NOTREACHED(); | 623 NOTREACHED(); |
| 615 return nullptr; | 624 return nullptr; |
| 616 } | 625 } |
| 617 | 626 |
| 618 // The "counts" data (including both samples and logged samples) is a delayed | 627 // The "counts" data (including both samples and logged samples) is a delayed |
| 619 // persistent allocation meaning that though its size and storage for a | 628 // persistent allocation meaning that though its size and storage for a |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 960 while (true) { | 969 while (true) { |
| 961 std::unique_ptr<HistogramBase> histogram = | 970 std::unique_ptr<HistogramBase> histogram = |
| 962 import_iterator_.GetNextWithIgnore(record_to_ignore); | 971 import_iterator_.GetNextWithIgnore(record_to_ignore); |
| 963 if (!histogram) | 972 if (!histogram) |
| 964 break; | 973 break; |
| 965 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); | 974 StatisticsRecorder::RegisterOrDeleteDuplicate(histogram.release()); |
| 966 } | 975 } |
| 967 } | 976 } |
| 968 | 977 |
| 969 } // namespace base | 978 } // namespace base |
| OLD | NEW |