xref: /aosp_15_r20/external/perfetto/src/profiling/memory/unwinding_fuzzer.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stddef.h>
18 #include <stdint.h>
19 
20 #include "perfetto/ext/base/file_utils.h"
21 #include "perfetto/ext/base/utils.h"
22 #include "perfetto/ext/tracing/core/basic_types.h"
23 #include "src/profiling/common/unwind_support.h"
24 #include "src/profiling/memory/shared_ring_buffer.h"
25 #include "src/profiling/memory/unwinding.h"
26 #include "src/profiling/memory/unwound_messages.h"
27 
28 namespace perfetto {
29 namespace profiling {
30 namespace {
31 
32 class NopDelegate : public UnwindingWorker::Delegate {
PostAllocRecord(UnwindingWorker *,std::unique_ptr<AllocRecord>)33   void PostAllocRecord(UnwindingWorker*,
34                        std::unique_ptr<AllocRecord>) override {}
PostFreeRecord(UnwindingWorker *,std::vector<FreeRecord>)35   void PostFreeRecord(UnwindingWorker*, std::vector<FreeRecord>) override {}
PostHeapNameRecord(UnwindingWorker *,HeapNameRecord)36   void PostHeapNameRecord(UnwindingWorker*, HeapNameRecord) override {}
PostSocketDisconnected(UnwindingWorker *,DataSourceInstanceID,pid_t,SharedRingBuffer::Stats)37   void PostSocketDisconnected(UnwindingWorker*,
38                               DataSourceInstanceID,
39                               pid_t,
40                               SharedRingBuffer::Stats) override {}
PostDrainDone(UnwindingWorker *,DataSourceInstanceID)41   void PostDrainDone(UnwindingWorker*, DataSourceInstanceID) override {}
42 };
43 
FuzzUnwinding(const uint8_t * data,size_t size)44 int FuzzUnwinding(const uint8_t* data, size_t size) {
45   SharedRingBuffer::Buffer buf(const_cast<uint8_t*>(data), size, 0u);
46 
47   pid_t self_pid = getpid();
48   DataSourceInstanceID id = 0;
49   UnwindingMetadata metadata(base::OpenFile("/proc/self/maps", O_RDONLY),
50                              base::OpenFile("/proc/self/mem", O_RDONLY));
51 
52   NopDelegate nop_delegate;
53   UnwindingWorker::ClientData client_data{id,
54                                           /*sock=*/{},
55                                           std::move(metadata),
56                                           /*shmem=*/{},
57                                           /*client_config=*/{},
58                                           /*stream_allocations=*/false,
59                                           /*drain_bytes=*/0,
60                                           /*free_records=*/{}};
61 
62   AllocRecordArena arena;
63   UnwindingWorker::HandleBuffer(nullptr, &arena, buf, &client_data, self_pid,
64                                 &nop_delegate);
65   return 0;
66 }
67 
68 }  // namespace
69 }  // namespace profiling
70 }  // namespace perfetto
71 
72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
73 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
75   return perfetto::profiling::FuzzUnwinding(data, size);
76 }
77