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 <cstdint>
19
20 #include <algorithm>
21
22 #include "perfetto/base/flat_set.h"
23 #include "perfetto/base/logging.h"
24 #include "perfetto/ext/base/utils.h"
25 #include "src/traced/probes/ftrace/cpu_reader.h"
26 #include "src/traced/probes/ftrace/ftrace_config_muxer.h"
27 #include "src/traced/probes/ftrace/test/cpu_reader_support.h"
28 #include "src/tracing/core/null_trace_writer.h"
29
30 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
31 #include "protos/perfetto/trace/ftrace/ftrace_stats.pbzero.h"
32
33 namespace perfetto {
34
35 using perfetto::protos::pbzero::FtraceEventBundle;
36
37 void FuzzCpuReaderProcessPagesForDataSource(const uint8_t* data, size_t size);
38
39 // TODO(rsavitski): make the fuzzer generate multi-page payloads.
FuzzCpuReaderProcessPagesForDataSource(const uint8_t * data,size_t size)40 void FuzzCpuReaderProcessPagesForDataSource(const uint8_t* data, size_t size) {
41 ProtoTranslationTable* table = GetTable("synthetic");
42 if (!table) {
43 PERFETTO_FATAL(
44 "Could not read table. "
45 "This fuzzer must be run in the root directory.");
46 }
47
48 static uint8_t* g_page = new uint8_t[base::GetSysPageSize()];
49 memset(g_page, 0, base::GetSysPageSize());
50 memcpy(g_page, data, std::min(size_t(base::GetSysPageSize()), size));
51
52 FtraceMetadata metadata{};
53 FtraceDataSourceConfig ds_config{/*event_filter=*/EventFilter{},
54 /*syscall_filter=*/EventFilter{},
55 DisabledCompactSchedConfigForTesting(),
56 /*print_filter=*/std::nullopt,
57 /*atrace_apps=*/{},
58 /*atrace_categories=*/{},
59 /*atrace_categories_prefer_track_event=*/{},
60 /*symbolize_ksyms=*/false,
61 /*preserve_ftrace_buffer=*/false,
62 /*syscalls_returning_fd=*/{}};
63 ds_config.event_filter.AddEnabledEvent(
64 table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
65 ds_config.event_filter.AddEnabledEvent(
66 table->EventToFtraceId(GroupAndName("ftrace", "print")));
67
68 NullTraceWriter null_writer;
69 auto compact_sched_buf = std::make_unique<CompactSchedBuffer>();
70 base::FlatSet<protos::pbzero::FtraceParseStatus> parse_errors;
71 uint64_t last_read_event_ts = 0;
72 CpuReader::ProcessPagesForDataSource(
73 &null_writer, &metadata, /*cpu=*/0, &ds_config, &parse_errors,
74 &last_read_event_ts, g_page,
75 /*pages_read=*/1, compact_sched_buf.get(), table, /*symbolizer*/ nullptr,
76 /*ftrace_clock_snapshot=*/nullptr,
77 protos::pbzero::FTRACE_CLOCK_UNSPECIFIED);
78 }
79
80 } // namespace perfetto
81
82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
83
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
85 perfetto::FuzzCpuReaderProcessPagesForDataSource(data, size);
86 return 0;
87 }
88