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
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
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/histogram_functions.h"
13 #include "base/metrics/persistent_histogram_allocator.h"
14 #include "base/metrics/persistent_memory_allocator.h"
15
16 struct Environment {
EnvironmentEnvironment17 Environment() { logging::SetMinLogLevel(logging::LOGGING_FATAL); }
18 };
19
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)20 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
21 static Environment env;
22
23 // Copy data into a non-const vector.
24 std::vector<uint8_t> data_copy(data, data + size);
25
26 // PersistentMemoryAllocator segments must be aligned and an acceptable size.
27 if (!base::PersistentMemoryAllocator::IsMemoryAcceptable(
28 data_copy.data(), data_copy.size(), /*page_size=*/0,
29 /*readonly=*/false)) {
30 return 0;
31 }
32
33 std::unique_ptr<base::PersistentMemoryAllocator> memory_allocator =
34 std::make_unique<base::PersistentMemoryAllocator>(
35 data_copy.data(), data_copy.size(), /*page_size=*/0, /*id=*/0,
36 /*name=*/"",
37 /*access_mode=*/
38 base::FilePersistentMemoryAllocator::kReadWriteExisting);
39
40 std::unique_ptr<base::PersistentHistogramAllocator> histogram_allocator =
41 std::make_unique<base::PersistentHistogramAllocator>(
42 std::move(memory_allocator));
43
44 base::PersistentHistogramAllocator::Iterator hist_iter(
45 histogram_allocator.get());
46 while (true) {
47 std::unique_ptr<base::HistogramBase> histogram = hist_iter.GetNext();
48 if (!histogram) {
49 break;
50 }
51 histogram_allocator->MergeHistogramDeltaToStatisticsRecorder(
52 histogram.get());
53 }
54
55 return 0;
56 }
57