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/utils.h"
21 #include "perfetto/ext/tracing/core/basic_types.h"
22 #include "src/profiling/common/unwind_support.h"
23 #include "src/profiling/memory/shared_ring_buffer.h"
24 #include "src/profiling/memory/unwinding.h"
25 #include "src/profiling/memory/unwound_messages.h"
26
27 namespace perfetto {
28 namespace profiling {
29 namespace {
30
31 class NopDelegate : public UnwindingWorker::Delegate {
PostAllocRecord(AllocRecord)32 void PostAllocRecord(AllocRecord) override {}
PostFreeRecord(FreeRecord)33 void PostFreeRecord(FreeRecord) override {}
PostSocketDisconnected(DataSourceInstanceID,pid_t,SharedRingBuffer::Stats)34 void PostSocketDisconnected(DataSourceInstanceID,
35 pid_t,
36 SharedRingBuffer::Stats) override {}
37 };
38
FuzzUnwinding(const uint8_t * data,size_t size)39 int FuzzUnwinding(const uint8_t* data, size_t size) {
40 SharedRingBuffer::Buffer buf(const_cast<uint8_t*>(data), size);
41
42 pid_t self_pid = getpid();
43 DataSourceInstanceID id = 0;
44 UnwindingMetadata metadata(base::OpenFile("/proc/self/maps", O_RDONLY),
45 base::OpenFile("/proc/self/mem", O_RDONLY));
46
47 NopDelegate nop_delegate;
48 UnwindingWorker::HandleBuffer(buf, &metadata, id, self_pid, &nop_delegate);
49 return 0;
50 }
51
52 } // namespace
53 } // namespace profiling
54 } // namespace perfetto
55
56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
57
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
59 return perfetto::profiling::FuzzUnwinding(data, size);
60 }
61