Brendon Tiszka | 5c4a786 | 2024-02-24 21:04:57 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 51d26a4 | 2024-04-25 14:23:56 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 10 | #include "base/metrics/persistent_histogram_allocator.h" |
| 11 | |
Brendon Tiszka | 5c4a786 | 2024-02-24 21:04:57 | [diff] [blame] | 12 | #include "base/logging.h" |
| 13 | #include "base/metrics/histogram.h" |
| 14 | #include "base/metrics/histogram_functions.h" |
Brendon Tiszka | 5c4a786 | 2024-02-24 21:04:57 | [diff] [blame] | 15 | #include "base/metrics/persistent_memory_allocator.h" |
| 16 | |
| 17 | struct Environment { |
| 18 | Environment() { logging::SetMinLogLevel(logging::LOGGING_FATAL); } |
| 19 | }; |
| 20 | |
| 21 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 22 | static Environment env; |
| 23 | |
Brendon Tiszka | 0693830 | 2024-03-04 21:54:03 | [diff] [blame] | 24 | // Copy data into a non-const vector. |
| 25 | std::vector<uint8_t> data_copy(data, data + size); |
| 26 | |
Brendon Tiszka | 5c4a786 | 2024-02-24 21:04:57 | [diff] [blame] | 27 | // PersistentMemoryAllocator segments must be aligned and an acceptable size. |
| 28 | if (!base::PersistentMemoryAllocator::IsMemoryAcceptable( |
Brendon Tiszka | 0693830 | 2024-03-04 21:54:03 | [diff] [blame] | 29 | data_copy.data(), data_copy.size(), /*page_size=*/0, |
| 30 | /*readonly=*/false)) { |
Brendon Tiszka | 5c4a786 | 2024-02-24 21:04:57 | [diff] [blame] | 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | std::unique_ptr<base::PersistentMemoryAllocator> memory_allocator = |
| 35 | std::make_unique<base::PersistentMemoryAllocator>( |
Brendon Tiszka | 0693830 | 2024-03-04 21:54:03 | [diff] [blame] | 36 | data_copy.data(), data_copy.size(), /*page_size=*/0, /*id=*/0, |
Brendon Tiszka | 5c4a786 | 2024-02-24 21:04:57 | [diff] [blame] | 37 | /*name=*/"", |
| 38 | /*access_mode=*/ |
| 39 | base::FilePersistentMemoryAllocator::kReadWriteExisting); |
| 40 | |
| 41 | std::unique_ptr<base::PersistentHistogramAllocator> histogram_allocator = |
| 42 | std::make_unique<base::PersistentHistogramAllocator>( |
| 43 | std::move(memory_allocator)); |
| 44 | |
| 45 | base::PersistentHistogramAllocator::Iterator hist_iter( |
| 46 | histogram_allocator.get()); |
| 47 | while (true) { |
| 48 | std::unique_ptr<base::HistogramBase> histogram = hist_iter.GetNext(); |
| 49 | if (!histogram) { |
| 50 | break; |
| 51 | } |
| 52 | histogram_allocator->MergeHistogramDeltaToStatisticsRecorder( |
| 53 | histogram.get()); |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |