• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ClientApi_fuzzer.h"
17 
18 using namespace OHOS::Developtools::HiPerf;
19 
20 namespace OHOS {
21 const static int32_t MAX_TEST_ARGS_NUMBER = 20;
22 const static size_t MAX_TEST_ARGS_LEN = 4096;
DataToStringVector(const uint8_t * data,size_t size)23 std::vector<std::string> DataToStringVector(const uint8_t *data, size_t size)
24 {
25     const char *argptr = reinterpret_cast<const char *>(data);
26     const char *argsdata = argptr;
27     std::vector<std::string> argv;
28     // argv[*]
29     for (size_t i = 0; i < std::min(MAX_TEST_ARGS_LEN, size); i++) {
30         if (argsdata[i] == '\0') {
31             argv.emplace_back(argptr);
32             argptr = &argsdata[i + 1];
33         }
34         if (argv.size() > MAX_TEST_ARGS_NUMBER) {
35             break;
36         }
37     }
38     return argv;
39 }
40 
DataToPidVector(const uint8_t * data,size_t size)41 std::vector<pid_t> DataToPidVector(const uint8_t *data, size_t size)
42 {
43     std::vector<pid_t> argv;
44     // argv[*]
45     for (size_t i = 0; i < std::min(MAX_TEST_ARGS_LEN, size); i++) {
46         argv.emplace_back(static_cast<int>(data[i]));
47         if (argv.size() > MAX_TEST_ARGS_NUMBER) {
48             break;
49         }
50     }
51     return argv;
52 }
53 
FuzzClientApiOption(const uint8_t * data,size_t size)54 bool FuzzClientApiOption(const uint8_t *data, size_t size)
55 {
56     HiperfClient::RecordOption opt;
57     std::string stringArg(reinterpret_cast<const char *>(data), size);
58     std::vector<std::string> stringArgs = DataToStringVector(data, size);
59     std::vector<pid_t> pids = DataToPidVector(data, size);
60 
61     opt.SetOutputFilename(stringArg);
62     opt.SetTimeStopSec(size);
63     opt.SetFrequency(size);
64     opt.SetPeriod(size);
65     opt.SetSelectEvents(stringArgs);
66     opt.SetSelectGroups(stringArgs);
67     opt.SetSelectPids(pids);
68     opt.SetSelectTids(pids);
69     opt.SetCpuPercent(size);
70     opt.SetCallGraph(stringArg);
71     opt.SetSymbolDir(stringArg);
72     opt.SetDataLimit(stringArg);
73     opt.SetAppPackage(stringArg);
74     opt.SetClockId(stringArg);
75     opt.SetVecBranchSampleTypes(stringArgs);
76     opt.SetMmapPages(size);
77     opt.GetOptionVecString();
78 
79     HiperfClient::Client myHiperf;
80     myHiperf.Start(opt);
81     myHiperf.Stop();
82     return 0;
83 }
84 
FuzzClientApiClient(const uint8_t * data,size_t size)85 bool FuzzClientApiClient(const uint8_t *data, size_t size)
86 {
87     HiperfClient::Client myHiperf;
88     std::string stringArg(reinterpret_cast<const char *>(data), size);
89     std::vector<std::string> stringArgs = DataToStringVector(data, size);
90 
91     myHiperf.Setup(stringArg);
92     myHiperf.IsReady();
93     myHiperf.Start(stringArgs);
94     myHiperf.Pause();
95     myHiperf.Resume();
96     myHiperf.Stop();
97     return 0;
98 }
99 } // namespace OHOS
100 
101 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)102 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
103 {
104 /* Run your code on data */
105 #ifdef DEBUG_HIPERF_FUZZ
106     ScopeDebugLevel mix(LEVEL_DEBUG, true);
107     DebugLogger::GetInstance()->Disable(false);
108 #else
109     OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w");
110 #endif
111 
112     OHOS::FuzzClientApiClient(data, size);
113     OHOS::FuzzClientApiOption(data, size);
114     return 0;
115 }
116