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 /*symbolize_ksyms=*/false,
60 /*preserve_ftrace_buffer=*/false,
61 /*syscalls_returning_fd=*/{}};
62 ds_config.event_filter.AddEnabledEvent(
63 table->EventToFtraceId(GroupAndName("sched", "sched_switch")));
64 ds_config.event_filter.AddEnabledEvent(
65 table->EventToFtraceId(GroupAndName("ftrace", "print")));
66
67 NullTraceWriter null_writer;
68 auto compact_sched_buf = std::make_unique<CompactSchedBuffer>();
69 base::FlatSet<protos::pbzero::FtraceParseStatus> parse_errors;
70 uint64_t last_read_event_ts = 0;
71 CpuReader::ProcessPagesForDataSource(
72 &null_writer, &metadata, /*cpu=*/0, &ds_config, &parse_errors,
73 &last_read_event_ts, g_page,
74 /*pages_read=*/1, compact_sched_buf.get(), table, /*symbolizer*/ nullptr,
75 /*ftrace_clock_snapshot=*/nullptr,
76 protos::pbzero::FTRACE_CLOCK_UNSPECIFIED);
77 }
78
79 } // namespace perfetto
80
81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
82
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
84 perfetto::FuzzCpuReaderProcessPagesForDataSource(data, size);
85 return 0;
86 }
87