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