1 /*
2  * Copyright 2020 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 <android-base/file.h>
18 #include <fuzzer/FuzzedDataProvider.h>
19 
20 #include <iostream>
21 
22 #include "PerformanceProfiler.h"
23 #include "ProcStatCollector.h"
24 #include "UidIoStatsCollector.h"
25 
26 namespace android {
27 namespace automotive {
28 namespace watchdog {
29 
30 using ::android::base::WriteStringToFile;
31 
32 struct statsInfo {
33     std::string instr[13];
34     int len[13];
35 };
36 
37 statsInfo info = {{"cpu", "procs_running", "procs_blocked", "cpu0", "cpu1", "cpu2", "cpu3", "intr",
38                    "ctxt", "btime", "process", "procs_sleeping", "softirq"},
39                   {10, 1, 1, 10, 10, 10, 10, 31, 1, 1, 1, 1, 11}};
40 
LLVMFuzzerTestOneInput(const uint8_t * data,std::size_t size)41 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, std::size_t size) {
42     FuzzedDataProvider fdp(data, size);
43     std::string uidIoStatsSnapshot;
44     std::string procStatsSnapshot;
45 
46     // Prepare for UidIoStatsCollector data
47     int count = 0;
48     while (fdp.remaining_bytes() > (size / 2)) {
49         uint64_t val = fdp.ConsumeIntegral<uint64_t>();
50         uidIoStatsSnapshot += (((count % 11) == 0 ? "" : " ") + std::to_string(val));
51         if (count > 0 && count % 11 == 10) {
52             uidIoStatsSnapshot += "\n";
53         }
54         count++;
55     }
56 
57     // Prepare for ProcStats data
58     while (fdp.remaining_bytes() > 5) {
59         uint32_t choose = fdp.ConsumeIntegralInRange<uint32_t>(0, 3);
60         uint32_t idx = choose;
61         switch (choose) {
62             case 0:
63             case 1:
64             case 2:
65                 break;
66             case 3:
67                 idx = fdp.ConsumeIntegralInRange<uint32_t>(3, 12);
68                 break;
69             default:
70                 break;
71         }
72         procStatsSnapshot += info.instr[idx];
73         for (int i = 0; i < info.len[idx]; i++) {
74             uint64_t val = fdp.ConsumeIntegral<uint64_t>();
75             procStatsSnapshot += " " + std::to_string(val);
76         }
77         procStatsSnapshot += "\n";
78     }
79 
80     if (uidIoStatsSnapshot.size() > 0 && procStatsSnapshot.size() > 0) {
81       // Test UidIoStatsCollector
82       TemporaryFile tf1;
83       WriteStringToFile(uidIoStatsSnapshot, tf1.path);
84       sp<UidIoStatsCollector> uidIoStatsCollector =
85           sp<UidIoStatsCollector>::make(tf1.path);
86       uidIoStatsCollector->collect();
87       // Test procStatCollector
88       TemporaryFile tf2;
89       WriteStringToFile(procStatsSnapshot, tf2.path);
90       sp<ProcStatCollector> procStatCollector =
91           sp<ProcStatCollector>::make(tf2.path);
92       procStatCollector->collect();
93     }
94     return 0;
95 }
96 
97 }  // namespace watchdog
98 }  // namespace automotive
99 }  // namespace android
100