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