• 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 "CommandLine_fuzzer.h"
17 
18 extern int hiperf_fuzzer_main(const int argc, const char *argv[]);
19 namespace OHOS {
20 const static int32_t MAX_TEST_ARGS_NUMBER = 20;
21 const static size_t MAX_TEST_ARGS_LEN = 4096;
22 
FuzzCommandLine(const char * subcommand,const uint8_t * data,size_t size)23 bool FuzzCommandLine(const char *subcommand, 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<const char *> argv;
28 
29     // argv[0]
30     argv.emplace_back("hiperf");
31 
32     // argv[1]
33     if (subcommand != nullptr and subcommand[0] != '\0') {
34         argv.emplace_back(subcommand);
35     }
36 
37     // argv[2]
38     argv.emplace_back(argptr);
39 
40     // argv[*]
41     for (size_t i = 0; i < std::min(MAX_TEST_ARGS_LEN, size); i++) {
42         if (argsdata[i] == '\0') {
43             argv.emplace_back(argptr);
44             argptr = &argsdata[i + 1];
45         }
46         if (argv.size() > MAX_TEST_ARGS_NUMBER) {
47             break;
48         }
49     }
50 #ifdef DEBUG_HIPERF_FUZZ
51     std::cout << " size " << argv.size() << std::endl;
52 #endif
53     hiperf_fuzzer_main(static_cast<int>(argv.size()), argv.data());
54 
55     return 0;
56 }
57 } // namespace OHOS
58 
59 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
61 {
62 #ifdef DEBUG_HIPERF_FUZZ
63     ScopeDebugLevel mix(LEVEL_VERBOSE, true);
64     DebugLogger::GetInstance()->Disable(false);
65 #else
66     OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w");
67 #endif
68     /* Run your code on data */
69     OHOS::FuzzCommandLine("", data, size);
70     OHOS::FuzzCommandLine("stat", data, size);
71     OHOS::FuzzCommandLine("record", data, size);
72     OHOS::FuzzCommandLine("report", data, size);
73     OHOS::FuzzCommandLine("list", data, size);
74     OHOS::FuzzCommandLine("help", data, size);
75     return 0;
76 }
77