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 {}
41 };
42 
FuzzUnwinding(const uint8_t * data,size_t size)43 int FuzzUnwinding(const uint8_t* data, size_t size) {
44   SharedRingBuffer::Buffer buf(const_cast<uint8_t*>(data), size, 0u);
45 
46   pid_t self_pid = getpid();
47   DataSourceInstanceID id = 0;
48   UnwindingMetadata metadata(base::OpenFile("/proc/self/maps", O_RDONLY),
49                              base::OpenFile("/proc/self/mem", O_RDONLY));
50 
51   NopDelegate nop_delegate;
52   UnwindingWorker::ClientData client_data{
53       id, {}, std::move(metadata), {}, {}, {}, {},
54   };
55 
56   AllocRecordArena arena;
57   UnwindingWorker::HandleBuffer(nullptr, &arena, buf, &client_data, self_pid,
58                                 &nop_delegate);
59   return 0;
60 }
61 
62 }  // namespace
63 }  // namespace profiling
64 }  // namespace perfetto
65 
66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
67 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
69   return perfetto::profiling::FuzzUnwinding(data, size);
70 }
71