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/base/utils.h"
21 #include "perfetto/tracing/core/basic_types.h"
22 #include "src/profiling/memory/shared_ring_buffer.h"
23 #include "src/profiling/memory/unwinding.h"
24 #include "src/profiling/memory/unwound_messages.h"
25
26 namespace perfetto {
27 namespace profiling {
28 namespace {
29
30 class NopDelegate : public UnwindingWorker::Delegate {
PostAllocRecord(AllocRecord)31 void PostAllocRecord(AllocRecord) override {}
PostFreeRecord(FreeRecord)32 void PostFreeRecord(FreeRecord) override {}
PostSocketDisconnected(DataSourceInstanceID,pid_t,SharedRingBuffer::Stats)33 void PostSocketDisconnected(DataSourceInstanceID,
34 pid_t,
35 SharedRingBuffer::Stats) override {}
36 };
37
FuzzUnwinding(const uint8_t * data,size_t size)38 int FuzzUnwinding(const uint8_t* data, size_t size) {
39 SharedRingBuffer::Buffer buf(const_cast<uint8_t*>(data), size);
40
41 pid_t self_pid = getpid();
42 DataSourceInstanceID id = 0;
43 UnwindingMetadata metadata(self_pid,
44 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