1 /*
2 * Copyright (c) 2024-2025 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
20 namespace OHOS {
21 namespace HiviewDFX {
22 const int FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH = 50;
DumpStackTraceTest(const uint8_t * data,size_t size)23 void DumpStackTraceTest(const uint8_t* data, size_t size)
24 {
25 struct TestArkFrameData {
26 int pid;
27 int tid;
28 char option[FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH];
29 };
30 if (data == nullptr || size <= sizeof(TestArkFrameData)) {
31 return;
32 }
33 const auto* testData = reinterpret_cast<const TestArkFrameData *>(data);
34 std::string msg;
35
36 std::shared_ptr<DfxDumpCatcher> catcher = std::make_shared<DfxDumpCatcher>();
37 catcher->DumpCatch(testData->pid, testData->tid, msg);
38 std::string processdumpCmd = "dumpcatcher -p " + std::to_string(testData->pid) + " -t " +
39 std::to_string(testData->tid);
40 system(processdumpCmd.c_str());
41 std::string processdumpInvalidCmd = std::string("dumpcatcher -") +
42 std::string(testData->option, FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH) + " -p " +
43 std::to_string(testData->pid) + " -t " + std::to_string(testData->tid);
44 system(processdumpInvalidCmd.c_str());
45 }
46 } // namespace HiviewDFX
47 } // namespace OHOS
48
49 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)50 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
51 {
52 if (data == nullptr || size == 0) {
53 return 0;
54 }
55
56 /* Run your code on data */
57 OHOS::HiviewDFX::DumpStackTraceTest(data, size);
58 return 0;
59 }
60