1 /*
2 * Copyright (c) 2024 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 "faultloggerddumpcatcher_fuzzer.h"
17
18 #include "dfx_dump_catcher.h"
19 #include "faultloggerd_fuzzertest_common.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 const int FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH = 50;
24
DumpStackTraceTest(const uint8_t * data,size_t size)25 void DumpStackTraceTest(const uint8_t* data, size_t size)
26 {
27 int pid;
28 int tid;
29 int offsetTotalLength = sizeof(pid) + sizeof(tid) +
30 (2 * FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH); // 2 : Offset by 2 string length
31 if (offsetTotalLength > size) {
32 return;
33 }
34
35 STREAM_TO_VALUEINFO(data, pid);
36 STREAM_TO_VALUEINFO(data, tid);
37
38 std::string msg(reinterpret_cast<const char*>(data), FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH);
39 data += FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH;
40 std::string invalidOption(reinterpret_cast<const char*>(data), FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH);
41 data += FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH;
42
43 std::shared_ptr<DfxDumpCatcher> catcher = std::make_shared<DfxDumpCatcher>();
44 catcher->DumpCatch(pid, tid, msg);
45
46 std::string processdumpCmd = "dumpcatcher -p " + std::to_string(pid) + " -t " + std::to_string(tid);
47 system(processdumpCmd.c_str());
48
49 std::string processdumpInvalidCmd = "dumpcatcher -" + invalidOption + " -p " +
50 std::to_string(pid) + " -t " + std::to_string(tid);
51 system(processdumpInvalidCmd.c_str());
52 }
53 } // namespace HiviewDFX
54 } // namespace OHOS
55
56 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
58 {
59 if (data == nullptr || size == 0) {
60 return 0;
61 }
62
63 /* Run your code on data */
64 OHOS::HiviewDFX::DumpStackTraceTest(data, size);
65 return 0;
66 }
67