blob: 4388791eb7cd624a7342113725697eb19a867597 [file] [log] [blame]
Brendon Tiszka5c4a7862024-02-24 21:04:571// 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
danakj51d26a42024-04-25 14:23:565#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 Kasting134ef9af2024-12-28 02:30:0910#include "base/metrics/persistent_histogram_allocator.h"
11
Brendon Tiszka5c4a7862024-02-24 21:04:5712#include "base/logging.h"
13#include "base/metrics/histogram.h"
14#include "base/metrics/histogram_functions.h"
Brendon Tiszka5c4a7862024-02-24 21:04:5715#include "base/metrics/persistent_memory_allocator.h"
16
17struct Environment {
18 Environment() { logging::SetMinLogLevel(logging::LOGGING_FATAL); }
19};
20
21extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
22 static Environment env;
23
Brendon Tiszka06938302024-03-04 21:54:0324 // Copy data into a non-const vector.
25 std::vector<uint8_t> data_copy(data, data + size);
26
Brendon Tiszka5c4a7862024-02-24 21:04:5727 // PersistentMemoryAllocator segments must be aligned and an acceptable size.
28 if (!base::PersistentMemoryAllocator::IsMemoryAcceptable(
Brendon Tiszka06938302024-03-04 21:54:0329 data_copy.data(), data_copy.size(), /*page_size=*/0,
30 /*readonly=*/false)) {
Brendon Tiszka5c4a7862024-02-24 21:04:5731 return 0;
32 }
33
34 std::unique_ptr<base::PersistentMemoryAllocator> memory_allocator =
35 std::make_unique<base::PersistentMemoryAllocator>(
Brendon Tiszka06938302024-03-04 21:54:0336 data_copy.data(), data_copy.size(), /*page_size=*/0, /*id=*/0,
Brendon Tiszka5c4a7862024-02-24 21:04:5737 /*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}