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/ext/base/utils.h"
24 #include "perfetto/protozero/scattered_stream_null_delegate.h"
25 #include "perfetto/protozero/scattered_stream_writer.h"
26 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
27 #include "src/traced/probes/ftrace/cpu_reader.h"
28 #include "src/traced/probes/ftrace/ftrace_config_muxer.h"
29 #include "src/traced/probes/ftrace/test/cpu_reader_support.h"
30 #include "src/tracing/core/null_trace_writer.h"
31 
32 namespace perfetto {
33 namespace {
34 
35 uint8_t g_page[base::kPageSize];
36 
37 }  // namespace
38 
39 using perfetto::protos::pbzero::FtraceEventBundle;
40 
41 void FuzzCpuReaderParsePage(const uint8_t* data, size_t size);
42 void FuzzCpuReaderProcessPagesForDataSource(const uint8_t* data, size_t size);
43 
44 // TODO(rsavitski): make the fuzzer generate multi-page payloads.
FuzzCpuReaderProcessPagesForDataSource(const uint8_t * data,size_t size)45 void FuzzCpuReaderProcessPagesForDataSource(const uint8_t* data, size_t size) {
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   FtraceMetadata metadata{};
56   FtraceDataSourceConfig ds_config{EventFilter{},
57                                    DisabledCompactSchedConfigForTesting(),
58                                    {},
59                                    {},
60                                    /*symbolize_ksyms=*/false};
61   ds_config.event_filter.AddEnabledEvent(
62       table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
63   ds_config.event_filter.AddEnabledEvent(
64       table->EventToFtraceId(GroupAndName("ftrace", "print")));
65 
66   NullTraceWriter null_writer;
67   CpuReader::ProcessPagesForDataSource(&null_writer, &metadata, /*cpu=*/0,
68                                        &ds_config, g_page, /*pages_read=*/1,
69                                        table, /*symbolizer*/ nullptr);
70 }
71 
72 }  // namespace perfetto
73 
74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
75 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
77   perfetto::FuzzCpuReaderProcessPagesForDataSource(data, size);
78   return 0;
79 }
80