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 "CommandLine_fuzzer.h"
17 #include "subcommand_help.h"
18 #include "subcommand_list.h"
19 #include "subcommand_record.h"
20 #include "subcommand_report.h"
21 #include "subcommand_stat.h"
22
23 namespace OHOS {
24 const static int32_t MAX_TEST_ARGS_NUMBER = 20;
25 const static size_t MAX_TEST_ARGS_LEN = 4096;
26
FuzzCommandLine(const char * subcommand,const uint8_t * data,size_t size)27 bool FuzzCommandLine(const char *subcommand, const uint8_t *data, size_t size)
28 {
29 char buf[DATA_MAX_SIZE] = { 0 };
30 if (memcpy_s(buf, sizeof(buf) - 1, data, size) != 0) { // 1 : make sure end of '\0'
31 return false;
32 }
33 const char *argptr = reinterpret_cast<const char *>(buf);
34 const char *argsdata = argptr;
35 std::vector<const char *> argv;
36
37 // argv[0]
38 argv.emplace_back("hiperf");
39
40 // argv[1]
41 if (subcommand != nullptr and subcommand[0] != '\0') {
42 argv.emplace_back(subcommand);
43 }
44
45 // argv[2]
46 argv.emplace_back(argptr);
47
48 // argv[*]
49 for (size_t i = 0; i < std::min(MAX_TEST_ARGS_LEN, size); i++) {
50 if (argsdata[i] == '\0') {
51 argv.emplace_back(argptr);
52 argptr = &argsdata[i + 1];
53 }
54 if (argv.size() > MAX_TEST_ARGS_NUMBER) {
55 break;
56 }
57 }
58 #ifdef DEBUG_HIPERF_FUZZ
59 std::cout << " size " << argv.size() << std::endl;
60 #endif
61 HiperfFuzzerMain(static_cast<int>(argv.size()), argv.data());
62 return 0;
63 }
64 } // namespace OHOS
65
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69 #ifdef DEBUG_HIPERF_FUZZ
70 ScopeDebugLevel mix(LEVEL_VERBOSE, true);
71 DebugLogger::GetInstance()->Disable(false);
72 #else
73 OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w");
74 #endif
75 /* Run your code on data */
76 OHOS::FuzzCommandLine("", data, size);
77 OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands();
78 OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("stat",
79 std::make_unique<OHOS::Developtools::HiPerf::SubCommandStat>());
80 OHOS::FuzzCommandLine("stat", data, size);
81 OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands();
82 OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("record",
83 std::make_unique<OHOS::Developtools::HiPerf::SubCommandRecord>());
84 OHOS::FuzzCommandLine("record", data, size);
85 usleep(100000); // sleep 100000 us
86 OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands();
87 OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("report",
88 std::make_unique<OHOS::Developtools::HiPerf::SubCommandReport>());
89 OHOS::FuzzCommandLine("report", data, size);
90 OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands();
91 OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("list",
92 std::make_unique<OHOS::Developtools::HiPerf::SubCommandList>());
93 OHOS::FuzzCommandLine("list", data, size);
94 OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands();
95 OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("help",
96 std::make_unique<OHOS::Developtools::HiPerf::SubCommandHelp>());
97 OHOS::FuzzCommandLine("help", data, size);
98 return 0;
99 }
100