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 <algorithm>
21
22 #include "perfetto/base/logging.h"
23 #include "perfetto/base/utils.h"
24 #include "perfetto/protozero/scattered_stream_null_delegate.h"
25 #include "perfetto/protozero/scattered_stream_writer.h"
26 #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
27 #include "src/traced/probes/ftrace/cpu_reader.h"
28 #include "test/cpu_reader_support.h"
29
30 namespace perfetto {
31 namespace {
32
33 uint8_t g_page[base::kPageSize];
34
35 } // namespace
36
37 using perfetto::protos::pbzero::FtraceEventBundle;
38
39 void FuzzCpuReaderParsePage(const uint8_t* data, size_t size);
40
FuzzCpuReaderParsePage(const uint8_t * data,size_t size)41 void FuzzCpuReaderParsePage(const uint8_t* data, size_t size) {
42 protozero::ScatteredStreamWriterNullDelegate delegate(base::kPageSize);
43 protozero::ScatteredStreamWriter stream(&delegate);
44 FtraceEventBundle writer;
45
46 ProtoTranslationTable* table = GetTable("synthetic");
47 if (!table) {
48 PERFETTO_FATAL(
49 "Could not read table. "
50 "This fuzzer must be run in the root directory.");
51 }
52 memset(g_page, 0, base::kPageSize);
53 memcpy(g_page, data, std::min(base::kPageSize, size));
54
55 EventFilter filter;
56 filter.AddEnabledEvent(
57 table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
58 filter.AddEnabledEvent(
59 table->EventToFtraceId(GroupAndName("ftrace", "print")));
60
61 writer.Reset(&stream);
62 FtraceMetadata metadata{};
63 CpuReader::ParsePage(g_page, &filter, &writer, table, &metadata);
64 }
65
66 } // namespace perfetto
67
68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
69
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
71 perfetto::FuzzCpuReaderParsePage(data, size);
72 return 0;
73 }
74