• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 /* This files contains faultlog fuzzer test modules. */
17 
18 #include "faultloggerd_fuzzer.h"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <iostream>
23 #include "dfx_dump_catcher.h"
24 #include "securec.h"
25 
26 using namespace OHOS::HiviewDFX;
27 using namespace std;
28 
29 static const int PID_SIZE = 4;
30 static const int RAND_BUF_LIMIT = 9;
31 
32 namespace OHOS {
DumpStackTraceTest(const uint8_t * data,size_t size)33 bool DumpStackTraceTest(const uint8_t* data, size_t size)
34 {
35     if (size < RAND_BUF_LIMIT) {
36         return true;
37     }
38     shared_ptr<DfxDumpCatcher> catcher = make_shared<DfxDumpCatcher>();
39     string msg;
40     int pid[1];
41     int tid[1];
42     errno_t err = memcpy_s(pid, sizeof(pid), data, PID_SIZE);
43     if (err != EOK) {
44         cout << "DumpStackTraceTest :: memcpy_s pid failed" << endl;
45     }
46     data += PID_SIZE;
47     err = memcpy_s(tid, sizeof(tid), data, PID_SIZE);
48     if (err != EOK) {
49         cout << "DumpStackTraceTest :: memcpy_s tid failed" << endl;
50     }
51     data += PID_SIZE;
52     char invalidOption = *data;
53     cout << "pid = " << pid[0] << " tid = " << tid[0] << " invalidOpt = " << invalidOption << endl;
54 
55     if (catcher->DumpCatch(pid[0], tid[0], msg)) {
56         cout << msg << endl;
57     } else {
58         cout << "DumpStackTraceTest :: dumpcatch failed." << endl;
59     }
60 
61     string processdumpCmd = "dumpcatcher -p " + to_string(pid[0]) + " -t " + to_string(tid[0]);
62     system(processdumpCmd.c_str());
63 
64     string processdumpInvalidCmd = "dumpcatcher -" + to_string(invalidOption) + " -p " +
65         to_string(pid[0]) + " -t " + to_string(tid[0]);
66     system(processdumpInvalidCmd.c_str());
67 
68     return true;
69 }
70 } // namespace OHOS
71 
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75     /* Run your code on data */
76     OHOS::DumpStackTraceTest(data, size);
77     return 0;
78 }
79